From e6c8fdc5f863dc6b4d8dae9e41c4e9680bbf5135 Mon Sep 17 00:00:00 2001 From: Aidan Hobson Sayers Date: Sat, 4 Mar 2017 00:15:51 +0000 Subject: [PATCH] Allow constructing a client with a raw ::hyper::Client --- .appveyor.yml | 1 + .travis.yml | 1 + Cargo.toml | 6 +++++- src/client.rs | 12 ++++++++++-- src/lib.rs | 2 ++ 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index e75d59feb..8ec374ea0 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -13,4 +13,5 @@ install: build: false test_script: - cargo build --verbose + - cargo build --verbose --no-default-features - cargo test --verbose diff --git a/.travis.yml b/.travis.yml index 8084a04c0..37454bdce 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,6 +19,7 @@ cache: script: - cargo build --verbose + - cargo build --verbose --no-default-features - cargo test --verbose notifications: diff --git a/Cargo.toml b/Cargo.toml index 4d8d8ed00..93f607784 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ categories = ["web-programming::http-client"] [dependencies] hyper = "0.10.2" -hyper-native-tls = "0.2" +hyper-native-tls = { version = "0.2", optional = true } log = "0.3" serde = "0.9" serde_json = "0.9" @@ -21,3 +21,7 @@ libflate = "0.1.3" [dev-dependencies] env_logger = "0.3" + +[features] +default = ["default-tls"] +default-tls = ["hyper-native-tls"] diff --git a/src/client.rs b/src/client.rs index 42b994eaf..f083abf33 100644 --- a/src/client.rs +++ b/src/client.rs @@ -32,16 +32,23 @@ pub struct Client { impl Client { /// Constructs a new `Client`. + #[cfg(feature = "default-tls")] pub fn new() -> ::Result { let mut client = try!(new_hyper_client()); client.set_redirect_policy(::hyper::client::RedirectPolicy::FollowNone); - Ok(Client { + Ok(Client::with_hyper_client(client)) + } + + /// Constructs a new `Client` with the provided `::hyper::Client` without + /// altering any settings. + pub fn with_hyper_client(client: ::hyper::Client) -> Client { + Client { inner: Arc::new(ClientRef { hyper: client, redirect_policy: Mutex::new(RedirectPolicy::default()), auto_ungzip: AtomicBool::new(true), }), - }) + } } /// Enable auto gzip decompression by checking the ContentEncoding response header. @@ -112,6 +119,7 @@ struct ClientRef { auto_ungzip: AtomicBool, } +#[cfg(feature = "default-tls")] fn new_hyper_client() -> ::Result<::hyper::Client> { use hyper_native_tls::NativeTlsClient; Ok(::hyper::Client::with_connector( diff --git a/src/lib.rs b/src/lib.rs index 72c065a46..0943587ed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -102,6 +102,7 @@ extern crate hyper; #[macro_use] extern crate log; extern crate libflate; +#[cfg(feature = "default-tls")] extern crate hyper_native_tls; extern crate serde; extern crate serde_json; @@ -143,6 +144,7 @@ mod redirect; /// reqwest::get("https://www.rust-lang.org").unwrap() /// .read_to_string(&mut result); /// ``` +#[cfg(feature = "default-tls")] pub fn get(url: T) -> ::Result { let client = try!(Client::new()); client.get(url).send()