2019-03-26 05:58:01 +01:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2019-03-26 19:41:38 +01:00
|
|
|
pub use actix_http::client::{ConnectError, InvalidUrl, SendRequestError};
|
2019-03-27 04:45:00 +01:00
|
|
|
pub use actix_http::error::PayloadError;
|
2019-03-27 07:25:24 +01:00
|
|
|
pub use actix_http::{http, RequestHead};
|
2019-03-26 05:58:01 +01:00
|
|
|
|
|
|
|
use actix_http::client::Connector;
|
|
|
|
use actix_http::http::{HttpTryFrom, Method, Uri};
|
|
|
|
|
|
|
|
mod builder;
|
|
|
|
mod connect;
|
|
|
|
mod request;
|
2019-03-26 19:41:38 +01:00
|
|
|
mod response;
|
2019-03-27 05:54:57 +01:00
|
|
|
pub mod test;
|
2019-03-26 05:58:01 +01:00
|
|
|
|
|
|
|
pub use self::builder::ClientBuilder;
|
|
|
|
pub use self::request::ClientRequest;
|
2019-03-26 19:41:38 +01:00
|
|
|
pub use self::response::ClientResponse;
|
2019-03-26 05:58:01 +01:00
|
|
|
|
|
|
|
use self::connect::{Connect, ConnectorWrapper};
|
|
|
|
|
|
|
|
/// An HTTP Client Request
|
|
|
|
///
|
|
|
|
/// ```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)]
|
|
|
|
pub struct Client {
|
|
|
|
pub(crate) connector: Rc<RefCell<dyn Connect>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Client {
|
|
|
|
fn default() -> Self {
|
|
|
|
Client {
|
|
|
|
connector: Rc::new(RefCell::new(ConnectorWrapper(
|
|
|
|
Connector::new().service(),
|
|
|
|
))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
2019-03-26 17:11:27 +01:00
|
|
|
/// Create new client instance with default settings.
|
|
|
|
pub fn new() -> Client {
|
|
|
|
Client::default()
|
|
|
|
}
|
|
|
|
|
2019-03-26 05:58:01 +01: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>,
|
|
|
|
{
|
|
|
|
ClientRequest::new(method, url, self.connector.clone())
|
|
|
|
}
|
|
|
|
|
2019-03-27 07:25:24 +01: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>,
|
|
|
|
{
|
|
|
|
let mut req =
|
|
|
|
ClientRequest::new(head.method.clone(), url, self.connector.clone());
|
|
|
|
|
|
|
|
for (key, value) in &head.headers {
|
|
|
|
req.head.headers.insert(key.clone(), value.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
req
|
|
|
|
}
|
|
|
|
|
2019-03-26 05:58:01 +01:00
|
|
|
pub fn get<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: HttpTryFrom<U>,
|
|
|
|
{
|
|
|
|
ClientRequest::new(Method::GET, url, self.connector.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn head<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: HttpTryFrom<U>,
|
|
|
|
{
|
|
|
|
ClientRequest::new(Method::HEAD, url, self.connector.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn put<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: HttpTryFrom<U>,
|
|
|
|
{
|
|
|
|
ClientRequest::new(Method::PUT, url, self.connector.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn post<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: HttpTryFrom<U>,
|
|
|
|
{
|
|
|
|
ClientRequest::new(Method::POST, url, self.connector.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn patch<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: HttpTryFrom<U>,
|
|
|
|
{
|
|
|
|
ClientRequest::new(Method::PATCH, url, self.connector.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: HttpTryFrom<U>,
|
|
|
|
{
|
|
|
|
ClientRequest::new(Method::DELETE, url, self.connector.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn options<U>(&self, url: U) -> ClientRequest
|
|
|
|
where
|
|
|
|
Uri: HttpTryFrom<U>,
|
|
|
|
{
|
|
|
|
ClientRequest::new(Method::OPTIONS, url, self.connector.clone())
|
|
|
|
}
|
|
|
|
}
|