16827: Fix getFileNamesFromResponse
[arvados.git] / sdk / R / R / HttpParser.R
index 09304fc59928c4dcd469f37dbf1795d9ca3c8444..60bf7828278859e14fefaacebbcb5d762f1dbe99 100644 (file)
@@ -1,22 +1,56 @@
-#' HttpParser
-#'
-HttpParser <- setRefClass(
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: Apache-2.0
+
+HttpParser <- R6::R6Class(
 
     "HttrParser",
 
-    fields = list(
-    ),
+    public = list(
+
+        validContentTypes = NULL,
+
+        initialize = function()
+        {
+            self$validContentTypes <- c("text", "raw")
+        },
+
+        parseJSONResponse = function(serverResponse)
+        {
+            parsed_response <- httr::content(serverResponse,
+                                             as = "parsed",
+                                             type = "application/json")
+        },
+
+        parseResponse = function(serverResponse, outputType)
+        {
+            parsed_response <- httr::content(serverResponse, as = outputType)
+        },
 
-    methods = list(
-        initialize = function() 
+        getFileNamesFromResponse = function(response, uri)
         {
+            text <- rawToChar(response$content)
+            doc <- XML::xmlParse(text, asText=TRUE)
+            base <- paste("/", strsplit(uri, "/")[[1]][4], "/", sep="")
+            result <- unlist(
+                XML::xpathApply(doc, "//D:response/D:href", function(node) {
+                    sub(base, "", URLdecode(XML::xmlValue(node)), fixed=TRUE)
+                })
+            )
+            result[result != ""]
         },
 
-        parseCollectionGet = function(server_response) 
+        getFileSizesFromResponse = function(response, uri)
         {
-            parsed_response <- httr::content(server_response, as = "parsed", type = "application/json")
+            text <- rawToChar(response$content)
+            doc <- XML::xmlParse(text, asText=TRUE)
+
+            base <- paste(paste("/", strsplit(uri, "/")[[1]][-1:-3], sep="", collapse=""), "/", sep="")
+            result <- XML::xpathApply(doc, "//D:response/D:propstat/D:prop/D:getcontentlength", function(node) {
+              XML::xmlValue(node)
+            })
 
-            #Todo(Fudo): Create new Collection object and populate it
+            unlist(result)
         }
     )
 )