Merge branch '13383-trash-workers'
[arvados.git] / sdk / R / R / util.R
index f6a582ff177b3b9aea4234486868142e19f10c59..57dd75f228ea410800897b60031d5734ca215f85 100644 (file)
@@ -1,3 +1,39 @@
+#' listAll
+#'
+#' List all resources even if the number of items is greater than maximum API limit.
+#'
+#' @param fn Arvados method used to retrieve items from REST service.
+#' @param ... Optional arguments which will be pased to fn .
+#' @examples
+#' \dontrun{
+#' arv <- Arvados$new("your Arvados token", "example.arvadosapi.com")
+#' cl <- listAll(arv$collections.list, filters = list(list("name", "like", "test%"))
+#' }
+#' @export 
+listAll <- function(fn, ...)
+{
+    offset <- 0
+    itemsAvailable <- .Machine$integer.max
+    items <- c()
+
+    while(length(items) < itemsAvailable)
+    {
+        serverResponse <- fn(offset = offset, ...)
+
+        if(!is.null(serverResponse$errors))
+            stop(serverResponse$errors)
+
+        items          <- c(items, serverResponse$items)
+        offset         <- length(items)
+        itemsAvailable <- serverResponse$items_available
+    }
+
+    items
+}
+
+
+#NOTE: Package private functions
+
 trimFromStart <- function(sample, trimCharacters)
 {
     if(startsWith(sample, trimCharacters))
@@ -14,22 +50,23 @@ trimFromEnd <- function(sample, trimCharacters)
     sample
 }
 
-RListToPythonList <- function(sample, separator = ", ")
+RListToPythonList <- function(RList, separator = ", ")
 {
-    pythonArrayContent <- sapply(sample, function(sampleUnit)
+    pythonArrayContent <- sapply(RList, function(elementInList)
     {
-        if((is.vector(sampleUnit) || is.list(sampleUnit)) &&
-            length(sampleUnit) > 1)
+        if((is.vector(elementInList) || is.list(elementInList)) &&
+            length(elementInList) > 1)
         {
-            return(RListToPythonList(sampleUnit, separator))
+            return(RListToPythonList(elementInList, separator))
         }
         else
         {
-            return(paste0("\"", sampleUnit, "\""))
+            return(paste0("\"", elementInList, "\""))
         }
     })
 
-    return(paste0("[", paste0(pythonArrayContent, collapse = separator), "]"))
+    pythonArray <- paste0("[", paste0(pythonArrayContent, collapse = separator), "]")
+    pythonArray
 }
 
 appendToStartIfNotExist <- function(sample, characters)
@@ -48,6 +85,5 @@ splitToPathAndName = function(path)
     nameAndPath$name <- components[length(components)]
     nameAndPath$path <- trimFromStart(paste0(components[-length(components)], collapse = "/"),
                                       "/")
-    
     nameAndPath
 }