2019-03-27 16:24:55 +00:00
|
|
|
//! An HTTP Client
|
|
|
|
//!
|
|
|
|
//! ```rust
|
|
|
|
//! # use futures::future::{Future, lazy};
|
|
|
|
//! use actix_rt::System;
|
|
|
|
//! use awc::Client;
|
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! System::new("test").block_on(lazy(|| {
|
|
|
|
//! let mut client = Client::default();
|
|
|
|
//!
|
|
|
|
//! client.get("http://www.rust-lang.org") // <- Create request builder
|
|
|
|
//! .header("User-Agent", "Actix-web")
|
|
|
|
//! .send() // <- Send http request
|
|
|
|
//! .map_err(|_| ())
|
|
|
|
//! .and_then(|response| { // <- server http response
|
|
|
|
//! println!("Response: {:?}", response);
|
|
|
|
//! Ok(())
|
|
|
|
//! })
|
|
|
|
//! }));
|
|
|
|
//! }
|
|
|
|
//! ```
|
2019-03-26 04:58:01 +00:00
|
|
|
use std::cell::RefCell;
|
|
|
|
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-03-30 04:13:39 +00:00
|
|
|
pub use actix_http::{client::Connector, cookie, http};
|
2019-03-26 04:58:01 +00:00
|
|
|
|
2019-03-29 04:48:35 +00:00
|
|
|
use actix_http::http::{HeaderMap, HttpTryFrom, Method, Uri};
|
2019-03-27 16:24:55 +00:00
|
|
|
use actix_http::RequestHead;
|
2019-03-26 04:58:01 +00:00
|
|
|
|
|
|
|
mod builder;
|
|
|
|
mod connect;
|
2019-03-28 01:53:19 +00:00
|
|
|
pub mod error;
|
2019-03-26 04:58:01 +00:00
|
|
|
mod request;
|
2019-03-26 18:41:38 +00:00
|
|
|
mod response;
|
2019-03-27 04:54:57 +00:00
|
|
|
pub mod test;
|
2019-03-29 20:49:21 +00:00
|
|
|
pub mod ws;
|
2019-03-26 04:58:01 +00:00
|
|
|
|
|
|
|
pub use self::builder::ClientBuilder;
|
|
|
|
pub use self::request::ClientRequest;
|
2019-04-01 18:51:18 +00:00
|
|
|
pub use self::response::{ClientResponse, JsonBody, MessageBody};
|
2019-03-26 04:58:01 +00:00
|
|
|
|
|
|
|
use self::connect::{Connect, ConnectorWrapper};
|
|
|
|
|
2019-03-27 16:24:55 +00:00
|
|
|
/// An HTTP Client
|
2019-03-26 04:58:01 +00:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use futures::future::{Future, lazy};
|
|
|
|
/// use actix_rt::System;
|
|
|
|
/// use awc::Client;
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// System::new("test").block_on(lazy(|| {
|
|
|
|
/// let mut client = Client::default();
|
|
|
|
///
|
|
|
|
/// client.get("http://www.rust-lang.org") // <- Create request builder
|
|
|
|
/// .header("User-Agent", "Actix-web")
|
|
|
|
/// .send() // <- Send http request
|
|
|
|
/// .map_err(|_| ())
|
|
|
|
/// .and_then(|response| { // <- server http response
|
|
|
|
/// println!("Response: {:?}", response);
|
|
|
|
/// Ok(())
|
|
|
|
/// })
|
|
|
|
/// }));
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[derive(Clone)]
|
2019-03-29 05:33:41 +00:00
|
|
|
pub struct Client(Rc<ClientConfig>);
|
2019-03-29 04:48:35 +00:00
|
|
|
|
|
|
|
pub(crate) struct ClientConfig {
|
2019-03-29 05:33:41 +00:00
|
|
|
pub(crate) connector: RefCell<Box<dyn Connect>>,
|
2019-03-29 04:48:35 +00:00
|
|
|
pub(crate) headers: HeaderMap,
|
2019-03-29 05:33:41 +00:00
|
|
|
pub(crate) timeout: Option<Duration>,
|
2019-03-26 04:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Client {
|
|
|
|
fn default() -> Self {
|
2019-03-29 05:33:41 +00:00
|
|
|
Client(Rc::new(ClientConfig {
|
|
|
|
connector: RefCell::new(Box::new(ConnectorWrapper(
|
2019-03-26 04:58:01 +00:00
|
|
|
Connector::new().service(),
|
|
|
|
))),
|
2019-03-29 05:33:41 +00:00
|
|
|
headers: HeaderMap::new(),
|
|
|
|
timeout: Some(Duration::from_secs(5)),
|
|
|
|
}))
|
2019-03-26 04:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
2019-03-26 16:11:27 +00:00
|
|
|
/// Create new client instance with default settings.
|
|
|
|
pub fn new() -> Client {
|
|
|
|
Client::default()
|
|
|
|
}
|
|
|
|
|
2019-03-26 04:58:01 +00:00
|
|
|
/// Build client instance.
|
|
|
|
pub fn build() -> ClientBuilder {
|
|
|
|
ClientBuilder::new()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct HTTP request.
|
|
|
|
pub fn request<U>(&self, method: Method, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: HttpTryFrom<U>,
|
|
|
|
{
|
2019-03-29 05:33:41 +00:00
|
|
|
let mut req = ClientRequest::new(method, url, self.0.clone());
|
2019-03-29 04:48:35 +00:00
|
|
|
|
2019-03-29 05:33:41 +00:00
|
|
|
for (key, value) in &self.0.headers {
|
2019-04-02 19:51:16 +00:00
|
|
|
req = req.set_header_if_none(key.clone(), value.clone());
|
2019-03-29 04:48:35 +00:00
|
|
|
}
|
|
|
|
req
|
2019-03-26 04:58:01 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 06:25:24 +00:00
|
|
|
/// Create `ClientRequest` from `RequestHead`
|
|
|
|
///
|
|
|
|
/// It is useful for proxy requests. This implementation
|
|
|
|
/// copies all headers and the method.
|
|
|
|
pub fn request_from<U>(&self, url: U, head: &RequestHead) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: HttpTryFrom<U>,
|
|
|
|
{
|
2019-03-29 04:48:35 +00:00
|
|
|
let mut req = self.request(head.method.clone(), url);
|
2019-03-27 06:25:24 +00:00
|
|
|
for (key, value) in &head.headers {
|
2019-04-02 19:51:16 +00:00
|
|
|
req = req.set_header_if_none(key.clone(), value.clone());
|
2019-03-27 06:25:24 +00:00
|
|
|
}
|
|
|
|
req
|
|
|
|
}
|
|
|
|
|
2019-03-29 20:49:21 +00:00
|
|
|
/// Construct HTTP *GET* request.
|
2019-03-26 04:58:01 +00:00
|
|
|
pub fn get<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: HttpTryFrom<U>,
|
|
|
|
{
|
2019-03-29 04:48:35 +00:00
|
|
|
self.request(Method::GET, url)
|
2019-03-26 04:58:01 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 20:49:21 +00:00
|
|
|
/// Construct HTTP *HEAD* request.
|
2019-03-26 04:58:01 +00:00
|
|
|
pub fn head<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: HttpTryFrom<U>,
|
|
|
|
{
|
2019-03-29 04:48:35 +00:00
|
|
|
self.request(Method::HEAD, url)
|
2019-03-26 04:58:01 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 20:49:21 +00:00
|
|
|
/// Construct HTTP *PUT* request.
|
2019-03-26 04:58:01 +00:00
|
|
|
pub fn put<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: HttpTryFrom<U>,
|
|
|
|
{
|
2019-03-29 04:48:35 +00:00
|
|
|
self.request(Method::PUT, url)
|
2019-03-26 04:58:01 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 20:49:21 +00:00
|
|
|
/// Construct HTTP *POST* request.
|
2019-03-26 04:58:01 +00:00
|
|
|
pub fn post<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: HttpTryFrom<U>,
|
|
|
|
{
|
2019-03-29 04:48:35 +00:00
|
|
|
self.request(Method::POST, url)
|
2019-03-26 04:58:01 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 20:49:21 +00:00
|
|
|
/// Construct HTTP *PATCH* request.
|
2019-03-26 04:58:01 +00:00
|
|
|
pub fn patch<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: HttpTryFrom<U>,
|
|
|
|
{
|
2019-03-29 04:48:35 +00:00
|
|
|
self.request(Method::PATCH, url)
|
2019-03-26 04:58:01 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 20:49:21 +00:00
|
|
|
/// Construct HTTP *DELETE* request.
|
2019-03-26 04:58:01 +00:00
|
|
|
pub fn delete<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: HttpTryFrom<U>,
|
|
|
|
{
|
2019-03-29 04:48:35 +00:00
|
|
|
self.request(Method::DELETE, url)
|
2019-03-26 04:58:01 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 20:49:21 +00:00
|
|
|
/// Construct HTTP *OPTIONS* request.
|
2019-03-26 04:58:01 +00:00
|
|
|
pub fn options<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: HttpTryFrom<U>,
|
|
|
|
{
|
2019-03-29 04:48:35 +00:00
|
|
|
self.request(Method::OPTIONS, url)
|
2019-03-26 04:58:01 +00:00
|
|
|
}
|
2019-03-28 01:53:19 +00:00
|
|
|
|
2019-03-29 20:49:21 +00:00
|
|
|
/// Construct WebSockets request.
|
|
|
|
pub fn ws<U>(&self, url: U) -> ws::WebsocketsRequest
|
2019-03-28 01:53:19 +00:00
|
|
|
where
|
|
|
|
Uri: HttpTryFrom<U>,
|
|
|
|
{
|
2019-03-29 20:49:21 +00:00
|
|
|
let mut req = ws::WebsocketsRequest::new(url, self.0.clone());
|
2019-03-29 05:33:41 +00:00
|
|
|
for (key, value) in &self.0.headers {
|
2019-03-29 04:48:35 +00:00
|
|
|
req.head.headers.insert(key.clone(), value.clone());
|
|
|
|
}
|
|
|
|
req
|
2019-03-28 01:53:19 +00:00
|
|
|
}
|
2019-03-26 04:58:01 +00:00
|
|
|
}
|