Arvados class now accepts additional parameter which specifies max
[arvados.git] / sdk / R / R / Arvados.R
index b44e105af7ce711bb7629b94f22a2e52dd51e53a..ea4384b5b47ecb97d9b2aaa0348b79738ae2e180 100644 (file)
@@ -1,3 +1,4 @@
+source("./R/RESTService.R")
 source("./R/HttpRequest.R")
 source("./R/HttpParser.R")
 
@@ -5,70 +6,178 @@ source("./R/HttpParser.R")
 #'
 #' All Arvados logic is inside this class
 #'
-#' @field token represents user authentification token.
-#' @field host represents server name we wish to connect to.
+#' @field token Token represents user authentification token.
+#' @field host Host represents server name we wish to connect to.
+#' @examples arv = Arvados$new("token", "host_name")
 #' @export Arvados
-Arvados <- setRefClass(
+Arvados <- R6::R6Class(
 
     "Arvados",
 
-    fields = list(
-        token = "character",
-        host  = "character"
-    ),
+    public = list(
+
+        initialize = function(authToken = NULL, hostName = NULL, numRetries = 0)
+        {
+            if(!is.null(hostName))
+               Sys.setenv(ARVADOS_API_HOST = hostName)
+
+            if(!is.null(authToken))
+                Sys.setenv(ARVADOS_API_TOKEN = authToken)
+
+            hostName  <- Sys.getenv("ARVADOS_API_HOST");
+            token     <- Sys.getenv("ARVADOS_API_TOKEN");
+
+            if(hostName == "" | token == "")
+                stop(paste("Please provide host name and authentification token",
+                           "or set ARVADOS_API_HOST and ARVADOS_API_TOKEN",
+                           "environment variables."))
+
+            private$numRetries  <- numRetries
+            private$REST  <- RESTService$new(token, hostName,
+                                             HttpRequest$new(), HttpParser$new(),
+                                             numRetries)
 
-    methods = list(
+            private$token <- private$REST$token
+            private$host  <- private$REST$hostName
+        },
 
-        initialize = function(auth_token, host_name) 
+        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,
+
+        getNumRetries = function() private$REST$numRetries,
+        setNumRetries = function(newNumOfRetries)
+        {
+            private$REST$setNumRetries(newNumOfRetries)
+        },
+
+        getCollection = function(uuid)
         {
-            #Todo(Fudo): Validate token
-            token <<- auth_token
-            host  <<- host_name
+            collection <- private$REST$getResource("collections", uuid)
+            collection
+        },
+
+        listCollections = function(filters = NULL, limit = 100, offset = 0)
+        {
+            if(!is.null(filters))
+                names(filters) <- c("collection")
+
+            collections <- private$REST$listResources("collections", filters,
+                                                      limit, offset)
+            collections
+        },
+
+        listAllCollections = function(filters = NULL)
+        {
+            if(!is.null(filters))
+                names(filters) <- c("collection")
+
+            collectionURL <- paste0(private$host, "collections")
+            allCollection <- private$REST$fetchAllItems(collectionURL, filters)
+            allCollection
+        },
+
+        deleteCollection = function(uuid)
+        {
+            removedCollection <- private$REST$deleteResource("collections", uuid)
+            removedCollection
+        },
+
+        updateCollection = function(uuid, newContent)
+        {
+            body <- list(list())
+            names(body) <- c("collection")
+            body$collection <- newContent
+
+            updatedCollection <- private$REST$updateResource("collections",
+                                                             uuid, body)
+            updatedCollection
+        },
+
+        createCollection = function(content)
+        {
+            body <- list(list())
+            names(body) <- c("collection")
+            body$collection <- content
+
+            newCollection <- private$REST$createResource("collections", body)
+            newCollection
+        },
+
+        getProject = function(uuid)
+        {
+            project <- private$REST$getResource("groups", uuid)
+            project
+        },
+
+        createProject = function(content)
+        {
+            body <- list(list())
+            names(body) <- c("group")
+            body$group <- c("group_class" = "project", content)
+
+            newProject <- private$REST$createResource("groups", body)
+            newProject
+        },
+
+        updateProject = function(uuid, newContent)
+        {
+            body <- list(list())
+            names(body) <- c("group")
+            body$group <- newContent
+
+            updatedProject <- private$REST$updateResource("groups", uuid, body)
+            updatedProject
+        },
+
+        listProjects = function(filters = NULL, limit = 100, offset = 0)
+        {
+            if(!is.null(filters))
+                names(filters) <- c("groups")
+
+            filters[[length(filters) + 1]] <- list("group_class", "=", "project")
+
+            projects <- private$REST$listResources("groups", filters, limit, offset)
+            projects
+        },
+
+        listAllProjects = function(filters = NULL)
+        {
+            if(!is.null(filters))
+                names(filters) <- c("groups")
+
+            filters[[length(filters) + 1]] <- list("group_class", "=", "project")
+
+            projectURL <- paste0(private$host, "groups")
+
+            result <- private$REST$fetchAllItems(projectURL, filters)
+            result
+        },
+
+        deleteProject = function(uuid)
+        {
+            removedProject <- private$REST$deleteResource("groups", uuid)
+            removedProject
         }
-    )
-)
+    ),
 
-#' collection_get
-#'
-#' Get Arvados collection
-#'
-#' @name collection_get
-#' @field uuid UUID of the given collection
-Arvados$methods(
-
-    collection_get = function(uuid) 
-    {
-        collection_relative_url <- paste0("collections/", uuid)
-        http_request <- HttpRequest("GET", token, host, collection_relative_url) 
-        server_response <- http_request$execute()
-
-        httpParser <- HttpParser()
-        collection <- httpParser$parseCollectionGet(server_response)
-        class(collection) <- "ArvadosCollection"
-
-        return(collection)
-    }
-)
+    private = list(
 
-#' collection_list
-#'
-#' List Arvados collections based on filter matching
-#'
-#' @name collection_list
-#' @field uuid UUID of the given collection
-Arvados$methods(
-
-    collection_list = function(filters = NULL, limit = NULL, offset = NULL) 
-    {
-        #Todo(Fudo): Implement limit and offset
-        collection_relative_url <- "collections"
-        http_request <- HttpRequest("GET", token, host, collection_relative_url, filters) 
-        server_response <- http_request$execute()
-
-        httpParser <- HttpParser()
-        collection <- httpParser$parseCollectionGet(server_response)
-        class(collection) <- "ArvadosCollectionList"
-
-        return(collection)
-    }
+        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")
+}