2019-03-26 04:58:01 +00:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::fmt;
|
|
|
|
use std::rc::Rc;
|
2019-03-29 05:33:41 +00:00
|
|
|
use std::time::Duration;
|
2019-03-26 04:58:01 +00:00
|
|
|
|
2019-04-20 01:03:44 +00:00
|
|
|
use actix_http::client::{Connect, ConnectError, Connection, Connector};
|
|
|
|
use actix_http::http::{header, HeaderMap, HeaderName, HttpTryFrom};
|
2019-03-26 18:41:38 +00:00
|
|
|
use actix_service::Service;
|
2019-03-26 04:58:01 +00:00
|
|
|
|
2019-03-29 05:33:41 +00:00
|
|
|
use crate::connect::ConnectorWrapper;
|
2019-03-29 04:48:35 +00:00
|
|
|
use crate::{Client, ClientConfig};
|
2019-03-26 04:58:01 +00:00
|
|
|
|
|
|
|
/// An HTTP Client builder
|
|
|
|
///
|
|
|
|
/// This type can be used to construct an instance of `Client` through a
|
|
|
|
/// builder-like pattern.
|
|
|
|
pub struct ClientBuilder {
|
2019-03-29 05:33:41 +00:00
|
|
|
config: ClientConfig,
|
2019-03-26 04:58:01 +00:00
|
|
|
default_headers: bool,
|
|
|
|
allow_redirects: bool,
|
|
|
|
max_redirects: usize,
|
|
|
|
}
|
|
|
|
|
2019-07-17 09:08:30 +00:00
|
|
|
impl Default for ClientBuilder {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 04:58:01 +00:00
|
|
|
impl ClientBuilder {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
ClientBuilder {
|
|
|
|
default_headers: true,
|
|
|
|
allow_redirects: true,
|
|
|
|
max_redirects: 10,
|
2019-03-29 05:33:41 +00:00
|
|
|
config: ClientConfig {
|
|
|
|
headers: HeaderMap::new(),
|
|
|
|
timeout: Some(Duration::from_secs(5)),
|
|
|
|
connector: RefCell::new(Box::new(ConnectorWrapper(
|
2019-04-05 18:34:27 +00:00
|
|
|
Connector::new().finish(),
|
2019-03-29 05:33:41 +00:00
|
|
|
))),
|
|
|
|
},
|
2019-03-26 04:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:41:38 +00:00
|
|
|
/// Use custom connector service.
|
|
|
|
pub fn connector<T>(mut self, connector: T) -> Self
|
|
|
|
where
|
2019-04-20 01:03:44 +00:00
|
|
|
T: Service<Request = Connect, Error = ConnectError> + 'static,
|
2019-03-26 18:41:38 +00:00
|
|
|
T::Response: Connection,
|
|
|
|
<T::Response as Connection>::Future: 'static,
|
|
|
|
T::Future: 'static,
|
|
|
|
{
|
2019-03-29 05:33:41 +00:00
|
|
|
self.config.connector = RefCell::new(Box::new(ConnectorWrapper(connector)));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set request timeout
|
|
|
|
///
|
|
|
|
/// Request timeout is the total time before a response must be received.
|
|
|
|
/// Default value is 5 seconds.
|
|
|
|
pub fn timeout(mut self, timeout: Duration) -> Self {
|
|
|
|
self.config.timeout = Some(timeout);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Disable request timeout.
|
|
|
|
pub fn disable_timeout(mut self) -> Self {
|
|
|
|
self.config.timeout = None;
|
2019-03-26 18:41:38 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-26 04:58:01 +00:00
|
|
|
/// Do not follow redirects.
|
|
|
|
///
|
|
|
|
/// Redirects are allowed by default.
|
|
|
|
pub fn disable_redirects(mut self) -> Self {
|
|
|
|
self.allow_redirects = false;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set max number of redirects.
|
|
|
|
///
|
|
|
|
/// Max redirects is set to 10 by default.
|
|
|
|
pub fn max_redirects(mut self, num: usize) -> Self {
|
|
|
|
self.max_redirects = num;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Do not add default request headers.
|
2019-03-27 05:33:01 +00:00
|
|
|
/// By default `Date` and `User-Agent` headers are set.
|
|
|
|
pub fn no_default_headers(mut self) -> Self {
|
2019-03-26 04:58:01 +00:00
|
|
|
self.default_headers = false;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-29 21:07:37 +00:00
|
|
|
/// Add default header. Headers added by this method
|
2019-03-27 05:33:01 +00:00
|
|
|
/// get added to every request.
|
2019-03-26 04:58:01 +00:00
|
|
|
pub fn header<K, V>(mut self, key: K, value: V) -> Self
|
|
|
|
where
|
|
|
|
HeaderName: HttpTryFrom<K>,
|
|
|
|
<HeaderName as HttpTryFrom<K>>::Error: fmt::Debug,
|
2019-03-29 04:48:35 +00:00
|
|
|
V: header::IntoHeaderValue,
|
2019-03-26 04:58:01 +00:00
|
|
|
V::Error: fmt::Debug,
|
|
|
|
{
|
|
|
|
match HeaderName::try_from(key) {
|
|
|
|
Ok(key) => match value.try_into() {
|
|
|
|
Ok(value) => {
|
2019-03-29 05:33:41 +00:00
|
|
|
self.config.headers.append(key, value);
|
2019-03-26 04:58:01 +00:00
|
|
|
}
|
|
|
|
Err(e) => log::error!("Header value error: {:?}", e),
|
|
|
|
},
|
|
|
|
Err(e) => log::error!("Header name error: {:?}", e),
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-29 04:48:35 +00:00
|
|
|
/// Set client wide HTTP basic authorization header
|
|
|
|
pub fn basic_auth<U>(self, username: U, password: Option<&str>) -> Self
|
|
|
|
where
|
|
|
|
U: fmt::Display,
|
|
|
|
{
|
|
|
|
let auth = match password {
|
|
|
|
Some(password) => format!("{}:{}", username, password),
|
2019-07-01 03:34:42 +00:00
|
|
|
None => format!("{}:", username),
|
2019-03-29 04:48:35 +00:00
|
|
|
};
|
|
|
|
self.header(
|
|
|
|
header::AUTHORIZATION,
|
|
|
|
format!("Basic {}", base64::encode(&auth)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set client wide HTTP bearer authentication header
|
|
|
|
pub fn bearer_auth<T>(self, token: T) -> Self
|
|
|
|
where
|
|
|
|
T: fmt::Display,
|
|
|
|
{
|
|
|
|
self.header(header::AUTHORIZATION, format!("Bearer {}", token))
|
|
|
|
}
|
|
|
|
|
2019-03-27 05:33:01 +00:00
|
|
|
/// Finish build process and create `Client` instance.
|
2019-03-26 04:58:01 +00:00
|
|
|
pub fn finish(self) -> Client {
|
2019-03-29 05:33:41 +00:00
|
|
|
Client(Rc::new(self.config))
|
2019-03-26 04:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-29 04:48:35 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn client_basic_auth() {
|
2019-05-12 15:34:51 +00:00
|
|
|
let client = ClientBuilder::new().basic_auth("username", Some("password"));
|
|
|
|
assert_eq!(
|
|
|
|
client
|
|
|
|
.config
|
|
|
|
.headers
|
|
|
|
.get(header::AUTHORIZATION)
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap(),
|
|
|
|
"Basic dXNlcm5hbWU6cGFzc3dvcmQ="
|
|
|
|
);
|
|
|
|
|
|
|
|
let client = ClientBuilder::new().basic_auth("username", None);
|
|
|
|
assert_eq!(
|
|
|
|
client
|
|
|
|
.config
|
|
|
|
.headers
|
|
|
|
.get(header::AUTHORIZATION)
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap(),
|
2019-07-01 03:34:42 +00:00
|
|
|
"Basic dXNlcm5hbWU6"
|
2019-05-12 15:34:51 +00:00
|
|
|
);
|
2019-03-29 04:48:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn client_bearer_auth() {
|
2019-05-12 15:34:51 +00:00
|
|
|
let client = ClientBuilder::new().bearer_auth("someS3cr3tAutht0k3n");
|
|
|
|
assert_eq!(
|
|
|
|
client
|
|
|
|
.config
|
|
|
|
.headers
|
|
|
|
.get(header::AUTHORIZATION)
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap(),
|
|
|
|
"Bearer someS3cr3tAutht0k3n"
|
|
|
|
);
|
2019-03-29 04:48:35 +00:00
|
|
|
}
|
|
|
|
}
|