2018-04-10 06:57:40 +02:00
|
|
|
//! Http client api
|
|
|
|
//!
|
2018-05-30 01:32:39 +02:00
|
|
|
//! ```rust
|
2018-04-10 06:57:40 +02:00
|
|
|
//! # extern crate actix_web;
|
|
|
|
//! # extern crate futures;
|
2018-05-29 19:31:37 +02:00
|
|
|
//! # extern crate tokio;
|
2018-04-10 06:57:40 +02:00
|
|
|
//! # use futures::Future;
|
2018-05-30 01:32:39 +02:00
|
|
|
//! # use std::process;
|
2018-04-10 06:57:40 +02:00
|
|
|
//! use actix_web::client;
|
|
|
|
//!
|
|
|
|
//! fn main() {
|
2018-05-29 19:31:37 +02:00
|
|
|
//! tokio::run({
|
2018-04-10 06:57:40 +02:00
|
|
|
//! client::get("http://www.rust-lang.org") // <- Create request builder
|
|
|
|
//! .header("User-Agent", "Actix-web")
|
|
|
|
//! .finish().unwrap()
|
|
|
|
//! .send() // <- Send http request
|
|
|
|
//! .map_err(|_| ())
|
|
|
|
//! .and_then(|response| { // <- server http response
|
|
|
|
//! println!("Response: {:?}", response);
|
2018-05-30 01:32:39 +02:00
|
|
|
//! # process::exit(0);
|
2018-04-10 06:57:40 +02:00
|
|
|
//! Ok(())
|
|
|
|
//! })
|
|
|
|
//! });
|
|
|
|
//! }
|
|
|
|
//! ```
|
2018-05-30 01:32:39 +02:00
|
|
|
|
|
|
|
mod body;
|
2018-01-30 22:04:52 +01:00
|
|
|
mod connector;
|
2018-01-28 07:03:03 +01:00
|
|
|
mod parser;
|
2018-04-14 01:02:01 +02:00
|
|
|
mod pipeline;
|
2018-01-29 20:39:26 +01:00
|
|
|
mod request;
|
2018-01-28 07:03:03 +01:00
|
|
|
mod response;
|
2018-01-29 23:44:25 +01:00
|
|
|
mod writer;
|
2018-01-28 07:03:03 +01:00
|
|
|
|
2018-05-30 01:32:39 +02:00
|
|
|
pub use self::body::{ClientBody, ClientBodyStream};
|
2018-05-17 21:20:20 +02:00
|
|
|
pub use self::connector::{
|
|
|
|
ClientConnector, ClientConnectorError, ClientConnectorStats, Connect, Connection,
|
|
|
|
Pause, Resume,
|
|
|
|
};
|
2018-04-14 01:02:01 +02:00
|
|
|
pub(crate) use self::parser::{HttpResponseParser, HttpResponseParserError};
|
2018-02-19 12:11:11 +01:00
|
|
|
pub use self::pipeline::{SendRequest, SendRequestError};
|
2018-01-29 20:39:26 +01:00
|
|
|
pub use self::request::{ClientRequest, ClientRequestBuilder};
|
2018-02-28 00:03:28 +01:00
|
|
|
pub use self::response::ClientResponse;
|
2018-02-19 12:11:11 +01:00
|
|
|
pub(crate) use self::writer::HttpClientWriter;
|
2018-03-05 22:02:31 +01:00
|
|
|
|
|
|
|
use error::ResponseError;
|
2018-04-06 19:09:31 +02:00
|
|
|
use http::Method;
|
2018-03-31 08:07:33 +02:00
|
|
|
use httpresponse::HttpResponse;
|
2018-03-05 22:02:31 +01:00
|
|
|
|
|
|
|
/// Convert `SendRequestError` to a `HttpResponse`
|
|
|
|
impl ResponseError for SendRequestError {
|
|
|
|
fn error_response(&self) -> HttpResponse {
|
|
|
|
match *self {
|
2018-05-10 18:37:38 +02:00
|
|
|
SendRequestError::Timeout => HttpResponse::GatewayTimeout(),
|
2018-03-31 08:07:33 +02:00
|
|
|
SendRequestError::Connector(_) => HttpResponse::BadGateway(),
|
|
|
|
_ => HttpResponse::InternalServerError(),
|
2018-04-14 01:02:01 +02:00
|
|
|
}.into()
|
2018-03-05 22:02:31 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-06 19:09:31 +02:00
|
|
|
|
2018-04-07 17:00:57 +02:00
|
|
|
/// Create request builder for `GET` requests
|
2018-04-06 19:09:31 +02:00
|
|
|
///
|
2018-05-30 01:32:39 +02:00
|
|
|
///
|
|
|
|
/// ```rust
|
2018-04-06 19:09:31 +02:00
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # extern crate futures;
|
2018-05-30 01:32:39 +02:00
|
|
|
/// # extern crate tokio;
|
|
|
|
/// # extern crate env_logger;
|
|
|
|
/// # use futures::Future;
|
|
|
|
/// # use std::process;
|
2018-04-06 19:09:31 +02:00
|
|
|
/// use actix_web::client;
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2018-05-29 19:31:37 +02:00
|
|
|
/// tokio::run(
|
2018-04-06 19:09:31 +02:00
|
|
|
/// client::get("http://www.rust-lang.org") // <- Create request builder
|
|
|
|
/// .header("User-Agent", "Actix-web")
|
|
|
|
/// .finish().unwrap()
|
|
|
|
/// .send() // <- Send http request
|
|
|
|
/// .map_err(|_| ())
|
2018-05-29 19:31:37 +02:00
|
|
|
/// .and_then(|response| { // <- server http response
|
2018-04-06 19:09:31 +02:00
|
|
|
/// println!("Response: {:?}", response);
|
2018-05-30 01:32:39 +02:00
|
|
|
/// # process::exit(0);
|
2018-04-06 19:09:31 +02:00
|
|
|
/// Ok(())
|
2018-05-29 19:31:37 +02:00
|
|
|
/// }));
|
2018-04-06 19:09:31 +02:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn get<U: AsRef<str>>(uri: U) -> ClientRequestBuilder {
|
|
|
|
let mut builder = ClientRequest::build();
|
|
|
|
builder.method(Method::GET).uri(uri);
|
|
|
|
builder
|
|
|
|
}
|
|
|
|
|
2018-04-07 17:00:57 +02:00
|
|
|
/// Create request builder for `HEAD` requests
|
2018-04-06 19:09:31 +02:00
|
|
|
pub fn head<U: AsRef<str>>(uri: U) -> ClientRequestBuilder {
|
|
|
|
let mut builder = ClientRequest::build();
|
|
|
|
builder.method(Method::HEAD).uri(uri);
|
|
|
|
builder
|
|
|
|
}
|
|
|
|
|
2018-04-07 17:00:57 +02:00
|
|
|
/// Create request builder for `POST` requests
|
2018-04-06 19:09:31 +02:00
|
|
|
pub fn post<U: AsRef<str>>(uri: U) -> ClientRequestBuilder {
|
|
|
|
let mut builder = ClientRequest::build();
|
|
|
|
builder.method(Method::POST).uri(uri);
|
|
|
|
builder
|
|
|
|
}
|
|
|
|
|
2018-04-07 17:00:57 +02:00
|
|
|
/// Create request builder for `PUT` requests
|
2018-04-06 19:09:31 +02:00
|
|
|
pub fn put<U: AsRef<str>>(uri: U) -> ClientRequestBuilder {
|
|
|
|
let mut builder = ClientRequest::build();
|
|
|
|
builder.method(Method::PUT).uri(uri);
|
|
|
|
builder
|
|
|
|
}
|
|
|
|
|
2018-04-07 17:00:57 +02:00
|
|
|
/// Create request builder for `DELETE` requests
|
2018-04-06 19:09:31 +02:00
|
|
|
pub fn delete<U: AsRef<str>>(uri: U) -> ClientRequestBuilder {
|
|
|
|
let mut builder = ClientRequest::build();
|
|
|
|
builder.method(Method::DELETE).uri(uri);
|
|
|
|
builder
|
|
|
|
}
|