From 9c038ee1891afe204b06c2e22dcdc947f6d3469b Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 25 Nov 2018 20:14:42 -1000 Subject: [PATCH] allow to use Uri for client request --- src/client/connector.rs | 6 ++++++ src/client/mod.rs | 2 +- src/client/request.rs | 40 +++++++++++++++++++++++++--------------- src/lib.rs | 3 +++ 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/client/connector.rs b/src/client/connector.rs index 81808521..42cba9de 100644 --- a/src/client/connector.rs +++ b/src/client/connector.rs @@ -62,6 +62,12 @@ impl Default for Connector { } impl Connector { + /// Use custom resolver. + pub fn resolver(mut self, resolver: Resolver) -> Self { + self.resolver = resolver;; + self + } + /// Use custom resolver configuration. pub fn resolver_config(mut self, cfg: ResolverConfig, opts: ResolverOpts) -> Self { self.resolver = Resolver::new(cfg, opts); diff --git a/src/client/mod.rs b/src/client/mod.rs index 76c3f8b8..dcc4f5d4 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -13,4 +13,4 @@ pub use self::connection::Connection; pub use self::connector::Connector; pub use self::error::{ConnectorError, InvalidUrlKind, SendRequestError}; pub use self::request::{ClientRequest, ClientRequestBuilder}; -pub use self::response::ClientResponse; +pub use self::response::ClientResponse; \ No newline at end of file diff --git a/src/client/request.rs b/src/client/request.rs index 735ce493..dd29d797 100644 --- a/src/client/request.rs +++ b/src/client/request.rs @@ -7,7 +7,6 @@ use bytes::{BufMut, Bytes, BytesMut}; use cookie::{Cookie, CookieJar}; use futures::{Future, Stream}; use percent_encoding::{percent_encode, USERINFO_ENCODE_SET}; -use urlcrate::Url; use body::{BodyStream, MessageBody}; use error::Error; @@ -63,35 +62,50 @@ impl ClientRequest<()> { } /// Create request builder for `GET` request - pub fn get>(uri: U) -> ClientRequestBuilder { + pub fn get(uri: U) -> ClientRequestBuilder + where + Uri: HttpTryFrom, + { let mut builder = ClientRequest::build(); builder.method(Method::GET).uri(uri); builder } /// Create request builder for `HEAD` request - pub fn head>(uri: U) -> ClientRequestBuilder { + pub fn head(uri: U) -> ClientRequestBuilder + where + Uri: HttpTryFrom, + { let mut builder = ClientRequest::build(); builder.method(Method::HEAD).uri(uri); builder } /// Create request builder for `POST` request - pub fn post>(uri: U) -> ClientRequestBuilder { + pub fn post(uri: U) -> ClientRequestBuilder + where + Uri: HttpTryFrom, + { let mut builder = ClientRequest::build(); builder.method(Method::POST).uri(uri); builder } /// Create request builder for `PUT` request - pub fn put>(uri: U) -> ClientRequestBuilder { + pub fn put(uri: U) -> ClientRequestBuilder + where + Uri: HttpTryFrom, + { let mut builder = ClientRequest::build(); builder.method(Method::PUT).uri(uri); builder } /// Create request builder for `DELETE` request - pub fn delete>(uri: U) -> ClientRequestBuilder { + pub fn delete(uri: U) -> ClientRequestBuilder + where + Uri: HttpTryFrom, + { let mut builder = ClientRequest::build(); builder.method(Method::DELETE).uri(uri); builder @@ -202,15 +216,11 @@ pub struct ClientRequestBuilder { impl ClientRequestBuilder { /// Set HTTP URI of request. #[inline] - pub fn uri>(&mut self, uri: U) -> &mut Self { - match Url::parse(uri.as_ref()) { - Ok(url) => self._uri(url.as_str()), - Err(_) => self._uri(uri.as_ref()), - } - } - - fn _uri(&mut self, url: &str) -> &mut Self { - match Uri::try_from(url) { + pub fn uri(&mut self, uri: U) -> &mut Self + where + Uri: HttpTryFrom, + { + match Uri::try_from(uri) { Ok(uri) => { if let Some(parts) = parts(&mut self.head, &self.err) { parts.uri = uri; diff --git a/src/lib.rs b/src/lib.rs index 5256dd19..4870eb64 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -163,6 +163,9 @@ pub mod http { #[doc(hidden)] pub use modhttp::{uri, Error, HeaderMap, HttpTryFrom, Uri}; + #[doc(hidden)] + pub use modhttp::uri::PathAndQuery; + pub use cookie::{Cookie, CookieBuilder}; /// Various http headers