1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-02 13:29:24 +00:00

Refactor/client builder (#2053)

This commit is contained in:
fakeshadow 2021-03-07 15:57:32 -08:00 committed by GitHub
parent 2d3a0d6038
commit 5b4105e1e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 11 deletions

View file

@ -12,7 +12,6 @@
### Removed
* `ClientBuilder::default` function [#2008]
* `ClientBuilder::disable_redirects` and `ClientBuilder::max_redirects` method [#2008]
[#1931]: https://github.com/actix/actix-web/pull/1931
[#1981]: https://github.com/actix/actix-web/pull/1981

View file

@ -14,7 +14,7 @@ use actix_service::{boxed, Service};
use crate::connect::DefaultConnector;
use crate::error::SendRequestError;
use crate::middleware::{NestTransform, Transform};
use crate::middleware::{NestTransform, Redirect, Transform};
use crate::{Client, ClientConfig, ConnectRequest, ConnectResponse, ConnectorService};
/// An HTTP Client builder
@ -31,6 +31,7 @@ pub struct ClientBuilder<S = (), Io = (), M = ()> {
connector: Connector<S, Io>,
middleware: M,
local_address: Option<IpAddr>,
max_redirects: u8,
}
impl ClientBuilder {
@ -54,6 +55,7 @@ impl ClientBuilder {
max_http_version: None,
stream_window_size: None,
conn_window_size: None,
max_redirects: 10,
}
}
}
@ -86,6 +88,7 @@ where
max_http_version: self.max_http_version,
stream_window_size: self.stream_window_size,
conn_window_size: self.conn_window_size,
max_redirects: self.max_redirects,
}
}
@ -118,6 +121,22 @@ where
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
/// HTTP2 stream-level flow control for received data.
///
@ -209,11 +228,28 @@ where
timeout: self.timeout,
connector: self.connector,
local_address: self.local_address,
max_redirects: self.max_redirects,
}
}
/// Finish build process and create `Client` instance.
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
M: Transform<ConnectorService, ConnectRequest> + 'static,
M::Transform:

View file

@ -107,7 +107,7 @@ use actix_http::{
RequestHead,
};
use actix_rt::net::TcpStream;
use actix_service::{boxed, Service};
use actix_service::Service;
mod builder;
mod connect;
@ -157,13 +157,7 @@ pub(crate) struct ClientConfig {
impl Default for Client {
fn default() -> Self {
Client(Rc::new(ClientConfig {
connector: boxed::service(self::connect::DefaultConnector::new(
Connector::new().finish(),
)),
headers: HeaderMap::new(),
timeout: Some(Duration::from_secs(5)),
}))
ClientBuilder::new().finish()
}
}

View file

@ -292,7 +292,7 @@ mod tests {
#[actix_rt::test]
async fn test_basic_redirect() {
let client = ClientBuilder::new()
.connector(crate::Connector::new())
.disable_redirects()
.wrap(Redirect::new().max_redirect_times(10))
.finish();
@ -318,6 +318,7 @@ mod tests {
#[actix_rt::test]
async fn test_redirect_limit() {
let client = ClientBuilder::new()
.disable_redirects()
.wrap(Redirect::new().max_redirect_times(1))
.connector(crate::Connector::new())
.finish();