Moved all classes from RefClass OOP model to R6 and made improvements
[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             print(private$webDavHostName)
49         },
50
51         getToken    = function() private$token,
52         getHostName = function() private$host,
53
54         #Todo(Fudo): Hardcoded credentials to WebDAV server. Remove them later
55         getWebDavHostName = function() private$webDavHostName,
56
57         getCollection = function(uuid) 
58         {
59             collectionURL <- paste0(private$host, "collections/", uuid)
60             headers <- list(Authorization = paste("OAuth2", private$token))
61
62             serverResponse <- private$http$GET(collectionURL, headers)
63
64             collection <- private$httpParser$parseJSONResponse(serverResponse)
65
66             if(!is.null(collection$errors))
67                 stop(collection$errors)       
68
69             collection
70         },
71
72         listCollections = function(filters = NULL, limit = 100, offset = 0) 
73         {
74             collectionURL <- paste0(private$host, "collections")
75             headers <- list(Authorization = paste("OAuth2", private$token))
76
77             serverResponse <- private$http$GET(collectionURL, headers, NULL, filters, limit, offset)
78
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             body <- jsonlite::toJSON(body, auto_unbox = T)
110
111             serverResponse <- private$http$PUT(collectionURL, headers, body)
112
113             collection <- private$httpParser$parseJSONResponse(serverResponse)
114
115             if(!is.null(collection$errors))
116                 stop(collection$errors)       
117
118             collection
119         },
120
121         createCollection = function(body) 
122         {
123             collectionURL <- paste0(private$host, "collections")
124             headers <- list("Authorization" = paste("OAuth2", private$token),
125                             "Content-Type"  = "application/json")
126             body <- jsonlite::toJSON(body, auto_unbox = T)
127
128             serverResponse <- private$http$POST(collectionURL, headers, body)
129
130             collection <- private$httpParser$parseJSONResponse(serverResponse)
131
132             if(!is.null(collection$errors))
133                 stop(collection$errors)       
134
135             collection
136         }
137
138     ),
139     
140     private = list(
141
142         token          = NULL,
143         host           = NULL,
144         webDavHostName = NULL,
145         http           = NULL,
146         httpParser     = NULL
147     ),
148     
149     cloneable = FALSE
150 )