1 source("./R/HttpRequest.R")
2 source("./R/HttpParser.R")
6 #' All Arvados logic is inside this class
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")
12 Arvados <- R6::R6Class(
18 initialize = function(auth_token = NULL, host_name = NULL)
20 if(!is.null(host_name))
21 Sys.setenv(ARVADOS_API_HOST = host_name)
23 if(!is.null(auth_token))
24 Sys.setenv(ARVADOS_API_TOKEN = auth_token)
26 host <- Sys.getenv("ARVADOS_API_HOST");
27 token <- Sys.getenv("ARVADOS_API_TOKEN");
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."))
35 host <- paste0("https://", host, "/arvados/", version, "/")
37 private$http <- HttpRequest$new()
38 private$httpParser <- HttpParser$new()
39 private$token <- token
41 private$rawHost <- host_name
44 getToken = function() private$token,
45 getHostName = function() private$host,
47 getHttpClient = function() private$http,
48 setHttpClient = function(newClient) private$http <- newClient,
50 getHttpParser = function() private$httpParser,
51 setHttpParser = function(newParser) private$httpParser <- newParser,
53 getWebDavHostName = function()
55 if(is.null(private$webDavHostName))
57 discoveryDocumentURL <- paste0("https://", private$rawHost,
58 "/discovery/v1/apis/arvados/v1/rest")
60 headers <- list(Authorization = paste("OAuth2", private$token))
62 serverResponse <- private$http$GET(discoveryDocumentURL, headers)
64 discoveryDocument <- private$httpParser$parseJSONResponse(serverResponse)
65 private$webDavHostName <- discoveryDocument$keepWebServiceUrl
67 if(is.null(private$webDavHostName))
68 stop("Unable to find WebDAV server.")
71 private$webDavHostName
74 getCollection = function(uuid)
76 collectionURL <- paste0(private$host, "collections/", uuid)
77 headers <- list(Authorization = paste("OAuth2", private$token))
79 serverResponse <- private$http$GET(collectionURL, headers)
81 collection <- private$httpParser$parseJSONResponse(serverResponse)
83 if(!is.null(collection$errors))
84 stop(collection$errors)
89 listCollections = function(filters = NULL, limit = 100, offset = 0)
91 collectionURL <- paste0(private$host, "collections")
92 headers <- list(Authorization = paste("OAuth2", private$token))
95 names(filters) <- c("collection")
97 serverResponse <- private$http$GET(collectionURL, headers, filters,
100 collections <- private$httpParser$parseJSONResponse(serverResponse)
102 if(!is.null(collections$errors))
103 stop(collections$errors)
108 listAllCollections = function(filters = NULL)
110 if(!is.null(filters))
111 names(filters) <- c("collection")
113 collectionURL <- paste0(private$host, "collections")
114 private$fetchAllItems(collectionURL, filters)
117 deleteCollection = function(uuid)
119 collectionURL <- paste0(private$host, "collections/", uuid)
120 headers <- list("Authorization" = paste("OAuth2", private$token),
121 "Content-Type" = "application/json")
123 serverResponse <- private$http$DELETE(collectionURL, headers)
125 collection <- private$httpParser$parseJSONResponse(serverResponse)
127 if(!is.null(collection$errors))
128 stop(collection$errors)
133 updateCollection = function(uuid, newContent)
135 collectionURL <- paste0(private$host, "collections/", uuid)
136 headers <- list("Authorization" = paste("OAuth2", private$token),
137 "Content-Type" = "application/json")
140 #test if this is needed
141 names(body) <- c("collection")
142 body$collection <- newContent
144 body <- jsonlite::toJSON(body, auto_unbox = T)
146 serverResponse <- private$http$PUT(collectionURL, headers, body)
148 collection <- private$httpParser$parseJSONResponse(serverResponse)
150 if(!is.null(collection$errors))
151 stop(collection$errors)
156 createCollection = function(content)
158 collectionURL <- paste0(private$host, "collections")
159 headers <- list("Authorization" = paste("OAuth2", private$token),
160 "Content-Type" = "application/json")
163 names(body) <- c("collection")
164 body$collection <- content
166 body <- jsonlite::toJSON(body, auto_unbox = T)
168 serverResponse <- private$http$POST(collectionURL, headers, body)
170 collection <- private$httpParser$parseJSONResponse(serverResponse)
172 if(!is.null(collection$errors))
173 stop(collection$errors)
178 getProject = function(uuid)
180 projectURL <- paste0(private$host, "groups/", uuid)
181 headers <- list(Authorization = paste("OAuth2", private$token))
183 serverResponse <- private$http$GET(projectURL, headers)
185 project <- private$httpParser$parseJSONResponse(serverResponse)
187 if(!is.null(project$errors))
193 createProject = function(content)
195 projectURL <- paste0(private$host, "groups")
196 headers <- list("Authorization" = paste("OAuth2", private$token),
197 "Content-Type" = "application/json")
200 names(body) <- c("group")
201 body$group <- c("group_class" = "project", content)
202 body <- jsonlite::toJSON(body, auto_unbox = T)
204 serverResponse <- private$http$POST(projectURL, headers, body)
206 project <- private$httpParser$parseJSONResponse(serverResponse)
208 if(!is.null(project$errors))
214 updateProject = function(uuid, newContent)
216 projectURL <- paste0(private$host, "groups/", uuid)
217 headers <- list("Authorization" = paste("OAuth2", private$token),
218 "Content-Type" = "application/json")
221 names(body) <- c("group")
222 body$group <- newContent
223 body <- jsonlite::toJSON(body, auto_unbox = T)
225 serverResponse <- private$http$PUT(projectURL, headers, body)
227 project <- private$httpParser$parseJSONResponse(serverResponse)
229 if(!is.null(project$errors))
235 listProjects = function(filters = NULL, limit = 100, offset = 0)
237 projectURL <- paste0(private$host, "groups")
238 headers <- list(Authorization = paste("OAuth2", private$token))
240 if(!is.null(filters))
241 names(filters) <- c("groups")
243 filters[[length(filters) + 1]] <- list("group_class", "=", "project")
245 serverResponse <- private$http$GET(projectURL, headers, filters,
248 projects <- private$httpParser$parseJSONResponse(serverResponse)
250 if(!is.null(projects$errors))
251 stop(projects$errors)
256 listAllProjects = function(filters = NULL)
258 if(!is.null(filters))
259 names(filters) <- c("groups")
261 filters[[length(filters) + 1]] <- list("group_class", "=", "project")
263 projectURL <- paste0(private$host, "groups")
265 private$fetchAllItems(projectURL, filters)
268 deleteProject = function(uuid)
270 projectURL <- paste0(private$host, "groups/", uuid)
271 headers <- list("Authorization" = paste("OAuth2", private$token),
272 "Content-Type" = "application/json")
274 serverResponse <- private$http$DELETE(projectURL, headers)
276 project <- private$httpParser$parseJSONResponse(serverResponse)
278 if(!is.null(project$errors))
290 webDavHostName = NULL,
294 fetchAllItems = function(resourceURL, filters)
296 headers <- list(Authorization = paste("OAuth2", private$token))
299 itemsAvailable <- .Machine$integer.max
301 while(length(items) < itemsAvailable)
303 serverResponse <- private$http$GET(url = resourceURL,
305 queryFilters = filters,
309 parsedResponse <- private$httpParser$parseJSONResponse(serverResponse)
311 if(!is.null(parsedResponse$errors))
312 stop(parsedResponse$errors)
314 items <- c(items, parsedResponse$items)
315 offset <- length(items)
316 itemsAvailable <- parsedResponse$items_available