Added code to notify user when API is unable to locate webDAV server.
[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
31                      ARVADOS_API_HOST and ARVADOS_API_TOKEN environmental variables.")
32
33             discoveryDocumentURL <- paste0("https://", host,
34                                            "/discovery/v1/apis/arvados/v1/rest")
35
36             version <- "v1"
37             host  <- paste0("https://", host, "/arvados/", version, "/")
38
39             private$http       <- HttpRequest$new()
40             private$httpParser <- HttpParser$new()
41             private$token      <- token
42             private$host       <- host
43             
44             headers <- list(Authorization = paste("OAuth2", private$token))
45
46             serverResponse <- private$http$GET(discoveryDocumentURL, headers)
47
48             discoveryDocument <- private$httpParser$parseJSONResponse(serverResponse)
49             private$webDavHostName <- discoveryDocument$keepWebServiceUrl
50
51             if(is.null(private$webDavHostName))
52                 stop("Unable to find WebDAV server.")
53         },
54
55         getToken    = function() private$token,
56         getHostName = function() private$host,
57
58         getWebDavHostName = function() private$webDavHostName,
59
60         getCollection = function(uuid) 
61         {
62             collectionURL <- paste0(private$host, "collections/", uuid)
63             headers <- list(Authorization = paste("OAuth2", private$token))
64
65             serverResponse <- private$http$GET(collectionURL, headers)
66
67             collection <- private$httpParser$parseJSONResponse(serverResponse)
68
69             if(!is.null(collection$errors))
70                 stop(collection$errors)       
71
72             collection
73         },
74
75         listCollections = function(filters = NULL, limit = 100, offset = 0) 
76         {
77             collectionURL <- paste0(private$host, "collections")
78             headers <- list(Authorization = paste("OAuth2", private$token))
79
80             if(!is.null(filters))
81                 names(filters) <- c("collection")
82
83             serverResponse <- private$http$GET(collectionURL, headers, filters,
84                                                limit, offset)
85
86             collection <- private$httpParser$parseJSONResponse(serverResponse)
87
88             if(!is.null(collection$errors))
89                 stop(collection$errors)       
90
91             collection
92         },
93
94         deleteCollection = function(uuid) 
95         {
96             collectionURL <- paste0(private$host, "collections/", uuid)
97             headers <- list("Authorization" = paste("OAuth2", private$token),
98                             "Content-Type"  = "application/json")
99
100             serverResponse <- private$http$DELETE(collectionURL, headers)
101
102             collection <- private$httpParser$parseJSONResponse(serverResponse)
103
104             if(!is.null(collection$errors))
105                 stop(collection$errors)       
106
107             collection
108         },
109
110         updateCollection = function(uuid, newContent) 
111         {
112             collectionURL <- paste0(private$host, "collections/", uuid)
113             headers <- list("Authorization" = paste("OAuth2", private$token),
114                             "Content-Type"  = "application/json")
115
116             body <- list(list())
117             names(body) <- c("collection")
118             body$collection <- newContent
119
120             body <- jsonlite::toJSON(body, auto_unbox = T)
121
122             serverResponse <- private$http$PUT(collectionURL, headers, body)
123
124             collection <- private$httpParser$parseJSONResponse(serverResponse)
125
126             if(!is.null(collection$errors))
127                 stop(collection$errors)       
128
129             collection
130         },
131
132         createCollection = function(content) 
133         {
134             collectionURL <- paste0(private$host, "collections")
135             headers <- list("Authorization" = paste("OAuth2", private$token),
136                             "Content-Type"  = "application/json")
137
138             body <- list(list())
139             names(body) <- c("collection")
140             body$collection <- content
141
142             body <- jsonlite::toJSON(body, auto_unbox = T)
143
144             serverResponse <- private$http$POST(collectionURL, headers, body)
145
146             collection <- private$httpParser$parseJSONResponse(serverResponse)
147
148             if(!is.null(collection$errors))
149                 stop(collection$errors)       
150
151             collection
152         },
153
154         getProject = function(uuid)
155         {
156             projectURL <- paste0(private$host, "groups/", uuid)
157             headers <- list(Authorization = paste("OAuth2", private$token))
158
159             serverResponse <- private$http$GET(projectURL, headers)
160
161             project <- private$httpParser$parseJSONResponse(serverResponse)
162
163             if(!is.null(project$errors))
164                 stop(project$errors)       
165
166             project
167         },
168
169         createProject = function(content) 
170         {
171             projectURL <- paste0(private$host, "groups")
172             headers <- list("Authorization" = paste("OAuth2", private$token),
173                             "Content-Type"  = "application/json")
174
175             body <- list(list())
176             names(body) <- c("group")
177             body$group <- c("group_class" = "project", content)
178             body <- jsonlite::toJSON(body, auto_unbox = T)
179
180             serverResponse <- private$http$POST(projectURL, headers, body)
181
182             project <- private$httpParser$parseJSONResponse(serverResponse)
183
184             if(!is.null(project$errors))
185                 stop(project$errors)       
186
187             project
188         },
189
190         updateProject = function(uuid, newContent) 
191         {
192             projectURL <- paste0(private$host, "groups/", uuid)
193             headers <- list("Authorization" = paste("OAuth2", private$token),
194                             "Content-Type"  = "application/json")
195
196             body <- list(list())
197             names(body) <- c("group")
198             body$group <- newContent
199             body <- jsonlite::toJSON(body, auto_unbox = T)
200
201             serverResponse <- private$http$PUT(projectURL, headers, body)
202
203             project <- private$httpParser$parseJSONResponse(serverResponse)
204
205             if(!is.null(project$errors))
206                 stop(project$errors)       
207
208             project
209         },
210
211         listProjects = function(filters = NULL, limit = 100, offset = 0) 
212         {
213             projectURL <- paste0(private$host, "groups")
214             headers <- list(Authorization = paste("OAuth2", private$token))
215
216             if(!is.null(filters))
217                 names(filters) <- c("groups")
218
219             filters[[length(filters) + 1]] <- list("group_class", "=", "project")
220
221             serverResponse <- private$http$GET(projectURL, headers, filters,
222                                                limit, offset)
223
224             projects <- private$httpParser$parseJSONResponse(serverResponse)
225
226             if(!is.null(projects$errors))
227                 stop(projects$errors)       
228
229             projects
230         },
231
232         deleteProject = function(uuid) 
233         {
234             projectURL <- paste0(private$host, "groups/", uuid)
235             headers <- list("Authorization" = paste("OAuth2", private$token),
236                             "Content-Type"  = "application/json")
237
238             serverResponse <- private$http$DELETE(projectURL, headers)
239
240             project <- private$httpParser$parseJSONResponse(serverResponse)
241
242             if(!is.null(project$errors))
243                 stop(project$errors)       
244
245             project
246         }
247     ),
248     
249     private = list(
250
251         token          = NULL,
252         host           = NULL,
253         webDavHostName = NULL,
254         http           = NULL,
255         httpParser     = NULL
256     ),
257     
258     cloneable = FALSE
259 )