-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export req_perform_opinionated (#24)
- Loading branch information
1 parent
dc7f95a
commit 3bc6eb7
Showing
8 changed files
with
155 additions
and
66 deletions.
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#' Perform a request with opinionated defaults | ||
#' | ||
#' This function ensures that a request has [httr2::req_retry()] applied, and | ||
#' then performs the request, using either [httr2::req_perform_iterative()] (if | ||
#' a `next_req` function is supplied) or [httr2::req_perform()] (if not). | ||
#' | ||
#' @inheritParams .shared-parameters | ||
#' @inheritParams httr2::req_perform_iterative | ||
#' @inheritParams rlang::args_dots_empty | ||
#' @param max_reqs The maximum number of separate requests to perform. Passed to | ||
#' the max_reqs argument of [httr2::req_perform_iterative()] when `next_req` | ||
#' is supplied. The default `2` should likely be changed to `Inf` after you | ||
#' validate the function. | ||
#' @param max_tries_per_req The maximum number of times to attempt each | ||
#' individual request. Passed to the `max_tries` argument of | ||
#' [httr2::req_retry()]. | ||
#' | ||
#' @return A list of [httr2::response()] objects, one for each request | ||
#' performed. | ||
#' @export | ||
req_perform_opinionated <- function(req, | ||
..., | ||
next_req = NULL, | ||
max_reqs = 2, | ||
max_tries_per_req = 3) { | ||
rlang::check_dots_empty() | ||
req <- .req_apply_retry_default(req, max_tries_per_req) | ||
if (is.null(next_req)) { | ||
return(list(req_perform(req))) | ||
} | ||
req_perform_iterative( | ||
req, | ||
next_req = next_req, | ||
max_reqs = max_reqs | ||
) | ||
} | ||
|
||
.req_apply_retry_default <- function(req, max_tries_per_req) { | ||
if ( | ||
any(c("retry_max_wait", "retry_max_tries") %in% names(req$policies)) || | ||
is.null(max_tries_per_req) | ||
) { | ||
return(req) | ||
} | ||
return(httr2::req_retry(req, max_tries = max_tries_per_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
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
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,33 @@ | ||
test_that("req_perform_opinionated() applies iteration when appropriate", { | ||
local_mocked_bindings( | ||
req_perform = function(req) { | ||
"req_perform" | ||
}, | ||
req_perform_iterative = function(req, next_req = NULL, max_reqs = Inf) { | ||
"req_perform_iterative" | ||
} | ||
) | ||
req <- httr2::request("https://example.com") | ||
expect_identical(req_perform_opinionated(req), list("req_perform")) | ||
expect_identical( | ||
req_perform_opinionated(req, next_req = c), | ||
"req_perform_iterative" | ||
) | ||
}) | ||
|
||
test_that("req_perform_opinionated applies retries when appropriate", { | ||
local_mocked_bindings( | ||
req_perform = function(req) req, | ||
req_perform_iterative = function(req, ...) req | ||
) | ||
req <- httr2::request("https://example.com") | ||
expect_identical( | ||
req_perform_opinionated(req), | ||
list(httr2::req_retry(req, max_tries = 3)) | ||
) | ||
req_with_retries <- httr2::req_retry(req, max_seconds = 10) | ||
expect_identical( | ||
req_perform_opinionated(req_with_retries), | ||
list(req_with_retries) | ||
) | ||
}) |