Skip to content

Commit c74fb50

Browse files
committed
feat(aggregator-discovery): implement aggregator discovery in client builder
1 parent f620d4d commit c74fb50

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

mithril-client/src/client.rs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use anyhow::{Context, anyhow};
22
#[cfg(feature = "fs")]
33
use chrono::Utc;
44
use mithril_aggregator_discovery::{
5-
AggregatorDiscoverer, HttpConfigAggregatorDiscoverer, MithrilNetwork,
5+
AggregatorDiscoverer, CapableAggregatorDiscoverer, HttpConfigAggregatorDiscoverer,
6+
MithrilNetwork,
67
};
78
use mithril_common::messages::AggregatorCapabilities;
89
use reqwest::Url;
@@ -258,6 +259,13 @@ impl ClientBuilder {
258259
self
259260
}
260261

262+
/// Sets the default aggregator discoverer to use to find the aggregator endpoint when in automatic discovery.
263+
pub fn with_default_aggregator_discoverer(mut self) -> ClientBuilder {
264+
self.aggregator_discoverer = Some(Arc::new(HttpConfigAggregatorDiscoverer::default()));
265+
266+
self
267+
}
268+
261269
/// Returns a `Client` that uses the dependencies provided to this `ClientBuilder`.
262270
///
263271
/// The builder will try to create the missing dependencies using default implementations
@@ -390,18 +398,31 @@ impl ClientBuilder {
390398
) -> Result<AggregatorHTTPClient, anyhow::Error> {
391399
let aggregator_endpoint = match self.aggregator_discovery {
392400
AggregatorDiscoveryType::Url(ref url) => url.clone(),
393-
AggregatorDiscoveryType::Automatic(ref network) => match &self.aggregator_discoverer {
394-
Some(discoverer) => discoverer
395-
.get_available_aggregators(network.to_owned())
396-
.await
397-
.with_context(|| "Discovering aggregator endpoint failed")?
398-
.next()
399-
.unwrap()
400-
.into(),
401-
None => {
402-
return Err(anyhow!("The aggregator discoverer must be provided to build the client with automatic discovery using the 'with_aggregator_discoverer' function").into());
401+
AggregatorDiscoveryType::Automatic(ref network) => {
402+
match self.aggregator_discoverer.clone() {
403+
Some(discoverer) => {
404+
let discoverer = if let Some(capabilities) = &self.aggregator_capabilities {
405+
Arc::new(CapableAggregatorDiscoverer::new(
406+
capabilities.to_owned(),
407+
discoverer.clone(),
408+
)) as Arc<dyn AggregatorDiscoverer>
409+
} else {
410+
discoverer as Arc<dyn AggregatorDiscoverer>
411+
};
412+
413+
discoverer
414+
.get_available_aggregators(network.to_owned())
415+
.await
416+
.with_context(|| "Discovering aggregator endpoint failed")?
417+
.next()
418+
.ok_or(anyhow!("No aggregator was available through discovery"))?
419+
.into()
420+
}
421+
None => {
422+
return Err(anyhow!("The aggregator discoverer must be provided to build the client with automatic discovery using the 'with_aggregator_discoverer' function").into());
423+
}
403424
}
404-
},
425+
}
405426
};
406427
let endpoint_url = Url::parse(&aggregator_endpoint).with_context(|| {
407428
format!("Invalid aggregator endpoint, it must be a correctly formed url: '{aggregator_endpoint}'")

0 commit comments

Comments
 (0)