Implement copy method, update move method and remove trailing
[arvados.git] / sdk / R / R / HttpRequest.R
index ddb8f71c649e5a8bf6d8af5fc99fc2fc9345efa0..07defca90f4c99e8be9f8a73f7412f398ab1a701 100644 (file)
-source("./R/custom_classes.R")
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: Apache-2.0
 
-HttpRequest <- setRefClass(
+source("./R/util.R")
+
+HttpRequest <- R6::R6Class(
 
     "HttrRequest",
 
-    fields = list(
+    public = list(
 
-        GET    = "function",
-        PUT    = "function",
-        POST   = "function",
-        DELETE = "function"
-    ),
+        validContentTypes = NULL,
+        validVerbs = NULL,
 
-    methods = list(
-        initialize = function() 
+        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)
-            }
-
-            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)
+            self$validContentTypes <- c("text", "raw")
+            self$validVerbs <- c("GET", "POST", "PUT", "DELETE", "PROPFIND", "MOVE", "COPY")
+        },
 
-                serverResponse <- httr::PUT(url = url, config = headers, body = body)
-            }
+        exec = function(verb, url, headers = NULL, body = NULL, queryParams = NULL,
+                        retryTimes = 0)
+        {
+            if(!(verb %in% self$validVerbs))
+                stop("Http verb is not valid.")
 
-            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)
+            urlQuery <- self$createQuery(queryParams)
+            url      <- paste0(url, urlQuery)
 
-                serverResponse <- httr::POST(url = url, config = headers, body = body)
-            }
+            config <- httr::add_headers(unlist(headers))
+            if(toString(Sys.getenv("ARVADOS_API_HOST_INSECURE") == "TRUE"))
+               config$options = list(ssl_verifypeer = 0L)
 
-            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)
+            # times = 1 regular call + numberOfRetries
+            response <- httr::RETRY(verb, url = url, body = body,
+                                    config = config, times = retryTimes + 1)
+        },
 
-                serverResponse <- httr::DELETE(url = url, config = headers)
-            }
+        createQuery = function(queryParams)
+        {
+            queryParams <- Filter(Negate(is.null), queryParams)
 
-            # Private methods
-            .createQuery <- function(filters, limit, offset)
+            query <- sapply(queryParams, function(param)
             {
-                finalQuery <- "?alt=json"
+                if(is.list(param) || length(param) > 1)
+                    param <- RListToPythonList(param, ",")
 
-                if(!is.null(filters))
-                {
-                    filters <- sapply(filters, function(filter)
-                    {
-                        if(length(filter) != 3)
-                            stop("Filter list must have exacthey 3 elements.")
+                URLencode(as.character(param), reserved = T, repeated = T)
 
-                        attributeAndOperator = filter[c(1, 2)]
-                        filterList = filter[[3]]
-                        filterListIsPrimitive = TRUE
-                        if(length(filterList) > 1)
-                            filterListIsPrimitive = FALSE
+            }, USE.NAMES = TRUE)
 
-                        attributeAndOperator <- sapply(attributeAndOperator, function(component) {
-                            component <- paste0("\"", component, "\"")
-                        })
-
-                        filterList <- sapply(unlist(filterList), function(filter) {
-                            filter <- paste0("\"", filter, "\"")
-                        })
-
-                        filterList <- paste(filterList, collapse = ",+")
-
-                        if(!filterListIsPrimitive)
-                            filterList <- paste0("[", filterList, "]")
-
-                        filter <- c(attributeAndOperator, filterList)
-
-                        queryParameter <- paste(filter, collapse = ",+")
-                        queryParameter <- paste0("[", queryParameter, "]")
-            
-                    })
-
-                    filters <- paste(filters, collapse = ",+")
-                    filters <- paste0("[", filters, "]")
-
-                    encodedQuery <- URLencode(filters, reserved = T, repeated = T)
+            if(length(query) > 0)
+            {
+                query <- paste0(names(query), "=", query, collapse = "&")
 
-                    finalQuery <- paste0(finalQuery, "&filters=", encodedQuery)
+                return(paste0("/?", query))
+            }
 
-                    #Todo(Fudo): This is a hack for now. Find a proper solution.
-                    finalQuery <- stringr::str_replace_all(finalQuery, "%2B", "+")
-                }
+            return("")
+        },
 
-                if(!is.null(limit))
-                {
-                    if(!is.numeric(limit))
-                        stop("Limit must be a numeric type.")
-                    
-                    finalQuery <- paste0(finalQuery, "&limit=", limit)
-                }
+        getConnection = function(url, headers, openMode)
+        {
+            h <- curl::new_handle()
+            curl::handle_setheaders(h, .list = headers)
 
-                if(!is.null(offset))
-                {
-                    if(!is.numeric(offset))
-                        stop("Offset must be a numeric type.")
-                    
-                    finalQuery <- paste0(finalQuery, "&offset=", offset)
-                }
+            if(toString(Sys.getenv("ARVADOS_API_HOST_INSECURE") == "TRUE"))
+               curl::handle_setopt(h, ssl_verifypeer = 0L)
 
-                finalQuery
-            }
+            conn <- curl::curl(url = url, open = openMode, handle = h)
         }
-    )
+    ),
+
+    cloneable = FALSE
 )