Skip to content

Commit

Permalink
feat(rpc): Allow overriding the underlying reqwest client of the `H…
Browse files Browse the repository at this point in the history
…ttpClient` (#1421)

* 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.

* Add changelog entry

---------

Co-authored-by: Romain Ruetschi <romain@informal.systems>
  • Loading branch information
KaiserKarel and romac authored May 29, 2024
1 parent 2135196 commit 04356a3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/features/1421-reqwest-client.md
Original file line number Diff line number Diff line change
@@ -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))
55 changes: 41 additions & 14 deletions rpc/src/client/transport/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub struct Builder {
proxy_url: Option<HttpClientUrl>,
user_agent: Option<String>,
timeout: Duration,
client: Option<reqwest::Client>,
}

impl Builder {
Expand Down Expand Up @@ -94,28 +95,46 @@ impl Builder {
self
}

/// Specify the custom User-Agent header the client.
/// Specify the custom User-Agent header used by the client.
pub fn user_agent(mut self, agent: String) -> Self {
self.user_agent = Some(agent);
self
}

/// Use the provided client instead of building one internally.
///
/// ## Warning
/// This will override the following options set on the builder:
/// `timeout`, `user_agent`, and `proxy_url`.
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<HttpClient, Error> {
let builder = reqwest::ClientBuilder::new()
.user_agent(self.user_agent.unwrap_or(USER_AGENT.to_string()))
.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(self.user_agent.unwrap_or_else(|| USER_AGENT.to_string()))
.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(),
Expand All @@ -125,6 +144,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<U>(url: U) -> Result<Self, Error>
Expand Down Expand Up @@ -161,6 +187,7 @@ impl HttpClient {
proxy_url: None,
user_agent: None,
timeout: Duration::from_secs(30),
client: None,
}
}

Expand Down

0 comments on commit 04356a3

Please sign in to comment.