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

Record requests in responses and errors #359

Merged
merged 5 commits into from
Oct 27, 2023
Merged
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
7 changes: 4 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ export(resp_url_queries)
export(resp_url_query)
export(response)
export(response_json)
export(resps_combine)
export(resps_errors)
export(resps_responses)
export(resps_data)
export(resps_failures)
export(resps_requests)
export(resps_successes)
export(secret_decrypt)
export(secret_decrypt_file)
export(secret_encrypt)
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# httr2 (development version)

* A new family of functions `resps_successes()`, `resps_failures()`,
`resps_requests()` and `resps_data()` make it easier to work with lists of
responses as returned by `req_perform_parallel()` and
`req_perform_iteratively()` (#357).

* The request is now stored in the response object (and errors that httr2
throws), making it easier to debug when things go wrong (#357).

* New `oauth_token_cached()` that allows you to get an OAuth token while still
taking advantage of httr2's caching and auto-renewal features. For expert
use only (#328).
Expand Down
62 changes: 47 additions & 15 deletions R/iterate-responses.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,63 @@
#' Tools for working with lists of responses
#'
#' * `resps_combine()` combines the data from each response into a single
#' vector.
#' * `resps_response()` returns all successful responses.
#' * `resps_error()` returns all errors.
#' @description
#' These function provide a basic toolkit for operating with lists of
#' responses as returned [req_perform_parallel()] and
#' [req_perform_iteratively()].
#'
#' * `resps_successes()` returns a list successful responses.
#' * `resps_failures()` returns a list failed responses (i.e. errors).
#' * `resps_requests()` returns the list of requests that corresponds to
#' each request.
#' * `resps_data()` returns all the data in a single vector or data frame.
#' It requires the vctrs package to be installed.
#'
#' @export
#' @param resps A list of responses (possibly including errors).
#' @param resp_data A function that takes a response (`resp`) and
#' returns the data foind inside that response as a vector or data frame.
resps_combine <- function(resps, resp_data) {
check_installed("vctrs")
#' @examples
#' reqs <- list(
#' request(example_url()) |> req_url_path("/ip"),
#' request(example_url()) |> req_url_path("/user-agent"),
#' request(example_url()) |> req_template("/status/:status", status = 404),
#' request("INVALID")
#' )
#' resps <- req_perform_parallel(reqs)
#'
#' # find successful responses
#' resps |> resps_successes()
#'
#' # collect all their data
#' resps |> resps_successes() |> resps_data(\(resp) resp_body_json(resp))
#'
#' # find requests corresponding to failure responses
#' resps |> resps_failures() |> resps_requests()
resps_successes <- function(resps) {
resps[resps_ok(resps)]
}

check_function2(resp_data, "resp")
vctrs::list_unchop(lapply(resps, resp_data))
#' @export
#' @rdname resps_successes
resps_failures <- function(resps) {
resps[!resps_ok(resps)]
}
resps_is_resp <- function(resps) {

resps_ok <- function(resps) {
vapply(resps, is_response, logical(1))
}

#' @export
#' @rdname resps_combine
resps_responses <- function(resps) {
resps[resps_is_resp(resps)]
#' @rdname resps_successes
resps_requests <- function(resps) {
lapply(resps, function(x) x$request)
}

#' @export
#' @rdname resps_combine
resps_errors <- function(resps) {
resps[!resps_is_resp(resps)]
#' @rdname resps_successes
resps_data <- function(resps, resp_data) {
check_installed("vctrs")
check_function2(resp_data, "resp")

vctrs::list_unchop(lapply(resps, resp_data))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe resps_data() should check that all responses were successful. And I get this beauty when applying to an error:

devtools::load_all("~/GitHub/httr2/")
#> ℹ Loading httr2
reqs <- list(
  request(example_url()) |> req_template("/status/:status", status = 404),
  request("INVALID")
)
resps <- req_perform_parallel(reqs)

resps |> resps_data(\(resp) resp_body_json(resp))
#> Error in `env_has()` at httr2/R/resp-body.R:82:2:
#> ! `env` must be an environment, not `NULL`.
#> Backtrace:
#>     ▆
#>  1. └─httr2::resps_data(resps, function(resp) resp_body_json(resp))
#>  2.   ├─vctrs::list_unchop(lapply(resps, resp_data)) at httr2/R/iterate-responses.R:62:2
#>  3.   └─base::lapply(resps, resp_data)
#>  4.     └─global FUN(X[[i]], ...)
#>  5.       └─httr2::resp_body_json(resp)
#>  6.         └─rlang::env_has(env = resp$cache) at httr2/R/resp-body.R:82:2
#>  7.           └─rlang:::check_environment(env)
#>  8.             └─rlang:::stop_input_type(...)
#>  9.               └─rlang::abort(message, ..., call = call, arg = arg)

Created on 2023-10-27 with reprex v2.0.2

This suggests that we should be more careful with the caching in resp_body_json() and resp_body_xml()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooops, no, that's just because check_response(resp) should come before the cache lookup, not after 😬

}
4 changes: 2 additions & 2 deletions R/iterate.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' using a callback function, `next_req`, to define the next request based on
#' the current request and response. You will probably want to it pair with an
#' [iteration helper][iterate_with_offset] and use a
#' [multi-response handler][resps_combine] to process the result.
#' [multi-response handler][resps_successes] to process the result.
#'
#' @inheritParams req_perform
#' @param next_req A function that takes the previous response (`resp`) and
Expand All @@ -32,7 +32,7 @@
#'
#' resps <- req_perform_iteratively(req, iterate_with_offset("page_index"))
#'
#' resps |> resps_combine(function(resp) {
#' resps |> resps_data(function(resp) {
#' data <- resp_body_json(resp)$data
#' data.frame(
#' Sepal.Length = sapply(data, `[[`, "Sepal.Length"),
Expand Down
14 changes: 9 additions & 5 deletions R/multi-req.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@
#' )
#' # req_perform_parallel() will always succeed
#' resps <- req_perform_parallel(reqs)
#' # you'll need to inspect the results to figure out which requests fails
#' fail <- vapply(resps, inherits, "error", FUN.VALUE = logical(1))
#' resps[fail]
#'
#' # Inspect the successful responses
#' resps |> resps_successes()
#'
#' # And the failed responses
#' resps |> resps_failures() |> resps_requests()
req_perform_parallel <- function(reqs, paths = NULL, pool = NULL, cancel_on_error = FALSE) {
if (!is.null(paths)) {
if (length(reqs) != length(paths)) {
Expand Down Expand Up @@ -161,7 +164,8 @@ Performance <- R6Class("Performance", public = list(
url = res$url,
status_code = res$status_code,
headers = as_headers(res$headers),
body = body
body = body,
request = self$req
)
resp <- cache_post_fetch(self$reqs, resp, path = self$paths)

Expand All @@ -172,7 +176,7 @@ Performance <- R6Class("Performance", public = list(
},

fail = function(msg) {
self$resp <- error_cnd("httr2_failure", message = msg)
self$resp <- error_cnd("httr2_failure", message = msg, request = self$req)
signal("", class = "httr2:::failed")
},

Expand Down
6 changes: 4 additions & 2 deletions R/req-perform.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ req_perform <- function(
message = "Failed to perform HTTP request.",
class = "httr2_failure",
parent = err,
request = req,
call = error_call,
trace = trace_back()
)
Expand Down Expand Up @@ -146,7 +147,7 @@ handle_resp <- function(req, resp, error_call = caller_env()) {
cnd_signal(resp)
} else if (error_is_error(req, resp)) {
body <- error_body(req, resp, error_call)
resp_abort(resp, body, call = error_call)
resp_abort(resp, req, body, call = error_call)
} else {
resp
}
Expand All @@ -173,7 +174,8 @@ req_perform1 <- function(req, path = NULL, handle = NULL) {
url = res$url,
status_code = res$status_code,
headers = as_headers(res$headers),
body = body
body = body,
request = req
)
the$last_response <- resp
resp
Expand Down
17 changes: 13 additions & 4 deletions R/req.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,24 @@ is_request <- function(x) {
inherits(x, "httr2_request")
}

check_request <- function(req, arg = caller_arg(req), call = caller_env()) {
if (!missing(req) && is_request(req)) {
return(invisible(NULL))
check_request <- function(req,
arg = caller_arg(req),
call = caller_env(),
allow_null = FALSE) {
if (!missing(req)) {
if (is_request(req)) {
return(invisible(NULL))
}

if (allow_null && is.null(req)) {
return(invisible(NULL))
}
}

stop_input_type(
req,
"an HTTP request object",
allow_null = FALSE,
allow_null = allow_null,
arg = arg,
call = call
)
Expand Down
11 changes: 6 additions & 5 deletions R/resp-body.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@ resp_body_string <- function(resp, encoding = NULL) {
#' @rdname resp_body_raw
#' @export
resp_body_json <- function(resp, check_type = TRUE, simplifyVector = FALSE, ...) {
check_response(resp)
check_installed("jsonlite")

key <- body_cache_key("json", simplifyVector = simplifyVector, ...)
if (env_has(resp$cache, key)) {
return(resp$cache[[key]])
}

check_response(resp)
check_installed("jsonlite")
resp_check_content_type(
resp,
valid_types = "application/json",
Expand Down Expand Up @@ -114,14 +115,14 @@ resp_body_html <- function(resp, check_type = TRUE, ...) {
#' @rdname resp_body_raw
#' @export
resp_body_xml <- function(resp, check_type = TRUE, ...) {
check_response(resp)
check_installed("xml2")

key <- body_cache_key("xml", ...)
if (env_has(resp$cache, key)) {
return(resp$cache[[key]])
}


check_response(resp)
check_installed("xml2")
resp_check_content_type(
resp,
valid_types = c("application/xml", "text/xml"),
Expand Down
5 changes: 3 additions & 2 deletions R/resp-status.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ resp_check_status <- function(resp, info = NULL, error_call = caller_env()) {
if (!resp_is_error(resp)) {
invisible(resp)
} else {
resp_abort(resp, info, call = error_call)
resp_abort(resp, resp$request, info, call = error_call)
}
}

resp_abort <- function(resp, info = NULL, call = caller_env()) {
resp_abort <- function(resp, req, info = NULL, call = caller_env()) {
status <- resp_status(resp)
desc <- resp_status_desc(resp)
message <- glue("HTTP {status} {desc}.")
Expand All @@ -80,6 +80,7 @@ resp_abort <- function(resp, info = NULL, call = caller_env()) {
status = status,
resp = resp,
class = c(glue("httr2_http_{status}"), "httr2_http"),
request = req,
call = call
)
}
Expand Down
3 changes: 3 additions & 0 deletions R/resp.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ new_response <- function(method,
status_code,
headers,
body,
request = NULL,
error_call = caller_env()) {
check_string(method, call = error_call)
check_string(url, call = error_call)
check_number_whole(status_code, call = error_call)
check_request(request, allow_null = TRUE)

headers <- as_headers(headers, error_call = error_call)
# ensure we always have a date field
Expand All @@ -91,6 +93,7 @@ new_response <- function(method,
status_code = status_code,
headers = headers,
body = body,
request = request,
cache = new_environment()
),
class = "httr2_response"
Expand Down
8 changes: 5 additions & 3 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ reference:
- starts_with("req_auth")
- starts_with("req_oauth")

- title: Perform one or more requests
- title: Perform a request
contents:
- starts_with("req_perform")
- req_perform
- req_perform_stream

- subtitle: Control the process
desc: >
Expand All @@ -55,9 +56,10 @@ reference:
- req_throttle
- req_retry

- title: Iteration
- title: Perform multiple requests
contents:
- req_perform_iteratively
- req_perform_parallel
- starts_with("iterate_")
- starts_with("resps_")

Expand Down
4 changes: 2 additions & 2 deletions man/req_perform_iteratively.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions man/req_perform_parallel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 0 additions & 28 deletions man/resps_combine.Rd

This file was deleted.

Loading
Loading