use std::time::Duration;
use mssf_com::{
FabricClient::IFabricInfrastructureServiceClient, FabricCommon::IFabricStringResult,
};
use crate::{
WString, runtime::executor::BoxedCancelToken, strings::StringResult,
sync::fabric_begin_end_proxy, types::Uri,
};
#[derive(Debug, Clone)]
pub struct InfrastructureServiceClient {
com: IFabricInfrastructureServiceClient,
}
impl From<IFabricInfrastructureServiceClient> for InfrastructureServiceClient {
fn from(value: IFabricInfrastructureServiceClient) -> Self {
Self { com: value }
}
}
impl From<InfrastructureServiceClient> for IFabricInfrastructureServiceClient {
fn from(value: InfrastructureServiceClient) -> Self {
value.com
}
}
impl InfrastructureServiceClient {
pub async fn invoke_infrastructure_query(
&self,
service_name: &Uri,
command: &WString,
timeout: Duration,
cancellation_token: Option<BoxedCancelToken>,
) -> crate::Result<WString> {
let com1 = &self.com;
let com2 = self.com.clone();
let result: IFabricStringResult = fabric_begin_end_proxy(
move |callback| unsafe {
com1.BeginInvokeInfrastructureQuery(
service_name.as_raw(),
command.as_pcwstr(),
timeout.as_millis().try_into()?,
callback,
)
},
move |context| unsafe { com2.EndInvokeInfrastructureQuery(context) },
cancellation_token,
)
.await??;
Ok(StringResult::from(&result).into_inner())
}
}