@@ -62,6 +62,7 @@ pub struct Builder {
62
62
compat : CompatMode ,
63
63
proxy_url : Option < HttpClientUrl > ,
64
64
timeout : Duration ,
65
+ client : Option < reqwest:: Client > ,
65
66
}
66
67
67
68
impl Builder {
@@ -93,22 +94,35 @@ impl Builder {
93
94
self
94
95
}
95
96
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
+
96
103
/// Try to create a client with the options specified for this builder.
97
104
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
+ }
111
124
} ;
125
+
112
126
Ok ( HttpClient {
113
127
inner,
114
128
url : self . url . into ( ) ,
@@ -118,6 +132,13 @@ impl Builder {
118
132
}
119
133
120
134
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
+
121
142
/// Construct a new Tendermint RPC HTTP/S client connecting to the given
122
143
/// URL.
123
144
pub fn new < U > ( url : U ) -> Result < Self , Error >
0 commit comments