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.

§TLS defaults

With the openssl feature enabled (it takes precedence over rustls), HTTPS connections verify the peer certificate against OpenSSL’s default trust store (the system roots) and check that it matches the request host. With only the rustls feature, peers are verified against the bundled webpki-roots. Without either feature, HTTPS is unavailable.

To trust private roots, or to skip verification for testing, pass your own configuration to openssl(), rustls() or secure_connector.

Source§

impl<M> ClientBuilder<M>

Source

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

Uses an OpenSSL connector for secure connections.

The connector’s verification settings are used as given. Hostname verification follows the connector’s configure() defaults, so it is enabled unless set_verify(SslVerifyMode::NONE) disables verification altogether.

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
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V