Skip to contents

A client carries what many requests share: a base URL, default headers and query parameters, policy defaults, and the connection pool. It is never the first argument of a request function — see below.

Usage

zu_client(
  base_url = NULL,
  headers = NULL,
  query = NULL,
  timeout = NULL,
  redirects = NULL,
  verify = NULL,
  max_body = NULL,
  user_agent = NULL,
  check = NULL,
  decode = NULL,
  transport = zu_native_transport(),
  pool = zu_pool(),
  retry = NULL,
  middleware = NULL,
  hooks = NULL,
  proxy = NULL,
  tls = NULL,
  http2 = NULL
)

Arguments

base_url

Optional base URL, so that zu_get("/users", client = api) works. Joining is textual, not RFC 3986 resolution: see Details.

headers

Named character vector of default headers.

query

Named list of default query parameters.

timeout

Total seconds for a request, redirects included. DNS resolution is not bounded by it (nor by Ctrl-C); see zuhttp_tls.

redirects

Maximum redirects to follow. 0 returns the 3xx itself. A Location that is not a valid URL reference — a raw space or non-ASCII bytes — raises zu_url_error rather than being repaired (§8.2), and a redirect back to a URL already requested raises zu_redirect_error.

verify

Verify the certificate and hostname. Leave this TRUE.

max_body

Maximum decoded body size in bytes; Inf for no limit.

user_agent

Default User-Agent. Unset, requests send zuhttp/<version>.

check

Raise a condition for 4xx and 5xx responses (§31.14). FALSE returns the response whatever its status.

decode

Decompress the body transparently (§21.2).

transport

The object that actually performs requests. The default is the network; zu_mock_transport() and zu_cassette_transport() replace it in tests.

pool

Connection reuse settings from zu_pool(), or NULL to open a fresh connection for every request.

retry

A zu_retry() policy. The default retries nothing.

middleware

A function of (req, next_fn), or a list of them, wrapping request execution (§31.13). The first is outermost.

hooks

Lifecycle observers from zu_hooks().

proxy

Proxy URL, e.g. "http://proxy:3128". Leave unset to honour the environment (§20.1); pass FALSE to force a direct connection. Not NULL — under the §31.9 merge rules NULL means "reset to the package default", which for a proxy is "consult the environment". FALSE is the only spelling that can mean "definitely do not proxy" without making this one argument an exception to a rule every other policy argument follows.

tls

Certificate trust settings from zu_tls().

http2

HTTP/2 use: "auto" (the default) offers it to every HTTPS server and uses it when the server chooses it, and HTTP/1.1 otherwise; "required" fails with zu_connect_error before sending anything when the connection is not HTTP/2, and on an http:// URL speaks cleartext HTTP/2 (h2c) directly, without an Upgrade; "off" uses HTTP/1.1 only. zu_resp_connection() reports which version a response came over. A client setting only: there is no per-request form, because it chooses the connection, not the request.

Value

A zu_client object.

Details

base_url is joined to a request path, not resolved against it: a client with base_url = "https://api.example.com/v1" and a request path of "/users" produces https://api.example.com/v1/users. RFC 3986 resolution would give https://api.example.com/users — the /v1 silently dropped — which is the single most common surprise in libraries that resolve here. An absolute request URL ignores base_url entirely.

The client is never the first argument

zu_get(url, ..., client = api), never zu_get(api, url). Overloading argument one by type breaks autocomplete, makes dispatch murky, and turns a misplaced argument into a confusing error instead of a clear one (§31.3).

See also

zu_client_update() to derive a client without mutating it.

Examples

api <- zu_client(
  base_url = "https://api.example.com",
  headers = c(Accept = "application/json")
)
api
#> <zu_client>
#>   base_url:  https://api.example.com
#>   Accept: application/json
#>   timeout: 30  (default)
#>   redirects: 10  (default)
#>   verify: TRUE  (default)
#>   max_body: 16777216  (default)
#>   check: TRUE  (default)
#>   decode: TRUE  (default)
#>   retry: off (1 attempt)  (default)
#>   http2: auto  (default)