1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 00:21:08 +01:00
actix-web/awc/src/lib.rs

210 lines
5.5 KiB
Rust
Raw Normal View History

2019-12-08 07:31:16 +01:00
#![deny(rust_2018_idioms, warnings)]
#![allow(
clippy::type_complexity,
clippy::borrow_interior_mutable_const,
clippy::needless_doctest_main
)]
2019-03-27 17:24:55 +01:00
//! An HTTP Client
//!
//! ```rust
//! use futures::future::{lazy, Future};
2019-03-27 17:24:55 +01:00
//! use actix_rt::System;
//! use awc::Client;
//!
2019-11-26 06:25:50 +01:00
//! #[actix_rt::main]
//! async fn main() {
//! let mut client = Client::default();
2019-03-27 17:24:55 +01:00
//!
2019-11-26 06:25:50 +01:00
//! let response = client.get("http://www.rust-lang.org") // <- Create request builder
2019-12-08 07:31:16 +01:00
//! .header("User-Agent", "Actix-web")
//! .send() // <- Send http request
//! .await;
2019-11-26 06:25:50 +01:00
//!
//! println!("Response: {:?}", response);
2019-03-27 17:24:55 +01:00
//! }
//! ```
2019-03-26 05:58:01 +01:00
use std::cell::RefCell;
2019-12-05 18:35:43 +01:00
use std::convert::TryFrom;
2019-03-26 05:58:01 +01:00
use std::rc::Rc;
2019-03-29 06:33:41 +01:00
use std::time::Duration;
2019-03-26 05:58:01 +01:00
2019-03-30 05:13:39 +01:00
pub use actix_http::{client::Connector, cookie, http};
2019-03-26 05:58:01 +01:00
2019-12-05 18:35:43 +01:00
use actix_http::http::{Error as HttpError, HeaderMap, Method, Uri};
2019-03-27 17:24:55 +01:00
use actix_http::RequestHead;
2019-03-26 05:58:01 +01:00
mod builder;
mod connect;
2019-03-28 02:53:19 +01:00
pub mod error;
mod frozen;
2019-03-26 05:58:01 +01:00
mod request;
mod response;
mod sender;
2019-03-27 05:54:57 +01:00
pub mod test;
pub mod ws;
2019-03-26 05:58:01 +01:00
pub use self::builder::ClientBuilder;
2019-06-12 11:52:48 +02:00
pub use self::connect::BoxedSocket;
pub use self::frozen::{FrozenClientRequest, FrozenSendBuilder};
2019-03-26 05:58:01 +01:00
pub use self::request::ClientRequest;
2019-04-01 20:51:18 +02:00
pub use self::response::{ClientResponse, JsonBody, MessageBody};
pub use self::sender::SendClientRequest;
2019-03-26 05:58:01 +01:00
use self::connect::{Connect, ConnectorWrapper};
2019-03-27 17:24:55 +01:00
/// An HTTP Client
2019-03-26 05:58:01 +01:00
///
/// ```rust
/// use awc::Client;
///
2019-12-08 07:31:16 +01:00
/// #[actix_rt::main]
/// async fn main() {
/// let mut client = Client::default();
2019-03-26 05:58:01 +01:00
///
2019-12-08 07:31:16 +01:00
/// let res = client.get("http://www.rust-lang.org") // <- Create request builder
/// .header("User-Agent", "Actix-web")
/// .send() // <- Send http request
/// .await; // <- send request and wait for response
///
/// println!("Response: {:?}", res);
2019-03-26 05:58:01 +01:00
/// }
/// ```
#[derive(Clone)]
2019-03-29 06:33:41 +01:00
pub struct Client(Rc<ClientConfig>);
pub(crate) struct ClientConfig {
2019-03-29 06:33:41 +01:00
pub(crate) connector: RefCell<Box<dyn Connect>>,
pub(crate) headers: HeaderMap,
2019-03-29 06:33:41 +01:00
pub(crate) timeout: Option<Duration>,
2019-03-26 05:58:01 +01:00
}
impl Default for Client {
fn default() -> Self {
2019-03-29 06:33:41 +01:00
Client(Rc::new(ClientConfig {
connector: RefCell::new(Box::new(ConnectorWrapper(
Connector::new().finish(),
2019-03-26 05:58:01 +01:00
))),
2019-03-29 06:33:41 +01:00
headers: HeaderMap::new(),
timeout: Some(Duration::from_secs(5)),
}))
2019-03-26 05:58:01 +01:00
}
}
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
2019-12-05 18:35:43 +01:00
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
2019-03-26 05:58:01 +01:00
{
2019-03-29 06:33:41 +01:00
let mut req = ClientRequest::new(method, url, self.0.clone());
for (key, value) in self.0.headers.iter() {
2019-04-02 21:51:16 +02:00
req = req.set_header_if_none(key.clone(), value.clone());
}
req
2019-03-26 05:58:01 +01:00
}
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
2019-12-05 18:35:43 +01:00
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
2019-03-27 07:25:24 +01:00
{
let mut req = self.request(head.method.clone(), url);
for (key, value) in head.headers.iter() {
2019-04-02 21:51:16 +02:00
req = req.set_header_if_none(key.clone(), value.clone());
2019-03-27 07:25:24 +01:00
}
req
}
/// Construct HTTP *GET* request.
2019-03-26 05:58:01 +01:00
pub fn get<U>(&self, url: U) -> ClientRequest
where
2019-12-05 18:35:43 +01:00
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
2019-03-26 05:58:01 +01:00
{
self.request(Method::GET, url)
2019-03-26 05:58:01 +01:00
}
/// Construct HTTP *HEAD* request.
2019-03-26 05:58:01 +01:00
pub fn head<U>(&self, url: U) -> ClientRequest
where
2019-12-05 18:35:43 +01:00
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
2019-03-26 05:58:01 +01:00
{
self.request(Method::HEAD, url)
2019-03-26 05:58:01 +01:00
}
/// Construct HTTP *PUT* request.
2019-03-26 05:58:01 +01:00
pub fn put<U>(&self, url: U) -> ClientRequest
where
2019-12-05 18:35:43 +01:00
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
2019-03-26 05:58:01 +01:00
{
self.request(Method::PUT, url)
2019-03-26 05:58:01 +01:00
}
/// Construct HTTP *POST* request.
2019-03-26 05:58:01 +01:00
pub fn post<U>(&self, url: U) -> ClientRequest
where
2019-12-05 18:35:43 +01:00
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
2019-03-26 05:58:01 +01:00
{
self.request(Method::POST, url)
2019-03-26 05:58:01 +01:00
}
/// Construct HTTP *PATCH* request.
2019-03-26 05:58:01 +01:00
pub fn patch<U>(&self, url: U) -> ClientRequest
where
2019-12-05 18:35:43 +01:00
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
2019-03-26 05:58:01 +01:00
{
self.request(Method::PATCH, url)
2019-03-26 05:58:01 +01:00
}
/// Construct HTTP *DELETE* request.
2019-03-26 05:58:01 +01:00
pub fn delete<U>(&self, url: U) -> ClientRequest
where
2019-12-05 18:35:43 +01:00
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
2019-03-26 05:58:01 +01:00
{
self.request(Method::DELETE, url)
2019-03-26 05:58:01 +01:00
}
/// Construct HTTP *OPTIONS* request.
2019-03-26 05:58:01 +01:00
pub fn options<U>(&self, url: U) -> ClientRequest
where
2019-12-05 18:35:43 +01:00
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
2019-03-26 05:58:01 +01:00
{
self.request(Method::OPTIONS, url)
2019-03-26 05:58:01 +01:00
}
2019-03-28 02:53:19 +01:00
/// Construct WebSockets request.
pub fn ws<U>(&self, url: U) -> ws::WebsocketsRequest
2019-03-28 02:53:19 +01:00
where
2019-12-05 18:35:43 +01:00
Uri: TryFrom<U>,
<Uri as TryFrom<U>>::Error: Into<HttpError>,
2019-03-28 02:53:19 +01:00
{
let mut req = ws::WebsocketsRequest::new(url, self.0.clone());
for (key, value) in self.0.headers.iter() {
req.head.headers.insert(key.clone(), value.clone());
}
req
2019-03-28 02:53:19 +01:00
}
2019-03-26 05:58:01 +01:00
}