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>
impl ClientBuilder<Identity>
Sourcepub fn new() -> Self
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>
impl<M> ClientBuilder<M>
Sourcepub fn openssl(self, config: OpensslConnector) -> Self
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.
Sourcepub fn rustls(self, config: RustlsClientConfig) -> Self
pub fn rustls(self, config: RustlsClientConfig) -> Self
Uses a rustls connector for secure connections.
Sourcepub fn connector<T>(
self,
f: impl IntoService<T, SharedCfg, TcpConnect<Url>>,
) -> Selfwhere
T: Service<SharedCfg, TcpConnect<Url>, Error = Error<ConnectError>> + 'static,
IoBoxed: From<T::Res>,
pub fn connector<T>(
self,
f: impl IntoService<T, SharedCfg, TcpConnect<Url>>,
) -> Selfwhere
T: Service<SharedCfg, TcpConnect<Url>, Error = Error<ConnectError>> + 'static,
IoBoxed: From<T::Res>,
Uses a custom connector for unsecured connections.
Sourcepub fn secure_connector<T>(
self,
f: impl IntoService<T, SharedCfg, TcpConnect<Url>>,
) -> Selfwhere
T: Service<SharedCfg, TcpConnect<Url>, Error = Error<ConnectError>> + 'static,
IoBoxed: From<T::Res>,
pub fn secure_connector<T>(
self,
f: impl IntoService<T, SharedCfg, TcpConnect<Url>>,
) -> Selfwhere
T: Service<SharedCfg, TcpConnect<Url>, Error = Error<ConnectError>> + 'static,
IoBoxed: From<T::Res>,
Uses a custom connector for secure connections.
Sourcepub fn middleware<U>(self, mw: U) -> ClientBuilder<Stack<U, M>>
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());
}Sourcepub fn build(self, cfg: impl Into<SharedCfg>) -> Clientwhere
M: Middleware<Sender, SharedCfg>,
M::Service: Service<SharedCfg, ServiceRequest, Res = ServiceResponse, Error = Error<ClientError>> + 'static,
pub fn build(self, cfg: impl Into<SharedCfg>) -> Clientwhere
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);
}