use std::cell::RefCell; use std::fmt; use std::rc::Rc; use actix_http::client::{ConnectError, Connection, Connector}; use actix_http::http::{header, HeaderMap, HeaderName, HttpTryFrom, Uri}; use actix_service::Service; use crate::connect::{Connect, ConnectorWrapper}; use crate::{Client, ClientConfig}; /// An HTTP Client builder /// /// This type can be used to construct an instance of `Client` through a /// builder-like pattern. pub struct ClientBuilder { connector: Rc>, default_headers: bool, allow_redirects: bool, max_redirects: usize, headers: HeaderMap, } impl ClientBuilder { pub fn new() -> Self { ClientBuilder { default_headers: true, allow_redirects: true, max_redirects: 10, headers: HeaderMap::new(), connector: Rc::new(RefCell::new(ConnectorWrapper( Connector::new().service(), ))), } } /// Use custom connector service. pub fn connector(mut self, connector: T) -> Self where T: Service + 'static, T::Response: Connection, ::Future: 'static, T::Future: 'static, { self.connector = Rc::new(RefCell::new(ConnectorWrapper(connector))); self } /// 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. /// By default `Date` and `User-Agent` headers are set. pub fn no_default_headers(mut self) -> Self { self.default_headers = false; self } /// Add default header. Headers adds byt this method /// get added to every request. pub fn header(mut self, key: K, value: V) -> Self where HeaderName: HttpTryFrom, >::Error: fmt::Debug, V: header::IntoHeaderValue, V::Error: fmt::Debug, { match HeaderName::try_from(key) { Ok(key) => match value.try_into() { Ok(value) => { self.headers.append(key, value); } Err(e) => log::error!("Header value error: {:?}", e), }, Err(e) => log::error!("Header name error: {:?}", e), } self } /// Set client wide HTTP basic authorization header pub fn basic_auth(self, username: U, password: Option<&str>) -> Self where U: fmt::Display, { let auth = match password { Some(password) => format!("{}:{}", username, password), None => format!("{}", username), }; self.header( header::AUTHORIZATION, format!("Basic {}", base64::encode(&auth)), ) } /// Set client wide HTTP bearer authentication header pub fn bearer_auth(self, token: T) -> Self where T: fmt::Display, { self.header(header::AUTHORIZATION, format!("Bearer {}", token)) } /// Finish build process and create `Client` instance. pub fn finish(self) -> Client { Client { connector: self.connector, config: Rc::new(ClientConfig { headers: self.headers, }), } } } #[cfg(test)] mod tests { use super::*; use crate::test; #[test] fn client_basic_auth() { test::run_on(|| { let client = ClientBuilder::new().basic_auth("username", Some("password")); assert_eq!( client .headers .get(header::AUTHORIZATION) .unwrap() .to_str() .unwrap(), "Basic dXNlcm5hbWU6cGFzc3dvcmQ=" ); let client = ClientBuilder::new().basic_auth("username", None); assert_eq!( client .headers .get(header::AUTHORIZATION) .unwrap() .to_str() .unwrap(), "Basic dXNlcm5hbWU=" ); }); } #[test] fn client_bearer_auth() { test::run_on(|| { let client = ClientBuilder::new().bearer_auth("someS3cr3tAutht0k3n"); assert_eq!( client .headers .get(header::AUTHORIZATION) .unwrap() .to_str() .unwrap(), "Bearer someS3cr3tAutht0k3n" ); }) } }