Arvados class now accepts additional parameter which specifies max
[arvados.git] / sdk / R / R / Arvados.R
index 582a7ab481393798d061f8d1cec574894bf421cd..ea4384b5b47ecb97d9b2aaa0348b79738ae2e180 100644 (file)
@@ -1,6 +1,6 @@
+source("./R/RESTService.R")
 source("./R/HttpRequest.R")
 source("./R/HttpParser.R")
-source("./R/RESTService.R")
 
 #' Arvados SDK Object
 #'
@@ -16,97 +16,56 @@ Arvados <- R6::R6Class(
 
     public = list(
 
-        initialize = function(auth_token = NULL, host_name = NULL)
+        initialize = function(authToken = NULL, hostName = NULL, numRetries = 0)
         {
-            if(!is.null(host_name))
-               Sys.setenv(ARVADOS_API_HOST = host_name)
+            if(!is.null(hostName))
+               Sys.setenv(ARVADOS_API_HOST = hostName)
 
-            if(!is.null(auth_token))
-                Sys.setenv(ARVADOS_API_TOKEN = auth_token)
+            if(!is.null(authToken))
+                Sys.setenv(ARVADOS_API_TOKEN = authToken)
 
-            host_name  <- Sys.getenv("ARVADOS_API_HOST");
-            token <- Sys.getenv("ARVADOS_API_TOKEN");
+            hostName  <- Sys.getenv("ARVADOS_API_HOST");
+            token     <- Sys.getenv("ARVADOS_API_TOKEN");
 
-            if(host_name == "" | token == "")
-                stop(paste0("Please provide host name and authentification token",
-                            " or set ARVADOS_API_HOST and ARVADOS_API_TOKEN",
-                            " environment variables."))
+            if(hostName == "" | token == "")
+                stop(paste("Please provide host name and authentification token",
+                           "or set ARVADOS_API_HOST and ARVADOS_API_TOKEN",
+                           "environment variables."))
 
-            version <- "v1"
-            host  <- paste0("https://", host_name, "/arvados/", version, "/")
+            private$numRetries  <- numRetries
+            private$REST  <- RESTService$new(token, hostName,
+                                             HttpRequest$new(), HttpParser$new(),
+                                             numRetries)
 
-            private$http       <- HttpRequest$new()
-            private$httpParser <- HttpParser$new()
-            private$REST       <- RESTService$new(self)
-            private$token      <- token
-            private$host       <- host
-            private$rawHost    <- host_name
+            private$token <- private$REST$token
+            private$host  <- private$REST$hostName
         },
 
-        getToken    = function() private$token,
-        getHostName = function() private$host,
-
-        getHttpClient = function() private$http,
-        setHttpClient = function(newClient) private$http <- newClient,
-
-        getHttpParser = function() private$httpParser,
-        setHttpParser = function(newParser) private$httpParser <- newParser,
-
-        getRESTService = function() private$REST,
-        setRESTService = function(newRESTService) private$REST <- newRESTService,
+        getToken          = function() private$REST$token,
+        getHostName       = function() private$REST$hostName,
+        getWebDavHostName = function() private$REST$getWebDavHostName(),
+        getRESTService    = function() private$REST,
+        setRESTService    = function(newRESTService) private$REST <- newRESTService,
 
-        getWebDavHostName = function()
+        getNumRetries = function() private$REST$numRetries,
+        setNumRetries = function(newNumOfRetries)
         {
-            if(is.null(private$webDavHostName))
-            {
-                discoveryDocumentURL <- paste0("https://", private$rawHost,
-                                               "/discovery/v1/apis/arvados/v1/rest")
-
-                headers <- list(Authorization = paste("OAuth2", private$token))
-
-                serverResponse <- private$http$GET(discoveryDocumentURL, headers)
-
-                discoveryDocument <- private$httpParser$parseJSONResponse(serverResponse)
-                private$webDavHostName <- discoveryDocument$keepWebServiceUrl
-
-                if(is.null(private$webDavHostName))
-                    stop("Unable to find WebDAV server.")
-            }
-
-            private$webDavHostName
+            private$REST$setNumRetries(newNumOfRetries)
         },
 
         getCollection = function(uuid)
         {
-            collectionURL <- paste0(private$host, "collections/", uuid)
-            headers <- list(Authorization = paste("OAuth2", private$token))
-
-            serverResponse <- private$http$GET(collectionURL, headers)
-
-            collection <- private$httpParser$parseJSONResponse(serverResponse)
-
-            if(!is.null(collection$errors))
-                stop(collection$errors)
-
+            collection <- private$REST$getResource("collections", uuid)
             collection
         },
 
         listCollections = function(filters = NULL, limit = 100, offset = 0)
         {
-            collectionURL <- paste0(private$host, "collections")
-            headers <- list(Authorization = paste("OAuth2", private$token))
-
             if(!is.null(filters))
                 names(filters) <- c("collection")
 
-            serverResponse <- private$http$GET(collectionURL, headers, filters,
-                                               limit, offset)
-
-            collections <- private$httpParser$parseJSONResponse(serverResponse)
-
-            if(!is.null(collections$errors))
-                stop(collections$errors)
-
+            collections <- private$REST$listResources("collections", filters,
+                                                      limit, offset)
             collections
         },
 
@@ -116,145 +75,71 @@ Arvados <- R6::R6Class(
                 names(filters) <- c("collection")
 
             collectionURL <- paste0(private$host, "collections")
-            private$fetchAllItems(collectionURL, filters)
+            allCollection <- private$REST$fetchAllItems(collectionURL, filters)
+            allCollection
         },
 
         deleteCollection = function(uuid)
         {
-            collectionURL <- paste0(private$host, "collections/", uuid)
-            headers <- list("Authorization" = paste("OAuth2", private$token),
-                            "Content-Type"  = "application/json")
-
-            serverResponse <- private$http$DELETE(collectionURL, headers)
-
-            collection <- private$httpParser$parseJSONResponse(serverResponse)
-
-            if(!is.null(collection$errors))
-                stop(collection$errors)
-
-            collection
+            removedCollection <- private$REST$deleteResource("collections", uuid)
+            removedCollection
         },
 
         updateCollection = function(uuid, newContent)
         {
-            collectionURL <- paste0(private$host, "collections/", uuid)
-            headers <- list("Authorization" = paste("OAuth2", private$token),
-                            "Content-Type"  = "application/json")
-
             body <- list(list())
-            #test if this is needed
             names(body) <- c("collection")
             body$collection <- newContent
 
-            body <- jsonlite::toJSON(body, auto_unbox = T)
-
-            serverResponse <- private$http$PUT(collectionURL, headers, body)
-
-            collection <- private$httpParser$parseJSONResponse(serverResponse)
-
-            if(!is.null(collection$errors))
-                stop(collection$errors)
-
-            collection
+            updatedCollection <- private$REST$updateResource("collections",
+                                                             uuid, body)
+            updatedCollection
         },
 
         createCollection = function(content)
         {
-            collectionURL <- paste0(private$host, "collections")
-            headers <- list("Authorization" = paste("OAuth2", private$token),
-                            "Content-Type"  = "application/json")
-
             body <- list(list())
             names(body) <- c("collection")
             body$collection <- content
 
-            body <- jsonlite::toJSON(body, auto_unbox = T)
-
-            serverResponse <- private$http$POST(collectionURL, headers, body)
-
-            collection <- private$httpParser$parseJSONResponse(serverResponse)
-
-            if(!is.null(collection$errors))
-                stop(collection$errors)
-
-            collection
+            newCollection <- private$REST$createResource("collections", body)
+            newCollection
         },
 
         getProject = function(uuid)
         {
-            projectURL <- paste0(private$host, "groups/", uuid)
-            headers <- list(Authorization = paste("OAuth2", private$token))
-
-            serverResponse <- private$http$GET(projectURL, headers)
-
-            project <- private$httpParser$parseJSONResponse(serverResponse)
-
-            if(!is.null(project$errors))
-                stop(project$errors)
-
+            project <- private$REST$getResource("groups", uuid)
             project
         },
 
         createProject = function(content)
         {
-            projectURL <- paste0(private$host, "groups")
-            headers <- list("Authorization" = paste("OAuth2", private$token),
-                            "Content-Type"  = "application/json")
-
             body <- list(list())
             names(body) <- c("group")
             body$group <- c("group_class" = "project", content)
-            body <- jsonlite::toJSON(body, auto_unbox = T)
-
-            serverResponse <- private$http$POST(projectURL, headers, body)
-
-            project <- private$httpParser$parseJSONResponse(serverResponse)
-
-            if(!is.null(project$errors))
-                stop(project$errors)
 
-            project
+            newProject <- private$REST$createResource("groups", body)
+            newProject
         },
 
         updateProject = function(uuid, newContent)
         {
-            projectURL <- paste0(private$host, "groups/", uuid)
-            headers <- list("Authorization" = paste("OAuth2", private$token),
-                            "Content-Type"  = "application/json")
-
             body <- list(list())
             names(body) <- c("group")
             body$group <- newContent
-            body <- jsonlite::toJSON(body, auto_unbox = T)
-
-            serverResponse <- private$http$PUT(projectURL, headers, body)
-
-            project <- private$httpParser$parseJSONResponse(serverResponse)
 
-            if(!is.null(project$errors))
-                stop(project$errors)
-
-            project
+            updatedProject <- private$REST$updateResource("groups", uuid, body)
+            updatedProject
         },
 
         listProjects = function(filters = NULL, limit = 100, offset = 0)
         {
-            projectURL <- paste0(private$host, "groups")
-            headers <- list(Authorization = paste("OAuth2", private$token))
-
             if(!is.null(filters))
                 names(filters) <- c("groups")
 
             filters[[length(filters) + 1]] <- list("group_class", "=", "project")
 
-            serverResponse <- private$http$GET(projectURL, headers, filters,
-                                               limit, offset)
-
-            projects <- private$httpParser$parseJSONResponse(serverResponse)
-
-            if(!is.null(projects$errors))
-                stop(projects$errors)
-
+            projects <- private$REST$listResources("groups", filters, limit, offset)
             projects
         },
 
@@ -267,64 +152,32 @@ Arvados <- R6::R6Class(
 
             projectURL <- paste0(private$host, "groups")
 
-            private$fetchAllItems(projectURL, filters)
+            result <- private$REST$fetchAllItems(projectURL, filters)
+            result
         },
 
         deleteProject = function(uuid)
         {
-            projectURL <- paste0(private$host, "groups/", uuid)
-            headers <- list("Authorization" = paste("OAuth2", private$token),
-                            "Content-Type"  = "application/json")
-
-            serverResponse <- private$http$DELETE(projectURL, headers)
-
-            project <- private$httpParser$parseJSONResponse(serverResponse)
-
-            if(!is.null(project$errors))
-                stop(project$errors)
-
-            project
+            removedProject <- private$REST$deleteResource("groups", uuid)
+            removedProject
         }
     ),
 
     private = list(
 
-        token          = NULL,
-        host           = NULL,
-        rawHost        = NULL,
-        webDavHostName = NULL,
-        http           = NULL,
-        httpParser     = NULL,
-        REST           = NULL,
-
-        fetchAllItems = function(resourceURL, filters)
-        {
-            headers <- list(Authorization = paste("OAuth2", private$token))
-
-            offset <- 0
-            itemsAvailable <- .Machine$integer.max
-            items <- c()
-            while(length(items) < itemsAvailable)
-            {
-                serverResponse <- private$http$GET(url          = resourceURL,
-                                                   headers      = headers,
-                                                   queryFilters = filters,
-                                                   limit        = NULL,
-                                                   offset       = offset)
-
-                parsedResponse <- private$httpParser$parseJSONResponse(serverResponse)
-
-                if(!is.null(parsedResponse$errors))
-                    stop(parsedResponse$errors)
-
-                items          <- c(items, parsedResponse$items)
-                offset         <- length(items)
-                itemsAvailable <- parsedResponse$items_available
-            }
-
-            items
-        }
+        token      = NULL,
+        host       = NULL,
+        REST       = NULL,
+        numRetries = NULL
     ),
 
     cloneable = FALSE
 )
+
+#' @export print.Arvados
+print.Arvados = function(arvados)
+{
+    cat(paste0("Type:  ", "\"", "Arvados",             "\""), sep = "\n")
+    cat(paste0("Host:  ", "\"", arvados$getHostName(), "\""), sep = "\n")
+    cat(paste0("Token: ", "\"", arvados$getToken(),    "\""), sep = "\n")
+}