mirror of
https://github.com/actix/actix-web.git
synced 2024-12-23 00:26:34 +00:00
Refactor/client builder (#2053)
This commit is contained in:
parent
2d3a0d6038
commit
5b4105e1e6
4 changed files with 41 additions and 11 deletions
|
@ -12,7 +12,6 @@
|
||||||
|
|
||||||
### Removed
|
### Removed
|
||||||
* `ClientBuilder::default` function [#2008]
|
* `ClientBuilder::default` function [#2008]
|
||||||
* `ClientBuilder::disable_redirects` and `ClientBuilder::max_redirects` method [#2008]
|
|
||||||
|
|
||||||
[#1931]: https://github.com/actix/actix-web/pull/1931
|
[#1931]: https://github.com/actix/actix-web/pull/1931
|
||||||
[#1981]: https://github.com/actix/actix-web/pull/1981
|
[#1981]: https://github.com/actix/actix-web/pull/1981
|
||||||
|
|
|
@ -14,7 +14,7 @@ use actix_service::{boxed, Service};
|
||||||
|
|
||||||
use crate::connect::DefaultConnector;
|
use crate::connect::DefaultConnector;
|
||||||
use crate::error::SendRequestError;
|
use crate::error::SendRequestError;
|
||||||
use crate::middleware::{NestTransform, Transform};
|
use crate::middleware::{NestTransform, Redirect, Transform};
|
||||||
use crate::{Client, ClientConfig, ConnectRequest, ConnectResponse, ConnectorService};
|
use crate::{Client, ClientConfig, ConnectRequest, ConnectResponse, ConnectorService};
|
||||||
|
|
||||||
/// An HTTP Client builder
|
/// An HTTP Client builder
|
||||||
|
@ -31,6 +31,7 @@ pub struct ClientBuilder<S = (), Io = (), M = ()> {
|
||||||
connector: Connector<S, Io>,
|
connector: Connector<S, Io>,
|
||||||
middleware: M,
|
middleware: M,
|
||||||
local_address: Option<IpAddr>,
|
local_address: Option<IpAddr>,
|
||||||
|
max_redirects: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ClientBuilder {
|
impl ClientBuilder {
|
||||||
|
@ -54,6 +55,7 @@ impl ClientBuilder {
|
||||||
max_http_version: None,
|
max_http_version: None,
|
||||||
stream_window_size: None,
|
stream_window_size: None,
|
||||||
conn_window_size: None,
|
conn_window_size: None,
|
||||||
|
max_redirects: 10,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,6 +88,7 @@ where
|
||||||
max_http_version: self.max_http_version,
|
max_http_version: self.max_http_version,
|
||||||
stream_window_size: self.stream_window_size,
|
stream_window_size: self.stream_window_size,
|
||||||
conn_window_size: self.conn_window_size,
|
conn_window_size: self.conn_window_size,
|
||||||
|
max_redirects: self.max_redirects,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,6 +121,22 @@ where
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Do not follow redirects.
|
||||||
|
///
|
||||||
|
/// Redirects are allowed by default.
|
||||||
|
pub fn disable_redirects(mut self) -> Self {
|
||||||
|
self.max_redirects = 0;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set max number of redirects.
|
||||||
|
///
|
||||||
|
/// Max redirects is set to 10 by default.
|
||||||
|
pub fn max_redirects(mut self, num: u8) -> Self {
|
||||||
|
self.max_redirects = num;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Indicates the initial window size (in octets) for
|
/// Indicates the initial window size (in octets) for
|
||||||
/// HTTP2 stream-level flow control for received data.
|
/// HTTP2 stream-level flow control for received data.
|
||||||
///
|
///
|
||||||
|
@ -209,11 +228,28 @@ where
|
||||||
timeout: self.timeout,
|
timeout: self.timeout,
|
||||||
connector: self.connector,
|
connector: self.connector,
|
||||||
local_address: self.local_address,
|
local_address: self.local_address,
|
||||||
|
max_redirects: self.max_redirects,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Finish build process and create `Client` instance.
|
/// Finish build process and create `Client` instance.
|
||||||
pub fn finish(self) -> Client
|
pub fn finish(self) -> Client
|
||||||
|
where
|
||||||
|
M: Transform<ConnectorService, ConnectRequest> + 'static,
|
||||||
|
M::Transform:
|
||||||
|
Service<ConnectRequest, Response = ConnectResponse, Error = SendRequestError>,
|
||||||
|
{
|
||||||
|
let redirect_time = self.max_redirects;
|
||||||
|
|
||||||
|
if redirect_time > 0 {
|
||||||
|
self.wrap(Redirect::new().max_redirect_times(redirect_time))
|
||||||
|
._finish()
|
||||||
|
} else {
|
||||||
|
self._finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn _finish(self) -> Client
|
||||||
where
|
where
|
||||||
M: Transform<ConnectorService, ConnectRequest> + 'static,
|
M: Transform<ConnectorService, ConnectRequest> + 'static,
|
||||||
M::Transform:
|
M::Transform:
|
||||||
|
|
|
@ -107,7 +107,7 @@ use actix_http::{
|
||||||
RequestHead,
|
RequestHead,
|
||||||
};
|
};
|
||||||
use actix_rt::net::TcpStream;
|
use actix_rt::net::TcpStream;
|
||||||
use actix_service::{boxed, Service};
|
use actix_service::Service;
|
||||||
|
|
||||||
mod builder;
|
mod builder;
|
||||||
mod connect;
|
mod connect;
|
||||||
|
@ -157,13 +157,7 @@ pub(crate) struct ClientConfig {
|
||||||
|
|
||||||
impl Default for Client {
|
impl Default for Client {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Client(Rc::new(ClientConfig {
|
ClientBuilder::new().finish()
|
||||||
connector: boxed::service(self::connect::DefaultConnector::new(
|
|
||||||
Connector::new().finish(),
|
|
||||||
)),
|
|
||||||
headers: HeaderMap::new(),
|
|
||||||
timeout: Some(Duration::from_secs(5)),
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -292,7 +292,7 @@ mod tests {
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_basic_redirect() {
|
async fn test_basic_redirect() {
|
||||||
let client = ClientBuilder::new()
|
let client = ClientBuilder::new()
|
||||||
.connector(crate::Connector::new())
|
.disable_redirects()
|
||||||
.wrap(Redirect::new().max_redirect_times(10))
|
.wrap(Redirect::new().max_redirect_times(10))
|
||||||
.finish();
|
.finish();
|
||||||
|
|
||||||
|
@ -318,6 +318,7 @@ mod tests {
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_redirect_limit() {
|
async fn test_redirect_limit() {
|
||||||
let client = ClientBuilder::new()
|
let client = ClientBuilder::new()
|
||||||
|
.disable_redirects()
|
||||||
.wrap(Redirect::new().max_redirect_times(1))
|
.wrap(Redirect::new().max_redirect_times(1))
|
||||||
.connector(crate::Connector::new())
|
.connector(crate::Connector::new())
|
||||||
.finish();
|
.finish();
|
||||||
|
|
Loading…
Reference in a new issue