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

A ClientBuilder can be used to create a Client with custom configuration.

Implementations§

source§

impl ClientBuilder

source

pub fn new() -> ClientBuilder

Constructs a new ClientBuilder.

This is the same as Client::builder().

source

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

Returns a Client that uses this ClientBuilder configuration.

Errors

This method fails if a TLS backend cannot be initialized, or the resolver cannot load the system configuration.

source

pub fn user_agent<V>(self, value: V) -> ClientBuilder

Sets the User-Agent header to be used by this client.

Example
// Name your user agent after your app?
static APP_USER_AGENT: &str = concat!(
    env!("CARGO_PKG_NAME"),
    "/",
    env!("CARGO_PKG_VERSION"),
);

let client = nightfly::Client::builder()
    .user_agent(APP_USER_AGENT)
    .build()?;
let res = client.get("https://www.rust-lang.org").send();
source

pub fn default_headers(self, headers: HeaderMap) -> ClientBuilder

Sets the default headers for every request.

Example
use nightfly::header;
let mut headers = header::HeaderMap::new();
headers.insert("X-MY-HEADER", header::HeaderValue::from_static("value"));

// Consider marking security-sensitive headers with `set_sensitive`.
let mut auth_value = header::HeaderValue::from_static("secret");
auth_value.set_sensitive(true);
headers.insert(header::AUTHORIZATION, auth_value);

// get a client builder
let client = nightfly::Client::builder()
    .default_headers(headers)
    .build()?;
let res = client.get("https://www.rust-lang.org").send();

Override the default headers:

use nightfly::header;
let mut headers = header::HeaderMap::new();
headers.insert("X-MY-HEADER", header::HeaderValue::from_static("value"));

// get a client builder
let client = nightfly::Client::builder()
    .default_headers(headers)
    .build()?;
let res = client
    .get("https://www.rust-lang.org")
    .header("X-MY-HEADER", "new_value")
    .send()
    ;
source

pub fn gzip(self, enable: bool) -> ClientBuilder

Enable auto gzip decompression by checking the Content-Encoding response header.

If auto gzip decompression is turned on:

  • When sending a request and if the request’s headers do not already contain an Accept-Encoding and Range values, the Accept-Encoding header is set to gzip. The request body is not automatically compressed.
  • When receiving a response, if its headers contain a Content-Encoding value of gzip, both Content-Encoding and Content-Length are removed from the headers’ set. The response body is automatically decompressed.

If the gzip feature is turned on, the default option is enabled.

source

pub fn brotli(self, enable: bool) -> ClientBuilder

Enable auto brotli decompression by checking the Content-Encoding response header.

If auto brotli decompression is turned on:

  • When sending a request and if the request’s headers do not already contain an Accept-Encoding and Range values, the Accept-Encoding header is set to br. The request body is not automatically compressed.
  • When receiving a response, if its headers contain a Content-Encoding value of br, both Content-Encoding and Content-Length are removed from the headers’ set. The response body is automatically decompressed.
source

pub fn deflate(self, enable: bool) -> ClientBuilder

Enable auto deflate decompression by checking the Content-Encoding response header.

If auto deflate decompression is turned on:

  • When sending a request and if the request’s headers do not already contain an Accept-Encoding and Range values, the Accept-Encoding header is set to deflate. The request body is not automatically compressed.
  • When receiving a response, if it’s headers contain a Content-Encoding value that equals to deflate, both values Content-Encoding and Content-Length are removed from the headers’ set. The response body is automatically decompressed.
source

pub fn no_gzip(self) -> ClientBuilder

Disable auto response body gzip decompression.

This method exists even if the optional gzip feature is not enabled. This can be used to ensure a Client doesn’t use gzip decompression even if another dependency were to enable the optional gzip feature.

source

pub fn no_brotli(self) -> ClientBuilder

Disable auto response body brotli decompression.

This method exists even if the optional brotli feature is not enabled. This can be used to ensure a Client doesn’t use brotli decompression even if another dependency were to enable the optional brotli feature.

source

pub fn no_deflate(self) -> ClientBuilder

Disable auto response body deflate decompression.

This method exists even if the optional deflate feature is not enabled. This can be used to ensure a Client doesn’t use deflate decompression even if another dependency were to enable the optional deflate feature.

source

pub fn redirect(self, policy: Policy) -> ClientBuilder

Set a RedirectPolicy for this client.

Default will follow redirects up to a maximum of 10.

source

pub fn referer(self, enable: bool) -> ClientBuilder

Enable or disable automatic setting of the Referer header.

Default is true.

source

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

Enables a request timeout.

The timeout is applied from when the request starts connecting until the response body has finished.

Default is no timeout.

source

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

Set a timeout for only the connect phase of a Client.

Default is None.

source

pub fn connection_verbose(self, verbose: bool) -> ClientBuilder

Set whether connections should emit verbose logs.

Enabling this option will emit log messages at the TRACE level for read and write operations on connections.

source

pub fn pool_idle_timeout<D>(self, val: D) -> ClientBuilder
where D: Into<Option<Duration>>,

