daml_grpc/service/
daml_ledger_configuration_service.rs

1use std::convert::TryFrom;
2
3use futures::stream::StreamExt;
4use futures::Stream;
5use tonic::transport::Channel;
6use tracing::{instrument, trace};
7
8use crate::data::DamlError;
9use crate::data::DamlLedgerConfiguration;
10use crate::data::DamlResult;
11use crate::grpc_protobuf::com::daml::ledger::api::v1::ledger_configuration_service_client::LedgerConfigurationServiceClient;
12use crate::grpc_protobuf::com::daml::ledger::api::v1::GetLedgerConfigurationRequest;
13use crate::service::common::make_request;
14use crate::util::Required;
15
16/// Subscribe to configuration changes of a Daml ledger.
17#[derive(Debug)]
18pub struct DamlLedgerConfigurationService<'a> {
19    channel: Channel,
20    ledger_id: &'a str,
21    auth_token: Option<&'a str>,
22}
23
24impl<'a> DamlLedgerConfigurationService<'a> {
25    pub fn new(channel: Channel, ledger_id: &'a str, auth_token: Option<&'a str>) -> Self {
26        Self {
27            channel,
28            ledger_id,
29            auth_token,
30        }
31    }
32
33    /// Override the JWT token to use for this service.
34    pub fn with_token(self, auth_token: &'a str) -> Self {
35        Self {
36            auth_token: Some(auth_token),
37            ..self
38        }
39    }
40
41    /// Override the ledger id to use for this service.
42    pub fn with_ledger_id(self, ledger_id: &'a str) -> Self {
43        Self {
44            ledger_id,
45            ..self
46        }
47    }
48
49    /// DOCME fully document this
50    #[instrument(skip(self))]
51    pub async fn get_ledger_configuration(
52        &self,
53    ) -> DamlResult<impl Stream<Item = DamlResult<DamlLedgerConfiguration>>> {
54        let payload = GetLedgerConfigurationRequest {
55            ledger_id: self.ledger_id.to_string(),
56        };
57        trace!(payload = ?payload, token = ?self.auth_token);
58        let config_stream =
59            self.client().get_ledger_configuration(make_request(payload, self.auth_token)?).await?.into_inner();
60        Ok(config_stream.inspect(|response| trace!(?response)).map(|item| match item {
61            Ok(config) => DamlLedgerConfiguration::try_from(config.ledger_configuration.req()?),
62            Err(e) => Err(DamlError::from(e)),
63        }))
64    }
65
66    fn client(&self) -> LedgerConfigurationServiceClient<Channel> {
67        LedgerConfigurationServiceClient::new(self.channel.clone())
68    }
69}