use std::fmt::Debug;
use chrono::{DateTime, Utc};
use tonic::transport::Channel;
use tracing::{instrument, trace};
use crate::data::DamlResult;
use crate::grpc_protobuf::com::daml::ledger::api::v2::testing::time_service_client::TimeServiceClient;
use crate::grpc_protobuf::com::daml::ledger::api::v2::testing::{GetTimeRequest, SetTimeRequest};
use crate::service::common::make_request;
use crate::util;
use crate::util::Required;
#[derive(Debug)]
pub struct DamlTimeService<'a> {
channel: Channel,
auth_token: Option<&'a str>,
}
impl<'a> DamlTimeService<'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_time(&self) -> DamlResult<DateTime<Utc>> {
let payload = GetTimeRequest {};
trace!(payload = ?payload, token = ?self.auth_token);
let response = self.client().get_time(make_request(payload, self.auth_token)?).await?.into_inner();
trace!(?response);
util::from_grpc_timestamp(&response.current_time.req()?)
}
#[instrument(skip(self))]
pub async fn set_time(
&self,
current_time: impl Into<DateTime<Utc>> + Debug,
new_time: impl Into<DateTime<Utc>> + Debug,
) -> DamlResult<()> {
let payload = SetTimeRequest {
current_time: Some(util::to_grpc_timestamp(current_time.into())?),
new_time: Some(util::to_grpc_timestamp(new_time.into())?),
};
trace!(payload = ?payload, token = ?self.auth_token);
self.client().set_time(make_request(payload, self.auth_token)?).await?;
Ok(())
}
fn client(&self) -> TimeServiceClient<Channel> {
TimeServiceClient::new(self.channel.clone())
}
}