2020-10-30 03:50:53 +01:00
|
|
|
//! `awc` is a HTTP and WebSocket client library built on the Actix ecosystem.
|
2020-09-09 15:24:12 +02:00
|
|
|
//!
|
2021-06-19 21:23:06 +02:00
|
|
|
//! # Making a GET request
|
2021-01-08 13:00:58 +01:00
|
|
|
//! ```no_run
|
2020-09-09 15:24:12 +02:00
|
|
|
//! # #[actix_rt::main]
|
|
|
|
//! # async fn main() -> Result<(), awc::error::SendRequestError> {
|
|
|
|
//! let mut client = awc::Client::default();
|
|
|
|
//! let response = client.get("http://www.rust-lang.org") // <- Create request builder
|
2021-01-15 03:11:10 +01:00
|
|
|
//! .insert_header(("User-Agent", "Actix-web"))
|
2020-09-09 15:24:12 +02:00
|
|
|
//! .send() // <- Send http request
|
|
|
|
//! .await?;
|
|
|
|
//!
|
|
|
|
//! println!("Response: {:?}", response);
|
|
|
|
//! # Ok(())
|
|
|
|
//! # }
|
|
|
|
//! ```
|
|
|
|
//!
|
2021-06-19 21:23:06 +02:00
|
|
|
//! # Making POST requests
|
|
|
|
//! ## Raw body contents
|
2021-01-08 13:00:58 +01:00
|
|
|
//! ```no_run
|
2020-09-09 15:24:12 +02:00
|
|
|
//! # #[actix_rt::main]
|
|
|
|
//! # async fn main() -> Result<(), awc::error::SendRequestError> {
|
|
|
|
//! let mut client = awc::Client::default();
|
|
|
|
//! let response = client.post("http://httpbin.org/post")
|
|
|
|
//! .send_body("Raw body contents")
|
|
|
|
//! .await?;
|
|
|
|
//! # Ok(())
|
|
|
|
//! # }
|
|
|
|
//! ```
|
|
|
|
//!
|
2021-06-19 21:23:06 +02:00
|
|
|
//! ## Forms
|
2021-01-08 13:00:58 +01:00
|
|
|
//! ```no_run
|
2020-09-09 15:24:12 +02:00
|
|
|
//! # #[actix_rt::main]
|
|
|
|
//! # async fn main() -> Result<(), awc::error::SendRequestError> {
|
|
|
|
//! let params = [("foo", "bar"), ("baz", "quux")];
|
|
|
|
//!
|
|
|
|
//! let mut client = awc::Client::default();
|
|
|
|
//! let response = client.post("http://httpbin.org/post")
|
|
|
|
//! .send_form(¶ms)
|
|
|
|
//! .await?;
|
|
|
|
//! # Ok(())
|
|
|
|
//! # }
|
|
|
|
//! ```
|
2019-03-27 17:24:55 +01:00
|
|
|
//!
|
2021-06-19 21:23:06 +02:00
|
|
|
//! ## JSON
|
2021-01-08 13:00:58 +01:00
|
|
|
//! ```no_run
|
2020-09-09 15:24:12 +02:00
|
|
|
//! # #[actix_rt::main]
|
|
|
|
//! # async fn main() -> Result<(), awc::error::SendRequestError> {
|
|
|
|
//! let request = serde_json::json!({
|
|
|
|
//! "lang": "rust",
|
|
|
|
//! "body": "json"
|
|
|
|
//! });
|
2019-11-26 06:25:50 +01:00
|
|
|
//!
|
2020-09-09 15:24:12 +02:00
|
|
|
//! let mut client = awc::Client::default();
|
|
|
|
//! let response = client.post("http://httpbin.org/post")
|
|
|
|
//! .send_json(&request)
|
|
|
|
//! .await?;
|
|
|
|
//! # Ok(())
|
|
|
|
//! # }
|
2019-03-27 17:24:55 +01:00
|
|
|
//! ```
|
2020-09-09 15:24:12 +02:00
|
|
|
//!
|
2021-06-19 21:23:06 +02:00
|
|
|
//! # Response Compression
|
|
|
|
//! All [official][iana-encodings] and common content encoding codecs are supported, optionally.
|
2020-09-09 15:24:12 +02:00
|
|
|
//!
|
2021-06-19 21:23:06 +02:00
|
|
|
//! The `Accept-Encoding` header will automatically be populated with enabled codecs and added to
|
|
|
|
//! outgoing requests, allowing servers to select their `Content-Encoding` accordingly.
|
|
|
|
//!
|
|
|
|
//! Feature flags enable these codecs according to the table below. By default, all `compress-*`
|
|
|
|
//! features are enabled.
|
|
|
|
//!
|
|
|
|
//! | Feature | Codecs |
|
|
|
|
//! | ----------------- | ------------- |
|
|
|
|
//! | `compress-brotli` | brotli |
|
|
|
|
//! | `compress-gzip` | gzip, deflate |
|
|
|
|
//! | `compress-zstd` | zstd |
|
|
|
|
//!
|
|
|
|
//! [iana-encodings]: https://www.iana.org/assignments/http-parameters/http-parameters.xhtml#content-coding
|
|
|
|
//!
|
|
|
|
//! # WebSocket support
|
2021-01-08 13:00:58 +01:00
|
|
|
//! ```no_run
|
2020-09-09 15:24:12 +02:00
|
|
|
//! # #[actix_rt::main]
|
|
|
|
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
//! use futures_util::{sink::SinkExt, stream::StreamExt};
|
|
|
|
//! let (_resp, mut connection) = awc::Client::new()
|
|
|
|
//! .ws("ws://echo.websocket.org")
|
|
|
|
//! .connect()
|
|
|
|
//! .await?;
|
|
|
|
//!
|
|
|
|
//! connection
|
2021-01-04 12:27:32 +01:00
|
|
|
//! .send(awc::ws::Message::Text("Echo".into()))
|
2020-09-09 15:24:12 +02:00
|
|
|
//! .await?;
|
|
|
|
//! let response = connection.next().await.unwrap()?;
|
|
|
|
//! # assert_eq!(response, awc::ws::Frame::Text("Echo".as_bytes().into()));
|
|
|
|
//! # Ok(())
|
|
|
|
//! # }
|
|
|
|
//! ```
|
|
|
|
|
2020-10-30 03:50:53 +01:00
|
|
|
#![deny(rust_2018_idioms)]
|
|
|
|
#![allow(
|
|
|
|
clippy::type_complexity,
|
|
|
|
clippy::borrow_interior_mutable_const,
|
|
|
|
clippy::needless_doctest_main
|
|
|
|
)]
|
|
|
|
#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
|
|
|
|
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
|
|
|
|
|
2019-03-26 05:58:01 +01:00
|
|
|
mod builder;
|
2021-10-26 01:37:40 +02:00
|
|
|
mod client;
|
2019-03-26 05:58:01 +01:00
|
|
|
mod connect;
|
2019-03-28 02:53:19 +01:00
|
|
|
pub mod error;
|
2019-09-12 06:40:56 +02:00
|
|
|
mod frozen;
|
2021-02-28 19:17:08 +01:00
|
|
|
pub mod middleware;
|
2019-03-26 05:58:01 +01:00
|
|
|
mod request;
|
2019-03-26 19:41:38 +01:00
|
|
|
mod response;
|
2019-09-12 06:40:56 +02:00
|
|
|
mod sender;
|
2019-03-27 05:54:57 +01:00
|
|
|
pub mod test;
|
2019-03-29 21:49:21 +01:00
|
|
|
pub mod ws;
|
2019-03-26 05:58:01 +01:00
|
|
|
|
2021-10-26 01:37:40 +02:00
|
|
|
pub use actix_http::http;
|
|
|
|
#[cfg(feature = "cookies")]
|
|
|
|
pub use cookie;
|
|
|
|
|
2019-03-26 05:58:01 +01:00
|
|
|
pub use self::builder::ClientBuilder;
|
2021-10-26 01:37:40 +02:00
|
|
|
pub use self::client::Connector;
|
2021-03-18 18:53:22 +01:00
|
|
|
pub use self::connect::{BoxConnectorService, BoxedSocket, ConnectRequest, ConnectResponse};
|
2019-09-12 06:40:56 +02:00
|
|
|
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};
|
2019-09-12 06:40:56 +02:00
|
|
|
pub use self::sender::SendClientRequest;
|
2019-03-26 05:58:01 +01:00
|
|
|
|
2021-10-26 01:37:40 +02:00
|
|
|
use std::{convert::TryFrom, rc::Rc, time::Duration};
|
|
|
|
|
|
|
|
use actix_http::{
|
|
|
|
http::{Error as HttpError, HeaderMap, Method, Uri},
|
|
|
|
RequestHead,
|
|
|
|
};
|
|
|
|
use actix_rt::net::TcpStream;
|
|
|
|
use actix_service::Service;
|
|
|
|
|
|
|
|
use self::client::{TcpConnect, TcpConnectError, TcpConnection};
|
|
|
|
|
2020-09-09 15:24:12 +02:00
|
|
|
/// An asynchronous HTTP and WebSocket client.
|
|
|
|
///
|
2021-06-19 21:23:06 +02:00
|
|
|
/// You should take care to create, at most, one `Client` per thread. Otherwise, expect higher CPU
|
|
|
|
/// and memory usage.
|
|
|
|
///
|
2021-06-17 18:57:58 +02:00
|
|
|
/// # Examples
|
2021-03-25 09:45:52 +01:00
|
|
|
/// ```
|
2019-03-26 05:58:01 +01:00
|
|
|
/// 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
|
|
|
///
|
2021-06-19 21:23:06 +02:00
|
|
|
/// let res = client.get("http://www.rust-lang.org")
|
|
|
|
/// .insert_header(("User-Agent", "my-app/1.2"))
|
|
|
|
/// .send()
|
|
|
|
/// .await;
|
2019-12-08 07:31:16 +01:00
|
|
|
///
|
|
|
|
/// println!("Response: {:?}", res);
|
2019-03-26 05:58:01 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[derive(Clone)]
|
2021-03-18 18:53:22 +01:00
|
|
|
pub struct Client(ClientConfig);
|
2019-03-29 05:48:35 +01:00
|
|
|
|
2021-03-18 18:53:22 +01:00
|
|
|
#[derive(Clone)]
|
2019-03-29 05:48:35 +01:00
|
|
|
pub(crate) struct ClientConfig {
|
2021-03-18 18:53:22 +01:00
|
|
|
pub(crate) connector: BoxConnectorService,
|
|
|
|
pub(crate) headers: Rc<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 {
|
2021-03-08 00:57:32 +01:00
|
|
|
ClientBuilder::new().finish()
|
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()
|
|
|
|
}
|
|
|
|
|
2020-09-11 10:24:39 +02:00
|
|
|
/// Create `Client` builder.
|
|
|
|
/// This function is equivalent of `ClientBuilder::new()`.
|
2021-02-18 13:30:09 +01:00
|
|
|
pub fn builder() -> ClientBuilder<
|
|
|
|
impl Service<
|
|
|
|
TcpConnect<Uri>,
|
|
|
|
Response = TcpConnection<Uri, TcpStream>,
|
|
|
|
Error = TcpConnectError,
|
|
|
|
> + Clone,
|
|
|
|
> {
|
2019-03-26 05:58:01 +01:00
|
|
|
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());
|
2019-03-29 05:48:35 +01:00
|
|
|
|
2021-01-15 03:11:10 +01:00
|
|
|
for header in self.0.headers.iter() {
|
|
|
|
req = req.insert_header_if_none(header);
|
2019-03-29 05:48:35 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2019-03-29 05:48:35 +01:00
|
|
|
let mut req = self.request(head.method.clone(), url);
|
2021-01-15 03:11:10 +01:00
|
|
|
for header in head.headers.iter() {
|
|
|
|
req = req.insert_header_if_none(header);
|
2019-03-27 07:25:24 +01:00
|
|
|
}
|
|
|
|
req
|
|
|
|
}
|
|
|
|
|
2019-03-29 21:49:21 +01:00
|
|
|
/// 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
|
|
|
{
|
2019-03-29 05:48:35 +01:00
|
|
|
self.request(Method::GET, url)
|
2019-03-26 05:58:01 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 21:49:21 +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
|
|
|
{
|
2019-03-29 05:48:35 +01:00
|
|
|
self.request(Method::HEAD, url)
|
2019-03-26 05:58:01 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 21:49:21 +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
|
|
|
{
|
2019-03-29 05:48:35 +01:00
|
|
|
self.request(Method::PUT, url)
|
2019-03-26 05:58:01 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 21:49:21 +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
|
|
|
{
|
2019-03-29 05:48:35 +01:00
|
|
|
self.request(Method::POST, url)
|
2019-03-26 05:58:01 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 21:49:21 +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
|
|
|
{
|
2019-03-29 05:48:35 +01:00
|
|
|
self.request(Method::PATCH, url)
|
2019-03-26 05:58:01 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 21:49:21 +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
|
|
|
{
|
2019-03-29 05:48:35 +01:00
|
|
|
self.request(Method::DELETE, url)
|
2019-03-26 05:58:01 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 21:49:21 +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
|
|
|
{
|
2019-03-29 05:48:35 +01:00
|
|
|
self.request(Method::OPTIONS, url)
|
2019-03-26 05:58:01 +01:00
|
|
|
}
|
2019-03-28 02:53:19 +01:00
|
|
|
|
2020-09-07 13:04:54 +02:00
|
|
|
/// Initialize a WebSocket connection.
|
|
|
|
/// Returns a WebSocket connection builder.
|
2019-03-29 21:49:21 +01:00
|
|
|
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
|
|
|
{
|
2019-03-29 21:49:21 +01:00
|
|
|
let mut req = ws::WebsocketsRequest::new(url, self.0.clone());
|
2019-04-07 00:02:02 +02:00
|
|
|
for (key, value) in self.0.headers.iter() {
|
2019-03-29 05:48:35 +01:00
|
|
|
req.head.headers.insert(key.clone(), value.clone());
|
|
|
|
}
|
|
|
|
req
|
2019-03-28 02:53:19 +01:00
|
|
|
}
|
2021-03-31 12:43:56 +02:00
|
|
|
|
|
|
|
/// Get default HeaderMap of Client.
|
|
|
|
///
|
|
|
|
/// Returns Some(&mut HeaderMap) when Client object is unique
|
|
|
|
/// (No other clone of client exists at the same time).
|
|
|
|
pub fn headers(&mut self) -> Option<&mut HeaderMap> {
|
|
|
|
Rc::get_mut(&mut self.0.headers)
|
|
|
|
}
|
2019-03-26 05:58:01 +01:00
|
|
|
}
|