From 78de557466248e1a193e07ae1945b29a23fc56a8 Mon Sep 17 00:00:00 2001 From: Fuad Muhic Date: Thu, 25 Jan 2018 15:12:42 +0100 Subject: [PATCH] ArvadosFile's connection method now returns curl connection instead of textConnection when called with 'r' parameter Arvados-DCO-1.1-Signed-off-by: Fuad Muhic --- sdk/R/R/ArvadosFile.R | 7 +++++-- sdk/R/R/HttpRequest.R | 2 +- sdk/R/R/RESTService.R | 14 ++++++++++++++ sdk/R/tests/testthat/fakes/FakeRESTService.R | 8 ++++++++ sdk/R/tests/testthat/test-ArvadosFile.R | 13 ++++++------- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/sdk/R/R/ArvadosFile.R b/sdk/R/R/ArvadosFile.R index 8ca818b828..ffb9a6b848 100644 --- a/sdk/R/R/ArvadosFile.R +++ b/sdk/R/R/ArvadosFile.R @@ -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") { diff --git a/sdk/R/R/HttpRequest.R b/sdk/R/R/HttpRequest.R index ebfb0c607d..ec364e5420 100644 --- a/sdk/R/R/HttpRequest.R +++ b/sdk/R/R/HttpRequest.R @@ -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) diff --git a/sdk/R/R/RESTService.R b/sdk/R/R/RESTService.R index c4790ae119..65c5302ba4 100644 --- a/sdk/R/R/RESTService.R +++ b/sdk/R/R/RESTService.R @@ -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 } ), diff --git a/sdk/R/tests/testthat/fakes/FakeRESTService.R b/sdk/R/tests/testthat/fakes/FakeRESTService.R index 9c7203f756..d370e87fbe 100644 --- a/sdk/R/tests/testthat/fakes/FakeRESTService.R +++ b/sdk/R/tests/testthat/fakes/FakeRESTService.R @@ -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 } ), diff --git a/sdk/R/tests/testthat/test-ArvadosFile.R b/sdk/R/tests/testthat/test-ArvadosFile.R index 4897056a1a..43c841bf8c 100644 --- a/sdk/R/tests/testthat/test-ArvadosFile.R +++ b/sdk/R/tests/testthat/test-ArvadosFile.R @@ -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) -- 2.30.2