ClientBuilder

Struct ClientBuilder 

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

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

§Example

use std::time::Duration;

let client = slinger::Client::builder()
    .timeout(Some(Duration::from_secs(10)))
    .build()?;

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 TLS backend cannot be initialized, or the resolver cannot load the system configuration.

§Panics

See docs on slinger::client for details.

Source

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

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

§Example
async fn doc() -> Result<(), slinger::Error> {
let ua = HeaderValue::from_static("XX_UA");
let client = slinger::Client::builder()
    .user_agent(ua)
    .build()?;
let res = client.get("https://www.rust-lang.org").send().await?;
Source

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

Sets the default headers for every request.

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

// 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 = slinger::Client::builder()
    .default_headers(headers)
    .build()?;
let res = client.get("https://www.rust-lang.org").send().await?;
Source

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

Set a redirect::Policy 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 proxy(self, proxy: Proxy) -> ClientBuilder

Add a Proxy to the list of proxies the Client will use.

§Note

Adding a proxy will disable the automatic usage of the “system” proxy.

Source

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

Set a timeout for connect, read and write operations of a Client.

Default is 30 seconds.

Pass None to disable timeout.

Source

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

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

Default is 10 seconds.

Source

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

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

Default is 30 seconds.

Source

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

Set whether sockets have TCP_NODELAY enabled.

Default is true.

Source

pub fn keepalive(self, keepalive: bool) -> ClientBuilder

Default is false.

Source

pub fn danger_accept_invalid_hostnames( self, accept_invalid_hostname: bool, ) -> ClientBuilder

Controls the use of hostname verification.

Defaults to false.

§Warning

You should think very carefully before you use this method. If hostname verification is not used, any valid certificate for any site will be trusted for use from any other. This introduces a significant vulnerability to man-in-the-middle attacks.

§Optional

This requires the optional tls feature to be enabled.

Source

pub fn danger_accept_invalid_certs( self, accept_invalid_certs: bool, ) -> ClientBuilder

Controls the use of certificate validation.

Defaults to false.

§Warning

You should think very carefully before using this method. If invalid certificates are trusted, any certificate for any site will be trusted for use. This includes expired certificates. This introduces significant vulnerabilities, and should only be used as a last resort.

Trait Implementations§

Source§

impl Clone for ClientBuilder

Source§

fn clone(&self) -> ClientBuilder

Returns a duplicate 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

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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

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.