use std::convert::TryFrom;
use std::fmt::Debug;
use tonic::transport::Channel;
use tracing::{instrument, trace};
use crate::data::DamlResult;
use crate::data::inspection::{DamlCommandState, DamlCommandStatus};
use crate::grpc_protobuf::com::daml::ledger::api::v2::admin::command_inspection_service_client::CommandInspectionServiceClient;
use crate::grpc_protobuf::com::daml::ledger::api::v2::admin::{CommandState, GetCommandStatusRequest};
use crate::service::common::make_request;
#[derive(Debug)]
pub struct DamlCommandInspectionService<'a> {
channel: Channel,
auth_token: Option<&'a str>,
}
impl<'a> DamlCommandInspectionService<'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_command_status(
&self,
command_id_prefix: impl Into<String> + Debug,
state: DamlCommandState,
limit: u32,
) -> DamlResult<Vec<DamlCommandStatus>> {
let payload = GetCommandStatusRequest {
command_id_prefix: command_id_prefix.into(),
state: CommandState::from(state) as i32,
limit,
};
trace!(payload = ?payload, token = ?self.auth_token);
let response = self.client().get_command_status(make_request(payload, self.auth_token)?).await?.into_inner();
trace!(?response);
response.command_status.into_iter().map(DamlCommandStatus::try_from).collect()
}
fn client(&self) -> CommandInspectionServiceClient<Channel> {
CommandInspectionServiceClient::new(self.channel.clone())
}
}