1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 09:42:40 +01:00

allow to use Uri for client request

This commit is contained in:
Nikolay Kim 2018-11-25 20:14:42 -10:00
parent 1a322966ff
commit 9c038ee189
4 changed files with 35 additions and 16 deletions

View File

@ -62,6 +62,12 @@ impl Default for Connector {
} }
impl Connector { impl Connector {
/// Use custom resolver.
pub fn resolver(mut self, resolver: Resolver<Connect>) -> Self {
self.resolver = resolver;;
self
}
/// Use custom resolver configuration. /// Use custom resolver configuration.
pub fn resolver_config(mut self, cfg: ResolverConfig, opts: ResolverOpts) -> Self { pub fn resolver_config(mut self, cfg: ResolverConfig, opts: ResolverOpts) -> Self {
self.resolver = Resolver::new(cfg, opts); self.resolver = Resolver::new(cfg, opts);

View File

@ -13,4 +13,4 @@ pub use self::connection::Connection;
pub use self::connector::Connector; pub use self::connector::Connector;
pub use self::error::{ConnectorError, InvalidUrlKind, SendRequestError}; pub use self::error::{ConnectorError, InvalidUrlKind, SendRequestError};
pub use self::request::{ClientRequest, ClientRequestBuilder}; pub use self::request::{ClientRequest, ClientRequestBuilder};
pub use self::response::ClientResponse; pub use self::response::ClientResponse;

View File

@ -7,7 +7,6 @@ use bytes::{BufMut, Bytes, BytesMut};
use cookie::{Cookie, CookieJar}; use cookie::{Cookie, CookieJar};
use futures::{Future, Stream}; use futures::{Future, Stream};
use percent_encoding::{percent_encode, USERINFO_ENCODE_SET}; use percent_encoding::{percent_encode, USERINFO_ENCODE_SET};
use urlcrate::Url;
use body::{BodyStream, MessageBody}; use body::{BodyStream, MessageBody};
use error::Error; use error::Error;
@ -63,35 +62,50 @@ impl ClientRequest<()> {
} }
/// Create request builder for `GET` request /// Create request builder for `GET` request
pub fn get<U: AsRef<str>>(uri: U) -> ClientRequestBuilder { pub fn get<U>(uri: U) -> ClientRequestBuilder
where
Uri: HttpTryFrom<U>,
{
let mut builder = ClientRequest::build(); let mut builder = ClientRequest::build();
builder.method(Method::GET).uri(uri); builder.method(Method::GET).uri(uri);
builder builder
} }
/// Create request builder for `HEAD` request /// Create request builder for `HEAD` request
pub fn head<U: AsRef<str>>(uri: U) -> ClientRequestBuilder { pub fn head<U>(uri: U) -> ClientRequestBuilder
where
Uri: HttpTryFrom<U>,
{
let mut builder = ClientRequest::build(); let mut builder = ClientRequest::build();
builder.method(Method::HEAD).uri(uri); builder.method(Method::HEAD).uri(uri);
builder builder
} }
/// Create request builder for `POST` request /// Create request builder for `POST` request
pub fn post<U: AsRef<str>>(uri: U) -> ClientRequestBuilder { pub fn post<U>(uri: U) -> ClientRequestBuilder
where
Uri: HttpTryFrom<U>,
{
let mut builder = ClientRequest::build(); let mut builder = ClientRequest::build();
builder.method(Method::POST).uri(uri); builder.method(Method::POST).uri(uri);
builder builder
} }
/// Create request builder for `PUT` request /// Create request builder for `PUT` request
pub fn put<U: AsRef<str>>(uri: U) -> ClientRequestBuilder { pub fn put<U>(uri: U) -> ClientRequestBuilder
where
Uri: HttpTryFrom<U>,
{
let mut builder = ClientRequest::build(); let mut builder = ClientRequest::build();
builder.method(Method::PUT).uri(uri); builder.method(Method::PUT).uri(uri);
builder builder
} }
/// Create request builder for `DELETE` request /// Create request builder for `DELETE` request
pub fn delete<U: AsRef<str>>(uri: U) -> ClientRequestBuilder { pub fn delete<U>(uri: U) -> ClientRequestBuilder
where
Uri: HttpTryFrom<U>,
{
let mut builder = ClientRequest::build(); let mut builder = ClientRequest::build();
builder.method(Method::DELETE).uri(uri); builder.method(Method::DELETE).uri(uri);
builder builder
@ -202,15 +216,11 @@ pub struct ClientRequestBuilder {
impl ClientRequestBuilder { impl ClientRequestBuilder {
/// Set HTTP URI of request. /// Set HTTP URI of request.
#[inline] #[inline]
pub fn uri<U: AsRef<str>>(&mut self, uri: U) -> &mut Self { pub fn uri<U>(&mut self, uri: U) -> &mut Self
match Url::parse(uri.as_ref()) { where
Ok(url) => self._uri(url.as_str()), Uri: HttpTryFrom<U>,
Err(_) => self._uri(uri.as_ref()), {
} match Uri::try_from(uri) {
}
fn _uri(&mut self, url: &str) -> &mut Self {
match Uri::try_from(url) {
Ok(uri) => { Ok(uri) => {
if let Some(parts) = parts(&mut self.head, &self.err) { if let Some(parts) = parts(&mut self.head, &self.err) {
parts.uri = uri; parts.uri = uri;

View File

@ -163,6 +163,9 @@ pub mod http {
#[doc(hidden)] #[doc(hidden)]
pub use modhttp::{uri, Error, HeaderMap, HttpTryFrom, Uri}; pub use modhttp::{uri, Error, HeaderMap, HttpTryFrom, Uri};
#[doc(hidden)]
pub use modhttp::uri::PathAndQuery;
pub use cookie::{Cookie, CookieBuilder}; pub use cookie::{Cookie, CookieBuilder};
/// Various http headers /// Various http headers