Builds an httr2::request() for a scholarly API, with its rate, retries and
timeout. It is not sent: give it to polite_request() with robots = FALSE,
or to httr2::req_perform_parallel() for APIs that allow several requests
at once. The clients of the package (crossref_request(),
scopus_request(), openalex_request()) are built on it.
Arguments
- base_url
Character. Root of the API, or a complete URL.
- path
Character or
NULL. Route appended tobase_url.- query
Named list of query parameters.
NULLelements are dropped.- headers
Named list of headers.
- secret_headers
Character. Names of the headers holding keys, which httr2 hides when printing.
- rate
Numeric or
NULL. Maximum requests per second, spaced evenly.- realm
Character or
NULL. Throttling group; by default the host.- max_tries
Integer. Attempts per request.
- backoff
Function of the attempt number returning the seconds to wait.
- transient
HTTP codes worth retrying, or a function of a response.
- timeout
Numeric. Seconds before the request is abandoned.
- user_agent
Character. By default the option
politecollect.user_agent.
Details
The throttle is a bucket of one token, refilled every 1 / rate seconds, so
that requests are evenly spaced. httr2's default bucket of 60 seconds would
allow a burst of 60 * rate requests at once.
Examples
req <- api_request(
"https://api.example.org", "works",
query = list(q = "money"), rate = 2, user_agent = "my_project (me@example.org)"
)
req$url
#> [1] "https://api.example.org/works?q=money"