ArvadosFile's connection method now returns curl connection instead of
authorFuad Muhic <fmuhic@capeannenterprises.com>
Thu, 25 Jan 2018 14:12:42 +0000 (15:12 +0100)
committerFuad Muhic <fmuhic@capeannenterprises.com>
Thu, 25 Jan 2018 14:12:42 +0000 (15:12 +0100)
textConnection when called with 'r' parameter

Arvados-DCO-1.1-Signed-off-by: Fuad Muhic <fmuhic@capeannenterprises.com>

sdk/R/R/ArvadosFile.R
sdk/R/R/HttpRequest.R
sdk/R/R/RESTService.R
sdk/R/tests/testthat/fakes/FakeRESTService.R
sdk/R/tests/testthat/test-ArvadosFile.R

index 8ca818b828f0b567899e911f38dbbeed5c1a98c4..ffb9a6b848727c1ef81322b610210e1c3f6c30e6 100644 (file)
@@ -92,9 +92,12 @@ ArvadosFile <- R6::R6Class(
 
         connection = function(rw)
         {
-            if (rw == "r") 
+            if (rw == "r" || rw == "rb"
             {
-                return(textConnection(self$read("text")))
+                REST <- private$collection$getRESTService()
+                return(REST$getConnection(private$collection$uuid,
+                                          self$getRelativePath(),
+                                          rw))
             }
             else if (rw == "w") 
             {
index ebfb0c607d3f4107e2fe5ec08c5d3332198f6485..ec364e5420ae15903ae478f17c36f2ca2d39a58b 100644 (file)
@@ -58,7 +58,7 @@ HttpRequest <- R6::R6Class(
             curl::handle_setopt(h, customrequest = "PROPFIND")
             curl::handle_setheaders(h, .list = headers)
 
-        propfindResponse <- curl::curl_fetch_memory(url, h)
+            propfindResponse <- curl::curl_fetch_memory(url, h)
         },
 
         MOVE = function(url, headers = NULL)
index c4790ae119ad564d53e624bfc3819bfe4d15a21c..65c5302ba45208c6d7f12d73caa29bf7e940f79c 100644 (file)
@@ -279,6 +279,20 @@ RESTService <- R6::R6Class(
                 stop(paste("Server code:", serverResponse$status_code))
 
             self$httpParser$parseResponse(serverResponse, "text")
+        },
+
+        getConnection = function(uuid, relativePath, openMode)
+        {
+            fileURL <- paste0(self$getWebDavHostName(), 
+                              "c=", uuid, "/", relativePath);
+            headers <- list(Authorization = paste("OAuth2", self$token))
+
+            h <- curl::new_handle()
+            curl::handle_setheaders(h, .list = headers)
+
+            conn <- curl::curl(url = fileURL, open = openMode, handle = h)
+
+            conn
         }
     ),
 
index 9c7203f75618fa21deda6974043a1167ecdc9cf1..d370e87fbe7e3ca581e4a36ac3a2a149989f18e1 100644 (file)
@@ -18,6 +18,7 @@ FakeRESTService <- R6::R6Class(
         getResourceSizeCallCount      = NULL,
         readCallCount                 = NULL,
         writeCallCount                = NULL,
+        getConnectionCallCount        = NULL,
         writeBuffer                   = NULL,
         filtersAreConfiguredCorrectly = NULL,
         bodyIsConfiguredCorrectly     = NULL,
@@ -43,6 +44,7 @@ FakeRESTService <- R6::R6Class(
             self$getResourceSizeCallCount      <- 0
             self$readCallCount                 <- 0
             self$writeCallCount                <- 0
+            self$getConnectionCallCount        <- 0
             self$filtersAreConfiguredCorrectly <- FALSE
             self$bodyIsConfiguredCorrectly     <- FALSE
 
@@ -152,6 +154,12 @@ FakeRESTService <- R6::R6Class(
             self$writeBuffer <- content
             self$writeCallCount <- self$writeCallCount + 1
             self$returnContent
+        },
+
+        getConnection = function(relativePath, uuid, openMode)
+        {
+            self$getConnectionCallCount <- self$getConnectionCallCount + 1
+            self$returnContent
         }
     ),
 
index 4897056a1a8c11019df0212f8b5673c707a37670..43c841bf8ce7906e2261a29e8e497173a57fcf5c 100644 (file)
@@ -106,12 +106,12 @@ test_that("read delegates reading operation to REST service class", {
     expect_that(fakeREST$readCallCount, equals(1))
 }) 
 
-test_that(paste("connect returns textConnection opened",
-                "in read mode when 'r' is passed as argument"), {
+test_that(paste("connection delegates connection creation ro RESTService class",
+                "which returns curl connection opened in read mode when", 
+                "'r' of 'rb' is passed as argument"), {
 
     collectionContent <- c("animal", "animal/fish")
-    readContent <- "file content"
-    fakeREST <- FakeRESTService$new(collectionContent, readContent)
+    fakeREST <- FakeRESTService$new(collectionContent)
 
     api <- Arvados$new("myToken", "myHostName")
     api$setRESTService(fakeREST)
@@ -120,13 +120,12 @@ test_that(paste("connect returns textConnection opened",
 
     connection <- fish$connection("r")
 
-    expect_that(readLines(connection), equals("file content"))
+    expect_that(fakeREST$getConnectionCallCount, equals(1))
 }) 
 
-test_that(paste("connect returns textConnection opened",
+test_that(paste("connection returns textConnection opened",
                 "in write mode when 'w' is passed as argument"), {
 
-
     collectionContent <- c("animal", "animal/fish")
     fakeREST <- FakeRESTService$new(collectionContent)