From 4c8abd5e908f8832e69284edfde4b8ecbd404841 Mon Sep 17 00:00:00 2001 From: karel Date: Sat, 18 May 2024 17:01:23 +0100 Subject: [PATCH 1/2] feat(rpc): expose reqwest client Previously, the reqwest client for HttpClient would be unconditionally built by the internal builder, meaning that useful middleware such as tower could not be applied. This commits adds two ways to create the HttpClient with a custom reqwest::Client, either through new_from_parts, or using the builder. --- rpc/src/client/transport/http.rs | 48 +++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/rpc/src/client/transport/http.rs b/rpc/src/client/transport/http.rs index ed1cff71c..8a86a79aa 100644 --- a/rpc/src/client/transport/http.rs +++ b/rpc/src/client/transport/http.rs @@ -62,6 +62,7 @@ pub struct Builder { compat: CompatMode, proxy_url: Option, timeout: Duration, + client: Option, } impl Builder { @@ -93,22 +94,35 @@ impl Builder { self } + /// Use the provided client instead of building one internally. + pub fn client(mut self, client: reqwest::Client) -> Self { + self.client = Some(client); + self + } + /// Try to create a client with the options specified for this builder. pub fn build(self) -> Result { - let builder = reqwest::ClientBuilder::new() - .user_agent(USER_AGENT) - .timeout(self.timeout); - let inner = match self.proxy_url { - None => builder.build().map_err(Error::http)?, - Some(proxy_url) => { - let proxy = if self.url.0.is_secure() { - Proxy::https(reqwest::Url::from(proxy_url.0)).map_err(Error::invalid_proxy)? - } else { - Proxy::http(reqwest::Url::from(proxy_url.0)).map_err(Error::invalid_proxy)? - }; - builder.proxy(proxy).build().map_err(Error::http)? - }, + let inner = if let Some(inner) = self.client { + inner + } else { + let builder = reqwest::ClientBuilder::new() + .user_agent(USER_AGENT) + .timeout(self.timeout); + match self.proxy_url { + None => builder.build().map_err(Error::http)?, + Some(proxy_url) => { + let proxy = if self.url.0.is_secure() { + Proxy::https(reqwest::Url::from(proxy_url.0)) + .map_err(Error::invalid_proxy)? + } else { + Proxy::http(reqwest::Url::from(proxy_url.0)) + .map_err(Error::invalid_proxy)? + }; + builder.proxy(proxy).build().map_err(Error::http)? + }, + } }; + Ok(HttpClient { inner, url: self.url.into(), @@ -118,6 +132,13 @@ impl Builder { } impl HttpClient { + /// Construct a new Tendermint RPC HTTP/S client connecting to the given + /// URL. This avoids using the `Builder` and thus does not perform any + /// validation of the configuration. + pub fn new_from_parts(inner: reqwest::Client, url: reqwest::Url, compat: CompatMode) -> Self { + Self { inner, url, compat } + } + /// Construct a new Tendermint RPC HTTP/S client connecting to the given /// URL. pub fn new(url: U) -> Result @@ -153,6 +174,7 @@ impl HttpClient { compat: Default::default(), proxy_url: None, timeout: Duration::from_secs(30), + client: None, } } From d4c1329aaa6bb326c488053839c851002e0efc40 Mon Sep 17 00:00:00 2001 From: Romain Ruetschi Date: Wed, 29 May 2024 09:59:20 +0200 Subject: [PATCH 2/2] Add changelog entry --- .changelog/unreleased/features/1421-reqwest-client.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changelog/unreleased/features/1421-reqwest-client.md diff --git a/.changelog/unreleased/features/1421-reqwest-client.md b/.changelog/unreleased/features/1421-reqwest-client.md new file mode 100644 index 000000000..a41cd9c66 --- /dev/null +++ b/.changelog/unreleased/features/1421-reqwest-client.md @@ -0,0 +1,2 @@ +- `[tendermint-rpc]` Add a `client(reqwest::Client)` method on `transport::http::Builder` to override the underlying `reqwest` client. + ([\#1421](https://github.com/informalsystems/tendermint-rs/pull/1421))