From af0c8d893d7f13145deeacc9305f2e269eedba85 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 6 Apr 2018 10:09:31 -0700 Subject: [PATCH] add shortcut method for client requests --- src/client/mod.rs | 63 +++++++++++++++++++++++++++++++++++++++++++ src/client/request.rs | 31 +++++++++++++++++++-- 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/src/client/mod.rs b/src/client/mod.rs index 5abe4ff6b..8becafc9d 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -14,6 +14,7 @@ pub(crate) use self::writer::HttpClientWriter; pub(crate) use self::parser::{HttpResponseParser, HttpResponseParserError}; use error::ResponseError; +use http::Method; use httpresponse::HttpResponse; @@ -28,3 +29,65 @@ impl ResponseError for SendRequestError { .into() } } + +/// Create request builder for `GET` request +/// +/// ```rust +/// # extern crate actix; +/// # extern crate actix_web; +/// # extern crate futures; +/// # use futures::Future; +/// use actix_web::client; +/// +/// fn main() { +/// let sys = actix::System::new("test"); +/// +/// actix::Arbiter::handle().spawn({ +/// 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); +/// # actix::Arbiter::system().do_send(actix::msgs::SystemExit(0)); +/// Ok(()) +/// }) +/// }); +/// +/// sys.run(); +/// } +/// ``` +pub fn get>(uri: U) -> ClientRequestBuilder { + let mut builder = ClientRequest::build(); + builder.method(Method::GET).uri(uri); + builder +} + +/// Create request builder for `HEAD` request +pub fn head>(uri: U) -> ClientRequestBuilder { + let mut builder = ClientRequest::build(); + builder.method(Method::HEAD).uri(uri); + builder +} + +/// Create request builder for `POST` request +pub fn post>(uri: U) -> ClientRequestBuilder { + let mut builder = ClientRequest::build(); + builder.method(Method::POST).uri(uri); + builder +} + +/// Create request builder for `PUT` request +pub fn put>(uri: U) -> ClientRequestBuilder { + let mut builder = ClientRequest::build(); + builder.method(Method::PUT).uri(uri); + builder +} + +/// Create request builder for `DELETE` request +pub fn delete>(uri: U) -> ClientRequestBuilder { + let mut builder = ClientRequest::build(); + builder.method(Method::DELETE).uri(uri); + builder +} diff --git a/src/client/request.rs b/src/client/request.rs index 8f2967ab4..d58a323a8 100644 --- a/src/client/request.rs +++ b/src/client/request.rs @@ -6,8 +6,6 @@ use std::time::Duration; use actix::{Addr, Unsync}; use cookie::{Cookie, CookieJar}; use bytes::{Bytes, BytesMut, BufMut}; -use http::{uri, HeaderMap, Method, Version, Uri, HttpTryFrom, Error as HttpError}; -use http::header::{self, HeaderName, HeaderValue}; use futures::Stream; use serde_json; use serde::Serialize; @@ -19,10 +17,39 @@ use error::Error; use header::{ContentEncoding, Header, IntoHeaderValue}; use httpmessage::HttpMessage; use httprequest::HttpRequest; +use http::{uri, HeaderMap, Method, Version, Uri, HttpTryFrom, Error as HttpError}; +use http::header::{self, HeaderName, HeaderValue}; use super::pipeline::SendRequest; use super::connector::{Connection, ClientConnector}; /// An HTTP Client Request +/// +/// ```rust +/// # extern crate actix; +/// # extern crate actix_web; +/// # extern crate futures; +/// # use futures::Future; +/// use actix_web::client::ClientRequest; +/// +/// fn main() { +/// let sys = actix::System::new("test"); +/// +/// actix::Arbiter::handle().spawn({ +/// ClientRequest::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); +/// # actix::Arbiter::system().do_send(actix::msgs::SystemExit(0)); +/// Ok(()) +/// }) +/// }); +/// +/// sys.run(); +/// } +/// ``` pub struct ClientRequest { uri: Uri, method: Method,