Set an optional timeout for idle sockets being kept-alive.

Pass None to disable timeout.

Default is 90 seconds.

source

pub fn pool_max_idle_per_host(self, max: usize) -> ClientBuilder

Sets the maximum idle connection per host allowed in the pool.

source

pub fn http1_title_case_headers(self) -> ClientBuilder

Send headers as title case instead of lowercase.

source

pub fn http1_allow_obsolete_multiline_headers_in_responses( self, value: bool ) -> ClientBuilder

Set whether HTTP/1 connections will accept obsolete line folding for header values.

Newline codepoints (\r and \n) will be transformed to spaces when parsing.

source

pub fn http1_only(self) -> ClientBuilder

Only use HTTP/1.

source

pub fn http09_responses(self) -> ClientBuilder

Allow HTTP/0.9 responses

source

pub fn http2_prior_knowledge(self) -> ClientBuilder

Only use HTTP/2.

source

pub fn http2_initial_stream_window_size( self, sz: impl Into<Option<u32>> ) -> ClientBuilder

Sets the SETTINGS_INITIAL_WINDOW_SIZE option for HTTP2 stream-level flow control.

Default is currently 65,535 but may change internally to optimize for common uses.

source

pub fn http2_initial_connection_window_size( self, sz: impl Into<Option<u32>> ) -> ClientBuilder

Sets the max connection-level flow control for HTTP2

Default is currently 65,535 but may change internally to optimize for common uses.

source

pub fn http2_adaptive_window(self, enabled: bool) -> ClientBuilder

Sets whether to use an adaptive flow control.

Enabling this will override the limits set in http2_initial_stream_window_size and http2_initial_connection_window_size.

source

pub fn http2_max_frame_size(self, sz: impl Into<Option<u32>>) -> ClientBuilder

Sets the maximum frame size to use for HTTP2.

Default is currently 16,384 but may change internally to optimize for common uses.

source

pub fn http2_keep_alive_interval( self, interval: impl Into<Option<Duration>> ) -> ClientBuilder

Sets an interval for HTTP2 Ping frames should be sent to keep a connection alive.

Pass None to disable HTTP2 keep-alive. Default is currently disabled.

source

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

Sets a timeout for receiving an acknowledgement of the keep-alive ping.

If the ping is not acknowledged within the timeout, the connection will be closed. Does nothing if http2_keep_alive_interval is disabled. Default is currently disabled.

source

pub fn http2_keep_alive_while_idle(self, enabled: bool) -> ClientBuilder

Sets whether HTTP2 keep-alive should apply while the connection is idle.

If disabled, keep-alive pings are only sent while there are open request/responses streams. If enabled, pings are also sent when no streams are active. Does nothing if http2_keep_alive_interval is disabled. Default is false.

source

pub fn tcp_nodelay(self, enabled: bool) -> ClientBuilder

Set whether sockets have SO_NODELAY enabled.

Default is true.

source

pub fn local_address<T>(self, addr: T) -> ClientBuilder
where T: Into<Option<IpAddr>>,

Bind to a local IP Address.

Example
use std::net::IpAddr;
let local_addr = IpAddr::from([12, 4, 1, 8]);
let client = nightfly::Client::builder()
    .local_address(local_addr)
    .build().unwrap();
source

pub fn tcp_keepalive<D>(self, val: D) -> ClientBuilder
where D: Into<Option<Duration>>,

Set that all sockets have SO_KEEPALIVE set with the supplied duration.

If None, the option will not be set.

source

pub fn no_trust_dns(self) -> ClientBuilder

Disables the trust-dns async resolver.

This method exists even if the optional trust-dns feature is not enabled. This can be used to ensure a Client doesn’t use the trust-dns async resolver even if another dependency were to enable the optional trust-dns feature.

source

pub fn https_only(self, enabled: bool) -> ClientBuilder

Restrict the Client to be used with HTTPS only requests.

Defaults to false.

source

pub fn resolve(self, domain: &str, addr: SocketAddr) -> ClientBuilder

Override DNS resolution for specific domains to a particular IP address.

Warning

Since the DNS protocol has no notion of ports, if you wish to send traffic to a particular port you must include this port in the URL itself, any port in the overridden addr will be ignored and traffic sent to the conventional port for the given scheme (e.g. 80 for http).

source

pub fn resolve_to_addrs( self, domain: &str, addrs: &[SocketAddr] ) -> ClientBuilder

Override DNS resolution for specific domains to particular IP addresses.

Warning

Since the DNS protocol has no notion of ports, if you wish to send traffic to a particular port you must include this port in the URL itself, any port in the overridden addresses will be ignored and traffic sent to the conventional port for the given scheme (e.g. 80 for http).

Trait Implementations§

source§

impl Clone for ClientBuilder

source§

fn clone(&self) -> ClientBuilder

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for ClientBuilder

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for ClientBuilder

source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for ClientBuilder

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Serialize for ClientBuilder

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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, 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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

§

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,