From 3e8c599c80c84682403fbad7c28014863be27d38 Mon Sep 17 00:00:00 2001 From: "dyfan.jones" Date: Fri, 16 Aug 2024 12:29:23 +0100 Subject: [PATCH 1/2] return error message if call has failed --- paws.common/NEWS.md | 1 + paws.common/R/handlers_rest.R | 2 +- paws.common/R/net.R | 22 ++++++++++++++++------ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/paws.common/NEWS.md b/paws.common/NEWS.md index 02317fcee..166cb2cb6 100644 --- a/paws.common/NEWS.md +++ b/paws.common/NEWS.md @@ -2,6 +2,7 @@ * build endpoint with host_prefix (#804), thanks to @joseale2310 and @lyschoening for raising issue. * fix `unix_time` ensure seconds is numeric (#804), thanks to @joseale2310 and @lyschoening for raising issue. * fix stop anonymous credentials removing `x-amz-*` headers (#815) thanks to @cgostic for raising issue +* fix s3 redirect for download_file # paws.common 0.7.4 * fix `transpose` to correctly parse lists with empty first elements (#791), thanks to @FMKerckhof for raising issue. diff --git a/paws.common/R/handlers_rest.R b/paws.common/R/handlers_rest.R index 5b78f70f7..3ea0fd38e 100644 --- a/paws.common/R/handlers_rest.R +++ b/paws.common/R/handlers_rest.R @@ -120,7 +120,7 @@ rest_unmarshal_meta <- function(request) { # Unmarshal the body from a REST protocol API response. rest_unmarshal <- function(request) { - values <- request$data + values <- request[["data"]] if ((payload_name <- tag_get(values, "payload")) != "") { if ((payload_type <- tag_get(values[[payload_name]], "type")) == "blob") { payload <- request$http_response$body diff --git a/paws.common/R/net.R b/paws.common/R/net.R index 68ebead8e..40460cd22 100644 --- a/paws.common/R/net.R +++ b/paws.common/R/net.R @@ -135,13 +135,11 @@ issue <- function(http_request) { ) response <- HttpResponse( - status_code = httr::status_code(r), - header = httr::headers(r), - content_length = as.integer(httr::headers(r)$`content-length`), + status_code = r$status_code, + header = r$headers, + content_length = as.integer(r$headers$`content-length`), # Prevent reading in data when output is set - body = ( - if (is.null(http_request$dest)) httr::content(r, as = "raw") else raw() - ) + body = resp_body(r, http_request$dest) ) # Decode gzipped response bodies that are not automatically decompressed @@ -153,6 +151,18 @@ issue <- function(http_request) { return(response) } +resp_body <- function(resp, path) { + if (is.null(path)) { + body <- httr::content(resp, as = "raw") + } else if (resp$status_code %in% c(301, 400)) { + body <- readBin(path, "raw", file.info(path)$size) + unlink(path) + } else { + body <- raw() + } + return(body) +} + # Return whether an HTTP response body is (still) compressed by checking # whether the body has a valid ZLIB header. # See http://www.faqs.org/rfcs/rfc1950.html. From 18c756cffd5d0098cbed0148f9973d035be9bdf0 Mon Sep 17 00:00:00 2001 From: "dyfan.jones" Date: Fri, 16 Aug 2024 12:31:13 +0100 Subject: [PATCH 2/2] return error message if call has failed or needs redirecting --- paws.common/R/net.R | 1 + 1 file changed, 1 insertion(+) diff --git a/paws.common/R/net.R b/paws.common/R/net.R index 40460cd22..255717253 100644 --- a/paws.common/R/net.R +++ b/paws.common/R/net.R @@ -154,6 +154,7 @@ issue <- function(http_request) { resp_body <- function(resp, path) { if (is.null(path)) { body <- httr::content(resp, as = "raw") + # return error message if call has failed or needs redirecting } else if (resp$status_code %in% c(301, 400)) { body <- readBin(path, "raw", file.info(path)$size) unlink(path)