Skip to content

Commit 67646cb

Browse files
committed
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.
1 parent 1b219d2 commit 67646cb

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

rpc/src/client/transport/http.rs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub struct Builder {
6262
compat: CompatMode,
6363
proxy_url: Option<HttpClientUrl>,
6464
timeout: Duration,
65+
client: Option<reqwest::Client>,
6566
}
6667

6768
impl Builder {
@@ -93,22 +94,35 @@ impl Builder {
9394
self
9495
}
9596

97+
/// Use the provided client instead of building one internally.
98+
pub fn client(mut self, client: reqwest::Client) -> Self {
99+
self.client = client;
100+
self
101+
}
102+
96103
/// Try to create a client with the options specified for this builder.
97104
pub fn build(self) -> Result<HttpClient, Error> {
98-
let builder = reqwest::ClientBuilder::new()
99-
.user_agent(USER_AGENT)
100-
.timeout(self.timeout);
101-
let inner = match self.proxy_url {
102-
None => builder.build().map_err(Error::http)?,
103-
Some(proxy_url) => {
104-
let proxy = if self.url.0.is_secure() {
105-
Proxy::https(reqwest::Url::from(proxy_url.0)).map_err(Error::invalid_proxy)?
106-
} else {
107-
Proxy::http(reqwest::Url::from(proxy_url.0)).map_err(Error::invalid_proxy)?
108-
};
109-
builder.proxy(proxy).build().map_err(Error::http)?
110-
},
105+
let inner = if let Some(inner) = self.client {
106+
inner
107+
} else {
108+
let builder = reqwest::ClientBuilder::new()
109+
.user_agent(USER_AGENT)
110+
.timeout(self.timeout);
111+
match self.proxy_url {
112+
None => builder.build().map_err(Error::http)?,
113+
Some(proxy_url) => {
114+
let proxy = if self.url.0.is_secure() {
115+
Proxy::https(reqwest::Url::from(proxy_url.0))
116+
.map_err(Error::invalid_proxy)?
117+
} else {
118+
Proxy::http(reqwest::Url::from(proxy_url.0))
119+
.map_err(Error::invalid_proxy)?
120+
};
121+
builder.proxy(proxy).build().map_err(Error::http)?
122+
},
123+
}
111124
};
125+
112126
Ok(HttpClient {
113127
inner,
114128
url: self.url.into(),
@@ -118,6 +132,13 @@ impl Builder {
118132
}
119133

120134
impl HttpClient {
135+
/// Construct a new Tendermint RPC HTTP/S client connecting to the given
136+
/// URL. This avoids using the `Builder` and thus does not perform any
137+
/// validation of the configuration.
138+
pub fn new_from_parts(inner: reqwest::Client, url: reqwest::Url, compat: CompatMode) -> Self {
139+
Self { inner, url, compat }
140+
}
141+
121142
/// Construct a new Tendermint RPC HTTP/S client connecting to the given
122143
/// URL.
123144
pub fn new<U>(url: U) -> Result<Self, Error>

0 commit comments

Comments
 (0)