to tree structure representing collection content.
Arvados-DCO-1.1-Signed-off-by: Fuad Muhic <fmuhic@capeannenterprises.com>
LazyData: true
RoxygenNote: 6.0.1.9000
Imports:
+ R6,
httr,
stringr,
jsonlite,
export(ArvadosFile)
export(Collection)
export(Subcollection)
-exportClasses(ArvadosFile)
-exportClasses(Collection)
-exportClasses(Subcollection)
source("./R/HttpRequest.R")
source("./R/HttpParser.R")
-source("./R/custom_classes.R")
#' Arvados SDK Object
#'
#'
#' @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)
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
)
-source("./R/HttpRequest.R")
-#' ArvadosFile Class
-#'
-#' @details
-#' Todo: Update description
-#' Subcollection
-#'
+#' ArvadosFile Object
+#'
+#' Update description
+#'
#' @export ArvadosFile
-#' @exportClass ArvadosFile
-ArvadosFile <- setRefClass(
+ArvadosFile <- R6::R6Class(
+
"ArvadosFile",
- fields = list(
- name = "character",
- relativePath = "character",
- read = "function"
+ public = list(
+
+ initialize = function(name, relativePath, api, collection)
+ {
+ private$name <- name
+ private$relativePath <- relativePath
+ private$api <- api
+ private$collection <- collection
+ private$http <- HttpRequest$new()
+ private$httpParser <- HttpParser$new()
+ },
+ getName = function() private$name,
- ),
- methods = list(
- initialize = function(subcollectionName, api)
+ getRelativePath = function() private$relativePath,
+
+ read = function(offset = 0, length = 0)
{
- name <<- subcollectionName
-
- read <<- function(offset = 0, length = 0)
- {
- if(offset < 0 || length < 0)
- stop("Offset and length must be positive values.")
-
- range = paste0("bytes=", offset, "-")
-
- if(length > 0)
- range = paste0(range, offset + length - 1)
-
- fileURL = paste0(api$getWebDavHostName(), relativePath);
- headers <- list(Authorization = paste("OAuth2", api$getWebDavToken()),
- Range = range)
-
- #TODO(Fudo): Move this to HttpRequest.R
- # serverResponse <- httr::GET(url = fileURL,
- # config = httr::add_headers(unlist(headers)))
- http <- HttpRequest()
- serverResponse <- http$GET(fileURL, headers)
- parsed_response <- httr::content(serverResponse, "raw")
- }
+ if(offset < 0 || length < 0)
+ stop("Offset and length must be positive values.")
+
+ range = paste0("bytes=", offset, "-")
+
+ if(length > 0)
+ range = paste0(range, offset + length - 1)
+
+ fileURL = paste0(private$api$getWebDavHostName(), "c=", private$collection$uuid, "/", private$relativePath);
+ headers <- list(Authorization = paste("OAuth2", private$api$getToken()),
+ Range = range)
+
+ #TODO(Fudo): Move this to HttpRequest.R
+ # serverResponse <- httr::GET(url = fileURL,
+ # config = httr::add_headers(unlist(headers)))
+ serverResponse <- private$http$GET(fileURL, headers)
+ parsed_response <- httr::content(serverResponse, "raw")
+
}
- )
+ ),
+
+ private = list(
+
+ name = NULL,
+ relativePath = NULL,
+ parent = NULL,
+ api = NULL,
+ collection = NULL,
+ http = NULL,
+ httpParser = NULL
+ ),
+
+ cloneable = FALSE
)
-source("./R/Arvados.R")
-source("./R/HttpParser.R")
source("./R/Subcollection.R")
source("./R/ArvadosFile.R")
-#' Collection Class
-#'
-#' @details
-#' Todo: Update description
-#' Collection
-#'
-#' @param uuid Object ID
-#' @param etag Object version
-#' @param owner_uuid No description
-#' @param created_at No description
-#' @param modified_by_client_uuid No description
-#' @param modified_by_user_uuid No description
-#' @param modified_at No description
-#' @param portable_data_hash No description
-#' @param replication_desired No description
-#' @param replication_confirmed_at No description
-#' @param replication_confirmed No description
-#' @param updated_at No description
-#' @param manifest_text No description
-#' @param name No description
-#' @param description No description
-#' @param properties No description
-#' @param delete_at No description
-#' @param file_names No description
-#' @param trash_at No description
-#' @param is_trashed No description
-#'
+#' Arvados Collection Object
+#'
+#' Update description
+#'
+#' @examples arv = Collection$new(api, uuid)
#' @export Collection
-
-#' @exportClass Collection
-Collection <- setRefClass(
+Collection <- R6::R6Class(
"Collection",
- fields = list(uuid = "ANY",
- items = "ANY",
- fileContent = "ANY",
- etag = "ANY",
- owner_uuid = "ANY",
- created_at = "ANY",
- modified_by_client_uuid = "ANY",
- modified_by_user_uuid = "ANY",
- modified_at = "ANY",
- portable_data_hash = "ANY",
- replication_desired = "ANY",
- replication_confirmed_at = "ANY",
- replication_confirmed = "ANY",
- updated_at = "ANY",
- manifest_text = "ANY",
- name = "ANY",
- description = "ANY",
- properties = "ANY",
- delete_at = "ANY",
- file_names = "ANY",
- trash_at = "ANY",
- is_trashed = "ANY",
-
- getCollectionContent = "function",
- get = "function"
- ),
+ public = list(
- methods = list(
+ #Todo(Fudo): Encapsulate this?
+ uuid = NULL,
+ etag = NULL,
+ owner_uuid = NULL,
+ created_at = NULL,
+ modified_by_client_uuid = NULL,
+ modified_by_user_uuid = NULL,
+ modified_at = NULL,
+ portable_data_hash = NULL,
+ replication_desired = NULL,
+ replication_confirmed_at = NULL,
+ replication_confirmed = NULL,
+ updated_at = NULL,
+ manifest_text = NULL,
+ name = NULL,
+ description = NULL,
+ properties = NULL,
+ delete_at = NULL,
+ file_names = NULL,
+ trash_at = NULL,
+ is_trashed = NULL,
- initialize = function(api, uuid)
+ initialize = function(api, uuid)
{
- result <- api$collection_get(uuid)
-
- # Private members
- uuid <<- result$uuid
- etag <<- result$etag
- owner_uuid <<- result$owner_uuid
- created_at <<- result$created_at
- modified_by_client_uuid <<- result$modified_by_client_uuid
- modified_by_user_uuid <<- result$modified_by_user_uuid
- modified_at <<- result$modified_at
- portable_data_hash <<- result$portable_data_hash
- replication_desired <<- result$replication_desired
- replication_confirmed_at <<- result$replication_confirmed_at
- replication_confirmed <<- result$replication_confirmed
- updated_at <<- result$updated_at
- manifest_text <<- result$manifest_text
- name <<- result$name
- description <<- result$description
- properties <<- result$properties
- delete_at <<- result$delete_at
- file_names <<- result$file_names
- trash_at <<- result$trash_at
- is_trashed <<- result$is_trashed
-
- # Public methods
-
- getCollectionContent <<- function()
- {
- #TODO(Fudo): Use proper URL here.
- uri <- URLencode(api$getWebDavHostName())
+ private$api <- api
+ result <- private$api$getCollection(uuid)
+
+ self$uuid <- result$uuid
+ self$etag <- result$etag
+ self$owner_uuid <- result$owner_uuid
+ self$created_at <- result$created_at
+ self$modified_by_client_uuid <- result$modified_by_client_uuid
+ self$modified_by_user_uuid <- result$modified_by_user_uuid
+ self$modified_at <- result$modified_at
+ self$portable_data_hash <- result$portable_data_hash
+ self$replication_desired <- result$replication_desired
+ self$replication_confirmed_at <- result$replication_confirmed_at
+ self$replication_confirmed <- result$replication_confirmed
+ self$updated_at <- result$updated_at
+ self$manifest_text <- result$manifest_text
+ self$name <- result$name
+ self$description <- result$description
+ self$properties <- result$properties
+ self$delete_at <- result$delete_at
+ self$file_names <- result$file_names
+ self$trash_at <- result$trash_at
+ self$is_trashed <- result$is_trashed
+
+ #Todo(Fudo): Replace this when you get access to webDAV server.
+ private$fileItems <- private$getCollectionContent()
- # fetch directory listing via curl and parse XML response
- h <- curl::new_handle()
- curl::handle_setopt(h, customrequest = "PROPFIND")
+ private$fileTree <- private$generateTree(private$fileItems)
+ },
- #TODO(Fudo): Use proper token here.
- curl::handle_setheaders(h, "Authorization" = paste("OAuth2", api$getWebDavToken()))
- response <- curl::curl_fetch_memory(uri, h)
+ printFileContent = function(pretty = TRUE)
+ {
+ if(pretty)
+ private$fileTree$printContent(0)
+ else
+ print(private$fileItems)
+
+ },
+
+ get = function(relativePath)
+ {
+ treeNode <- private$traverseInOrder(private$fileTree, function(node)
+ {
+ if(node$relativePath == relativePath)
+ return(node)
+ else
+ return(NULL)
+ })
- HttpParser()$parseWebDAVResponse(response, uri)
+ if(!is.null(treeNode))
+ {
+ return(private$createSubcollectionTree(treeNode))
}
+ else
+ {
+ return(NULL)
+ }
+ }
+ ),
+
+ active = list(
+ items = function(value)
+ {
+ if(missing(value))
+ return(private$fileItems)
+ else
+ print("Value is read-only.")
- get <<- function(pathToTheFile)
+ return(NULL)
+ }
+ ),
+
+ private = list(
+
+ api = NULL,
+ fileItems = NULL,
+ fileTree = NULL,
+
+ createSubcollectionTree = function(treeNode)
+ {
+ if(treeNode$hasChildren())
{
- fileWithPath <- unlist(stringr::str_split(pathToTheFile, "/"))
- fileWithPath <- fileWithPath[fileWithPath != ""]
+ children = NULL
- findFileIfExists <- function(name, node)
+ for(child in treeNode$children)
{
- matchPosition <- match(name, sapply(node$content, function(nodeInSubcollection) {nodeInSubcollection$name}), -1)
- if(matchPosition != -1)
- {
- return(node$content[[matchPosition]])
- }
- else
- {
- return(NULL)
- }
+ child <- private$createSubcollectionTree(child)
+ children <- c(children, child)
}
+
+ return(Subcollection$new(treeNode$name, treeNode$relativePath, children))
+ }
+ else
+ {
+ if(treeNode$type == "file")
+ return(ArvadosFile$new(treeNode$name, treeNode$relativePath, private$api, self))
+ else if(treeNode$type == "folder" || treeNode$type == "root")
+ return(Subcollection$new(treeNode$name, treeNode$relativePath, NULL))
+ }
+ },
+
+ createSubcollectionFromNode = function(treeNode, children)
+ {
+ subcollection = NULL
+ if(treeNode$type == "file")
+ subcollection = ArvadosFile$new(treeNode$name, treeNode$relativePath)
+ else if(treeNode$type == "folder" || treeNode$type == "root")
+ subcollection = Subcollection$new(treeNode$name, treeNode$relativePath, children)
+
+ subcollection
+ },
+
+ getCollectionContent = function()
+ {
+ #TODO(Fudo): Use proper URL here.
+ uri <- URLencode(paste0(private$api$getWebDavHostName(), "c=", self$uuid))
+
+ # fetch directory listing via curl and parse XML response
+ h <- curl::new_handle()
+ curl::handle_setopt(h, customrequest = "PROPFIND")
+
+ #TODO(Fudo): Use proper token here.
+ curl::handle_setheaders(h, "Authorization" = paste("OAuth2", private$api$getToken()))
+ response <- curl::curl_fetch_memory(uri, h)
+ print(response)
+
+ HttpParser$new()$parseWebDAVResponse(response, uri)
+ },
+
+ #Todo(Fudo): Move tree creation to another file.
+ generateTree = function(collectionContent)
+ {
+ treeBranches <- sapply(collectionContent, function(filePath)
+ {
+ splitPath <- unlist(strsplit(filePath, "/", fixed = TRUE))
+
+ pathEndsWithSlash <- substr(filePath, nchar(filePath), nchar(filePath)) == "/"
- nodeToCheck = .self$fileContent
- for(fileNameIndex in 1:length(fileWithPath))
+ branch = private$createBranch(splitPath, pathEndsWithSlash)
+ })
+
+ root <- TreeNode$new("./", "root")
+ root$relativePath = ""
+
+ sapply(treeBranches, function(branch)
+ {
+ private$addNode(root, branch)
+ })
+
+ root
+ },
+
+ createBranch = function(splitPath, pathEndsWithSlash)
+ {
+ branch <- NULL
+ lastElementIndex <- length(splitPath)
+
+ lastElementInPathType = "file"
+ if(pathEndsWithSlash)
+ lastElementInPathType = "folder"
+
+ for(elementIndex in lastElementIndex:1)
+ {
+ if(elementIndex == lastElementIndex)
+ {
+ branch = TreeNode$new(splitPath[[elementIndex]], lastElementInPathType)
+ }
+ else
{
- nodeToCheck <- findFileIfExists(fileWithPath[fileNameIndex], nodeToCheck)
- if(is.null(nodeToCheck))
- stop("File or folder you asked for is not part of the collection.")
+ newFolder = TreeNode$new(splitPath[[elementIndex]], "folder")
+ newFolder$addChild(branch)
+ branch = newFolder
}
- nodeToCheck
+ branch$relativePath <- paste(unlist(splitPath[1:elementIndex]), collapse = "/")
}
+ branch
+ },
- # Private methods
- .createCollectionContentTree <- function(fileStructure)
+ addNode = function(container, node)
+ {
+ child = container$getChild(node$name)
+
+ if(is.null(child))
{
- #TODO(Fudo): Refactor this.
- #TODO(Fudo): Find a way to link children to parents. (R has no pointers or references).
- treeBranches <- sapply(fileStructure, function(filePath)
- {
- fileWithPath <- unlist(stringr::str_split(filePath, "/"))
- file <- fileWithPath[length(fileWithPath), drop = T]
-
- if(file != "")
- {
- file <- ArvadosFile(file, api)
- file$relativePath <- filePath
- }
- else
- {
- file <- NULL
- }
-
- folders <- fileWithPath[-length(fileWithPath)]
-
- subcollections <- sapply(folders, function(folder)
- {
- folder <- Subcollection(folder)
- unname(folder)
- })
-
- if(!is.null(file))
- subcollections <- c(subcollections, file)
-
- if(length(subcollections) > 1)
- {
- for(subcollectionIndex in 1:(length(subcollections) - 1))
- {
- subcollections[[subcollectionIndex]]$relativePath <- paste(folders[1:(subcollectionIndex)], collapse = "/")
- subcollections[[subcollectionIndex]]$add(subcollections[[subcollectionIndex + 1]])
- }
- }
- subcollections[[1]]
- })
-
- root <- Subcollection(".")
-
- addIfExists <- function(firstNode, secondNode)
+ container$addChild(node)
+ }
+ else
+ {
+ private$addNode(child, node$getFirstChild())
+ }
+ },
+
+ traverseInOrder = function(node, predicate)
+ {
+ if(node$hasChildren())
+ {
+ result <- predicate(node)
+
+ if(!is.null(result))
+ return(result)
+
+ for(child in node$children)
{
- firstNodeContent <- sapply(firstNode$content, function(node) {node$name})
- if(length(firstNodeContent) == 0)
- {
- firstNode$add(secondNode)
- return()
- }
-
- matchPosition <- match(secondNode$name, firstNodeContent, -1)
- if(matchPosition != -1)
- {
- addIfExists(firstNode$content[[matchPosition]], secondNode$content[[1]])
- }
- else
- {
- firstNode$add(secondNode)
- }
+ result <- private$traverseInOrder(child, predicate)
+
+ if(!is.null(result))
+ return(result)
}
- sapply(treeBranches, function(branch)
- {
- addIfExists(root, branch)
- })
+ return(NULL)
+ }
+ else
+ {
+ return(predicate(node))
+ }
+ }
+
+ ),
+
+ cloneable = FALSE
+)
+
+TreeNode <- R6::R6Class(
+
+ "TreeNode",
+
+ public = list(
+
+ name = NULL,
+ relativePath = NULL,
+ children = NULL,
+ parent = NULL,
+ type = NULL,
+
+ initialize = function(name, type)
+ {
+ if(type == "folder")
+ name <- paste0(name, "/")
+
+ self$name <- name
+ self$type <- type
+ self$children <- list()
+ },
+
+ addChild = function(node)
+ {
+ self$children <- c(self$children, node)
+ node$setParent(self)
+ self
+ },
+
+ setParent = function(parent)
+ {
+ self$parent = parent
+ },
- root
+ getChild = function(childName)
+ {
+ for(child in self$children)
+ {
+ if(childName == child$name)
+ return(child)
}
- #Todo(Fudo): This is dummy data. Real content will come from WebDAV server.
- # testFileStructure <- c("math.h", "main.cpp", "emptyFolder/",
- # "java/render.java", "java/test/observer.java",
- # "java/test/observable.java",
- # "csharp/this.cs", "csharp/is.cs",
- # "csharp/dummy.cs", "csharp/file.cs")
- items <<- getCollectionContent()
- fileContent <<- .createCollectionContentTree(items)
+ return(NULL)
+ },
+
+ hasChildren = function()
+ {
+ if(length(self$children) != 0)
+ return(TRUE)
+ else
+ return(FALSE)
+ },
+
+ getFirstChild = function()
+ {
+ if(!self$hasChildren())
+ return(NULL)
+ else
+ return(self$children[[1]])
+ },
+
+ printContent = function(depth)
+ {
+ indentation <- paste(rep("....", depth), collapse = "")
+ print(paste0(indentation, self$name))
+
+ for(child in self$children)
+ child$printContent(depth + 1)
}
- )
+ ),
+
+ cloneable = FALSE
)
#' HttpParser
#'
-HttpParser <- setRefClass(
+HttpParser <- R6::R6Class(
"HttrParser",
- fields = list(
- ),
-
- methods = list(
+ public = list(
initialize = function()
{
},
- parseCollectionGet = function(serverResponse)
- {
- parsed_response <- httr::content(serverResponse, as = "parsed", type = "application/json")
-
- #Todo(Fudo): Create new Collection object and populate it
- },
-
parseJSONResponse = function(serverResponse)
{
parsed_response <- httr::content(serverResponse, as = "parsed", type = "application/json")
-
- #Todo(Fudo): Create new Collection object and populate it
},
+ #Todo(Fudo): Test this.
parseWebDAVResponse = function(response, uri)
{
- #Todo(Fudo): Move this to HttpParser.
text <- rawToChar(response$content)
+ print(text)
doc <- XML::xmlParse(text, asText=TRUE)
# calculate relative paths
})
)
result <- result[result != ""]
- #Todo(Fudo): Test this.
+
result[-1]
}
-
)
)
-source("./R/custom_classes.R")
-
-HttpRequest <- setRefClass(
+HttpRequest <- R6::R6Class(
"HttrRequest",
- fields = list(
-
- GET = "function",
- PUT = "function",
- POST = "function",
- DELETE = "function"
- ),
+ public = list(
- methods = list(
initialize = function()
{
- # Public methods
- GET <<- function(url, headers = NULL, body = NULL,
- queryFilters = NULL, limit = NULL, offset = NULL)
- {
- headers <- httr::add_headers(unlist(headers))
- query <- .createQuery(queryFilters, limit, offset)
- url <- paste0(url, query)
- print(url)
+ },
- serverResponse <- httr::GET(url = url, config = headers)
- }
+ GET = function(url, headers = NULL, body = NULL,
+ queryFilters = NULL, limit = NULL, offset = NULL)
+ {
+ headers <- httr::add_headers(unlist(headers))
+ query <- private$createQuery(queryFilters, limit, offset)
+ url <- paste0(url, query)
- PUT <<- function(url, headers = NULL, body = NULL,
- queryFilters = NULL, limit = 100, offset = 0)
- {
- headers <- httr::add_headers(unlist(headers))
- query <- .createQuery(queryFilters, limit, offset)
- url <- paste0(url, query)
+ serverResponse <- httr::GET(url = url, config = headers)
+ },
- serverResponse <- httr::PUT(url = url, config = headers, body = body)
- }
+ PUT = function(url, headers = NULL, body = NULL,
+ queryFilters = NULL, limit = 100, offset = 0)
+ {
+ headers <- httr::add_headers(unlist(headers))
+ query <- private$createQuery(queryFilters, limit, offset)
+ url <- paste0(url, query)
- POST <<- function(url, headers = NULL, body = NULL,
- queryFilters = NULL, limit = 100, offset = 0)
- {
- headers <- httr::add_headers(unlist(headers))
- query <- .createQuery(queryFilters, limit, offset)
- url <- paste0(url, query)
+ serverResponse <- httr::PUT(url = url, config = headers, body = body)
+ },
- serverResponse <- httr::POST(url = url, config = headers, body = body)
- }
+ POST = function(url, headers = NULL, body = NULL,
+ queryFilters = NULL, limit = 100, offset = 0)
+ {
+ headers <- httr::add_headers(unlist(headers))
+ query <- private$createQuery(queryFilters, limit, offset)
+ url <- paste0(url, query)
- DELETE <<- function(url, headers = NULL, body = NULL,
- queryFilters = NULL, limit = NULL, offset = NULL)
- {
- headers <- httr::add_headers(unlist(headers))
- query <- .createQuery(queryFilters, limit, offset)
- url <- paste0(url, query)
+ serverResponse <- httr::POST(url = url, config = headers, body = body)
+ },
- serverResponse <- httr::DELETE(url = url, config = headers)
- }
+ DELETE = function(url, headers = NULL, body = NULL,
+ queryFilters = NULL, limit = NULL, offset = NULL)
+ {
+ headers <- httr::add_headers(unlist(headers))
+ query <- private$createQuery(queryFilters, limit, offset)
+ url <- paste0(url, query)
- # Private methods
- .createQuery <- function(filters, limit, offset)
- {
- finalQuery <- "?alt=json"
+ serverResponse <- httr::DELETE(url = url, config = headers)
+ }
+ ),
- if(!is.null(filters))
- {
- filters <- sapply(filters, function(filter)
- {
- if(length(filter) != 3)
- stop("Filter list must have exacthey 3 elements.")
+ private = list(
- attributeAndOperator = filter[c(1, 2)]
- filterList = filter[[3]]
- filterListIsPrimitive = TRUE
- if(length(filterList) > 1)
- filterListIsPrimitive = FALSE
+ #Todo(Fudo): Refactor this and find a better way to build
+ # Python array from R list (recursion?)
+ createQuery = function(filters, limit, offset)
+ {
+ finalQuery <- "?alt=json"
- attributeAndOperator <- sapply(attributeAndOperator, function(component) {
- component <- paste0("\"", component, "\"")
- })
+ if(!is.null(filters))
+ {
+ filters <- sapply(filters, function(filter)
+ {
+ if(length(filter) != 3)
+ stop("Filter list must have exacthey 3 elements.")
- filterList <- sapply(unlist(filterList), function(filter) {
- filter <- paste0("\"", filter, "\"")
- })
+ attributeAndOperator = filter[c(1, 2)]
+ filterList = filter[[3]]
+ filterListIsPrimitive = TRUE
+ if(length(filterList) > 1)
+ filterListIsPrimitive = FALSE
- filterList <- paste(filterList, collapse = ",+")
+ attributeAndOperator <- sapply(attributeAndOperator, function(component) {
+ component <- paste0("\"", component, "\"")
+ })
- if(!filterListIsPrimitive)
- filterList <- paste0("[", filterList, "]")
+ filterList <- sapply(unlist(filterList), function(filter) {
+ filter <- paste0("\"", filter, "\"")
+ })
- filter <- c(attributeAndOperator, filterList)
+ filterList <- paste(filterList, collapse = ",+")
- queryParameter <- paste(filter, collapse = ",+")
- queryParameter <- paste0("[", queryParameter, "]")
-
- })
+ if(!filterListIsPrimitive)
+ filterList <- paste0("[", filterList, "]")
- filters <- paste(filters, collapse = ",+")
- filters <- paste0("[", filters, "]")
+ filter <- c(attributeAndOperator, filterList)
- encodedQuery <- URLencode(filters, reserved = T, repeated = T)
+ queryParameter <- paste(filter, collapse = ",+")
+ queryParameter <- paste0("[", queryParameter, "]")
+
+ })
- finalQuery <- paste0(finalQuery, "&filters=", encodedQuery)
+ filters <- paste(filters, collapse = ",+")
+ filters <- paste0("[", filters, "]")
- #Todo(Fudo): This is a hack for now. Find a proper solution.
- finalQuery <- stringr::str_replace_all(finalQuery, "%2B", "+")
- }
+ encodedQuery <- URLencode(filters, reserved = T, repeated = T)
- if(!is.null(limit))
- {
- if(!is.numeric(limit))
- stop("Limit must be a numeric type.")
-
- finalQuery <- paste0(finalQuery, "&limit=", limit)
- }
+ finalQuery <- paste0(finalQuery, "&filters=", encodedQuery)
- if(!is.null(offset))
- {
- if(!is.numeric(offset))
- stop("Offset must be a numeric type.")
-
- finalQuery <- paste0(finalQuery, "&offset=", offset)
- }
+ #Todo(Fudo): This is a hack for now. Find a proper solution.
+ finalQuery <- stringr::str_replace_all(finalQuery, "%2B", "+")
+ }
+
+ if(!is.null(limit))
+ {
+ if(!is.numeric(limit))
+ stop("Limit must be a numeric type.")
+
+ finalQuery <- paste0(finalQuery, "&limit=", limit)
+ }
- finalQuery
+ if(!is.null(offset))
+ {
+ if(!is.numeric(offset))
+ stop("Offset must be a numeric type.")
+
+ finalQuery <- paste0(finalQuery, "&offset=", offset)
}
+
+ finalQuery
}
- )
+ ),
+
+ cloneable = FALSE
)
-#' Subcollection Class
-#'
-#' @details
-#' Todo: Update description
-#' Subcollection
-#'
+#' Arvados SubCollection Object
+#'
+#' Update description
+#'
#' @export Subcollection
-#' @exportClass Subcollection
-Subcollection <- setRefClass(
+Subcollection <- R6::R6Class(
+
"Subcollection",
- fields = list(
- name = "character",
- relativePath = "character",
- content = "list"
- ),
- methods = list(
- initialize = function(subcollectionName)
+
+ public = list(
+
+ initialize = function(name, relativePath, children)
{
- name <<- subcollectionName
- content <<- list()
+ private$name <- name
+ private$relativePath <- relativePath
+ private$children <- children
},
- add = function(subcollectionContent)
- {
- content <<- c(content, subcollectionContent)
- }
- )
+
+ getName = function() private$name,
+
+ getRelativePath = function() private$relativePath,
+
+ setParent = function(parent) private$parent <- parent
+ ),
+
+ private = list(
+
+ name = NULL,
+ relativePath = NULL,
+ children = NULL,
+ parent = NULL
+ ),
+
+ cloneable = FALSE
)
+++ /dev/null
-setClassUnion("characterOrNull", c("character", "NULL"))
-setClassUnion("listOrNull", c("list", "NULL"))
-setClassUnion("numericOrNull", c("numeric", "NULL"))
--------------------------------------------------------------------------------------------------------------------------------
-Get collection:
+#Initialize API:
-arv <- Arvados("insert_token", "insert_host_name")
-arv$collection_get("uuid")
+arv <- Arvados$new("insert_token", "insert_host_name")
--------------------------------------------------------------------------------------------------------------------------------
-List collections:
+#Get collection:
-collectionList <- arv$collection_list(list("uuid", "=" "aaaaa-bbbbb-ccccccccccccccc"), limit = 10, offset = 2)
+arv$getCollection("uuid")
--------------------------------------------------------------------------------------------------------------------------------
-Delete collection:
+#List collections:
-deletedCollection <- arv$collection_delete("uuid")
+collectionList <- arv$listCollections(list("uuid", "=" "aaaaa-bbbbb-ccccccccccccccc"), limit = 10, offset = 2)
--------------------------------------------------------------------------------------------------------------------------------
-Update collection:
+#Delete collection:
-updatedCollection <- arv$collection_update("uuid", list(collection = list(name = "new_name", description = "new_desciption")))
+deletedCollection <- arv$deleteCollection("uuid")
--------------------------------------------------------------------------------------------------------------------------------
-Create collection:
+#Update collection:
-updatedCollection <- arv$collection_update("uuid", list(collection = list(name = "new_name", description = "new_desciption")))
+updatedCollection <- arv$updateCollection("uuid", list(collection = list(name = "new_name", description = "new_desciption")))
+
+--------------------------------------------------------------------------------------------------------------------------------
+
+#Create collection:
+
+updatedCollection <- arv$createCollection("uuid", list(collection = list(name = "new_name", description = "new_desciption")))
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
-Things I'm currently working on and are not finished.
+#Collection manipulation:
-Collection manipulation:
+--------------------------------------------------------------------------------------------------------------------------------
+
+--------------------------------------------------------------------------------------------------------------------------------
-arv <- Arvados("insert_token", "insert_host_name")
+#Create collection object:
-//This will be removed
-arv$setWebDavToken("webdav_token")
-arv$setWebDavHostName("full_path_to_the_collection_on_webdav_server")
+arv <- Arvados$new("insert_token", "insert_host_name")
+collection <- Collection$new(arv, "uuid")
-collection <- Collection(arv, "uuid")
--------------------------------------------------------------------------------------------------------------------------------
-This will return all files in collection as character vector.
-listOfFilesInCollection <- collection$items
+#Print content of the collection
+
+collection$printFileContent()
+
+#of if you just want a list of relative paths:
+
+collection$printFileContent(pretty = FALSE)
+
--------------------------------------------------------------------------------------------------------------------------------
-This will return ArvadosFile or Subcollection from internal tree-like structure.
+#This will return ArvadosFile or Subcollection from internal tree-like structure.
+
+arvadosFile <- collection$get("location/to/my/file.cpp")
-collection <- Collection(arv, "uuid")
+#or
-arvadosFile <- collection.get("location/to/my/file.exe")
arvadosSubcollection <- collection.get("location/to/my/directory/")
+--------------------------------------------------------------------------------------------------------------------------------
+
+#Read whole file or just a portion of it.
+
+arvadosFile$read(offset = 1024, length = 512)
--------------------------------------------------------------------------------------------------------------------------------
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/Arvados.R
-\docType{class}
-\name{Arvados-class}
-\alias{Arvados-class}
+\docType{data}
+\name{Arvados}
\alias{Arvados}
\title{Arvados SDK Object}
+\format{An object of class \code{R6ClassGenerator} of length 24.}
+\usage{
+Arvados
+}
\description{
All Arvados logic is inside this class
}
\item{\code{host}}{Host represents server name we wish to connect to.}
}}
-
\examples{
-arv = Arvados("token", "host_name")
+arv = Arvados$new("token", "host_name")
}
+\keyword{datasets}
+++ /dev/null
-% Generated by roxygen2: do not edit by hand
-% Please edit documentation in R/ArvadosFile.R
-\docType{class}
-\name{ArvadosFile-class}
-\alias{ArvadosFile-class}
-\alias{ArvadosFile}
-\title{ArvadosFile Class}
-\description{
-ArvadosFile Class
-}
-\details{
-Todo: Update description
-Subcollection
-}
-
--- /dev/null
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/ArvadosFile.R
+\docType{data}
+\name{ArvadosFile}
+\alias{ArvadosFile}
+\title{ArvadosFile Object}
+\format{An object of class \code{R6ClassGenerator} of length 24.}
+\usage{
+ArvadosFile
+}
+\description{
+Update description
+}
+\keyword{datasets}
+++ /dev/null
-% Generated by roxygen2: do not edit by hand
-% Please edit documentation in R/Collection.R
-\docType{class}
-\name{Collection-class}
-\alias{Collection-class}
-\alias{Collection}
-\title{Collection Class}
-\arguments{
-\item{uuid}{Object ID}
-
-\item{etag}{Object version}
-
-\item{owner_uuid}{No description}
-
-\item{created_at}{No description}
-
-\item{modified_by_client_uuid}{No description}
-
-\item{modified_by_user_uuid}{No description}
-
-\item{modified_at}{No description}
-
-\item{portable_data_hash}{No description}
-
-\item{replication_desired}{No description}
-
-\item{replication_confirmed_at}{No description}
-
-\item{replication_confirmed}{No description}
-
-\item{updated_at}{No description}
-
-\item{manifest_text}{No description}
-
-\item{name}{No description}
-
-\item{description}{No description}
-
-\item{properties}{No description}
-
-\item{delete_at}{No description}
-
-\item{file_names}{No description}
-
-\item{trash_at}{No description}
-
-\item{is_trashed}{No description}
-}
-\description{
-Collection Class
-}
-\details{
-Todo: Update description
-Collection
-}
-
--- /dev/null
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/Collection.R
+\docType{data}
+\name{Collection}
+\alias{Collection}
+\title{Arvados Collection Object}
+\format{An object of class \code{R6ClassGenerator} of length 24.}
+\usage{
+Collection
+}
+\description{
+Update description
+}
+\examples{
+arv = Collection$new(api, uuid)
+}
+\keyword{datasets}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/HttpParser.R
-\docType{class}
-\name{HttrParser-class}
-\alias{HttrParser-class}
+\docType{data}
+\name{HttpParser}
\alias{HttpParser}
\title{HttpParser}
+\format{An object of class \code{R6ClassGenerator} of length 24.}
+\usage{
+HttpParser
+}
\description{
HttpParser
}
-
+\keyword{datasets}
+++ /dev/null
-% Generated by roxygen2: do not edit by hand
-% Please edit documentation in R/Subcollection.R
-\docType{class}
-\name{Subcollection-class}
-\alias{Subcollection-class}
-\alias{Subcollection}
-\title{Subcollection Class}
-\description{
-Subcollection Class
-}
-\details{
-Todo: Update description
-Subcollection
-}
-
--- /dev/null
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/Subcollection.R
+\docType{data}
+\name{Subcollection}
+\alias{Subcollection}
+\title{Arvados SubCollection Object}
+\format{An object of class \code{R6ClassGenerator} of length 24.}
+\usage{
+Subcollection
+}
+\description{
+Update description
+}
+\keyword{datasets}