mithril_aggregator_discovery/
model.rs

1use std::time::Duration;
2
3use serde::Serialize;
4
5use mithril_aggregator_client::{AggregatorHttpClient, query::GetAggregatorFeaturesQuery};
6use mithril_common::{StdResult, messages::AggregatorCapabilities};
7
8/// Representation of an aggregator endpoint
9#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
10pub struct AggregatorEndpoint {
11    url: String,
12}
13
14impl AggregatorEndpoint {
15    const HTTP_TIMEOUT: Duration = Duration::from_secs(5);
16
17    /// Create a new AggregatorEndpoint instance
18    pub fn new(url: String) -> Self {
19        Self { url }
20    }
21
22    /// Retrieve the capabilities of the aggregator
23    pub async fn retrieve_capabilities(&self) -> StdResult<AggregatorCapabilities> {
24        let aggregator_client = AggregatorHttpClient::builder(self.url.clone())
25            .with_timeout(Self::HTTP_TIMEOUT)
26            .build()?;
27
28        Ok(aggregator_client
29            .send(GetAggregatorFeaturesQuery::current())
30            .await?
31            .capabilities)
32    }
33}
34
35impl From<AggregatorEndpoint> for String {
36    fn from(endpoint: AggregatorEndpoint) -> Self {
37        endpoint.url
38    }
39}
40
41impl std::fmt::Display for AggregatorEndpoint {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        write!(f, "{}", self.url)
44    }
45}