Skip to main content

ClientBuilder

Struct ClientBuilder 

Source
pub struct ClientBuilder<M = Identity> { /* private fields */ }
Expand description

An HTTP client builder.

This type configures connectors and middleware before constructing a Client.

Implementations§

Source§

impl ClientBuilder<Identity>

Source

pub fn new() -> Self

Creates a client builder with default connectors.

Source§

impl<M> ClientBuilder<M>

Source

pub fn openssl(self, config: OpensslConnector) -> Self

Uses an OpenSSL connector for secure connections.

Source

pub fn rustls(self, config: RustlsClientConfig) -> Self

Uses a rustls connector for secure connections.

Source

pub fn connector<T>( self, f: impl IntoService<T, SharedCfg, TcpConnect<Url>>, ) -> Self
where T: Service<SharedCfg, TcpConnect<Url>, Error = Error<ConnectError>> + 'static, IoBoxed: From<T::Res>,

Uses a custom connector for unsecured connections.

Source

pub fn secure_connector<T>( self, f: impl IntoService<T, SharedCfg, TcpConnect<Url>>, ) -> Self
where T: Service<SharedCfg, TcpConnect<Url>, Error = Error<ConnectError>> + 'static, IoBoxed: From<T::Res>,

Uses a custom connector for secure connections.

Source

pub fn middleware<U>(self, mw: U) -> ClientBuilder<Stack<U, M>>

Applies middleware to the client request service.

Middleware can inspect or modify every request and response.

use ntex::client::{Client, ServiceRequest};
use ntex::service::{fn_layer, cfg::SharedCfg};

#[ntex::main]
async fn main() {
    let client = Client::builder()
        .middleware(fn_layer(
            async move |mut req: ServiceRequest, svc| {
                println!("{:?}", req.head().uri);
                svc.call(req).await
            }
        ))
        .build(SharedCfg::default());
}
Source

pub fn build(self, cfg: impl Into<SharedCfg>) -> Client
where M: Middleware<Sender, SharedCfg>, M::Service: Service<SharedCfg, ServiceRequest, Res = ServiceResponse, Error = Error<ClientError>> + 'static,

Builds the Client using the supplied shared configuration.

Add a ClientConfig to SharedCfg to customize client behavior:

use ntex::SharedCfg;
use ntex::client::{Client, ClientConfig};
use ntex::time::Millis;

#[ntex::main]
async fn main() {
    let cfg = SharedCfg::new("HTTP-CLIENT")
        .add(
            ClientConfig::new()
                .set_response_timeout(Millis(10_000))
                .set_response_payload_limit(1024 * 1024),
        )
        .build();

    let _client = Client::builder().build(cfg);
}

Trait Implementations§

Source§

impl<M: Debug> Debug for ClientBuilder<M>

Source§

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

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

impl Default for ClientBuilder<Identity>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<M = Identity> !RefUnwindSafe for ClientBuilder<M>

§

impl<M = Identity> !Send for ClientBuilder<M>

§

impl<M = Identity> !Sync for ClientBuilder<M>

§

impl<M = Identity> !UnwindSafe for ClientBuilder<M>

§

impl<M> Freeze for ClientBuilder<M>
where M: Freeze,

§

impl<M> Unpin for ClientBuilder<M>
where M: Unpin,

§

impl<M> UnsafeUnpin for ClientBuilder<M>
where M: UnsafeUnpin,

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