Fixed some bugs and improved error handling.
[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             names(filters) <- c("collection")
77
78             serverResponse <- private$http$GET(collectionURL, headers, filters, limit, offset)
79             collection <- private$httpParser$parseJSONResponse(serverResponse)
80
81             if(!is.null(collection$errors))
82                 stop(collection$errors)       
83
84             collection
85         },
86
87         deleteCollection = function(uuid) 
88         {
89             collectionURL <- paste0(private$host, "collections/", uuid)
90             headers <- list("Authorization" = paste("OAuth2", private$token),
91                             "Content-Type"  = "application/json")
92
93             serverResponse <- private$http$DELETE(collectionURL, headers)
94
95             collection <- private$httpParser$parseJSONResponse(serverResponse)
96
97             if(!is.null(collection$errors))
98                 stop(collection$errors)       
99
100             collection
101         },
102
103         updateCollection = function(uuid, body) 
104         {
105             collectionURL <- paste0(private$host, "collections/", uuid)
106             headers <- list("Authorization" = paste("OAuth2", private$token),
107                             "Content-Type"  = "application/json")
108
109             names(body) <- c("collection")
110             body <- jsonlite::toJSON(body, auto_unbox = T)
111
112             serverResponse <- private$http$PUT(collectionURL, headers, body)
113
114             collection <- private$httpParser$parseJSONResponse(serverResponse)
115
116             if(!is.null(collection$errors))
117                 stop(collection$errors)       
118
119             collection
120         },
121
122         createCollection = function(body) 
123         {
124             collectionURL <- paste0(private$host, "collections")
125             headers <- list("Authorization" = paste("OAuth2", private$token),
126                             "Content-Type"  = "application/json")
127
128             names(body) <- c("collection")
129             body <- jsonlite::toJSON(body, auto_unbox = T)
130
131             serverResponse <- private$http$POST(collectionURL, headers, body)
132
133             collection <- private$httpParser$parseJSONResponse(serverResponse)
134
135             if(!is.null(collection$errors))
136                 stop(collection$errors)       
137
138             collection
139         },
140
141         getProject = function(uuid)
142         {
143             projectURL <- paste0(private$host, "groups/", uuid)
144             headers <- list(Authorization = paste("OAuth2", private$token))
145
146             serverResponse <- private$http$GET(projectURL, headers)
147
148             project <- private$httpParser$parseJSONResponse(serverResponse)
149
150             if(!is.null(project$errors))
151                 stop(project$errors)       
152
153             project
154         },
155
156         createProject = function(body) 
157         {
158             projectURL <- paste0(private$host, "groups")
159             headers <- list("Authorization" = paste("OAuth2", private$token),
160                             "Content-Type"  = "application/json")
161
162             names(body) <- c("group")
163             body$group <- c("group_class" = "project", body$group)
164             body <- jsonlite::toJSON(body, auto_unbox = T)
165
166             serverResponse <- private$http$POST(projectURL, headers, body)
167
168             project <- private$httpParser$parseJSONResponse(serverResponse)
169
170             if(!is.null(project$errors))
171                 stop(project$errors)       
172
173             project
174         },
175
176         updateProject = function(uuid, body) 
177         {
178             projectURL <- paste0(private$host, "groups/", uuid)
179             headers <- list("Authorization" = paste("OAuth2", private$token),
180                             "Content-Type"  = "application/json")
181
182             names(body) <- c("group")
183             body <- jsonlite::toJSON(body, auto_unbox = T)
184
185             serverResponse <- private$http$PUT(projectURL, headers, body)
186
187             project <- private$httpParser$parseJSONResponse(serverResponse)
188
189             if(!is.null(project$errors))
190                 stop(project$errors)       
191
192             project
193         },
194
195         listProjects = function(filters = NULL, limit = 100, offset = 0) 
196         {
197             projectURL <- paste0(private$host, "groups")
198             headers <- list(Authorization = paste("OAuth2", private$token))
199
200             names(filters) <- c("groups")
201             filters[[length(filters) + 1]] <- list("group_class", "=", "project")
202
203             serverResponse <- private$http$GET(projectURL, headers, filters, limit, offset)
204             projects <- private$httpParser$parseJSONResponse(serverResponse)
205
206             if(!is.null(projects$errors))
207                 stop(projects$errors)       
208
209             projects
210         },
211
212         deleteProject = function(uuid) 
213         {
214             projectURL <- paste0(private$host, "groups/", uuid)
215             headers <- list("Authorization" = paste("OAuth2", private$token),
216                             "Content-Type"  = "application/json")
217
218             serverResponse <- private$http$DELETE(projectURL, headers)
219
220             project <- private$httpParser$parseJSONResponse(serverResponse)
221
222             if(!is.null(project$errors))
223                 stop(project$errors)       
224
225             project
226         }
227     ),
228     
229     private = list(
230
231         token          = NULL,
232         host           = NULL,
233         webDavHostName = NULL,
234         http           = NULL,
235         httpParser     = NULL
236     ),
237     
238     cloneable = FALSE
239 )