This function implements an opinionated framework for preparing an API
request. It is intended to be used inside an API client package. It serves as
a wrapper around the req_
family of functions, such as httr2::request()
.
Usage
req_prepare(
base_url,
...,
path = NULL,
query = NULL,
body = NULL,
mime_type = NULL,
method = NULL,
additional_user_agent = NULL,
auth_fn = NULL,
auth_args = list(),
tidy_fn = NULL,
tidy_args = list(),
pagination_fn = NULL,
call = rlang::caller_env()
)
Arguments
- base_url
(
length-1 character
) The part of the url that is shared by all calls to the API. In some cases there may be a family of base URLs, from which you will need to choose one.- ...
These dots are for future extensions and must be empty.
- path
(
character
orlist
) The route to an API endpoint. Optionally, a list or character vector with the path as one or more unnamed arguments (which will be concatenated with "/") plus named arguments toglue::glue()
into the path.- query
(
character
orlist
) An optional list or character vector of parameters to pass in the query portion of the request. Can also include a.multi
argument to pass tohttr2::req_url_query()
to control how elements containing multiple values are handled.- body
(multiple types) An object to use as the body of the request. If any component of the body is a path, pass it through
fs::path()
or otherwise give it the class "fs_path" to indicate that it is a path.- mime_type
(
length-1 character
) The mime type of any files present in the body. Some APIs allow you to leave this as NULL for them to guess.- method
(
length-1 character
, optional) If the method is something other thanGET
orPOST
, supply it. Case is ignored.- additional_user_agent
(
length-1 character
) A string to identify where a request is coming from. We automatically include information about your package and nectar, but use this to provide additional details. DefaultNULL
.- auth_fn
(
function
) A function to use to authenticate the request. By default (NULL
), no authentication is performed.- auth_args
(
list
) An optional list of arguments to theauth_fn
function.- tidy_fn
(
function
) A function that will be invoked byresp_tidy()
to tidy the response.- tidy_args
(
list
) A list of additional arguments to pass totidy_fn
.- pagination_fn
(
function
) A function that takes the previous response (resp
) to generate the next request in a call tohttr2::req_perform_iterative()
. This function can usually be generated using one of the iteration helpers described inhttr2::iterate_with_offset()
. This function will be extracted from the request byreq_perform_opinionated()
and passed on asnext_req
tohttr2::req_perform_iterative()
.- call
(
environment
) The environment from which a function was called, e.g.rlang::caller_env()
(the default). The environment will be mentioned in error messages as the source of the error. This argument is particularly useful for functions that are intended to be called as utilities inside other functions.
Value
A httr2::request()
object with additional class nectar_request
.
See also
Other opinionated request functions:
req_init()
,
req_modify()
,
req_pagination_policy()
,
req_tidy_policy()