Skip to content

Commit

Permalink
crate: add hyper-client feature
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Fishrock123 committed Sep 21, 2020
1 parent f9316ea commit f45eb79
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Expand Up @@ -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
Expand Down
19 changes: 11 additions & 8 deletions src/client.rs
Expand Up @@ -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<http_client::isahc::IsahcClient> =
Lazy::new(http_client::isahc::IsahcClient::new);
#[cfg(any(feature = "curl-client", feature = "hyper-client"))]
static GLOBAL_CLIENT: Lazy<DefaultClient> =
Lazy::new(DefaultClient::new);

/// An HTTP client, capable of sending `Request`s and running a middleware stack.
///
Expand Down Expand Up @@ -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"))]
Expand Down
8 changes: 5 additions & 3 deletions src/lib.rs
Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit f45eb79

Please sign in to comment.