@@ -221,10 +221,13 @@ impl Drop for Lock<'_> {
221
221
}
222
222
}
223
223
224
+ /// Type alias for a Hyper client using a hyper_rusttls HttpsConnector
225
+ pub type HttpsClient = hyper:: Client < hyper_rustls:: HttpsConnector < HttpConnector > , Body > ;
226
+
224
227
#[ derive( Debug ) ]
225
228
/// This struct defines the consul client and allows access to the consul api via method syntax.
226
229
pub struct Consul {
227
- https_client : hyper :: Client < hyper_rustls :: HttpsConnector < HttpConnector > , Body > ,
230
+ https_client : HttpsClient ,
228
231
config : Config ,
229
232
#[ cfg( feature = "trace" ) ]
230
233
tracer : BoxedTracer ,
@@ -246,14 +249,54 @@ fn https_connector() -> hyper_rustls::HttpsConnector<HttpConnector> {
246
249
. build ( )
247
250
}
248
251
252
+ /// This struct defines a builder for the consul client
253
+ /// This allows a Consul client to be built using a custom HTTPS client
254
+ pub struct ConsulBuilder {
255
+ config : Config ,
256
+ https_client : Option < HttpsClient > ,
257
+ }
258
+
259
+ impl ConsulBuilder {
260
+ /// Creates a new instance of [`ConsulBuilder`](consul::ConsulBuilder)
261
+ pub fn new ( config : Config ) -> Self {
262
+ Self { config, https_client : None }
263
+ }
264
+
265
+ /// Sets the HTTPS client to be used when building an instance of [`Consul`](consul::Consul).
266
+ /// #Arguments:
267
+ /// - [HttpsClient](consul::HttpsClient)
268
+ pub fn with_https_client ( mut self , https_client : HttpsClient ) -> Self {
269
+ self . https_client = Some ( https_client) ;
270
+ self
271
+ }
272
+
273
+ /// Creates a new instance of [`Consul`](consul::Consul) using the supplied HTTPS client (if any).
274
+ pub fn build ( self ) -> Consul {
275
+ let https_client = self . https_client . unwrap_or_else ( || {
276
+ let https = https_connector ( ) ;
277
+ self . config . hyper_builder . build :: < _ , Body > ( https)
278
+ } ) ;
279
+
280
+ Consul :: new_with_client ( self . config , https_client)
281
+ }
282
+ }
283
+
249
284
impl Consul {
250
285
/// Creates a new instance of [`Consul`](consul::Consul).
251
286
/// This is the entry point for this crate.
252
287
/// #Arguments:
253
288
/// - [Config](consul::Config)
254
289
pub fn new ( config : Config ) -> Self {
255
- let https = https_connector ( ) ;
256
- let https_client = config. hyper_builder . build :: < _ , hyper:: Body > ( https) ;
290
+ ConsulBuilder :: new ( config)
291
+ . build ( )
292
+ }
293
+
294
+ /// Creates a new instance of [`Consul`](consul::Consul) using the supplied HTTPS client.
295
+ /// This is the entry point for this crate.
296
+ /// #Arguments:
297
+ /// - [Config](consul::Config)
298
+ /// - [HttpsClient](consul::HttpsClient)
299
+ pub fn new_with_client ( config : Config , https_client : HttpsClient ) -> Self {
257
300
Consul {
258
301
https_client,
259
302
config,
0 commit comments