Merge branch '21666-provision-test-improvement'
[arvados.git] / sdk / R / R / RESTService.R
index 867665299f49899909167c4fd838bc9ae1371dd8..5cbcb65f753b769ec108fa9f757ba6b1f219443d 100644 (file)
@@ -36,16 +36,13 @@ RESTService <- R6::R6Class(
         {
             if(is.null(private$webDavHostName))
             {
-                discoveryDocumentURL <- paste0("https://", private$rawHostName,
-                                               "/discovery/v1/apis/arvados/v1/rest")
+                publicConfigURL <- paste0("https://", private$rawHostName,
+                                               "/arvados/v1/config")
 
-                headers <- list(Authorization = paste("OAuth2", self$token))
-
-                serverResponse <- self$http$exec("GET", discoveryDocumentURL, headers,
-                                                 retryTimes = self$numRetries)
+                serverResponse <- self$http$exec("GET", publicConfigURL, retryTimes = self$numRetries)
 
-                discoveryDocument <- self$httpParser$parseJSONResponse(serverResponse)
-                private$webDavHostName <- discoveryDocument$keepWebServiceUrl
+                configDocument <- self$httpParser$parseJSONResponse(serverResponse)
+                private$webDavHostName <- configDocument$Services$WebDAVDownload$ExternalURL
 
                 if(is.null(private$webDavHostName))
                     stop("Unable to find WebDAV server.")
@@ -81,10 +78,10 @@ RESTService <- R6::R6Class(
         {
             collectionURL <- paste0(self$getWebDavHostName(), "c=", uuid, "/")
             fromURL <- paste0(collectionURL, from)
-            toURL <- paste0(collectionURL, to)
+            toURL <- paste0(collectionURL, trimFromStart(to, "/"))
 
             headers <- list("Authorization" = paste("OAuth2", self$token),
-                           "Destination" = toURL)
+                            "Destination" = toURL)
 
             serverResponse <- self$http$exec("MOVE", fromURL, headers,
                                              retryTimes = self$numRetries)
@@ -95,12 +92,31 @@ RESTService <- R6::R6Class(
             serverResponse
         },
 
-        getCollectionContent = function(uuid)
+        copy = function(from, to, uuid)
+        {
+            collectionURL <- paste0(self$getWebDavHostName(), "c=", uuid, "/")
+            fromURL <- paste0(collectionURL, from)
+            toURL <- paste0(collectionURL, trimFromStart(to, "/"))
+
+            headers <- list("Authorization" = paste("OAuth2", self$token),
+                            "Destination" = toURL)
+
+            serverResponse <- self$http$exec("COPY", fromURL, headers,
+                                             retryTimes = self$numRetries)
+
+            if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
+                stop(paste("Server code:", serverResponse$status_code))
+
+            serverResponse
+        },
+
+       getCollectionContent = function(uuid, relativePath = NULL)
+
         {
             collectionURL <- URLencode(paste0(self$getWebDavHostName(),
-                                              "c=", uuid))
+                                             "c=", uuid, "/", relativePath))
 
-            headers <- list("Authorization" = paste("OAuth2", self$token))
+            headers <- list("Authorization" = paste("Bearer", self$token))
 
             response <- self$http$exec("PROPFIND", collectionURL, headers,
                                        retryTimes = self$numRetries)
@@ -212,8 +228,12 @@ RESTService <- R6::R6Class(
             serverResponse <- self$http$exec("PUT", fileURL, headers, body,
                                              retryTimes = self$numRetries)
 
-            if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
-                stop(paste("Server code:", serverResponse$status_code))
+            if (serverResponse$status_code < 200){ # to wyrzuca błędy
+                stop(paste("Server code:", serverResponse$status_code))}
+            else if (serverResponse$status_code >= 300 & serverResponse$status_code < 422) {
+                stop(paste("Server code:", serverResponse$status_code))}
+            else if (serverResponse$status_code == 422 ) {
+                stop(paste("Project of that name already exists. If you want to change it use project_update() instead"))}
 
             paste("File created:", relativePath)
         }