Implemented custom print functions for all public classes
[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)
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(paste0("Please provide host name and authentification token",
32                             " or set ARVADOS_API_HOST and ARVADOS_API_TOKEN",
33                             " environment variables."))
34
35             private$REST  <- RESTService$new(token, hostName, NULL,
36                                              HttpRequest$new(), HttpParser$new())
37             private$token <- private$REST$token
38             private$host  <- private$REST$hostName
39         },
40
41         getToken          = function() private$REST$token,
42         getHostName       = function() private$REST$hostName,
43         getWebDavHostName = function() private$REST$getWebDavHostName(),
44         getRESTService    = function() private$REST,
45         setRESTService    = function(newRESTService) private$REST <- newRESTService,
46
47         getCollection = function(uuid)
48         {
49             collection <- private$REST$getResource("collections", uuid)
50             collection
51         },
52
53         listCollections = function(filters = NULL, limit = 100, offset = 0)
54         {
55             if(!is.null(filters))
56                 names(filters) <- c("collection")
57
58             collections <- private$REST$listResources("collections", filters,
59                                                       limit, offset)
60             collections
61         },
62
63         listAllCollections = function(filters = NULL)
64         {
65             if(!is.null(filters))
66                 names(filters) <- c("collection")
67
68             collectionURL <- paste0(private$host, "collections")
69             allCollection <- private$REST$fetchAllItems(collectionURL, filters)
70             allCollection
71         },
72
73         deleteCollection = function(uuid)
74         {
75             removedCollection <- private$REST$deleteResource("collections", uuid)
76             removedCollection
77         },
78
79         updateCollection = function(uuid, newContent)
80         {
81             body <- list(list())
82             names(body) <- c("collection")
83             body$collection <- newContent
84
85             updatedCollection <- private$REST$updateResource("collections",
86                                                              uuid, body)
87             updatedCollection
88         },
89
90         createCollection = function(content)
91         {
92             body <- list(list())
93             names(body) <- c("collection")
94             body$collection <- content
95
96             newCollection <- private$REST$createResource("collections", body)
97             newCollection
98         },
99
100         getProject = function(uuid)
101         {
102             project <- private$REST$getResource("groups", uuid)
103             project
104         },
105
106         createProject = function(content)
107         {
108             body <- list(list())
109             names(body) <- c("group")
110             body$group <- c("group_class" = "project", content)
111
112             newProject <- private$REST$createResource("groups", body)
113             newProject
114         },
115
116         updateProject = function(uuid, newContent)
117         {
118             body <- list(list())
119             names(body) <- c("group")
120             body$group <- newContent
121
122             updatedProject <- private$REST$updateResource("groups",
123                                                           uuid, body)
124             updatedProject
125         },
126
127         listProjects = function(filters = NULL, limit = 100, offset = 0)
128         {
129             if(!is.null(filters))
130                 names(filters) <- c("groups")
131
132             filters[[length(filters) + 1]] <- list("group_class", "=", "project")
133
134             projects <- private$REST$listResources("groups", filters,
135                                                    limit, offset)
136             projects
137         },
138
139         listAllProjects = function(filters = NULL)
140         {
141             if(!is.null(filters))
142                 names(filters) <- c("groups")
143
144             filters[[length(filters) + 1]] <- list("group_class", "=", "project")
145
146             projectURL <- paste0(private$host, "groups")
147
148             result <- private$REST$fetchAllItems(projectURL, filters)
149             result
150         },
151
152         deleteProject = function(uuid)
153         {
154             removedProject <- private$REST$deleteResource("groups", uuid)
155             removedProject
156         }
157     ),
158
159     private = list(
160
161         token = NULL,
162         host  = NULL,
163         REST  = NULL
164     ),
165
166     cloneable = FALSE
167 )
168
169 #' @export print.Arvados
170 print.Arvados = function(arvados)
171 {
172     cat(paste0("Type:  ", "\"", "Arvados", "\""), sep = "\n")
173     cat(paste0("Host:  ", "\"", arvados$getHostName(), "\""), sep = "\n")
174     cat(paste0("Token: ", "\"", arvados$getToken(), "\"") , sep = "\n")
175 }