Skip to main content

ClientBuilder

Struct ClientBuilder 

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

Builder for Client.

Implementations§

Source§

impl ClientBuilder

Source

pub fn new() -> Self

Creates an empty builder; Self::base_url is required before Self::build.

Source

pub fn base_url(self, base_url: impl AsRef<str>) -> Result<Self>

Sets the base URL (required).

Source

pub fn timeout(self, timeout: Duration) -> Self

Sets the default request timeout.

Source

pub fn retry(self, policy: RetryPolicy) -> Self

Sets the default RetryPolicy for all requests.

Source

pub fn auth(self, auth: Auth) -> Self

Sets default authentication for all requests.

Source

pub fn default_header( self, key: impl AsRef<str>, value: impl AsRef<str>, ) -> Result<Self>

Adds a default header applied to every request.

Source

pub fn hooks(self, hooks: Hooks) -> Self

Sets client-level lifecycle hooks.

Source

pub fn plugin<P: Plugin + 'static>(self, plugin: P) -> Self

Registers a [Plugin] on this client.

Source

pub fn reqwest_client(self, client: ReqwestClient) -> Self

Uses a custom reqwest client for the default ReqwestBackend.

Source

pub fn backend(self, backend: Arc<dyn HttpBackend>) -> Self

Use a custom HTTP backend (for testing or alternate transports).

§Examples
let client = ClientBuilder::new()
    .base_url("https://api.example.com")?
    .backend(Arc::new(MockBackend))
    .build()?;
Source

pub fn wire_concurrency_limit(self, limit: usize) -> Self

Declares the limit used by ConcurrencyLimitLayer on your transport stack so Self::build can warn when it matches Self::max_in_flight.

This does not configure Tower; it is only for diagnostics when stacking client and transport caps.

Source

pub fn max_in_flight(self, limit: usize) -> Self

Limits how many requests this client may have in flight at once (including retries).

Implemented with a tokio semaphore in the core client. This counts the full request lifecycle (hooks and retries), not just the transport hop. For wire-level limits only, use Self::transport_stack with Tower’s ConcurrencyLimitLayer (feature tower) instead of—or deliberately alongside—this setting.

Source

pub fn max_response_bytes(self, limit: u64) -> Self

Maximum response body size (in bytes) for RequestBuilder::send, send_json, and send_stream when the request does not set its own limit.

Source

pub fn retry_body_peek_bytes(self, limit: u64) -> Self

Maximum bytes read from a streaming body when a custom retry predicate is configured.

Defaults to 64 KiB. Capped by Self::max_response_bytes when that is also set.

Source

pub fn schema_registry(self, registry: Arc<SchemaRegistry>) -> Self

Available on crate feature schema only.

Attach a SchemaRegistry for strict route validation (feature schema).

Source

pub fn http_service<S>(self, service: S) -> Self
where S: Service<HttpRequest, Response = HttpResponse, Error = Error> + Clone + Send + 'static, S::Future: Send + 'static,

Available on crate feature tower only.

Use a Tower Service as the HTTP transport for buffered send() only.

send_stream() uses the default reqwest streaming transport without your Tower layers. For middleware on both paths, use Self::transport_stack.

Source

pub fn http_service_boxed(self, service: BoxHttpService) -> Self

Available on crate feature tower only.

Use a boxed Tower transport stack for buffered send() only (streaming uses plain reqwest).

Prefer Self::transport_stack when send_stream() must see the same middleware.

Source

pub fn transport_stack<F>(self, configure: F) -> Self

Available on crate feature tower only.

Build a Tower transport stack on top of the configured (or default) reqwest client.

Application hooks and RetryPolicy remain in the core client; only wire-level behavior is configured here.

§Examples
let client = ClientBuilder::new()
    .base_url("https://api.example.com")?
    .transport_stack(|buffered, streaming| {
        (
            ServiceBuilder::new()
                .layer(ConcurrencyLimitLayer::new(32))
                .service(buffered)
                .into_box(),
            ServiceBuilder::new()
                .layer(ConcurrencyLimitLayer::new(32))
                .service(streaming)
                .into_streaming_box(),
        )
    })
    .build()?;
Source

pub fn json_parser<F>(self, f: F) -> Self
where F: Fn(&Bytes) -> Result<Value, String> + Send + Sync + 'static,

Available on crate feature json only.

Sets a custom JSON parser for all responses from this client.

See crate::json_parser for the two-step BytesValueT pipeline vs the default single-step fast path, and Response::into_json_with for per-response BytesT without a global parser.

§Examples
let client = ClientBuilder::new()
    .base_url("https://api.example.com")?
    .json_parser(|body: &Bytes| {
        let slice = body.strip_prefix(b"\xef\xbb\xbf").unwrap_or(body);
        serde_json::from_slice(slice).map_err(|e| e.to_string())
    })
    .build()?;
Source

pub fn json_parser_fn(self, parser: JsonParserFn) -> Self

Available on crate feature json only.

Sets a custom JSON parser from an existing JsonParserFn.

Source

pub fn build(self) -> Result<Client>

Builds the Client. Requires Self::base_url.

§Examples
let client = ClientBuilder::new()
    .base_url("https://api.example.com")?
    .build()?;

Trait Implementations§

Source§

impl Default for ClientBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,