use std::convert::TryFrom;
use std::fmt::Debug;
use tonic::transport::Channel;
use tracing::{instrument, trace};
use crate::data::DamlResult;
use crate::data::event_query::DamlEventsByContractId;
use crate::data::filter::DamlEventFormat;
use crate::grpc_protobuf::com::daml::ledger::api::v2::GetEventsByContractIdRequest;
use crate::grpc_protobuf::com::daml::ledger::api::v2::event_query_service_client::EventQueryServiceClient;
use crate::service::common::make_request;
#[derive(Debug)]
pub struct DamlEventQueryService<'a> {
channel: Channel,
auth_token: Option<&'a str>,
}
impl<'a> DamlEventQueryService<'a> {
pub fn new(channel: Channel, auth_token: Option<&'a str>) -> Self {
Self {
channel,
auth_token,
}
}
pub fn with_token(self, auth_token: &'a str) -> Self {
Self {
auth_token: Some(auth_token),
..self
}
}
#[instrument(skip(self))]
pub async fn get_events_by_contract_id(
&self,
contract_id: impl Into<String> + Debug,
event_format: DamlEventFormat,
) -> DamlResult<DamlEventsByContractId> {
let payload = GetEventsByContractIdRequest {
contract_id: contract_id.into(),
event_format: Some(event_format.into()),
};
trace!(payload = ?payload, token = ?self.auth_token);
let response =
self.client().get_events_by_contract_id(make_request(payload, self.auth_token)?).await?.into_inner();
trace!(?response);
DamlEventsByContractId::try_from(response)
}
fn client(&self) -> EventQueryServiceClient<Channel> {
EventQueryServiceClient::new(self.channel.clone())
}
}