daml_grpc/service/
daml_event_query_service.rs1use std::convert::TryFrom;
2use std::fmt::Debug;
3
4use tonic::transport::Channel;
5use tracing::{instrument, trace};
6
7use crate::data::DamlResult;
8use crate::data::event_query::DamlEventsByContractId;
9use crate::data::filter::DamlEventFormat;
10use crate::grpc_protobuf::com::daml::ledger::api::v2::GetEventsByContractIdRequest;
11use crate::grpc_protobuf::com::daml::ledger::api::v2::event_query_service_client::EventQueryServiceClient;
12use crate::service::common::make_request;
13
14#[derive(Debug)]
24pub struct DamlEventQueryService<'a> {
25 channel: Channel,
26 auth_token: Option<&'a str>,
27}
28
29impl<'a> DamlEventQueryService<'a> {
30 pub fn new(channel: Channel, auth_token: Option<&'a str>) -> Self {
31 Self {
32 channel,
33 auth_token,
34 }
35 }
36
37 pub fn with_token(self, auth_token: &'a str) -> Self {
39 Self {
40 auth_token: Some(auth_token),
41 ..self
42 }
43 }
44
45 #[instrument(skip(self))]
53 pub async fn get_events_by_contract_id(
54 &self,
55 contract_id: impl Into<String> + Debug,
56 event_format: DamlEventFormat,
57 ) -> DamlResult<DamlEventsByContractId> {
58 let payload = GetEventsByContractIdRequest {
59 contract_id: contract_id.into(),
60 event_format: Some(event_format.into()),
61 };
62 trace!(payload = ?payload, token = ?self.auth_token);
63 let response =
64 self.client().get_events_by_contract_id(make_request(payload, self.auth_token)?).await?.into_inner();
65 trace!(?response);
66 DamlEventsByContractId::try_from(response)
67 }
68
69 fn client(&self) -> EventQueryServiceClient<Channel> {
70 EventQueryServiceClient::new(self.channel.clone())
71 }
72}