Arvados class now accepts additional parameter which specifies max
[arvados.git] / sdk / R / R / Arvados.R
index 7d61d773c03b44d3914e57e53ece449d0d5c3c8e..ea4384b5b47ecb97d9b2aaa0348b79738ae2e180 100644 (file)
@@ -1,3 +1,4 @@
+source("./R/RESTService.R")
 source("./R/HttpRequest.R")
 source("./R/HttpParser.R")
 
@@ -15,134 +16,168 @@ 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  <- 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 == "" | token == "")
-                stop("Please provide host name and authentification token or set ARVADOS_API_HOST and ARVADOS_API_TOKEN environmental variables.")
+            if(hostName == "" | token == "")
+                stop(paste("Please provide host name and authentification token",
+                           "or set ARVADOS_API_HOST and ARVADOS_API_TOKEN",
+                           "environment variables."))
 
-            discoveryDocumentURL <- paste0("https://", host, "/discovery/v1/apis/arvados/v1/rest")
+            private$numRetries  <- numRetries
+            private$REST  <- RESTService$new(token, hostName,
+                                             HttpRequest$new(), HttpParser$new(),
+                                             numRetries)
 
-            version <- "v1"
-            host  <- paste0("https://", host, "/arvados/", version, "/")
-
-            private$http       <- HttpRequest$new()
-            private$httpParser <- HttpParser$new()
-            private$token      <- token
-            private$host       <- host
-            
-            headers <- list(Authorization = paste("OAuth2", private$token))
-
-            serverResponse <- private$http$GET(discoveryDocumentURL, headers)
-
-            discoveryDocument <- private$httpParser$parseJSONResponse(serverResponse)
-            private$webDavHostName <- discoveryDocument$keepWebServiceUrl
+            private$token <- private$REST$token
+            private$host  <- private$REST$hostName
         },
 
-        getToken    = function() private$token,
-        getHostName = function() private$host,
+        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,
 
-        #Todo(Fudo): Hardcoded credentials to WebDAV server. Remove them later
-        getWebDavHostName = function() private$webDavHostName,
-
-        getCollection = function(uuid) 
+        getNumRetries = function() private$REST$numRetries,
+        setNumRetries = function(newNumOfRetries)
         {
-            collectionURL <- paste0(private$host, "collections/", uuid)
-            headers <- list(Authorization = paste("OAuth2", private$token))
-
-            serverResponse <- private$http$GET(collectionURL, headers)
+            private$REST$setNumRetries(newNumOfRetries)
+        },
 
-            collection <- private$httpParser$parseJSONResponse(serverResponse)
+        getCollection = function(uuid)
+        {
+            collection <- private$REST$getResource("collections", uuid)
+            collection
+        },
 
-            if(!is.null(collection$errors))
-                stop(collection$errors)       
+        listCollections = function(filters = NULL, limit = 100, offset = 0)
+        {
+            if(!is.null(filters))
+                names(filters) <- c("collection")
 
-            collection
+            collections <- private$REST$listResources("collections", filters,
+                                                      limit, offset)
+            collections
         },
 
-        listCollections = function(filters = NULL, limit = 100, offset = 0) 
+        listAllCollections = function(filters = NULL)
         {
+            if(!is.null(filters))
+                names(filters) <- c("collection")
+
             collectionURL <- paste0(private$host, "collections")
-            headers <- list(Authorization = paste("OAuth2", private$token))
+            allCollection <- private$REST$fetchAllItems(collectionURL, filters)
+            allCollection
+        },
 
-            serverResponse <- private$http$GET(collectionURL, headers, filters, limit, offset)
-            collection <- private$httpParser$parseJSONResponse(serverResponse)
+        deleteCollection = function(uuid)
+        {
+            removedCollection <- private$REST$deleteResource("collections", uuid)
+            removedCollection
+        },
 
-            if(!is.null(collection$errors))
-                stop(collection$errors)       
+        updateCollection = function(uuid, newContent)
+        {
+            body <- list(list())
+            names(body) <- c("collection")
+            body$collection <- newContent
 
-            collection
+            updatedCollection <- private$REST$updateResource("collections",
+                                                             uuid, body)
+            updatedCollection
         },
 
-        deleteCollection = function(uuid) 
+        createCollection = function(content)
         {
-            collectionURL <- paste0(private$host, "collections/", uuid)
-            headers <- list("Authorization" = paste("OAuth2", private$token),
-                            "Content-Type"  = "application/json")
+            body <- list(list())
+            names(body) <- c("collection")
+            body$collection <- content
 
-            serverResponse <- private$http$DELETE(collectionURL, headers)
+            newCollection <- private$REST$createResource("collections", body)
+            newCollection
+        },
 
-            collection <- private$httpParser$parseJSONResponse(serverResponse)
+        getProject = function(uuid)
+        {
+            project <- private$REST$getResource("groups", uuid)
+            project
+        },
 
-            if(!is.null(collection$errors))
-                stop(collection$errors)       
+        createProject = function(content)
+        {
+            body <- list(list())
+            names(body) <- c("group")
+            body$group <- c("group_class" = "project", content)
 
-            collection
+            newProject <- private$REST$createResource("groups", body)
+            newProject
         },
 
-        updateCollection = function(uuid, body) 
+        updateProject = function(uuid, newContent)
         {
-            collectionURL <- paste0(private$host, "collections/", 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(collectionURL, headers, body)
+            updatedProject <- private$REST$updateResource("groups", uuid, body)
+            updatedProject
+        },
 
-            collection <- private$httpParser$parseJSONResponse(serverResponse)
+        listProjects = function(filters = NULL, limit = 100, offset = 0)
+        {
+            if(!is.null(filters))
+                names(filters) <- c("groups")
 
-            if(!is.null(collection$errors))
-                stop(collection$errors)       
+            filters[[length(filters) + 1]] <- list("group_class", "=", "project")
 
-            collection
+            projects <- private$REST$listResources("groups", filters, limit, offset)
+            projects
         },
 
-        createCollection = function(body) 
+        listAllProjects = function(filters = NULL)
         {
-            collectionURL <- paste0(private$host, "collections")
-            headers <- list("Authorization" = paste("OAuth2", private$token),
-                            "Content-Type"  = "application/json")
-            body <- jsonlite::toJSON(body, auto_unbox = T)
+            if(!is.null(filters))
+                names(filters) <- c("groups")
 
-            serverResponse <- private$http$POST(collectionURL, headers, body)
+            filters[[length(filters) + 1]] <- list("group_class", "=", "project")
 
-            collection <- private$httpParser$parseJSONResponse(serverResponse)
+            projectURL <- paste0(private$host, "groups")
 
-            if(!is.null(collection$errors))
-                stop(collection$errors)       
+            result <- private$REST$fetchAllItems(projectURL, filters)
+            result
+        },
 
-            collection
+        deleteProject = function(uuid)
+        {
+            removedProject <- private$REST$deleteResource("groups", uuid)
+            removedProject
         }
-
     ),
-    
+
     private = list(
 
-        token          = NULL,
-        host           = NULL,
-        webDavHostName = NULL,
-        http           = NULL,
-        httpParser     = NULL
+        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")
+}