From f45eb79be576d536ea8612e4801c8b0f6d1d8071 Mon Sep 17 00:00:00 2001 From: Jeremiah Senkpiel Date: Fri, 18 Sep 2020 21:50:04 -0700 Subject: [PATCH] crate: add hyper-client feature Adds support for `http-client/hyper_client`, including global pooling for one-off surf methods. Also adjusts client configuration to fallback once again, starting with the highest specificity (wasm) and allowing e.g. `features = ["hyper-client"]` to override the default. --- Cargo.toml | 3 ++- src/client.rs | 19 +++++++++++-------- src/lib.rs | 8 +++++--- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 82db59c..3c209ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,8 +20,9 @@ edition = "2018" # when the default feature set is updated, verify that the `--features` flags in # `.github/workflows/ci.yaml` are updated accordingly default = ["curl-client", "middleware-logger", "encoding"] -h1-client = ["http-client/h1_client"] curl-client = ["http-client/curl_client", "once_cell"] +h1-client = ["http-client/h1_client"] +hyper-client = ["http-client/hyper_client", "once_cell", "async-std/tokio02"] wasm-client = ["http-client/wasm_client"] middleware-logger = [] # requires web-sys for TextDecoder on wasm diff --git a/src/client.rs b/src/client.rs index 0f4b1f0..aeb483f 100644 --- a/src/client.rs +++ b/src/client.rs @@ -10,17 +10,20 @@ use futures_util::future::BoxFuture; #[cfg(feature = "curl-client")] use http_client::isahc::IsahcClient as DefaultClient; -#[cfg(feature = "wasm-client")] -use http_client::wasm::WasmClient as DefaultClient; - #[cfg(feature = "h1-client")] use http_client::h1::H1Client as DefaultClient; -#[cfg(feature = "curl-client")] +#[cfg(feature = "hyper-client")] +use http_client::hyper::HyperClient as DefaultClient; + +#[cfg(feature = "wasm-client")] +use http_client::wasm::WasmClient as DefaultClient; + +#[cfg(any(feature = "curl-client", feature = "hyper-client"))] use once_cell::sync::Lazy; -#[cfg(feature = "curl-client")] -static GLOBAL_CLIENT: Lazy = - Lazy::new(http_client::isahc::IsahcClient::new); +#[cfg(any(feature = "curl-client", feature = "hyper-client"))] +static GLOBAL_CLIENT: Lazy = + Lazy::new(DefaultClient::new); /// An HTTP client, capable of sending `Request`s and running a middleware stack. /// @@ -108,7 +111,7 @@ impl Client { } pub(crate) fn new_shared() -> Self { - #[cfg(feature = "curl-client")] + #[cfg(any(feature = "curl-client", feature = "hyper-client"))] return Self::with_http_client(Arc::new(GLOBAL_CLIENT.clone())); #[cfg(not(feature = "curl-client"))] diff --git a/src/lib.rs b/src/lib.rs index 142f85f..409c11b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,8 +64,9 @@ //! # Features //! The following features are available. The default features are //! `curl-client`, `middleware-logger`, and `encoding` -//! - __`h1-client`:__ use `async-h1` as the HTTP backend. //! - __`curl-client` (default):__ use `curl` (through `isahc`) as the HTTP backend. +//! - __`h1-client`:__ use `async-h1` as the HTTP backend. +//! - __`hyper-client`:__ use `hyper` (hyper.rs) as the HTTP backend. //! - __`wasm-client`:__ use `window.fetch` as the HTTP backend. //! - __`middleware-logger` (default):__ enables logging requests and responses using a middleware. //! - __`encoding` (default):__ enables support for body encodings other than utf-8 @@ -79,11 +80,12 @@ #![doc(html_logo_url = "https://yoshuawuyts.com/assets/http-rs/logo-rounded.png")] #[cfg(not(any( - feature = "h1-client", feature = "curl-client", + feature = "h1-client", + feature = "hyper-client", feature = "wasm-client" )))] -compile_error!("A client backend must be set via surf features. Choose one: \"h1-client\", \"curl-client\", \"wasm-client\"."); +compile_error!("A single client backend must be set via surf features. Choose one: \"curl-client\" (default), \"h1-client\", \"hyper-client\", \"wasm-client\"."); mod client; mod request;