Moved all classes from RefClass OOP model to R6 and made improvements
[arvados.git] / sdk / R / R / Arvados.R
index ab38763e1643dff9df47aa66ed668eb32c93d92a..196fcfb51f9969de7b5a9e1ff0dc7c4738e6b7b9 100644 (file)
@@ -1,6 +1,5 @@
 source("./R/HttpRequest.R")
 source("./R/HttpParser.R")
-source("./R/custom_classes.R")
 
 #' Arvados SDK Object
 #'
@@ -8,35 +7,16 @@ 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(
-
-        getToken          = "function",
-        getHostName       = "function",
-
-        #Todo(Fudo): This is for debug only. Remove it.
-        getWebDavToken    = "function",
-        getWebDavHostName = "function",
-        setWebDavToken    = "function",
-        setWebDavHostName = "function",
-
-        collection_get    = "function",
-        collection_list   = "function",
-        collection_create = "function",
-        collection_update = "function",
-        collection_delete = "function"
-    ),
-
-    methods = list(
+    public = list(
 
         initialize = function(auth_token = NULL, host_name = NULL) 
         {
-            # Private state
             if(!is.null(host_name))
                Sys.setenv(ARVADOS_API_HOST  = host_name)
 
@@ -49,108 +29,122 @@ Arvados <- setRefClass(
             if(host == "" | token == "")
                 stop("Please provide host name and authentification token or set ARVADOS_API_HOST and ARVADOS_API_TOKEN environmental variables.")
 
+            discoveryDocumentURL <- paste0("https://", host, "/discovery/v1/apis/arvados/v1/rest")
+
             version <- "v1"
             host  <- paste0("https://", host, "/arvados/", version, "/")
 
-            # Public methods
-            getToken <<- function() { token }
-            getHostName <<- function() { host }
+            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
+            print(private$webDavHostName)
+        },
+
+        getToken    = function() private$token,
+        getHostName = function() private$host,
 
-            #Todo(Fudo): Hardcoded credentials to WebDAV server. Remove them later
-            getWebDavToken    <<- function() { webDavToken }
-            getWebDavHostName <<- function() { webDavHostName }
-            setWebDavToken    <<- function(token) { webDavToken <<- token }
-            setWebDavHostName <<- function(hostName) { webDavHostName <<- hostName }
+        #Todo(Fudo): Hardcoded credentials to WebDAV server. Remove them later
+        getWebDavHostName = function() private$webDavHostName,
 
-            collection_get <<- function(uuid) 
-            {
-                collectionURL <- paste0(host, "collections/", uuid)
-                headers <- list(Authorization = paste("OAuth2", token))
+        getCollection = function(uuid) 
+        {
+            collectionURL <- paste0(private$host, "collections/", uuid)
+            headers <- list(Authorization = paste("OAuth2", private$token))
 
-                http <- HttpRequest() 
-                serverResponse <- http$GET(collectionURL, headers)
+            serverResponse <- private$http$GET(collectionURL, headers)
 
-                httpParser <- HttpParser()
-                collection <- httpParser$parseJSONResponse(serverResponse)
+            collection <- private$httpParser$parseJSONResponse(serverResponse)
 
-                if(!is.null(collection$errors))
-                    stop(collection$errors)       
+            if(!is.null(collection$errors))
+                stop(collection$errors)       
 
-                collection
-            }
+            collection
+        },
 
-            collection_list <<- function(filters = NULL, limit = 100, offset = 0) 
-            {
-                collectionURL <- paste0(host, "collections")
-                headers <- list(Authorization = paste("OAuth2", token))
+        listCollections = function(filters = NULL, limit = 100, offset = 0) 
+        {
+            collectionURL <- paste0(private$host, "collections")
+            headers <- list(Authorization = paste("OAuth2", private$token))
 
-                http <- HttpRequest() 
-                serverResponse <- http$GET(collectionURL, headers, NULL, filters, limit, offset)
+            serverResponse <- private$http$GET(collectionURL, headers, NULL, filters, limit, offset)
 
-                httpParser <- HttpParser()
-                collection <- httpParser$parseJSONResponse(serverResponse)
+            collection <- private$httpParser$parseJSONResponse(serverResponse)
 
-                if(!is.null(collection$errors))
-                    stop(collection$errors)       
+            if(!is.null(collection$errors))
+                stop(collection$errors)       
 
-                collection
-            }
+            collection
+        },
 
-            collection_delete <<- function(uuid) 
-            {
-                collectionURL <- paste0(host, "collections/", uuid)
-                headers <- list("Authorization" = paste("OAuth2", token),
-                                "Content-Type"  = "application/json")
+        deleteCollection = function(uuid) 
+        {
+            collectionURL <- paste0(private$host, "collections/", uuid)
+            headers <- list("Authorization" = paste("OAuth2", private$token),
+                            "Content-Type"  = "application/json")
 
-                http <- HttpRequest() 
-                serverResponse <- http$DELETE(collectionURL, headers)
+            serverResponse <- private$http$DELETE(collectionURL, headers)
 
-                httpParser <- HttpParser()
-                collection <- httpParser$parseJSONResponse(serverResponse)
+            collection <- private$httpParser$parseJSONResponse(serverResponse)
 
-                if(!is.null(collection$errors))
-                    stop(collection$errors)       
+            if(!is.null(collection$errors))
+                stop(collection$errors)       
 
-                collection
-            }
+            collection
+        },
 
-            collection_update <<- function(uuid, body) 
-            {
-                collectionURL <- paste0(host, "collections/", uuid)
-                headers <- list("Authorization" = paste("OAuth2", token),
-                                "Content-Type"  = "application/json")
-                body <- jsonlite::toJSON(body, auto_unbox = T)
+        updateCollection = function(uuid, body) 
+        {
+            collectionURL <- paste0(private$host, "collections/", uuid)
+            headers <- list("Authorization" = paste("OAuth2", private$token),
+                            "Content-Type"  = "application/json")
 
-                http <- HttpRequest() 
-                serverResponse <- http$PUT(collectionURL, headers, body)
+            body <- jsonlite::toJSON(body, auto_unbox = T)
 
-                httpParser <- HttpParser()
-                collection <- httpParser$parseJSONResponse(serverResponse)
+            serverResponse <- private$http$PUT(collectionURL, headers, body)
 
-                if(!is.null(collection$errors))
-                    stop(collection$errors)       
+            collection <- private$httpParser$parseJSONResponse(serverResponse)
 
-                collection
-            }
+            if(!is.null(collection$errors))
+                stop(collection$errors)       
 
-            collection_create <<- function(body) 
-            {
-                collectionURL <- paste0(host, "collections")
-                headers <- list("Authorization" = paste("OAuth2", token),
-                                "Content-Type"  = "application/json")
-                body <- jsonlite::toJSON(body, auto_unbox = T)
+            collection
+        },
 
-                http <- HttpRequest() 
-                serverResponse <- http$POST(collectionURL, headers, body)
+        createCollection = function(body) 
+        {
+            collectionURL <- paste0(private$host, "collections")
+            headers <- list("Authorization" = paste("OAuth2", private$token),
+                            "Content-Type"  = "application/json")
+            body <- jsonlite::toJSON(body, auto_unbox = T)
 
-                httpParser <- HttpParser()
-                collection <- httpParser$parseJSONResponse(serverResponse)
+            serverResponse <- private$http$POST(collectionURL, headers, body)
 
-                if(!is.null(collection$errors))
-                    stop(collection$errors)       
+            collection <- private$httpParser$parseJSONResponse(serverResponse)
 
-                collection
-            }
+            if(!is.null(collection$errors))
+                stop(collection$errors)       
+
+            collection
         }
-    )
+
+    ),
+    
+    private = list(
+
+        token          = NULL,
+        host           = NULL,
+        webDavHostName = NULL,
+        http           = NULL,
+        httpParser     = NULL
+    ),
+    
+    cloneable = FALSE
 )