Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / sdk / R / R / RESTService.R
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 RESTService <- R6::R6Class(
6
7     "RESTService",
8
9     public = list(
10
11         token      = NULL,
12         http       = NULL,
13         httpParser = NULL,
14         numRetries = NULL,
15
16         initialize = function(token, rawHost,
17                               http, httpParser,
18                               numRetries     = 0,
19                               webDavHostName = NULL)
20         {
21             self$token      <- token
22             self$http       <- http
23             self$httpParser <- httpParser
24             self$numRetries <- numRetries
25
26             private$rawHostName    <- rawHost
27             private$webDavHostName <- webDavHostName
28         },
29
30         setNumConnRetries = function(newNumOfRetries)
31         {
32             self$numRetries <- newNumOfRetries
33         },
34
35         getWebDavHostName = function()
36         {
37             if(is.null(private$webDavHostName))
38             {
39                 publicConfigURL <- paste0("https://", private$rawHostName,
40                                                "/arvados/v1/config")
41
42                 serverResponse <- self$http$exec("GET", publicConfigURL, retryTimes = self$numRetries)
43
44                 configDocument <- self$httpParser$parseJSONResponse(serverResponse)
45                 private$webDavHostName <- configDocument$Services$WebDAVDownload$ExternalURL
46
47                 if(is.null(private$webDavHostName))
48                     stop("Unable to find WebDAV server.")
49             }
50
51             private$webDavHostName
52         },
53
54         create = function(files, uuid)
55         {
56             sapply(files, function(filePath)
57             {
58                 private$createNewFile(filePath, uuid, "text/html")
59             })
60         },
61
62         delete = function(relativePath, uuid)
63         {
64             fileURL <- paste0(self$getWebDavHostName(), "c=",
65                               uuid, "/", relativePath);
66             headers <- list(Authorization = paste("OAuth2", self$token))
67
68             serverResponse <- self$http$exec("DELETE", fileURL, headers,
69                                              retryTimes = self$numRetries)
70
71             if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
72                 stop(paste("Server code:", serverResponse$status_code))
73
74             serverResponse
75         },
76
77         move = function(from, to, uuid)
78         {
79             collectionURL <- paste0(self$getWebDavHostName(), "c=", uuid, "/")
80             fromURL <- paste0(collectionURL, from)
81             toURL <- paste0(collectionURL, trimFromStart(to, "/"))
82
83             headers <- list("Authorization" = paste("OAuth2", self$token),
84                             "Destination" = toURL)
85
86             serverResponse <- self$http$exec("MOVE", fromURL, headers,
87                                              retryTimes = self$numRetries)
88
89             if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
90                 stop(paste("Server code:", serverResponse$status_code))
91
92             serverResponse
93         },
94
95         copy = function(from, to, uuid)
96         {
97             collectionURL <- paste0(self$getWebDavHostName(), "c=", uuid, "/")
98             fromURL <- paste0(collectionURL, from)
99             toURL <- paste0(collectionURL, trimFromStart(to, "/"))
100
101             headers <- list("Authorization" = paste("OAuth2", self$token),
102                             "Destination" = toURL)
103
104             serverResponse <- self$http$exec("COPY", fromURL, headers,
105                                              retryTimes = self$numRetries)
106
107             if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
108                 stop(paste("Server code:", serverResponse$status_code))
109
110             serverResponse
111         },
112
113         getCollectionContent = function(uuid)
114         {
115             collectionURL <- URLencode(paste0(self$getWebDavHostName(),
116                                               "c=", uuid))
117
118             headers <- list("Authorization" = paste("Bearer", self$token))
119
120             response <- self$http$exec("PROPFIND", collectionURL, headers,
121                                        retryTimes = self$numRetries)
122
123             if(all(response == ""))
124                 stop("Response is empty, request may be misconfigured")
125
126             if(response$status_code < 200 || response$status_code >= 300)
127                 stop(paste("Server code:", response$status_code))
128
129             self$httpParser$getFileNamesFromResponse(response, collectionURL)
130         },
131
132         getResourceSize = function(relativePath, uuid)
133         {
134             collectionURL <- URLencode(paste0(self$getWebDavHostName(),
135                                               "c=", uuid))
136
137             subcollectionURL <- paste0(collectionURL, "/", relativePath);
138
139             headers <- list("Authorization" = paste("OAuth2", self$token))
140
141             response <- self$http$exec("PROPFIND", subcollectionURL, headers,
142                                        retryTimes = self$numRetries)
143
144             if(all(response == ""))
145                 stop("Response is empty, request may be misconfigured")
146
147             if(response$status_code < 200 || response$status_code >= 300)
148                 stop(paste("Server code:", response$status_code))
149
150             sizes <- self$httpParser$getFileSizesFromResponse(response,
151                                                               collectionURL)
152             as.numeric(sizes)
153         },
154
155         read = function(relativePath, uuid, contentType = "raw", offset = 0, length = 0)
156         {
157             fileURL <- paste0(self$getWebDavHostName(),
158                              "c=", uuid, "/", relativePath);
159
160             range <- paste0("bytes=", offset, "-")
161
162             if(length > 0)
163                 range = paste0(range, offset + length - 1)
164
165             if(offset == 0 && length == 0)
166             {
167                 headers <- list(Authorization = paste("OAuth2", self$token))
168             }
169             else
170             {
171                 headers <- list(Authorization = paste("OAuth2", self$token),
172                                 Range = range)
173             }
174
175             if(!(contentType %in% self$httpParser$validContentTypes))
176                 stop("Invalid contentType. Please use text or raw.")
177
178             serverResponse <- self$http$exec("GET", fileURL, headers,
179                                              retryTimes = self$numRetries)
180
181             if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
182                 stop(paste("Server code:", serverResponse$status_code))
183
184             self$httpParser$parseResponse(serverResponse, contentType)
185         },
186
187         write = function(relativePath, uuid, content, contentType)
188         {
189             fileURL <- paste0(self$getWebDavHostName(),
190                              "c=", uuid, "/", relativePath);
191             headers <- list(Authorization = paste("OAuth2", self$token),
192                             "Content-Type" = contentType)
193             body <- content
194
195             serverResponse <- self$http$exec("PUT", fileURL, headers, body,
196                                              retryTimes = self$numRetries)
197
198             if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
199                 stop(paste("Server code:", serverResponse$status_code))
200
201             self$httpParser$parseResponse(serverResponse, "text")
202         },
203
204         getConnection = function(relativePath, uuid, openMode)
205         {
206             fileURL <- paste0(self$getWebDavHostName(),
207                               "c=", uuid, "/", relativePath);
208             headers <- list(Authorization = paste("OAuth2", self$token))
209
210             conn <- self$http$getConnection(fileURL, headers, openMode)
211         }
212     ),
213
214     private = list(
215
216         webDavHostName = NULL,
217         rawHostName    = NULL,
218
219         createNewFile = function(relativePath, uuid, contentType)
220         {
221             fileURL <- paste0(self$getWebDavHostName(), "c=",
222                               uuid, "/", relativePath)
223             headers <- list(Authorization = paste("OAuth2", self$token),
224                             "Content-Type" = contentType)
225             body <- NULL
226
227             serverResponse <- self$http$exec("PUT", fileURL, headers, body,
228                                              retryTimes = self$numRetries)
229
230             if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
231                 stop(paste("Server code:", serverResponse$status_code))
232
233             paste("File created:", relativePath)
234         }
235     ),
236
237     cloneable = FALSE
238 )