Arvados class now accepts additional parameter which specifies max
[arvados.git] / sdk / R / R / Arvados.R
index a546f85fde6e4e7cdfb13093ecfa8310e78840bb..ea4384b5b47ecb97d9b2aaa0348b79738ae2e180 100644 (file)
@@ -1,6 +1,6 @@
+source("./R/RESTService.R")
 source("./R/HttpRequest.R")
 source("./R/HttpParser.R")
-source("./R/custom_classes.R")
 
 #' Arvados SDK Object
 #'
@@ -8,145 +8,176 @@ source("./R/custom_classes.R")
 #'
 #' @field token Token represents user authentification token.
 #' @field host Host represents server name we wish to connect to.
-#' @examples arv = Arvados("token", "host_name")
+#' @examples arv = Arvados$new("token", "host_name")
 #' @export Arvados
-Arvados <- setRefClass(
+Arvados <- R6::R6Class(
 
     "Arvados",
 
-    fields = list(
+    public = list(
 
-        getToken          = "function",
-        getHostName       = "function",
-
-        #Todo(Fudo): These are hardcoded and for debug only. Remove them later on.
-        getWebDavToken    = "function",
-        getWebDavHostName = "function",
-
-        collection_get    = "function",
-        collection_list   = "function",
-        collection_create = "function",
-        collection_update = "function",
-        collection_delete = "function"
-    ),
+        initialize = function(authToken = NULL, hostName = NULL, numRetries = 0)
+        {
+            if(!is.null(hostName))
+               Sys.setenv(ARVADOS_API_HOST = hostName)
 
-    methods = list(
+            if(!is.null(authToken))
+                Sys.setenv(ARVADOS_API_TOKEN = authToken)
 
-        initialize = function(auth_token = NULL, host_name = NULL, webDavToken = NULL, webDavHostName = NULL) 
-        {
-            # Private state
-            if(!is.null(host_name))
-               Sys.setenv(ARVADOS_API_HOST  = host_name)
+            hostName  <- Sys.getenv("ARVADOS_API_HOST");
+            token     <- Sys.getenv("ARVADOS_API_TOKEN");
 
-            if(!is.null(auth_token))
-                Sys.setenv(ARVADOS_API_TOKEN = auth_token)
+            if(hostName == "" | token == "")
+                stop(paste("Please provide host name and authentification token",
+                           "or set ARVADOS_API_HOST and ARVADOS_API_TOKEN",
+                           "environment variables."))
 
-            host  <- Sys.getenv("ARVADOS_API_HOST");
-            token <- Sys.getenv("ARVADOS_API_TOKEN");
+            private$numRetries  <- numRetries
+            private$REST  <- RESTService$new(token, hostName,
+                                             HttpRequest$new(), HttpParser$new(),
+                                             numRetries)
 
-            if(host == "" | token == "")
-                stop("Please provide host name and authentification token or set ARVADOS_API_HOST and ARVADOS_API_TOKEN environmental variables.")
+            private$token <- private$REST$token
+            private$host  <- private$REST$hostName
+        },
 
-            version <- "v1"
-            host  <- paste0("https://", host, "/arvados/", version, "/")
+        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,
 
-            # Public methods
-            getToken <<- function() { token }
-            getHostName <<- function() { host }
+        getNumRetries = function() private$REST$numRetries,
+        setNumRetries = function(newNumOfRetries)
+        {
+            private$REST$setNumRetries(newNumOfRetries)
+        },
 
-            #Todo(Fudo): Hardcoded credentials to WebDAV server. Remove them later
-            getWebDavToken    <<- function() { webDavToken }
-            getWebDavHostName <<- function() { webDavHostName }
+        getCollection = function(uuid)
+        {
+            collection <- private$REST$getResource("collections", uuid)
+            collection
+        },
 
-            collection_get <<- function(uuid) 
-            {
-                collection_url <- paste0(host, "collections/", uuid)
-                headers <- list(Authorization = paste("OAuth2", token))
+        listCollections = function(filters = NULL, limit = 100, offset = 0)
+        {
+            if(!is.null(filters))
+                names(filters) <- c("collection")
 
-                http <- HttpRequest() 
-                serverResponse <- http$GET(collection_url, headers)
+            collections <- private$REST$listResources("collections", filters,
+                                                      limit, offset)
+            collections
+        },
 
-                httpParser <- HttpParser()
-                collection <- httpParser$parseJSONResponse(serverResponse)
+        listAllCollections = function(filters = NULL)
+        {
+            if(!is.null(filters))
+                names(filters) <- c("collection")
 
-                if(!is.null(collection$errors))
-                    stop(collection$errors)       
+            collectionURL <- paste0(private$host, "collections")
+            allCollection <- private$REST$fetchAllItems(collectionURL, filters)
+            allCollection
+        },
 
-                collection
-            }
+        deleteCollection = function(uuid)
+        {
+            removedCollection <- private$REST$deleteResource("collections", uuid)
+            removedCollection
+        },
 
-            collection_list <<- function(filters = NULL, limit = 100, offset = 0) 
-            {
-                collection_url <- paste0(host, "collections")
-                headers <- list(Authorization = paste("OAuth2", token))
+        updateCollection = function(uuid, newContent)
+        {
+            body <- list(list())
+            names(body) <- c("collection")
+            body$collection <- newContent
 
-                http <- HttpRequest() 
-                serverResponse <- http$GET(collection_url, headers, NULL, filters, limit, offset)
+            updatedCollection <- private$REST$updateResource("collections",
+                                                             uuid, body)
+            updatedCollection
+        },
 
-                httpParser <- HttpParser()
-                collection <- httpParser$parseJSONResponse(serverResponse)
+        createCollection = function(content)
+        {
+            body <- list(list())
+            names(body) <- c("collection")
+            body$collection <- content
 
-                if(!is.null(collection$errors))
-                    stop(collection$errors)       
+            newCollection <- private$REST$createResource("collections", body)
+            newCollection
+        },
 
-                collection
-            }
+        getProject = function(uuid)
+        {
+            project <- private$REST$getResource("groups", uuid)
+            project
+        },
 
-            collection_delete <<- function(uuid) 
-            {
-                collection_url <- paste0(host, "collections/", uuid)
-                headers <- list("Authorization" = paste("OAuth2", token),
-                                "Content-Type"  = "application/json")
+        createProject = function(content)
+        {
+            body <- list(list())
+            names(body) <- c("group")
+            body$group <- c("group_class" = "project", content)
 
-                http <- HttpRequest() 
-                serverResponse <- http$DELETE(collection_url, headers)
+            newProject <- private$REST$createResource("groups", body)
+            newProject
+        },
 
-                httpParser <- HttpParser()
-                collection <- httpParser$parseJSONResponse(serverResponse)
+        updateProject = function(uuid, newContent)
+        {
+            body <- list(list())
+            names(body) <- c("group")
+            body$group <- newContent
 
-                if(!is.null(collection$errors))
-                    stop(collection$errors)       
+            updatedProject <- private$REST$updateResource("groups", uuid, body)
+            updatedProject
+        },
 
-                collection
-            }
+        listProjects = function(filters = NULL, limit = 100, offset = 0)
+        {
+            if(!is.null(filters))
+                names(filters) <- c("groups")
 
-            collection_update <<- function(uuid, body) 
-            {
-                collection_url <- paste0(host, "collections/", uuid)
-                headers <- list("Authorization" = paste("OAuth2", token),
-                                "Content-Type"  = "application/json")
-                body <- jsonlite::toJSON(body, auto_unbox = T)
+            filters[[length(filters) + 1]] <- list("group_class", "=", "project")
 
-                http <- HttpRequest() 
-                serverResponse <- http$PUT(collection_url, headers, body)
+            projects <- private$REST$listResources("groups", filters, limit, offset)
+            projects
+        },
 
-                httpParser <- HttpParser()
-                collection <- httpParser$parseJSONResponse(serverResponse)
+        listAllProjects = function(filters = NULL)
+        {
+            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
-            }
+            projectURL <- paste0(private$host, "groups")
 
-            collection_create <<- function(body) 
-            {
-                collection_url <- paste0(host, "collections")
-                headers <- list("Authorization" = paste("OAuth2", token),
-                                "Content-Type"  = "application/json")
-                body <- jsonlite::toJSON(body, auto_unbox = T)
+            result <- private$REST$fetchAllItems(projectURL, filters)
+            result
+        },
 
-                http <- HttpRequest() 
-                serverResponse <- http$POST(collection_url, headers, body)
+        deleteProject = function(uuid)
+        {
+            removedProject <- private$REST$deleteResource("groups", uuid)
+            removedProject
+        }
+    ),
 
-                httpParser <- HttpParser()
-                collection <- httpParser$parseJSONResponse(serverResponse)
+    private = list(
 
-                if(!is.null(collection$errors))
-                    stop(collection$errors)       
+        token      = NULL,
+        host       = NULL,
+        REST       = NULL,
+        numRetries = NULL
+    ),
 
-                collection
-            }
-        }
-    )
+    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")
+}