Skip to content

Commit 6645745

Browse files
authored
Merge pull request #45 from LeonHartley/main
Add `ConsulBuilder` allowing a consul client to be built using a custom HTTP connector, fixes #44
2 parents c756d9b + eb7bb02 commit 6645745

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

src/lib.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,13 @@ impl Drop for Lock<'_> {
221221
}
222222
}
223223

224+
/// Type alias for a Hyper client using a hyper_rusttls HttpsConnector
225+
pub type HttpsClient = hyper::Client<hyper_rustls::HttpsConnector<HttpConnector>, Body>;
226+
224227
#[derive(Debug)]
225228
/// This struct defines the consul client and allows access to the consul api via method syntax.
226229
pub struct Consul {
227-
https_client: hyper::Client<hyper_rustls::HttpsConnector<HttpConnector>, Body>,
230+
https_client: HttpsClient,
228231
config: Config,
229232
#[cfg(feature = "trace")]
230233
tracer: BoxedTracer,
@@ -246,14 +249,54 @@ fn https_connector() -> hyper_rustls::HttpsConnector<HttpConnector> {
246249
.build()
247250
}
248251

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+
249284
impl Consul {
250285
/// Creates a new instance of [`Consul`](consul::Consul).
251286
/// This is the entry point for this crate.
252287
/// #Arguments:
253288
/// - [Config](consul::Config)
254289
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 {
257300
Consul {
258301
https_client,
259302
config,

0 commit comments

Comments
 (0)