Added more unit tests to Arvados class
[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(paste0("Please provide host name and authentification token",
31                             " or set ARVADOS_API_HOST and ARVADOS_API_TOKEN",
32                             " environmental variables."))
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             private$rawHost    <- host_name
42         },
43
44         getToken    = function() private$token,
45         getHostName = function() private$host,
46
47         getHttpClient = function() private$http,
48         setHttpClient = function(newClient) private$http <- newClient,
49
50         getHttpParser = function() private$httpParser,
51         setHttpParser = function(newParser) private$httpParser <- newParser,
52
53         getWebDavHostName = function()
54         {
55             if(is.null(private$webDavHostName))
56             {
57                 discoveryDocumentURL <- paste0("https://", private$rawHost,
58                                                "/discovery/v1/apis/arvados/v1/rest")
59
60                 headers <- list(Authorization = paste("OAuth2", private$token))
61
62                 serverResponse <- private$http$GET(discoveryDocumentURL, headers)
63
64                 discoveryDocument <- private$httpParser$parseJSONResponse(serverResponse)
65                 private$webDavHostName <- discoveryDocument$keepWebServiceUrl
66
67                 if(is.null(private$webDavHostName))
68                     stop("Unable to find WebDAV server.")
69             }
70
71             private$webDavHostName
72         },
73
74         getCollection = function(uuid) 
75         {
76             collectionURL <- paste0(private$host, "collections/", uuid)
77             headers <- list(Authorization = paste("OAuth2", private$token))
78
79             serverResponse <- private$http$GET(collectionURL, headers)
80
81             collection <- private$httpParser$parseJSONResponse(serverResponse)
82
83             if(!is.null(collection$errors))
84                 stop(collection$errors)       
85
86             collection
87         },
88
89         listCollections = function(filters = NULL, limit = 100, offset = 0) 
90         {
91             collectionURL <- paste0(private$host, "collections")
92             headers <- list(Authorization = paste("OAuth2", private$token))
93
94             if(!is.null(filters))
95                 names(filters) <- c("collection")
96
97             serverResponse <- private$http$GET(collectionURL, headers, filters,
98                                                limit, offset)
99
100             collections <- private$httpParser$parseJSONResponse(serverResponse)
101
102             if(!is.null(collections$errors))
103                 stop(collections$errors)       
104
105             collections
106         },
107
108         listAllCollections = function(filters = NULL)
109         {
110             if(!is.null(filters))
111                 names(filters) <- c("collection")
112
113             collectionURL <- paste0(private$host, "collections")
114             private$fetchAllItems(collectionURL, filters)
115         },
116
117         deleteCollection = function(uuid) 
118         {
119             collectionURL <- paste0(private$host, "collections/", uuid)
120             headers <- list("Authorization" = paste("OAuth2", private$token),
121                             "Content-Type"  = "application/json")
122
123             serverResponse <- private$http$DELETE(collectionURL, headers)
124
125             collection <- private$httpParser$parseJSONResponse(serverResponse)
126
127             if(!is.null(collection$errors))
128                 stop(collection$errors)       
129
130             collection
131         },
132
133         updateCollection = function(uuid, newContent) 
134         {
135             collectionURL <- paste0(private$host, "collections/", uuid)
136             headers <- list("Authorization" = paste("OAuth2", private$token),
137                             "Content-Type"  = "application/json")
138
139             body <- list(list())
140             #test if this is needed
141             names(body) <- c("collection")
142             body$collection <- newContent
143
144             body <- jsonlite::toJSON(body, auto_unbox = T)
145
146             serverResponse <- private$http$PUT(collectionURL, headers, body)
147
148             collection <- private$httpParser$parseJSONResponse(serverResponse)
149
150             if(!is.null(collection$errors))
151                 stop(collection$errors)       
152
153             collection
154         },
155
156         createCollection = function(content) 
157         {
158             collectionURL <- paste0(private$host, "collections")
159             headers <- list("Authorization" = paste("OAuth2", private$token),
160                             "Content-Type"  = "application/json")
161
162             body <- list(list())
163             names(body) <- c("collection")
164             body$collection <- content
165
166             body <- jsonlite::toJSON(body, auto_unbox = T)
167
168             serverResponse <- private$http$POST(collectionURL, headers, body)
169
170             collection <- private$httpParser$parseJSONResponse(serverResponse)
171
172             if(!is.null(collection$errors))
173                 stop(collection$errors)       
174
175             collection
176         },
177
178         getProject = function(uuid)
179         {
180             projectURL <- paste0(private$host, "groups/", uuid)
181             headers <- list(Authorization = paste("OAuth2", private$token))
182
183             serverResponse <- private$http$GET(projectURL, headers)
184
185             project <- private$httpParser$parseJSONResponse(serverResponse)
186
187             if(!is.null(project$errors))
188                 stop(project$errors)       
189
190             project
191         },
192
193         createProject = function(content) 
194         {
195             projectURL <- paste0(private$host, "groups")
196             headers <- list("Authorization" = paste("OAuth2", private$token),
197                             "Content-Type"  = "application/json")
198
199             body <- list(list())
200             names(body) <- c("group")
201             body$group <- c("group_class" = "project", content)
202             body <- jsonlite::toJSON(body, auto_unbox = T)
203
204             serverResponse <- private$http$POST(projectURL, headers, body)
205
206             project <- private$httpParser$parseJSONResponse(serverResponse)
207
208             if(!is.null(project$errors))
209                 stop(project$errors)       
210
211             project
212         },
213
214         updateProject = function(uuid, newContent) 
215         {
216             projectURL <- paste0(private$host, "groups/", uuid)
217             headers <- list("Authorization" = paste("OAuth2", private$token),
218                             "Content-Type"  = "application/json")
219
220             body <- list(list())
221             names(body) <- c("group")
222             body$group <- newContent
223             body <- jsonlite::toJSON(body, auto_unbox = T)
224
225             serverResponse <- private$http$PUT(projectURL, headers, body)
226
227             project <- private$httpParser$parseJSONResponse(serverResponse)
228
229             if(!is.null(project$errors))
230                 stop(project$errors)       
231
232             project
233         },
234
235         listProjects = function(filters = NULL, limit = 100, offset = 0) 
236         {
237             projectURL <- paste0(private$host, "groups")
238             headers <- list(Authorization = paste("OAuth2", private$token))
239
240             if(!is.null(filters))
241                 names(filters) <- c("groups")
242
243             filters[[length(filters) + 1]] <- list("group_class", "=", "project")
244
245             serverResponse <- private$http$GET(projectURL, headers, filters,
246                                                limit, offset)
247
248             projects <- private$httpParser$parseJSONResponse(serverResponse)
249
250             if(!is.null(projects$errors))
251                 stop(projects$errors)       
252
253             projects
254         },
255
256         listAllProjects = function(filters = NULL)
257         {
258             if(!is.null(filters))
259                 names(filters) <- c("groups")
260
261             filters[[length(filters) + 1]] <- list("group_class", "=", "project")
262
263             projectURL <- paste0(private$host, "groups")
264
265             private$fetchAllItems(projectURL, filters)
266         },
267
268         deleteProject = function(uuid) 
269         {
270             projectURL <- paste0(private$host, "groups/", uuid)
271             headers <- list("Authorization" = paste("OAuth2", private$token),
272                             "Content-Type"  = "application/json")
273
274             serverResponse <- private$http$DELETE(projectURL, headers)
275
276             project <- private$httpParser$parseJSONResponse(serverResponse)
277
278             if(!is.null(project$errors))
279                 stop(project$errors)       
280
281             project
282         }
283     ),
284     
285     private = list(
286
287         token          = NULL,
288         host           = NULL,
289         rawHost        = NULL,
290         webDavHostName = NULL,
291         http           = NULL,
292         httpParser     = NULL,
293
294         fetchAllItems = function(resourceURL, filters)
295         {
296             headers <- list(Authorization = paste("OAuth2", private$token))
297
298             offset <- 0
299             itemsAvailable <- .Machine$integer.max
300             items <- c()
301             while(length(items) < itemsAvailable)
302             {
303                 serverResponse <- private$http$GET(url          = resourceURL,
304                                                    headers      = headers,
305                                                    queryFilters = filters,
306                                                    limit        = NULL,
307                                                    offset       = offset)
308
309                 parsedResponse <- private$httpParser$parseJSONResponse(serverResponse)
310
311                 if(!is.null(parsedResponse$errors))
312                     stop(parsedResponse$errors)       
313
314                 items          <- c(items, parsedResponse$items)
315                 offset         <- length(items)
316                 itemsAvailable <- parsedResponse$items_available
317             }
318
319             items
320         }
321     ),
322     
323     cloneable = FALSE
324 )