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
impl Client
Sourcepub fn new(base_url: &str) -> Result<Self, ClientError>
pub fn new(base_url: &str) -> Result<Self, ClientError>
Create a new client pointing at the given base URL with default config.
Sourcepub fn with_config(
base_url: &str,
config: ClientConfig,
) -> Result<Self, ClientError>
pub fn with_config( base_url: &str, config: ClientConfig, ) -> Result<Self, ClientError>
Create a client with a custom ClientConfig.
Sourcepub fn with_reqwest(base_url: &str, client: Client) -> Result<Self, ClientError>
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.
Sourcepub fn with_reqwest_and_config(
base_url: &str,
client: Client,
config: ClientConfig,
) -> Result<Self, ClientError>
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.
Sourcepub fn config(&self) -> &ClientConfig
pub fn config(&self) -> &ClientConfig
Returns a reference to the current ClientConfig.
Sourcepub async fn call<E: CallEndpoint>(
&self,
args: E::Args,
) -> Result<E::Response, ClientError>
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.
Sourcepub async fn call_with_query<E: CallEndpoint, Q: Serialize>(
&self,
args: E::Args,
query: &Q,
) -> Result<E::Response, ClientError>
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?;Sourcepub async fn call_full<E: CallEndpoint>(
&self,
args: E::Args,
) -> Result<TypedResponse<E::Response>, ClientError>
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.
Sourcepub fn request<E: CallEndpoint>(&self, args: E::Args) -> RequestBuilder<'_, E>
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
impl Client
Sourcepub async fn call_streaming<E: CallEndpoint>(
&self,
args: E::Args,
) -> Result<Response, ClientError>
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...
}