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() -> ClientBuilder

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

Source

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

Sets the base URL (required).

Source

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

Sets the default request timeout.

Source

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

Sets the default RetryPolicy for all requests.

Source

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

Sets default authentication for all requests.

Source

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

Adds a default header applied to every request.

Source

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

Sets client-level lifecycle hooks.

Source

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

Registers a [Plugin] on this client.

Source

pub fn reqwest_client(self, client: Client) -> ClientBuilder

Uses a custom reqwest client for the default ReqwestBackend.

Source

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

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 max_in_flight(self, limit: usize) -> ClientBuilder

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) -> ClientBuilder

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

Source

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

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 json_parser<F>(self, f: F) -> ClientBuilder
where F: Fn(&Bytes) -> Result<Value, String> + Send + Sync + 'static,

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: Arc<dyn Fn(&Bytes) -> Result<Value, String> + Sync + Send>, ) -> ClientBuilder

Sets a custom JSON parser from an existing JsonParserFn.

Source

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

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() -> ClientBuilder

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> 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