use std::convert::TryFrom;
use std::fmt::Debug;
use tonic::transport::Channel;
use tracing::{instrument, trace};
use crate::data::reassignment::DamlReassignmentCommands;
use crate::data::{DamlCommands, DamlResult};
use crate::grpc_protobuf::com::daml::ledger::api::v2::command_submission_service_client::CommandSubmissionServiceClient;
use crate::grpc_protobuf::com::daml::ledger::api::v2::{
Commands, ReassignmentCommands, SubmitReassignmentRequest, SubmitRequest,
};
use crate::service::common::make_request;
#[derive(Debug)]
pub struct DamlCommandSubmissionService<'a> {
channel: Channel,
auth_token: Option<&'a str>,
}
impl<'a> DamlCommandSubmissionService<'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 submit_request(&self, commands: impl Into<DamlCommands> + Debug) -> DamlResult<String> {
let commands = commands.into();
let command_id = commands.command_id.clone();
let payload = SubmitRequest {
commands: Some(Commands::try_from(commands)?),
};
trace!(payload = ?payload, token = ?self.auth_token);
self.client().submit(make_request(payload, self.auth_token)?).await?;
trace!(?command_id);
Ok(command_id)
}
#[instrument(skip(self))]
pub async fn submit_reassignment(
&self,
commands: impl Into<DamlReassignmentCommands> + Debug,
) -> DamlResult<String> {
let commands = commands.into();
let command_id = commands.command_id.clone();
let payload = SubmitReassignmentRequest {
reassignment_commands: Some(ReassignmentCommands::from(commands)),
};
trace!(payload = ?payload, token = ?self.auth_token);
self.client().submit_reassignment(make_request(payload, self.auth_token)?).await?;
trace!(?command_id);
Ok(command_id)
}
fn client(&self) -> CommandSubmissionServiceClient<Channel> {
CommandSubmissionServiceClient::new(self.channel.clone())
}
}