Skip to main content

Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

A type-safe HTTP client.

Calls are made via client.call::<EndpointType>(args), which is fully type-checked against the endpoint’s path captures, request body, and response type.

§Example

let client = Client::new("http://localhost:3000").unwrap();

// GET /users/42 — returns Json<User>
let user = client.call::<GetEndpoint<UserByIdPath, Json<User>>>((42u32,)).await?;

// POST /users — sends CreateUser, returns Json<User>
let new_user = client.call::<PostEndpoint<UsersPath, Json<CreateUser>, Json<User>>>(
    ((), CreateUser { name: "Alice".into(), email: "a@b.com".into() })
).await?;

Implementations§

Source§

impl Client

Source

pub fn new(base_url: &str) -> Result<Self, ClientError>

Create a new client pointing at the given base URL with default config.

Source

pub fn with_config( base_url: &str, config: ClientConfig, ) -> Result<Self, ClientError>

Create a client with a custom ClientConfig.

Source

pub fn with_reqwest(base_url: &str, client: Client) -> Result<Self, ClientError>

Create a client with a custom reqwest::Client.

Note: timeout settings from the provided reqwest::Client take precedence; the ClientConfig retry policy is still used.

Source

pub fn with_reqwest_and_config( base_url: &str, client: Client, config: ClientConfig, ) -> Result<Self, ClientError>

Create a client with both a custom reqwest::Client and config.

Timeout fields in config are ignored (the provided reqwest::Client owns its own timeout settings), but the retry policy is used.

Source

pub fn config(&self) -> &ClientConfig

Returns a reference to the current ClientConfig.

Source

pub async fn call<E: CallEndpoint>( &self, args: E::Args, ) -> Result<E::Response, ClientError>

Call an endpoint with the given arguments.

The endpoint type E determines the HTTP method, URL path, request body, and response type. All of these are verified at compile time.

If a retry policy is configured, retryable failures (matching status codes or timeouts) will be retried with exponential backoff and jitter.

Source

pub async fn call_with_query<E: CallEndpoint, Q: Serialize>( &self, args: E::Args, query: &Q, ) -> Result<E::Response, ClientError>

Call an endpoint with query parameters appended to the URL.

Works exactly like call but serializes query via serde_urlencoded and appends the result as a query string.

§Example
#[derive(Serialize)]
struct Pagination { page: u32, limit: u32 }

let users = client
    .call_with_query::<ListUsersEndpoint>((), &Pagination { page: 2, limit: 20 })
    .await?;
Source

pub async fn call_full<E: CallEndpoint>( &self, args: E::Args, ) -> Result<TypedResponse<E::Response>, ClientError>

Call an endpoint and return the full response metadata alongside the body.

This is the same as call but wraps the result in a TypedResponse that exposes the HTTP status code and headers.

If a retry policy is configured, retries are applied as with call.

Source

pub fn request<E: CallEndpoint>(&self, args: E::Args) -> RequestBuilder<'_, E>

Start building a request to an endpoint with per-call overrides.

Returns a RequestBuilder that allows adding extra headers, query parameters, or a per-request timeout before sending. Retries are not applied on the builder path.

§Example
let user = client
    .request::<GetUserEndpoint>((42u32,))
    .header(http::header::ACCEPT, HeaderValue::from_static("application/json"))
    .timeout(Duration::from_secs(5))
    .send()
    .await?;
Source§

impl Client

Source

pub async fn call_streaming<E: CallEndpoint>( &self, args: E::Args, ) -> Result<Response, ClientError>

Call an endpoint and return the raw response for streaming.

Unlike call, this does not deserialize the response body. The caller can stream the body using reqwest::Response::bytes_stream() or read it manually.

The request is built identically to call (path substitution, method, body serialization, interceptors), but the response is returned as-is after a status check. A non-2xx status code produces ClientError::Status.

Retries are not applied — streaming responses are not idempotent in general and partial reads cannot be rewound.

§Example
use futures::StreamExt;

let resp = client.call_streaming::<GetEndpoint<EventsPath, ()>>(()).await?;
let mut stream = resp.bytes_stream();
while let Some(chunk) = stream.next().await {
    let bytes = chunk?;
    // process chunk...
}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> IsEndpoint<T> for T