livery_client (livery v0.4.4)
View SourceA composable HTTP client: the outbound twin of the server middleware.
Build a client once with new/1 (a transport adapter, a base URL,
default headers, and a layer stack), then call it with get/2, post/3,
request/3,4. Layers run outermost-first and each is
call(Request, Next, State) -> {ok, response()} | {error, term()}, the
same shape as server middleware, with errors threaded as values.
Client = livery_client:new(#{
base_url => <<"https://api.example.com">>,
stack => [
livery_client:timeout(5000),
livery_client:retry(#{max => 3}),
livery_client:circuit_breaker(#{name => api}),
livery_client:concurrency(50)
]
}),
{ok, Resp} = livery_client:get(Client, <<"/users/42">>),
200 = livery_client:status(Resp).The transport is a livery_client_adapter; the default,
livery_client_hackney, speaks HTTP/1.1, HTTP/2, and HTTP/3.
Summary
Functions
Add an endpoint to a balance pool at runtime.
Spread requests across a pool of endpoints, with passive outlier
ejection and lazy half-open recovery. Opts: name (required),
endpoints (base URLs or a {Module, Arg} discovery pair), policy
(p2c | round_robin), eject_after, eject_for, fail_status.
With balance you pass paths; the chosen endpoint supplies the host.
A cookie jar (RFC 6265 client subset). Stores the Set-Cookie cookies a
response carries and sends the matching ones (host, path, secure) on later
requests through this layer, in an in-memory per-jar store shared across
the client's request processes. Opts: max_cookies (default 3000),
store (a livery_client_cookie_store callback module; default ETS).
Build a client. Opts: adapter (default livery_client_hackney),
adapter_opts, base_url, headers (defaults applied to every
request), stack (the layers).
Pull the next chunk of a {stream, Reader} response body.
Drain a {stream, Reader} response body to a single binary.
Point a request URL at a chosen endpoint. Strips any scheme+authority
the URL already carries and joins the endpoint with the remaining
path+query, so the balancer owns the host. Used by livery_client_balance.
Remove an endpoint from a balance pool at runtime.
Send a request. Opts: body (iodata | {full, _} | {stream, Fun}),
headers, timeout, stream (true to receive a {stream, Reader}
response body), meta.
Run a fully built request through the client's layer stack.
Cancel a push stream and release its connection.
Ask a flow => manual push stream for one more {chunk, _} message.
Ref is the value from the {push, Ref} response body.
Types
Functions
Add an endpoint to a balance pool at runtime.
Spread requests across a pool of endpoints, with passive outlier
ejection and lazy half-open recovery. Opts: name (required),
endpoints (base URLs or a {Module, Arg} discovery pair), policy
(p2c | round_robin), eject_after, eject_for, fail_status.
With balance you pass paths; the chosen endpoint supplies the host.
-spec concurrency(non_neg_integer()) -> entry().
-spec cookie_jar() -> entry().
A cookie jar (RFC 6265 client subset). Stores the Set-Cookie cookies a
response carries and sends the matching ones (host, path, secure) on later
requests through this layer, in an in-memory per-jar store shared across
the client's request processes. Opts: max_cookies (default 3000),
store (a livery_client_cookie_store callback module; default ETS).
Build a client. Opts: adapter (default livery_client_hackney),
adapter_opts, base_url, headers (defaults applied to every
request), stack (the layers).
Pull the next chunk of a {stream, Reader} response body.
Drain a {stream, Reader} response body to a single binary.
Point a request URL at a chosen endpoint. Strips any scheme+authority
the URL already carries and joins the endpoint with the remaining
path+query, so the balancer owns the host. Used by livery_client_balance.
Remove an endpoint from a balance pool at runtime.
Send a request. Opts: body (iodata | {full, _} | {stream, Fun}),
headers, timeout, stream (true to receive a {stream, Reader}
response body), meta.
Push streaming: set stream_to to a pid and the response is delivered to
it as ordered messages, freeing the pid to selectively receive body chunks
alongside its own control messages instead of dedicating a process to a
pull loop. The reply is {ok, #{body := {push, Ref}}}; the recipient then
receives
{livery_response, Ref, {status, Status, Headers}}
{livery_response, Ref, {chunk, Binary}} %% zero or more
{livery_response, Ref, done}
{livery_response, Ref, {error, Reason}} %% instead of done, on failureflow (default auto) pushes chunks as fast as the wire allows; manual
sends one chunk per stream_next/1 for backpressure. stop_stream/1
cancels the stream and drops its connection. Push streaming bypasses the
layer stack; the adapter owns the connection.
Run a fully built request through the client's layer stack.
-spec status(response()) -> 100..599.
-spec stop_stream(stream_ref()) -> ok.
Cancel a push stream and release its connection.
-spec stream_next(stream_ref()) -> ok | {error, term()}.
Ask a flow => manual push stream for one more {chunk, _} message.
Ref is the value from the {push, Ref} response body.
-spec timeout(pos_integer()) -> entry().