-
Notifications
You must be signed in to change notification settings - Fork 57
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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: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()
andresp_body_xml()
There was a problem hiding this comment.
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 😬