Skip to main content

ClientBuilder

Struct ClientBuilder 

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

A builder for configuring and creating a new Deboa client instance.

This builder allows you to configure various aspects of the HTTP client before constructing it. You can set timeouts, configure protocols, add error handlers, and more.

§Examples

use deboa::{Client, Result, HttpVersion};

#[tokio::main]
async fn main() -> Result<()> {
  let client = Client::builder()
    .connection_timeout(30)  // 30 seconds
    .request_timeout(10)     // 10 seconds
    .protocol(HttpVersion::Http2)  // Use HTTP/2
    .build();

  // Use the client to make requests...
  Ok(())
}

§Default Configuration

  • Connection timeout: 30 seconds
  • Request timeout: 30 seconds
  • Protocol: HTTP/1.1
  • No client certificates
  • No custom error catchers

Implementations§

Source§

impl ClientBuilder

Source

pub fn connection_timeout(self, connection_timeout: u64) -> Self

Sets the maximum duration to wait when connecting to a server.

This timeout affects the initial TCP connection establishment. If the server doesn’t respond within this duration, the connection will fail with a timeout error.

§Arguments
  • connection_timeout - The timeout in seconds.
§Examples
use deboa::Client;
let builder = Client::builder()
    .connection_timeout(10);  // 10 seconds
§Note

A value of 0 means no timeout (not recommended in production).

Source

pub fn request_timeout(self, request_timeout: u64) -> Self

Sets the maximum duration for the entire HTTP request/response cycle.

This includes connection time, request writing, server processing, and response reading. If the entire operation takes longer than this duration, it will be aborted.

§Arguments
  • request_timeout - The timeout in seconds.
§Examples
use deboa::Client;
let builder = Client::builder()
    .request_timeout(30);  // 30 seconds
§Note

A value of 0 means no timeout (not recommended in production).

Source

pub fn client_cert(self, client_cert: Identity) -> Self

Configures a client certificate for mutual TLS authentication.

This is used when the server requires client certificate authentication. The certificate should be in PEM format and include both the certificate and private key.

§Arguments
  • client_cert - The client certificate configuration.
§Examples
use deboa::{Client, Result, Identity};

#[tokio::main]

async fn main() -> Result<()> {
    let cert = Identity::from_pem_file("client.pem")?;
    let builder = Client::builder()
        .set_identity(cert);
    Ok(())
}
Source

pub fn identity(self, identity: Identity) -> Self

Configures a client certificate for mutual TLS authentication.

This is used when the server requires client certificate authentication. The certificate should be in PEM format and include both the certificate and private key.

§Arguments
  • identity - The client certificate file.
§Examples
use deboa::{Client, Identity, Result};

#[tokio::main]

async fn main() -> Result<()> {
    let cert = Identity::new("client.pem", Some("pw"))?;
    let builder = Client::builder()
        .identity(cert);
    Ok(())
}
Source

pub fn certificate(self, certificate: Certificate) -> Self

Configures a ca certificate.

§Arguments
  • certificate - The ca certificate file.
§Examples
use deboa::{Client, Certificate, Result};

#[tokio::main]

async fn main() -> Result<()> {
    let cert = Certificate::new("client.pem")?;
    let builder = Client::builder()
        .certificate(cert);
    Ok(())
}
Source

pub fn catch<C: DeboaCatcher>(self, catcher: C) -> Self

Adds an error handler for specific types of errors.

Catchers are called when an error occurs during request execution. They can be used to implement custom error handling logic, such as automatic retries, logging, or error transformation.

§Arguments
  • catcher - A function or closure that handles specific error types.
§Examples
§Automatic Retries
use deboa::{Client, Result, catcher::DeboaCatcher, request::DeboaRequest, response::DeboaResponse};

struct AddAuthorization;

#[deboa::async_trait]
impl DeboaCatcher for AddAuthorization {
    async fn on_request(&self, request: &mut DeboaRequest) -> Result<Option<DeboaResponse>> {
        println!("Request: {:?}", request.url());
        Ok(None)
    }

    async fn on_response(&self, response: &mut DeboaResponse) -> Result<()> {
        println!("Response: {:?}", response.status());
        Ok(())
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    let client = Client::builder()
        .catch(AddAuthorization)
        .build();
    Ok(())
}
Source

pub fn protocol(self, protocol: HttpVersion) -> Self

Sets the HTTP protocol version to use for requests.

By default, the client will use HTTP/1.1. You can choose to use HTTP/2 for better performance, especially for multiple requests to the same server.

§Arguments
  • protocol - The HTTP protocol version to use.
§Examples
use deboa::{Client, HttpVersion};

let builder = Client::builder()
    .protocol(HttpVersion::Http2);  // Use HTTP/2
§Note

The actual protocol version used may be negotiated with the server during the TLS handshake.

Source

pub fn skip_cert_verification(self, skip: bool) -> Self

Skip certificate verification.

§Arguments
  • skip - Whether to skip certificate verification.
§Examples
use deboa::Client;

let builder = Client::builder()
    .skip_cert_verification(true);  // Skip certificate verification
§Warning

This should only be used in development or testing environments. Never use this in production as it makes your application vulnerable to man-in-the-middle attacks.

§Note

This setting affects all connections made by the client. It is recommended to use this only for testing purposes.

§Safety

This function bypasses SSL certificate validation, which can expose your application to security risks. Only use this in controlled environments where you trust all network traffic.

Source

pub fn pool(self, pool: HttpConnectionPool) -> Self

Set a connection pool.

§Arguments
  • pool - The connection pool to use.
§Returns
  • Self - The builder.
§Example
use deboa::Client;

let client = Client::builder()
    .pool(HttpConnectionPool::default())
    .build();
Source

pub fn bind_addr(self, bind_addr: IpAddr) -> Self

Source

pub fn build(self) -> Client

Constructs a new Deboa client with the configured settings.

This consumes the builder and returns a new Deboa instance that can be used to make HTTP requests.

§Returns

A new Deboa client instance.

§Examples
use deboa::{Client, Result};

#[tokio::main]
async fn main() -> Result<()> {
  let client = Client::builder()
    .connection_timeout(10)
    .request_timeout(30)
    .build();

  // client is now ready to make requests
  Ok(())
}
§Panics

This method may panic if the underlying HTTP client cannot be created with the specified configuration.

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

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Source§

fn type_name(&self) -> &'static str

Source§

impl<T> AnySync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

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, 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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