1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-28 10:02:38 +01:00
actix-web/src/client/mod.rs

120 lines
3.5 KiB
Rust
Raw Normal View History

2018-04-10 06:57:40 +02:00
//! Http client api
//!
//! ```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;
//! # 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);
//! # process::exit(0);
2018-04-10 06:57:40 +02:00
//! Ok(())
//! })
//! });
//! }
//! ```
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;
mod request;
2018-01-28 07:03:03 +01:00
mod response;
mod writer;
2018-01-28 07:03:03 +01: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};
pub use self::request::{ClientRequest, ClientRequestBuilder};
pub use self::response::ClientResponse;
2018-02-19 12:11:11 +01:00
pub(crate) use self::writer::HttpClientWriter;
use error::ResponseError;
use http::Method;
use httpresponse::HttpResponse;
/// 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(),
SendRequestError::Connector(_) => HttpResponse::BadGateway(),
_ => HttpResponse::InternalServerError(),
2018-04-14 01:02:01 +02:00
}.into()
}
}
/// Create request builder for `GET` requests
///
///
/// ```rust
/// # extern crate actix_web;
/// # extern crate futures;
/// # extern crate tokio;
/// # extern crate env_logger;
/// # use futures::Future;
/// # use std::process;
/// use actix_web::client;
///
/// fn main() {
2018-05-29 19:31:37 +02:00
/// tokio::run(
/// 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
/// println!("Response: {:?}", response);
/// # process::exit(0);
/// Ok(())
2018-05-29 19:31:37 +02:00
/// }));
/// }
/// ```
pub fn get<U: AsRef<str>>(uri: U) -> ClientRequestBuilder {
let mut builder = ClientRequest::build();
builder.method(Method::GET).uri(uri);
builder
}
/// Create request builder for `HEAD` requests
pub fn head<U: AsRef<str>>(uri: U) -> ClientRequestBuilder {
let mut builder = ClientRequest::build();
builder.method(Method::HEAD).uri(uri);
builder
}
/// Create request builder for `POST` requests
pub fn post<U: AsRef<str>>(uri: U) -> ClientRequestBuilder {
let mut builder = ClientRequest::build();
builder.method(Method::POST).uri(uri);
builder
}
/// Create request builder for `PUT` requests
pub fn put<U: AsRef<str>>(uri: U) -> ClientRequestBuilder {
let mut builder = ClientRequest::build();
builder.method(Method::PUT).uri(uri);
builder
}
/// Create request builder for `DELETE` requests
pub fn delete<U: AsRef<str>>(uri: U) -> ClientRequestBuilder {
let mut builder = ClientRequest::build();
builder.method(Method::DELETE).uri(uri);
builder
}