pub struct ClientBuilder { /* private fields */ }Expand description
Builder for Client.
Implementations§
Source§impl ClientBuilder
impl ClientBuilder
Sourcepub fn new() -> ClientBuilder
pub fn new() -> ClientBuilder
Creates an empty builder; Self::base_url is required before Self::build.
Sourcepub fn base_url(self, base_url: impl AsRef<str>) -> Result<ClientBuilder, Error>
pub fn base_url(self, base_url: impl AsRef<str>) -> Result<ClientBuilder, Error>
Sets the base URL (required).
Sourcepub fn timeout(self, timeout: Duration) -> ClientBuilder
pub fn timeout(self, timeout: Duration) -> ClientBuilder
Sets the default request timeout.
Sourcepub fn retry(self, policy: RetryPolicy) -> ClientBuilder
pub fn retry(self, policy: RetryPolicy) -> ClientBuilder
Sets the default RetryPolicy for all requests.
Sourcepub fn auth(self, auth: Auth) -> ClientBuilder
pub fn auth(self, auth: Auth) -> ClientBuilder
Sets default authentication for all requests.
Sourcepub fn default_header(
self,
key: impl AsRef<str>,
value: impl AsRef<str>,
) -> Result<ClientBuilder, Error>
pub fn default_header( self, key: impl AsRef<str>, value: impl AsRef<str>, ) -> Result<ClientBuilder, Error>
Adds a default header applied to every request.
Sourcepub fn hooks(self, hooks: Hooks) -> ClientBuilder
pub fn hooks(self, hooks: Hooks) -> ClientBuilder
Sets client-level lifecycle hooks.
Sourcepub fn plugin<P>(self, plugin: P) -> ClientBuilderwhere
P: Plugin + 'static,
pub fn plugin<P>(self, plugin: P) -> ClientBuilderwhere
P: Plugin + 'static,
Registers a [Plugin] on this client.
Sourcepub fn reqwest_client(self, client: Client) -> ClientBuilder
pub fn reqwest_client(self, client: Client) -> ClientBuilder
Uses a custom reqwest client for the default ReqwestBackend.
Sourcepub fn backend(self, backend: Arc<dyn HttpBackend>) -> ClientBuilder
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()?;Sourcepub fn max_in_flight(self, limit: usize) -> ClientBuilder
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.
Sourcepub fn max_response_bytes(self, limit: u64) -> ClientBuilder
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.
Sourcepub fn retry_body_peek_bytes(self, limit: u64) -> ClientBuilder
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.
Sourcepub fn json_parser<F>(self, f: F) -> ClientBuilder
pub fn json_parser<F>(self, f: F) -> ClientBuilder
Sets a custom JSON parser for all responses from this client.
See crate::json_parser for the two-step Bytes → Value → T pipeline vs the
default single-step fast path, and Response::into_json_with
for per-response Bytes → T 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()?;Sourcepub fn json_parser_fn(
self,
parser: Arc<dyn Fn(&Bytes) -> Result<Value, String> + Sync + Send>,
) -> ClientBuilder
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.