Merge branch 'master' of git.curoverse.com:arvados into 11876-r-sdk
[arvados.git] / sdk / R / R / Arvados.R
1 source("./R/HttpRequest.R")
2 source("./R/HttpParser.R")
3
4 #' Arvados SDK Object
5 #'
6 #' All Arvados logic is inside this class
7 #'
8 #' @field token Token represents user authentification token.
9 #' @field host Host represents server name we wish to connect to.
10 #' @examples arv = Arvados$new("token", "host_name")
11 #' @export Arvados
12 Arvados <- R6::R6Class(
13
14     "Arvados",
15
16     public = list(
17
18         initialize = function(auth_token = NULL, host_name = NULL) 
19         {
20             if(!is.null(host_name))
21                Sys.setenv(ARVADOS_API_HOST  = host_name)
22
23             if(!is.null(auth_token))
24                 Sys.setenv(ARVADOS_API_TOKEN = auth_token)
25
26             host  <- Sys.getenv("ARVADOS_API_HOST");
27             token <- Sys.getenv("ARVADOS_API_TOKEN");
28
29             if(host == "" | token == "")
30                 stop("Please provide host name and authentification token or set ARVADOS_API_HOST and ARVADOS_API_TOKEN environmental variables.")
31
32             discoveryDocumentURL <- paste0("https://", host, "/discovery/v1/apis/arvados/v1/rest")
33
34             version <- "v1"
35             host  <- paste0("https://", host, "/arvados/", version, "/")
36
37             private$http <- HttpRequest$new()
38             private$httpParser <- HttpParser$new()
39             private$token <- token
40             private$host <- host
41             
42             headers <- list(Authorization = paste("OAuth2", private$token))
43
44             serverResponse <- private$http$GET(discoveryDocumentURL, headers)
45
46             discoveryDocument <- private$httpParser$parseJSONResponse(serverResponse)
47             private$webDavHostName <- discoveryDocument$keepWebServiceUrl
48         },
49
50         getToken    = function() private$token,
51         getHostName = function() private$host,
52
53         #Todo(Fudo): Hardcoded credentials to WebDAV server. Remove them later
54         getWebDavHostName = function() private$webDavHostName,
55
56         getCollection = function(uuid) 
57         {
58             collectionURL <- paste0(private$host, "collections/", uuid)
59             headers <- list(Authorization = paste("OAuth2", private$token))
60
61             serverResponse <- private$http$GET(collectionURL, headers)
62
63             collection <- private$httpParser$parseJSONResponse(serverResponse)
64
65             if(!is.null(collection$errors))
66                 stop(collection$errors)       
67
68             collection
69         },
70
71         listCollections = function(filters = NULL, limit = 100, offset = 0) 
72         {
73             collectionURL <- paste0(private$host, "collections")
74             headers <- list(Authorization = paste("OAuth2", private$token))
75
76             serverResponse <- private$http$GET(collectionURL, headers, NULL, filters, limit, offset)
77
78             collection <- private$httpParser$parseJSONResponse(serverResponse)
79
80             if(!is.null(collection$errors))
81                 stop(collection$errors)       
82
83             collection
84         },
85
86         deleteCollection = function(uuid) 
87         {
88             collectionURL <- paste0(private$host, "collections/", uuid)
89             headers <- list("Authorization" = paste("OAuth2", private$token),
90                             "Content-Type"  = "application/json")
91
92             serverResponse <- private$http$DELETE(collectionURL, headers)
93
94             collection <- private$httpParser$parseJSONResponse(serverResponse)
95
96             if(!is.null(collection$errors))
97                 stop(collection$errors)       
98
99             collection
100         },
101
102         updateCollection = function(uuid, body) 
103         {
104             collectionURL <- paste0(private$host, "collections/", uuid)
105             headers <- list("Authorization" = paste("OAuth2", private$token),
106                             "Content-Type"  = "application/json")
107
108             body <- jsonlite::toJSON(body, auto_unbox = T)
109
110             serverResponse <- private$http$PUT(collectionURL, headers, body)
111
112             collection <- private$httpParser$parseJSONResponse(serverResponse)
113
114             if(!is.null(collection$errors))
115                 stop(collection$errors)       
116
117             collection
118         },
119
120         createCollection = function(body) 
121         {
122             collectionURL <- paste0(private$host, "collections")
123             headers <- list("Authorization" = paste("OAuth2", private$token),
124                             "Content-Type"  = "application/json")
125             body <- jsonlite::toJSON(body, auto_unbox = T)
126
127             serverResponse <- private$http$POST(collectionURL, headers, body)
128
129             collection <- private$httpParser$parseJSONResponse(serverResponse)
130
131             if(!is.null(collection$errors))
132                 stop(collection$errors)       
133
134             collection
135         }
136
137     ),
138     
139     private = list(
140
141         token          = NULL,
142         host           = NULL,
143         webDavHostName = NULL,
144         http           = NULL,
145         httpParser     = NULL
146     ),
147     
148     cloneable = FALSE
149 )