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("Please provide host name and authentification token or set
31 ARVADOS_API_HOST and ARVADOS_API_TOKEN environmental variables.")
33 discoveryDocumentURL <- paste0("https://", host,
34 "/discovery/v1/apis/arvados/v1/rest")
37 host <- paste0("https://", host, "/arvados/", version, "/")
39 private$http <- HttpRequest$new()
40 private$httpParser <- HttpParser$new()
41 private$token <- token
44 headers <- list(Authorization = paste("OAuth2", private$token))
46 serverResponse <- private$http$GET(discoveryDocumentURL, headers)
48 discoveryDocument <- private$httpParser$parseJSONResponse(serverResponse)
49 private$webDavHostName <- discoveryDocument$keepWebServiceUrl
51 if(is.null(private$webDavHostName))
52 stop("Unable to find WebDAV server.")
55 getToken = function() private$token,
56 getHostName = function() private$host,
58 getWebDavHostName = function() private$webDavHostName,
60 getCollection = function(uuid)
62 collectionURL <- paste0(private$host, "collections/", uuid)
63 headers <- list(Authorization = paste("OAuth2", private$token))
65 serverResponse <- private$http$GET(collectionURL, headers)
67 collection <- private$httpParser$parseJSONResponse(serverResponse)
69 if(!is.null(collection$errors))
70 stop(collection$errors)
75 listCollections = function(filters = NULL, limit = 100, offset = 0)
77 collectionURL <- paste0(private$host, "collections")
78 headers <- list(Authorization = paste("OAuth2", private$token))
81 names(filters) <- c("collection")
83 serverResponse <- private$http$GET(collectionURL, headers, filters,
86 collection <- private$httpParser$parseJSONResponse(serverResponse)
88 if(!is.null(collection$errors))
89 stop(collection$errors)
94 deleteCollection = function(uuid)
96 collectionURL <- paste0(private$host, "collections/", uuid)
97 headers <- list("Authorization" = paste("OAuth2", private$token),
98 "Content-Type" = "application/json")
100 serverResponse <- private$http$DELETE(collectionURL, headers)
102 collection <- private$httpParser$parseJSONResponse(serverResponse)
104 if(!is.null(collection$errors))
105 stop(collection$errors)
110 updateCollection = function(uuid, newContent)
112 collectionURL <- paste0(private$host, "collections/", uuid)
113 headers <- list("Authorization" = paste("OAuth2", private$token),
114 "Content-Type" = "application/json")
117 names(body) <- c("collection")
118 body$collection <- newContent
120 body <- jsonlite::toJSON(body, auto_unbox = T)
122 serverResponse <- private$http$PUT(collectionURL, headers, body)
124 collection <- private$httpParser$parseJSONResponse(serverResponse)
126 if(!is.null(collection$errors))
127 stop(collection$errors)
132 createCollection = function(content)
134 collectionURL <- paste0(private$host, "collections")
135 headers <- list("Authorization" = paste("OAuth2", private$token),
136 "Content-Type" = "application/json")
139 names(body) <- c("collection")
140 body$collection <- content
142 body <- jsonlite::toJSON(body, auto_unbox = T)
144 serverResponse <- private$http$POST(collectionURL, headers, body)
146 collection <- private$httpParser$parseJSONResponse(serverResponse)
148 if(!is.null(collection$errors))
149 stop(collection$errors)
154 getProject = function(uuid)
156 projectURL <- paste0(private$host, "groups/", uuid)
157 headers <- list(Authorization = paste("OAuth2", private$token))
159 serverResponse <- private$http$GET(projectURL, headers)
161 project <- private$httpParser$parseJSONResponse(serverResponse)
163 if(!is.null(project$errors))
169 createProject = function(content)
171 projectURL <- paste0(private$host, "groups")
172 headers <- list("Authorization" = paste("OAuth2", private$token),
173 "Content-Type" = "application/json")
176 names(body) <- c("group")
177 body$group <- c("group_class" = "project", content)
178 body <- jsonlite::toJSON(body, auto_unbox = T)
180 serverResponse <- private$http$POST(projectURL, headers, body)
182 project <- private$httpParser$parseJSONResponse(serverResponse)
184 if(!is.null(project$errors))
190 updateProject = function(uuid, newContent)
192 projectURL <- paste0(private$host, "groups/", uuid)
193 headers <- list("Authorization" = paste("OAuth2", private$token),
194 "Content-Type" = "application/json")
197 names(body) <- c("group")
198 body$group <- newContent
199 body <- jsonlite::toJSON(body, auto_unbox = T)
201 serverResponse <- private$http$PUT(projectURL, headers, body)
203 project <- private$httpParser$parseJSONResponse(serverResponse)
205 if(!is.null(project$errors))
211 listProjects = function(filters = NULL, limit = 100, offset = 0)
213 projectURL <- paste0(private$host, "groups")
214 headers <- list(Authorization = paste("OAuth2", private$token))
216 if(!is.null(filters))
217 names(filters) <- c("groups")
219 filters[[length(filters) + 1]] <- list("group_class", "=", "project")
221 serverResponse <- private$http$GET(projectURL, headers, filters,
224 projects <- private$httpParser$parseJSONResponse(serverResponse)
226 if(!is.null(projects$errors))
227 stop(projects$errors)
232 deleteProject = function(uuid)
234 projectURL <- paste0(private$host, "groups/", uuid)
235 headers <- list("Authorization" = paste("OAuth2", private$token),
236 "Content-Type" = "application/json")
238 serverResponse <- private$http$DELETE(projectURL, headers)
240 project <- private$httpParser$parseJSONResponse(serverResponse)
242 if(!is.null(project$errors))
253 webDavHostName = NULL,