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
impl ClientBuilder
Sourcepub fn new() -> ClientBuilder
pub fn new() -> ClientBuilder
Constructs a new ClientBuilder
.
This is the same as Client::builder()
.
Sourcepub fn build(self) -> Result<Client>
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.
Sourcepub fn user_agent<V>(self, value: V) -> ClientBuilderwhere
V: Into<HeaderValue>,
pub fn user_agent<V>(self, value: V) -> ClientBuilderwhere
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?;
Sourcepub fn default_headers(self, headers: HeaderMap) -> ClientBuilder
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?;
Sourcepub fn redirect(self, policy: Policy) -> ClientBuilder
pub fn redirect(self, policy: Policy) -> ClientBuilder
Set a redirect::Policy
for this client.
Default will follow redirects up to a maximum of 10.
Sourcepub fn referer(self, enable: bool) -> ClientBuilder
pub fn referer(self, enable: bool) -> ClientBuilder
Enable or disable automatic setting of the Referer
header.
Default is true
.
Sourcepub fn proxy(self, proxy: Proxy) -> ClientBuilder
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.
Sourcepub fn timeout(self, timeout: Option<Duration>) -> ClientBuilder
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.
Sourcepub fn connect_timeout(self, timeout: Option<Duration>) -> ClientBuilder
pub fn connect_timeout(self, timeout: Option<Duration>) -> ClientBuilder
Set a timeout for only the connect phase of a Client
.
Default is 10 seconds.
Sourcepub fn read_timeout(self, timeout: Option<Duration>) -> ClientBuilder
pub fn read_timeout(self, timeout: Option<Duration>) -> ClientBuilder
Set a timeout for only the read phase of a Client
.
Default is 30 seconds.
Sourcepub fn tcp_nodelay(self, enabled: bool) -> ClientBuilder
pub fn tcp_nodelay(self, enabled: bool) -> ClientBuilder
Set whether sockets have TCP_NODELAY
enabled.
Default is true
.
Sourcepub fn keepalive(self, keepalive: bool) -> ClientBuilder
pub fn keepalive(self, keepalive: bool) -> ClientBuilder
Default is false
.
Sourcepub fn danger_accept_invalid_hostnames(
self,
accept_invalid_hostname: bool,
) -> ClientBuilder
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.
Sourcepub fn danger_accept_invalid_certs(
self,
accept_invalid_certs: bool,
) -> ClientBuilder
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
impl Clone for ClientBuilder
Source§fn clone(&self) -> ClientBuilder
fn clone(&self) -> ClientBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more