Fixed some bugs and improved error handling.
[arvados.git] / sdk / R / R / HttpRequest.R
index ea46beae03c574a34829721fa58c76c636488db7..83669072263249a91b6274d6bc0fda2463fb64c0 100644 (file)
@@ -4,12 +4,14 @@ HttpRequest <- R6::R6Class(
 
     public = list(
 
+        validContentTypes = NULL,
+
         initialize = function() 
         {
+            self$validContentTypes <- c("text", "raw")
         },
 
-        GET = function(url, headers = NULL, body = NULL,
-                       queryFilters = NULL, limit = NULL, offset = NULL)
+        GET = function(url, headers = NULL, queryFilters = NULL, limit = NULL, offset = NULL)
         {
             headers <- httr::add_headers(unlist(headers))
             query <- private$createQuery(queryFilters, limit, offset)
@@ -46,6 +48,24 @@ HttpRequest <- R6::R6Class(
             url <- paste0(url, query)
 
             serverResponse <- httr::DELETE(url = url, config = headers)
+        },
+
+        PROPFIND = function(url, headers = NULL)
+        {
+            h <- curl::new_handle()
+            curl::handle_setopt(h, customrequest = "PROPFIND")
+            curl::handle_setheaders(h, .list = headers)
+
+            propfindResponse <- curl::curl_fetch_memory(url, h)
+        },
+
+        MOVE = function(url, headers = NULL)
+        {
+            h <- curl::new_handle()
+            curl::handle_setopt(h, customrequest = "MOVE")
+            curl::handle_setheaders(h, .list = headers)
+
+            propfindResponse <- curl::curl_fetch_memory(url, h)
         }
     ),
 
@@ -55,7 +75,7 @@ HttpRequest <- R6::R6Class(
         # Python array from R list (recursion?)
         createQuery = function(filters, limit, offset)
         {
-            finalQuery <- "?alt=json"
+            finalQuery <- NULL
 
             if(!is.null(filters))
             {
@@ -95,10 +115,12 @@ HttpRequest <- R6::R6Class(
 
                 encodedQuery <- URLencode(filters, reserved = T, repeated = T)
 
-                finalQuery <- paste0(finalQuery, "&filters=", encodedQuery)
-
                 #Todo(Fudo): This is a hack for now. Find a proper solution.
-                finalQuery <- stringr::str_replace_all(finalQuery, "%2B", "+")
+                encodedQuery <- stringr::str_replace_all(encodedQuery, "%2B", "+")
+
+                finalQuery <- c(finalQuery, paste0("filters=", encodedQuery))
+
+                finalQuery
             }
 
             if(!is.null(limit))
@@ -106,7 +128,7 @@ HttpRequest <- R6::R6Class(
                 if(!is.numeric(limit))
                     stop("Limit must be a numeric type.")
                 
-                finalQuery <- paste0(finalQuery, "&limit=", limit)
+                finalQuery <- c(finalQuery, paste0("limit=", limit))
             }
 
             if(!is.null(offset))
@@ -114,7 +136,13 @@ HttpRequest <- R6::R6Class(
                 if(!is.numeric(offset))
                     stop("Offset must be a numeric type.")
                 
-                finalQuery <- paste0(finalQuery, "&offset=", offset)
+                finalQuery <- c(finalQuery, paste0("offset=", offset))
+            }
+
+            if(length(finalQuery) > 1)
+            {
+                finalQuery <- paste0(finalQuery, collapse = "&")
+                finalQuery <- paste0("?", finalQuery)
             }
 
             finalQuery