Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: expose s3HTTP error checking without parse_response to e.g allow e.g 404's to be surfaced #377

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions R/get_object.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#' @param request_body For \code{select_object}, an XML request body as described in the \href{https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectSELECTContent.html}{SELECT API documentation}.
#' @param headers List of request headers for the REST call.
#' @param parse_response Passed through to \code{\link{s3HTTP}}, as this function requires a non-default setting. There is probably no reason to ever change this.
#' @param do_standalone_check_for_http_errors Passed through to \code{\link{s3HTTP}}, as this function requires a non-default setting. There is probably no reason to ever change this.
#' @param as Passed through to \code{httr::content}.
#' @template dots
#' @details \code{get_object} retrieves an object into memory as a raw vector. This page describes \code{get_object} and several wrappers that provide additional useful functionality.
Expand Down Expand Up @@ -80,6 +81,7 @@ function(object,
bucket,
headers = list(),
parse_response = FALSE,
do_standalone_check_for_http_errors = FALSE,
as = "raw",
...) {
if (missing(bucket)) {
Expand All @@ -91,6 +93,7 @@ function(object,
path = paste0("/", object),
headers = headers,
parse_response = parse_response,
do_standalone_check_for_http_errors = do_standalone_check_for_http_errors,
...)
cont <- httr::content(r, as = as)
return(cont)
Expand Down Expand Up @@ -136,6 +139,7 @@ function(
request_body,
headers = list(),
parse_response = FALSE,
do_standalone_check_for_http_errors = TRUE,
...
) {
if (missing(bucket)) {
Expand All @@ -150,6 +154,7 @@ function(
query = list(select = "", "select-type" = "2"),
request_body = request_body,
parse_response = parse_response,
do_standalone_check_for_http_errors = do_standalone_check_for_http_errors,
...)
cont <- httr::content(r, as = "raw")
return(cont)
Expand Down
17 changes: 14 additions & 3 deletions R/s3HTTP.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#' @param secret A character string containing an AWS Secret Access Key. If missing, defaults to value stored in environment variable \env{AWS_SECRET_ACCESS_KEY}.
#' @param session_token Optionally, a character string containing an AWS temporary Session Token. If missing, defaults to value stored in environment variable \env{AWS_SESSION_TOKEN}.
#' @param use_https Optionally, a logical indicating whether to use HTTPS requests. Default is \code{TRUE}.
#' @param do_standalone_check_for_http_errors A logical indicating whether to check for HTTP error status codes. If \code{parse_response} is \code{TRUE} then this is ignored. Default is \code{FALSE}.
#' @param ... Additional arguments passed to an HTTP request function. such as \code{\link[httr]{GET}}.
#' @return the S3 response, or the relevant error.
#' @import httr
Expand Down Expand Up @@ -52,6 +53,7 @@ function(verb = "GET",
secret = NULL,
session_token = NULL,
use_https = TRUE,
do_standalone_check_for_http_errors = FALSE,
...) {

# locate and validate credentials
Expand Down Expand Up @@ -224,9 +226,15 @@ function(verb = "GET",
# handle response, failing if HTTP error occurs
if (isTRUE(parse_response)) {
out <- parse_aws_s3_response(r, Sig, verbose = verbose)
check_aws_s3_status(r, Sig, out, verbose = verbose)
} else {
# even if we don't parse the response, we can still check for HTTP errors
if (isTRUE(do_standalone_check_for_http_errors)) {
check_aws_s3_status(r, Sig, verbose = verbose)
}
out <- r
}

attributes(out) <- c(attributes(out), httr::headers(r))
out
}
Expand All @@ -247,6 +255,11 @@ parse_aws_s3_response <- function(r, Sig, verbose = getOption("verbose")){
} else {
response <- r
}

return(response)
}

check_aws_s3_status <- function(r, Sig, response = NULL, verbose = getOption("verbose")) {
if (isTRUE(verbose)) {
message(httr::http_status(r)[["message"]])
}
Expand All @@ -259,11 +272,9 @@ parse_aws_s3_response <- function(r, Sig, verbose = getOption("verbose")){
print(out)
httr::stop_for_status(r)
}
return(response)
}

setup_s3_url <-
function(bucketname,
setup_s3_url <- function(bucketname,
region,
path,
accelerate = FALSE,
Expand Down