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
impl ClientBuilder
Sourcepub fn connection_timeout(self, connection_timeout: u64) -> Self
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).
Sourcepub fn request_timeout(self, request_timeout: u64) -> Self
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).
Sourcepub fn client_cert(self, client_cert: Identity) -> Self
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(())
}Sourcepub fn identity(self, identity: Identity) -> Self
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(())
}Sourcepub fn certificate(self, certificate: Certificate) -> Self
pub fn certificate(self, certificate: Certificate) -> Self
Sourcepub fn catch<C: DeboaCatcher>(self, catcher: C) -> Self
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(())
}Sourcepub fn protocol(self, protocol: HttpVersion) -> Self
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.
Sourcepub fn skip_cert_verification(self, skip: bool) -> Self
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.
Sourcepub fn pool(self, pool: HttpConnectionPool) -> Self
pub fn pool(self, pool: HttpConnectionPool) -> Self
pub fn bind_addr(self, bind_addr: IpAddr) -> Self
Sourcepub fn build(self) -> Client
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.