Perform a request with opinionated defaults
Source:R/req_perform_opinionated.R
req_perform_opinionated.RdThis 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_fn function is supplied) or httr2::req_perform() (if not).
Usage
req_perform_opinionated(
req,
...,
next_req_fn = choose_pagination_fn(req),
max_reqs = 2,
max_tries_per_req = 3
)Arguments
- req
The first request to perform.
- ...
These dots are for future extensions and must be empty.
- next_req_fn
(
function) An optional function that takes the previous response (resp) and request (req), and returns a new request. This function is passed asnext_reqin a call tohttr2::req_perform_iterative(). This function can usually be generated using one of the iteration helpers described inhttr2::iterate_with_offset(). By default,choose_pagination_fn()is used to check for a pagination policy (seereq_pagination_policy()), and returnsNULLif no such policy is defined.- max_reqs
(
length-1 integer) The maximum number of separate requests to perform. Passed to the max_reqs argument ofhttr2::req_perform_iterative()whennext_reqis supplied. You will mostly likely want to change the default value (2) toInfafter you validate that the request works.- max_tries_per_req
(
length-1 integer) The maximum number of times to attempt each individual request. Passed to themax_triesargument ofhttr2::req_retry().
Value
Always returns a list of httr2::response() objects, one for each
request performed, to ensure that downstream operations are the same
regardless of the number of responses. The list has additional class
nectar_responses.
Examples
# Performs a single request and returns a list of responses
req <- httr2::request("https://jsonplaceholder.typicode.com/posts")
resps <- req_perform_opinionated(req)
httr2::resp_status(resps[[1]])
#> [1] 200
resp_parse(resps, response_parser = resp_tidy_json)
#> # A tibble: 100 × 4
#> userId id title body
#> <int> <int> <chr> <chr>
#> 1 1 1 sunt aut facere repellat provident occaecati excepturi op… "qui…
#> 2 1 2 qui est esse "est…
#> 3 1 3 ea molestias quasi exercitationem repellat qui ipsa sit a… "et …
#> 4 1 4 eum et est occaecati "ull…
#> 5 1 5 nesciunt quas odio "rep…
#> 6 1 6 dolorem eum magni eos aperiam quia "ut …
#> 7 1 7 magnam facilis autem "dol…
#> 8 1 8 dolorem dolore est ipsam "dig…
#> 9 1 9 nesciunt iure omnis dolorem tempora et accusantium "con…
#> 10 1 10 optio molestias id quia eum "quo…
#> # ℹ 90 more rows