-
Notifications
You must be signed in to change notification settings - Fork 59
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
First stab at req_perform_sequential() #361
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4c364f4
First stab at req_perform_sequential()
hadley ac5fb1e
Polish; document; test
hadley 32486e2
Merged origin/main into sequential
hadley e65fbb8
Merge commit '8ecb2c4e15b1503f4840280390ad5923af125944'
hadley 4acbf7f
Inherit from correct docs
hadley de25dce
Merge commit '1f9ebd926709d60948cb68829a2c8571809123a6'
hadley 85518ac
Add news bullet
hadley db3cc13
Apply suggestions from code review
hadley 5330ff6
Feedback from code review
hadley 3fa4e51
Check that paths is a character vector
hadley 10a0b76
Add missing namespace
hadley 914f0c8
Add to reference page
hadley 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#' Perform multiple requests in sequence | ||
#' | ||
#' Given a list of requests, this function performs each in turn, returning | ||
#' a list of responses. It's slower than [req_perform_parallel()] but | ||
#' has fewer limitations. | ||
#' | ||
#' @inheritParams req_perform_parallel | ||
#' @inheritParams req_perform_iteratively | ||
#' @param on_error What should happen if one of the requests throws an | ||
#' HTTP error? | ||
#' | ||
#' * `stop`, the default: stop iterating and throw an error | ||
#' * `return`: stop iterating and return all the successful responses so far. | ||
#' * `continue`: continue iterating, recording errors in the result. | ||
#' @export | ||
#' @return | ||
#' A list, the same length as `reqs`. | ||
#' | ||
#' If `on_error` is `"return"` and it errors on the ith request, the ith | ||
#' element of the result will be an error object, and the remaining elements | ||
#' will be `NULL`. | ||
#' | ||
#' If `on_error` is `"continue"`, it will be a mix of requests and errors. | ||
#' @examples | ||
#' # One use of req_perform_sequential() is if the API allows you to request | ||
#' # data for multiple objects, you want data for more objects than can fit | ||
#' # in one request. | ||
#' req <- request("https://api.restful-api.dev/objects") | ||
#' | ||
#' # Imagine we have 50 ids: | ||
#' ids <- sort(sample(100, 50)) | ||
#' | ||
#' # But the API only allows us to request 10 at time. So we first use split | ||
#' # and some modulo arithmetic magic to generate chunks of length 10 | ||
#' chunks <- unname(split(ids, (seq_along(ids) - 1) %/% 10)) | ||
#' | ||
#' # Then we use lapply to generate one request for each chunk: | ||
#' reqs <- chunks %>% lapply(\(idx) req %>% req_url_query(id = idx, .multi = "comma")) | ||
#' | ||
#' # Then we can perform them all and get the results | ||
#' \dontrun{ | ||
#' resps <- reqs %>% req_perform_sequential() | ||
#' resps_data(resps, \(resp) resp_body_json(resp)) | ||
#' } | ||
req_perform_sequential <- function(reqs, | ||
paths = NULL, | ||
on_error = c("stop", "return", "continue"), | ||
progress = TRUE) { | ||
if (!is_bare_list(reqs)) { | ||
stop_input_type(reqs, "a list") | ||
} | ||
if (!is.null(paths)) { | ||
check_character(paths) | ||
if (length(reqs) != length(paths)) { | ||
hadley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cli::cli_abort("If supplied, {.arg paths} must be the same length as {.arg req}.") | ||
} | ||
} | ||
on_error <- arg_match(on_error) | ||
err_catch <- on_error != "stop" | ||
err_return <- on_error == "return" | ||
|
||
progress <- create_progress_bar( | ||
total = length(reqs), | ||
name = "Iterating", | ||
config = progress | ||
) | ||
|
||
resps <- rep_along(reqs, list()) | ||
|
||
tryCatch({ | ||
for (i in seq_along(reqs)) { | ||
check_request(reqs[[i]], arg = glue::glue("req[[{i}]]")) | ||
|
||
if (err_catch) { | ||
resps[[i]] <- tryCatch( | ||
req_perform(reqs[[i]], path = paths[[i]]), | ||
httr2_error = function(err) err | ||
) | ||
} else { | ||
resps[[i]] <- req_perform(reqs[[i]], path = paths[[i]]) | ||
} | ||
if (err_return && is_error(resps[[i]])) { | ||
break | ||
} | ||
progress$update() | ||
} | ||
}, interrupt = function(cnd) { | ||
resps <- resps[seq_len(i)] | ||
cli::cli_alert_warning("Terminating iteration; returning {i} response{?s}.") | ||
}) | ||
progress$done() | ||
|
||
resps | ||
} |
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# checks its inputs | ||
|
||
Code | ||
req_perform_sequential(req) | ||
Condition | ||
Error in `req_perform_sequential()`: | ||
! `reqs` must be a list, not a <httr2_request> object. | ||
Code | ||
req_perform_sequential(list(req), letters) | ||
Condition | ||
Error in `req_perform_sequential()`: | ||
! If supplied, `paths` must be the same length as `req`. | ||
|
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
test_that("checks its inputs", { | ||
req <- request("http://example.com") | ||
expect_snapshot(error = TRUE, { | ||
req_perform_sequential(req) | ||
req_perform_sequential(list(req), letters) | ||
}) | ||
}) | ||
|
||
test_that("can download files", { | ||
reqs <- list(request_test("/json"), request_test("/html")) | ||
paths <- c(withr::local_tempfile(), withr::local_tempfile()) | ||
resps <- req_perform_sequential(reqs, paths) | ||
|
||
expect_equal(resps[[1]]$body, new_path(paths[[1]])) | ||
expect_equal(resps[[2]]$body, new_path(paths[[2]])) | ||
|
||
# And check that something was downloaded | ||
expect_gt(file.size(paths[[1]]), 0) | ||
expect_gt(file.size(paths[[2]]), 0) | ||
}) | ||
|
||
test_that("on_error = 'return' returns error", { | ||
reqs <- list2( | ||
request_test("/status/:status", status = 200), | ||
request_test("/status/:status", status = 200), | ||
request_test("/status/:status", status = 404), | ||
request_test("/status/:status", status = 200) | ||
) | ||
out <- req_perform_sequential(reqs, on_error = "return") | ||
expect_length(out, 4) | ||
expect_s3_class(out[[3]], "httr2_http_404") | ||
expect_equal(out[[4]], NULL) | ||
}) | ||
|
||
|
||
test_that("on_error = 'continue' captures both error types", { | ||
reqs <- list2( | ||
request_test("/status/:status", status = 404), | ||
request("INVALID"), | ||
) | ||
out <- req_perform_sequential(reqs, on_error = "continue") | ||
expect_s3_class(out[[1]], "httr2_http_404") | ||
expect_s3_class(out[[2]], "httr2_failure") | ||
}) | ||
|
||
test_that("on_error = 'return' returns error", { | ||
reqs <- list2( | ||
request_test("/status/:status", status = 200), | ||
request_test("/status/:status", status = 200), | ||
request_test("/status/:status", status = 404), | ||
request_test("/status/:status", status = 200) | ||
) | ||
out <- req_perform_sequential(reqs, on_error = "return") | ||
expect_length(out, 4) | ||
expect_s3_class(out[[3]], "httr2_http_404") | ||
expect_equal(out[[4]], NULL) | ||
}) |
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.
@mgirlich let me know what you think of this error handling, and if it looks good, I'll port to
req_perform_parallel()
.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.
And
req_perform_iterative()
, although that won't have the"continue"
option.