Merge branch 'master' of git.curoverse.com:arvados into 11876-r-sdk
[arvados.git] / sdk / R / R / Arvados.R
1 source("./R/RESTService.R")
2 source("./R/HttpRequest.R")
3 source("./R/HttpParser.R")
4
5 #' Arvados SDK Object
6 #'
7 #' All Arvados logic is inside this class
8 #'
9 #' @field token Token represents user authentification token.
10 #' @field host Host represents server name we wish to connect to.
11 #' @examples arv = Arvados$new("token", "host_name")
12 #' @export Arvados
13 Arvados <- R6::R6Class(
14
15     "Arvados",
16
17     public = list(
18
19         initialize = function(authToken = NULL, hostName = NULL, numRetries = 0)
20         {
21             if(!is.null(hostName))
22                Sys.setenv(ARVADOS_API_HOST = hostName)
23
24             if(!is.null(authToken))
25                 Sys.setenv(ARVADOS_API_TOKEN = authToken)
26
27             hostName  <- Sys.getenv("ARVADOS_API_HOST");
28             token     <- Sys.getenv("ARVADOS_API_TOKEN");
29
30             if(hostName == "" | token == "")
31                 stop(paste("Please provide host name and authentification token",
32                            "or set ARVADOS_API_HOST and ARVADOS_API_TOKEN",
33                            "environment variables."))
34
35             private$numRetries  <- numRetries
36             private$REST  <- RESTService$new(token, hostName,
37                                              HttpRequest$new(), HttpParser$new(),
38                                              numRetries)
39
40             private$token <- private$REST$token
41             private$host  <- private$REST$hostName
42         },
43
44         getToken          = function() private$REST$token,
45         getHostName       = function() private$REST$hostName,
46         getWebDavHostName = function() private$REST$getWebDavHostName(),
47         getRESTService    = function() private$REST,
48         setRESTService    = function(newRESTService) private$REST <- newRESTService,
49
50         getNumRetries = function() private$REST$numRetries,
51         setNumRetries = function(newNumOfRetries)
52         {
53             private$REST$setNumRetries(newNumOfRetries)
54         },
55
56         getCollection = function(uuid)
57         {
58             collection <- private$REST$getResource("collections", uuid)
59             collection
60         },
61
62         listCollections = function(filters = NULL, limit = 100, offset = 0)
63         {
64             if(!is.null(filters))
65                 names(filters) <- c("collection")
66
67             collections <- private$REST$listResources("collections", filters,
68                                                       limit, offset)
69             collections
70         },
71
72         listAllCollections = function(filters = NULL)
73         {
74             if(!is.null(filters))
75                 names(filters) <- c("collection")
76
77             collectionURL <- paste0(private$host, "collections")
78             allCollection <- private$REST$fetchAllItems(collectionURL, filters)
79             allCollection
80         },
81
82         deleteCollection = function(uuid)
83         {
84             removedCollection <- private$REST$deleteResource("collections", uuid)
85             removedCollection
86         },
87
88         updateCollection = function(uuid, newContent)
89         {
90             body <- list(list())
91             names(body) <- c("collection")
92             body$collection <- newContent
93
94             updatedCollection <- private$REST$updateResource("collections",
95                                                              uuid, body)
96             updatedCollection
97         },
98
99         createCollection = function(content)
100         {
101             body <- list(list())
102             names(body) <- c("collection")
103             body$collection <- content
104
105             newCollection <- private$REST$createResource("collections", body)
106             newCollection
107         },
108
109         getProject = function(uuid)
110         {
111             project <- private$REST$getResource("groups", uuid)
112             project
113         },
114
115         createProject = function(content)
116         {
117             body <- list(list())
118             names(body) <- c("group")
119             body$group <- c("group_class" = "project", content)
120
121             newProject <- private$REST$createResource("groups", body)
122             newProject
123         },
124
125         updateProject = function(uuid, newContent)
126         {
127             body <- list(list())
128             names(body) <- c("group")
129             body$group <- newContent
130
131             updatedProject <- private$REST$updateResource("groups", uuid, body)
132             updatedProject
133         },
134
135         listProjects = function(filters = NULL, limit = 100, offset = 0)
136         {
137             if(!is.null(filters))
138                 names(filters) <- c("groups")
139
140             filters[[length(filters) + 1]] <- list("group_class", "=", "project")
141
142             projects <- private$REST$listResources("groups", filters, limit, offset)
143             projects
144         },
145
146         listAllProjects = function(filters = NULL)
147         {
148             if(!is.null(filters))
149                 names(filters) <- c("groups")
150
151             filters[[length(filters) + 1]] <- list("group_class", "=", "project")
152
153             projectURL <- paste0(private$host, "groups")
154
155             result <- private$REST$fetchAllItems(projectURL, filters)
156             result
157         },
158
159         deleteProject = function(uuid)
160         {
161             removedProject <- private$REST$deleteResource("groups", uuid)
162             removedProject
163         }
164     ),
165
166     private = list(
167
168         token      = NULL,
169         host       = NULL,
170         REST       = NULL,
171         numRetries = NULL
172     ),
173
174     cloneable = FALSE
175 )
176
177 #' @export print.Arvados
178 print.Arvados = function(arvados)
179 {
180     cat(paste0("Type:  ", "\"", "Arvados",             "\""), sep = "\n")
181     cat(paste0("Host:  ", "\"", arvados$getHostName(), "\""), sep = "\n")
182     cat(paste0("Token: ", "\"", arvados$getToken(),    "\""), sep = "\n")
183 }