Issue #44: implement Rust client session values errors and CLI
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
use std::fmt;
|
||||
|
||||
use tonic::metadata::MetadataValue;
|
||||
use tonic::service::Interceptor;
|
||||
use tonic::{Request, Status};
|
||||
|
||||
/// API key wrapper that avoids exposing raw credentials in formatted output.
|
||||
#[derive(Clone, Eq, PartialEq)]
|
||||
pub struct ApiKey(String);
|
||||
@@ -28,3 +32,56 @@ impl fmt::Display for ApiKey {
|
||||
formatter.write_str("<redacted>")
|
||||
}
|
||||
}
|
||||
|
||||
/// `tonic` interceptor that attaches gateway API key metadata.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct AuthInterceptor {
|
||||
api_key: Option<ApiKey>,
|
||||
}
|
||||
|
||||
impl AuthInterceptor {
|
||||
pub fn new(api_key: Option<ApiKey>) -> Self {
|
||||
Self { api_key }
|
||||
}
|
||||
}
|
||||
|
||||
impl Interceptor for AuthInterceptor {
|
||||
fn call(&mut self, mut request: Request<()>) -> Result<Request<()>, Status> {
|
||||
if let Some(api_key) = &self.api_key {
|
||||
let header_value = format!("Bearer {}", api_key.expose_secret())
|
||||
.parse::<MetadataValue<_>>()
|
||||
.map_err(|_| Status::unauthenticated("invalid API key metadata"))?;
|
||||
request.metadata_mut().insert("authorization", header_value);
|
||||
}
|
||||
|
||||
Ok(request)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use tonic::service::Interceptor;
|
||||
use tonic::Request;
|
||||
|
||||
use super::{ApiKey, AuthInterceptor};
|
||||
|
||||
#[test]
|
||||
fn api_key_debug_is_redacted() {
|
||||
let key = ApiKey::new("mxgw_visible_secret");
|
||||
|
||||
assert_eq!(format!("{key:?}"), "ApiKey(\"<redacted>\")");
|
||||
assert!(!format!("{key:?}").contains("visible_secret"));
|
||||
assert_eq!(key.to_string(), "<redacted>");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn interceptor_attaches_bearer_metadata() {
|
||||
let mut interceptor = AuthInterceptor::new(Some(ApiKey::new("mxgw_fixture_secret")));
|
||||
let request = interceptor.call(Request::new(())).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
request.metadata().get("authorization").unwrap(),
|
||||
"Bearer mxgw_fixture_secret"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+103
-10
@@ -1,30 +1,123 @@
|
||||
use tonic::transport::Channel;
|
||||
use std::fs;
|
||||
|
||||
use crate::error::Error;
|
||||
use tonic::codegen::InterceptedService;
|
||||
use tonic::transport::{Certificate, Channel, ClientTlsConfig};
|
||||
use tonic::Request;
|
||||
|
||||
use crate::auth::AuthInterceptor;
|
||||
use crate::error::{ensure_command_success, Error};
|
||||
use crate::generated::mxaccess_gateway::v1::mx_access_gateway_client::MxAccessGatewayClient;
|
||||
use crate::generated::mxaccess_gateway::v1::{
|
||||
CloseSessionReply, CloseSessionRequest, MxCommandReply, MxCommandRequest, MxEvent,
|
||||
OpenSessionReply, OpenSessionRequest, StreamEventsRequest,
|
||||
};
|
||||
use crate::options::ClientOptions;
|
||||
use crate::session::Session;
|
||||
|
||||
pub type RawGatewayClient = MxAccessGatewayClient<InterceptedService<Channel, AuthInterceptor>>;
|
||||
pub type EventStream =
|
||||
std::pin::Pin<Box<dyn futures_core::Stream<Item = Result<MxEvent, Error>> + Send + 'static>>;
|
||||
|
||||
/// Thin owner for the generated gateway client.
|
||||
#[derive(Clone)]
|
||||
pub struct GatewayClient {
|
||||
inner: MxAccessGatewayClient<Channel>,
|
||||
inner: RawGatewayClient,
|
||||
call_timeout: std::time::Duration,
|
||||
}
|
||||
|
||||
impl GatewayClient {
|
||||
pub async fn connect(options: ClientOptions) -> Result<Self, Error> {
|
||||
let endpoint = Channel::from_shared(options.endpoint().to_owned()).map_err(|source| {
|
||||
Error::InvalidEndpoint {
|
||||
endpoint: options.endpoint().to_owned(),
|
||||
detail: source.to_string(),
|
||||
let mut endpoint =
|
||||
Channel::from_shared(options.endpoint().to_owned()).map_err(|source| {
|
||||
Error::InvalidEndpoint {
|
||||
endpoint: options.endpoint().to_owned(),
|
||||
detail: source.to_string(),
|
||||
}
|
||||
})?;
|
||||
endpoint = endpoint.connect_timeout(options.connect_timeout());
|
||||
|
||||
if !options.plaintext() {
|
||||
let mut tls = ClientTlsConfig::new();
|
||||
if let Some(server_name) = options.server_name_override() {
|
||||
tls = tls.domain_name(server_name.to_owned());
|
||||
}
|
||||
})?;
|
||||
if let Some(ca_file) = options.ca_file() {
|
||||
let certificate = fs::read(ca_file).map_err(|source| Error::InvalidEndpoint {
|
||||
endpoint: options.endpoint().to_owned(),
|
||||
detail: format!("failed to read CA file {}: {source}", ca_file.display()),
|
||||
})?;
|
||||
tls = tls.ca_certificate(Certificate::from_pem(certificate));
|
||||
}
|
||||
endpoint = endpoint.tls_config(tls)?;
|
||||
}
|
||||
|
||||
let channel = endpoint.connect().await?;
|
||||
let interceptor = AuthInterceptor::new(options.api_key().cloned());
|
||||
|
||||
Ok(Self {
|
||||
inner: MxAccessGatewayClient::new(channel),
|
||||
inner: MxAccessGatewayClient::with_interceptor(channel, interceptor),
|
||||
call_timeout: options.call_timeout(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> MxAccessGatewayClient<Channel> {
|
||||
pub fn raw_client(&mut self) -> &mut RawGatewayClient {
|
||||
&mut self.inner
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> RawGatewayClient {
|
||||
self.inner
|
||||
}
|
||||
|
||||
pub fn session(&self, session_id: impl Into<String>) -> Session {
|
||||
Session::new(session_id, self.clone())
|
||||
}
|
||||
|
||||
pub async fn open_session_raw(
|
||||
&self,
|
||||
request: OpenSessionRequest,
|
||||
) -> Result<OpenSessionReply, Error> {
|
||||
let mut client = self.inner.clone();
|
||||
let response = client.open_session(self.unary_request(request)).await?;
|
||||
Ok(response.into_inner())
|
||||
}
|
||||
|
||||
pub async fn open_session(&self, request: OpenSessionRequest) -> Result<Session, Error> {
|
||||
let reply = self.open_session_raw(request).await?;
|
||||
Ok(Session::new(reply.session_id, self.clone()))
|
||||
}
|
||||
|
||||
pub async fn close_session_raw(
|
||||
&self,
|
||||
request: CloseSessionRequest,
|
||||
) -> Result<CloseSessionReply, Error> {
|
||||
let mut client = self.inner.clone();
|
||||
let response = client.close_session(self.unary_request(request)).await?;
|
||||
Ok(response.into_inner())
|
||||
}
|
||||
|
||||
pub async fn invoke_raw(&self, request: MxCommandRequest) -> Result<MxCommandReply, Error> {
|
||||
let mut client = self.inner.clone();
|
||||
let response = client.invoke(self.unary_request(request)).await?;
|
||||
Ok(response.into_inner())
|
||||
}
|
||||
|
||||
pub async fn invoke(&self, request: MxCommandRequest) -> Result<MxCommandReply, Error> {
|
||||
ensure_command_success(self.invoke_raw(request).await?)
|
||||
}
|
||||
|
||||
pub async fn stream_events(&self, request: StreamEventsRequest) -> Result<EventStream, Error> {
|
||||
let mut client = self.inner.clone();
|
||||
let response = client.stream_events(self.unary_request(request)).await?;
|
||||
let stream = futures_util::StreamExt::map(response.into_inner(), |result| {
|
||||
result.map_err(Error::from)
|
||||
});
|
||||
|
||||
Ok(Box::pin(stream))
|
||||
}
|
||||
|
||||
fn unary_request<T>(&self, message: T) -> Request<T> {
|
||||
let mut request = Request::new(message);
|
||||
request.set_timeout(self.call_timeout);
|
||||
request
|
||||
}
|
||||
}
|
||||
|
||||
+149
-1
@@ -1,13 +1,161 @@
|
||||
use thiserror::Error as ThisError;
|
||||
use tonic::Code;
|
||||
|
||||
use crate::generated::mxaccess_gateway::v1::{MxCommandReply, ProtocolStatusCode};
|
||||
|
||||
#[derive(Debug, ThisError)]
|
||||
pub enum Error {
|
||||
#[error("invalid gateway endpoint `{endpoint}`: {detail}")]
|
||||
InvalidEndpoint { endpoint: String, detail: String },
|
||||
|
||||
#[error("invalid argument `{name}`: {detail}")]
|
||||
InvalidArgument { name: String, detail: String },
|
||||
|
||||
#[error("gateway transport error: {0}")]
|
||||
Transport(#[from] tonic::transport::Error),
|
||||
|
||||
#[error("authentication failed: {message}")]
|
||||
Authentication {
|
||||
message: String,
|
||||
#[source]
|
||||
status: Box<tonic::Status>,
|
||||
},
|
||||
|
||||
#[error("authorization failed: {message}")]
|
||||
Authorization {
|
||||
message: String,
|
||||
#[source]
|
||||
status: Box<tonic::Status>,
|
||||
},
|
||||
|
||||
#[error("gateway call timed out: {message}")]
|
||||
Timeout {
|
||||
message: String,
|
||||
#[source]
|
||||
status: Box<tonic::Status>,
|
||||
},
|
||||
|
||||
#[error("gateway call cancelled: {message}")]
|
||||
Cancelled {
|
||||
message: String,
|
||||
#[source]
|
||||
status: Box<tonic::Status>,
|
||||
},
|
||||
|
||||
#[error("gateway status error: {0}")]
|
||||
Status(#[from] tonic::Status),
|
||||
Status(Box<tonic::Status>),
|
||||
|
||||
#[error("gateway command failed: {0}")]
|
||||
Command(#[from] Box<CommandError>),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct CommandError {
|
||||
reply: MxCommandReply,
|
||||
}
|
||||
|
||||
impl CommandError {
|
||||
pub fn new(reply: MxCommandReply) -> Self {
|
||||
Self { reply }
|
||||
}
|
||||
|
||||
pub fn reply(&self) -> &MxCommandReply {
|
||||
&self.reply
|
||||
}
|
||||
|
||||
pub fn into_reply(self) -> MxCommandReply {
|
||||
self.reply
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for CommandError {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let status = self.reply.protocol_status.as_ref();
|
||||
let code = status
|
||||
.and_then(|status| ProtocolStatusCode::try_from(status.code).ok())
|
||||
.unwrap_or(ProtocolStatusCode::Unspecified);
|
||||
let message = status.map(|status| status.message.as_str()).unwrap_or("");
|
||||
|
||||
if message.is_empty() {
|
||||
write!(formatter, "{code:?}")
|
||||
} else {
|
||||
write!(formatter, "{code:?}: {message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for CommandError {}
|
||||
|
||||
impl From<tonic::Status> for Error {
|
||||
fn from(status: tonic::Status) -> Self {
|
||||
let message = redact_credentials(status.message());
|
||||
match status.code() {
|
||||
Code::Unauthenticated => Self::Authentication {
|
||||
message,
|
||||
status: Box::new(status),
|
||||
},
|
||||
Code::PermissionDenied => Self::Authorization {
|
||||
message,
|
||||
status: Box::new(status),
|
||||
},
|
||||
Code::DeadlineExceeded => Self::Timeout {
|
||||
message,
|
||||
status: Box::new(status),
|
||||
},
|
||||
Code::Cancelled => Self::Cancelled {
|
||||
message,
|
||||
status: Box::new(status),
|
||||
},
|
||||
_ => Self::Status(Box::new(status)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ensure_command_success(reply: MxCommandReply) -> Result<MxCommandReply, Error> {
|
||||
let code = reply
|
||||
.protocol_status
|
||||
.as_ref()
|
||||
.and_then(|status| ProtocolStatusCode::try_from(status.code).ok())
|
||||
.unwrap_or(ProtocolStatusCode::Unspecified);
|
||||
|
||||
if code == ProtocolStatusCode::Ok {
|
||||
Ok(reply)
|
||||
} else {
|
||||
Err(Box::new(CommandError::new(reply)).into())
|
||||
}
|
||||
}
|
||||
|
||||
fn redact_credentials(message: &str) -> String {
|
||||
message
|
||||
.split_whitespace()
|
||||
.map(|part| {
|
||||
if part.starts_with("mxgw_") || part.eq_ignore_ascii_case("bearer") {
|
||||
"<redacted>"
|
||||
} else {
|
||||
part
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use tonic::{Code, Status};
|
||||
|
||||
use super::Error;
|
||||
|
||||
#[test]
|
||||
fn classifies_authentication_status() {
|
||||
let error = Error::from(Status::new(
|
||||
Code::Unauthenticated,
|
||||
"invalid API key mxgw_visible_secret",
|
||||
));
|
||||
|
||||
let message = error.to_string();
|
||||
|
||||
assert!(matches!(error, Error::Authentication { .. }));
|
||||
assert!(message.contains("<redacted>"));
|
||||
assert!(!message.contains("visible_secret"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,10 @@ pub mod session;
|
||||
pub mod value;
|
||||
pub mod version;
|
||||
|
||||
pub use auth::ApiKey;
|
||||
pub use client::GatewayClient;
|
||||
pub use error::Error;
|
||||
pub use auth::{ApiKey, AuthInterceptor};
|
||||
pub use client::{EventStream, GatewayClient};
|
||||
pub use error::{CommandError, Error};
|
||||
pub use options::ClientOptions;
|
||||
pub use session::Session;
|
||||
pub use value::{MxArrayProjection, MxArrayValue, MxStatus, MxValue, MxValueProjection};
|
||||
pub use version::{CLIENT_VERSION, GATEWAY_PROTOCOL_VERSION, WORKER_PROTOCOL_VERSION};
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
use std::fmt;
|
||||
use std::path::PathBuf;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::auth::ApiKey;
|
||||
|
||||
@@ -7,6 +9,10 @@ pub struct ClientOptions {
|
||||
endpoint: String,
|
||||
api_key: Option<ApiKey>,
|
||||
plaintext: bool,
|
||||
ca_file: Option<PathBuf>,
|
||||
server_name_override: Option<String>,
|
||||
connect_timeout: Duration,
|
||||
call_timeout: Duration,
|
||||
}
|
||||
|
||||
impl ClientOptions {
|
||||
@@ -15,6 +21,10 @@ impl ClientOptions {
|
||||
endpoint: endpoint.into(),
|
||||
api_key: None,
|
||||
plaintext: true,
|
||||
ca_file: None,
|
||||
server_name_override: None,
|
||||
connect_timeout: Duration::from_secs(10),
|
||||
call_timeout: Duration::from_secs(30),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +33,31 @@ impl ClientOptions {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_plaintext(mut self, plaintext: bool) -> Self {
|
||||
self.plaintext = plaintext;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_ca_file(mut self, ca_file: impl Into<PathBuf>) -> Self {
|
||||
self.ca_file = Some(ca_file.into());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_server_name_override(mut self, server_name_override: impl Into<String>) -> Self {
|
||||
self.server_name_override = Some(server_name_override.into());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_connect_timeout(mut self, connect_timeout: Duration) -> Self {
|
||||
self.connect_timeout = connect_timeout;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_call_timeout(mut self, call_timeout: Duration) -> Self {
|
||||
self.call_timeout = call_timeout;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn endpoint(&self) -> &str {
|
||||
&self.endpoint
|
||||
}
|
||||
@@ -34,6 +69,22 @@ impl ClientOptions {
|
||||
pub fn plaintext(&self) -> bool {
|
||||
self.plaintext
|
||||
}
|
||||
|
||||
pub fn ca_file(&self) -> Option<&PathBuf> {
|
||||
self.ca_file.as_ref()
|
||||
}
|
||||
|
||||
pub fn server_name_override(&self) -> Option<&str> {
|
||||
self.server_name_override.as_deref()
|
||||
}
|
||||
|
||||
pub fn connect_timeout(&self) -> Duration {
|
||||
self.connect_timeout
|
||||
}
|
||||
|
||||
pub fn call_timeout(&self) -> Duration {
|
||||
self.call_timeout
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ClientOptions {
|
||||
@@ -49,6 +100,27 @@ impl fmt::Debug for ClientOptions {
|
||||
.field("endpoint", &self.endpoint)
|
||||
.field("api_key", &self.api_key.as_ref().map(|_| "<redacted>"))
|
||||
.field("plaintext", &self.plaintext)
|
||||
.field("ca_file", &self.ca_file)
|
||||
.field("server_name_override", &self.server_name_override)
|
||||
.field("connect_timeout", &self.connect_timeout)
|
||||
.field("call_timeout", &self.call_timeout)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::ClientOptions;
|
||||
use crate::auth::ApiKey;
|
||||
|
||||
#[test]
|
||||
fn debug_redacts_api_key() {
|
||||
let options =
|
||||
ClientOptions::new("http://localhost:5000").with_api_key(ApiKey::new("mxgw_secret"));
|
||||
|
||||
let debug = format!("{options:?}");
|
||||
|
||||
assert!(debug.contains("<redacted>"));
|
||||
assert!(!debug.contains("mxgw_secret"));
|
||||
}
|
||||
}
|
||||
|
||||
+222
-3
@@ -1,15 +1,234 @@
|
||||
use crate::client::{EventStream, GatewayClient};
|
||||
use crate::error::Error;
|
||||
use crate::generated::mxaccess_gateway::v1::mx_command::Payload;
|
||||
use crate::generated::mxaccess_gateway::v1::mx_command_reply;
|
||||
use crate::generated::mxaccess_gateway::v1::{
|
||||
AddItem2Command, AddItemCommand, AdviseCommand, CloseSessionRequest, MxCommand, MxCommandKind,
|
||||
MxCommandReply, MxCommandRequest, MxValue as ProtoMxValue, OpenSessionRequest, RegisterCommand,
|
||||
StreamEventsRequest, Write2Command, WriteCommand,
|
||||
};
|
||||
use crate::value::MxValue;
|
||||
|
||||
/// Session identifier returned by the gateway.
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
#[derive(Clone)]
|
||||
pub struct Session {
|
||||
id: String,
|
||||
client: GatewayClient,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
pub fn new(id: impl Into<String>) -> Self {
|
||||
Self { id: id.into() }
|
||||
pub(crate) fn new(id: impl Into<String>, client: GatewayClient) -> Self {
|
||||
Self {
|
||||
id: id.into(),
|
||||
client,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn id(&self) -> &str {
|
||||
&self.id
|
||||
}
|
||||
|
||||
pub async fn open(client: GatewayClient, client_session_name: &str) -> Result<Self, Error> {
|
||||
client
|
||||
.open_session(OpenSessionRequest {
|
||||
client_session_name: client_session_name.to_owned(),
|
||||
..OpenSessionRequest::default()
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn close(&self) -> Result<(), Error> {
|
||||
self.client
|
||||
.close_session_raw(CloseSessionRequest {
|
||||
session_id: self.id.clone(),
|
||||
client_correlation_id: "rust-client-close-session".to_owned(),
|
||||
})
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn register(&self, client_name: &str) -> Result<i32, Error> {
|
||||
let reply = self
|
||||
.invoke(
|
||||
MxCommandKind::Register,
|
||||
Payload::Register(RegisterCommand {
|
||||
client_name: client_name.to_owned(),
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(register_server_handle(&reply))
|
||||
}
|
||||
|
||||
pub async fn add_item(&self, server_handle: i32, item_definition: &str) -> Result<i32, Error> {
|
||||
let reply = self
|
||||
.invoke(
|
||||
MxCommandKind::AddItem,
|
||||
Payload::AddItem(AddItemCommand {
|
||||
server_handle,
|
||||
item_definition: item_definition.to_owned(),
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(add_item_handle(&reply))
|
||||
}
|
||||
|
||||
pub async fn add_item2(
|
||||
&self,
|
||||
server_handle: i32,
|
||||
item_definition: &str,
|
||||
item_context: &str,
|
||||
) -> Result<i32, Error> {
|
||||
let reply = self
|
||||
.invoke(
|
||||
MxCommandKind::AddItem2,
|
||||
Payload::AddItem2(AddItem2Command {
|
||||
server_handle,
|
||||
item_definition: item_definition.to_owned(),
|
||||
item_context: item_context.to_owned(),
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(add_item2_handle(&reply))
|
||||
}
|
||||
|
||||
pub async fn advise(&self, server_handle: i32, item_handle: i32) -> Result<(), Error> {
|
||||
self.invoke(
|
||||
MxCommandKind::Advise,
|
||||
Payload::Advise(AdviseCommand {
|
||||
server_handle,
|
||||
item_handle,
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn write(
|
||||
&self,
|
||||
server_handle: i32,
|
||||
item_handle: i32,
|
||||
value: MxValue,
|
||||
user_id: i32,
|
||||
) -> Result<(), Error> {
|
||||
self.invoke(
|
||||
MxCommandKind::Write,
|
||||
Payload::Write(WriteCommand {
|
||||
server_handle,
|
||||
item_handle,
|
||||
value: Some(value.into_proto()),
|
||||
user_id,
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn write2(
|
||||
&self,
|
||||
server_handle: i32,
|
||||
item_handle: i32,
|
||||
value: MxValue,
|
||||
timestamp_value: MxValue,
|
||||
user_id: i32,
|
||||
) -> Result<(), Error> {
|
||||
self.invoke(
|
||||
MxCommandKind::Write2,
|
||||
Payload::Write2(Write2Command {
|
||||
server_handle,
|
||||
item_handle,
|
||||
value: Some(value.into_proto()),
|
||||
timestamp_value: Some(timestamp_value.into_proto()),
|
||||
user_id,
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn events(&self) -> Result<EventStream, Error> {
|
||||
self.events_after(0).await
|
||||
}
|
||||
|
||||
pub async fn events_after(&self, after_worker_sequence: u64) -> Result<EventStream, Error> {
|
||||
self.client
|
||||
.stream_events(StreamEventsRequest {
|
||||
session_id: self.id.clone(),
|
||||
after_worker_sequence,
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn invoke_raw(
|
||||
&self,
|
||||
kind: MxCommandKind,
|
||||
payload: Payload,
|
||||
) -> Result<MxCommandReply, Error> {
|
||||
self.client
|
||||
.invoke_raw(self.command_request(kind, payload))
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn invoke(
|
||||
&self,
|
||||
kind: MxCommandKind,
|
||||
payload: Payload,
|
||||
) -> Result<MxCommandReply, Error> {
|
||||
self.client
|
||||
.invoke(self.command_request(kind, payload))
|
||||
.await
|
||||
}
|
||||
|
||||
fn command_request(&self, kind: MxCommandKind, payload: Payload) -> MxCommandRequest {
|
||||
MxCommandRequest {
|
||||
session_id: self.id.clone(),
|
||||
client_correlation_id: format!("rust-client-{}", kind.as_str_name()),
|
||||
command: Some(MxCommand {
|
||||
kind: kind as i32,
|
||||
payload: Some(payload),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn register_server_handle(reply: &MxCommandReply) -> i32 {
|
||||
match reply.payload.as_ref() {
|
||||
Some(mx_command_reply::Payload::Register(register)) => register.server_handle,
|
||||
_ => reply
|
||||
.return_value
|
||||
.as_ref()
|
||||
.and_then(int32_reply_value)
|
||||
.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_item_handle(reply: &MxCommandReply) -> i32 {
|
||||
match reply.payload.as_ref() {
|
||||
Some(mx_command_reply::Payload::AddItem(add_item)) => add_item.item_handle,
|
||||
_ => reply
|
||||
.return_value
|
||||
.as_ref()
|
||||
.and_then(int32_reply_value)
|
||||
.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_item2_handle(reply: &MxCommandReply) -> i32 {
|
||||
match reply.payload.as_ref() {
|
||||
Some(mx_command_reply::Payload::AddItem2(add_item)) => add_item.item_handle,
|
||||
_ => reply
|
||||
.return_value
|
||||
.as_ref()
|
||||
.and_then(int32_reply_value)
|
||||
.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn int32_reply_value(value: &ProtoMxValue) -> Option<i32> {
|
||||
match value.kind.as_ref()? {
|
||||
crate::generated::mxaccess_gateway::v1::mx_value::Kind::Int32Value(value) => Some(*value),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
+236
-6
@@ -1,9 +1,239 @@
|
||||
use crate::generated::mxaccess_gateway::v1::MxValue;
|
||||
use crate::generated::mxaccess_gateway::v1::mx_array::Values;
|
||||
use crate::generated::mxaccess_gateway::v1::mx_value::Kind;
|
||||
use crate::generated::mxaccess_gateway::v1::{
|
||||
BoolArray, DoubleArray, FloatArray, Int32Array, Int64Array, MxArray, MxDataType,
|
||||
MxStatusCategory, MxStatusProxy, MxStatusSource, MxValue as ProtoMxValue, RawArray,
|
||||
StringArray, TimestampArray,
|
||||
};
|
||||
|
||||
pub fn int32_value(value: i32) -> MxValue {
|
||||
MxValue {
|
||||
data_type: crate::generated::mxaccess_gateway::v1::MxDataType::Integer as i32,
|
||||
kind: Some(crate::generated::mxaccess_gateway::v1::mx_value::Kind::Int32Value(value)),
|
||||
..MxValue::default()
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct MxValue {
|
||||
raw: ProtoMxValue,
|
||||
projection: MxValueProjection,
|
||||
}
|
||||
|
||||
impl MxValue {
|
||||
pub fn from_proto(raw: ProtoMxValue) -> Self {
|
||||
let projection = MxValueProjection::from_proto(&raw);
|
||||
Self { raw, projection }
|
||||
}
|
||||
|
||||
pub fn bool(value: bool) -> Self {
|
||||
Self::from_proto(ProtoMxValue {
|
||||
data_type: MxDataType::Boolean as i32,
|
||||
variant_type: "VT_BOOL".to_owned(),
|
||||
kind: Some(Kind::BoolValue(value)),
|
||||
..ProtoMxValue::default()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn int32(value: i32) -> Self {
|
||||
Self::from_proto(ProtoMxValue {
|
||||
data_type: MxDataType::Integer as i32,
|
||||
variant_type: "VT_I4".to_owned(),
|
||||
kind: Some(Kind::Int32Value(value)),
|
||||
..ProtoMxValue::default()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn int64(value: i64) -> Self {
|
||||
Self::from_proto(ProtoMxValue {
|
||||
data_type: MxDataType::Integer as i32,
|
||||
variant_type: "VT_I8".to_owned(),
|
||||
kind: Some(Kind::Int64Value(value)),
|
||||
..ProtoMxValue::default()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn float(value: f32) -> Self {
|
||||
Self::from_proto(ProtoMxValue {
|
||||
data_type: MxDataType::Float as i32,
|
||||
variant_type: "VT_R4".to_owned(),
|
||||
kind: Some(Kind::FloatValue(value)),
|
||||
..ProtoMxValue::default()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn double(value: f64) -> Self {
|
||||
Self::from_proto(ProtoMxValue {
|
||||
data_type: MxDataType::Double as i32,
|
||||
variant_type: "VT_R8".to_owned(),
|
||||
kind: Some(Kind::DoubleValue(value)),
|
||||
..ProtoMxValue::default()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn string(value: impl Into<String>) -> Self {
|
||||
Self::from_proto(ProtoMxValue {
|
||||
data_type: MxDataType::String as i32,
|
||||
variant_type: "VT_BSTR".to_owned(),
|
||||
kind: Some(Kind::StringValue(value.into())),
|
||||
..ProtoMxValue::default()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn raw(&self) -> &ProtoMxValue {
|
||||
&self.raw
|
||||
}
|
||||
|
||||
pub fn projection(&self) -> &MxValueProjection {
|
||||
&self.projection
|
||||
}
|
||||
|
||||
pub fn into_proto(self) -> ProtoMxValue {
|
||||
self.raw
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MxValue> for ProtoMxValue {
|
||||
fn from(value: MxValue) -> Self {
|
||||
value.into_proto()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ProtoMxValue> for MxValue {
|
||||
fn from(value: ProtoMxValue) -> Self {
|
||||
Self::from_proto(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum MxValueProjection {
|
||||
Unset,
|
||||
Null,
|
||||
Bool(bool),
|
||||
Int32(i32),
|
||||
Int64(i64),
|
||||
Float(f32),
|
||||
Double(f64),
|
||||
String(String),
|
||||
Timestamp(prost_types::Timestamp),
|
||||
Array(MxArrayValue),
|
||||
Raw(Vec<u8>),
|
||||
}
|
||||
|
||||
impl MxValueProjection {
|
||||
fn from_proto(value: &ProtoMxValue) -> Self {
|
||||
if value.is_null {
|
||||
return Self::Null;
|
||||
}
|
||||
|
||||
match value.kind.as_ref() {
|
||||
Some(Kind::BoolValue(value)) => Self::Bool(*value),
|
||||
Some(Kind::Int32Value(value)) => Self::Int32(*value),
|
||||
Some(Kind::Int64Value(value)) => Self::Int64(*value),
|
||||
Some(Kind::FloatValue(value)) => Self::Float(*value),
|
||||
Some(Kind::DoubleValue(value)) => Self::Double(*value),
|
||||
Some(Kind::StringValue(value)) => Self::String(value.clone()),
|
||||
Some(Kind::TimestampValue(value)) => Self::Timestamp(*value),
|
||||
Some(Kind::ArrayValue(value)) => Self::Array(MxArrayValue::from_proto(value.clone())),
|
||||
Some(Kind::RawValue(value)) => Self::Raw(value.clone()),
|
||||
None => Self::Unset,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct MxArrayValue {
|
||||
raw: MxArray,
|
||||
projection: MxArrayProjection,
|
||||
}
|
||||
|
||||
impl MxArrayValue {
|
||||
pub fn from_proto(raw: MxArray) -> Self {
|
||||
let projection = MxArrayProjection::from_proto(&raw);
|
||||
Self { raw, projection }
|
||||
}
|
||||
|
||||
pub fn string(values: Vec<String>) -> Self {
|
||||
Self::from_proto(MxArray {
|
||||
element_data_type: MxDataType::String as i32,
|
||||
variant_type: "VT_ARRAY|VT_BSTR".to_owned(),
|
||||
dimensions: vec![values.len() as u32],
|
||||
values: Some(Values::StringValues(StringArray { values })),
|
||||
..MxArray::default()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn raw(&self) -> &MxArray {
|
||||
&self.raw
|
||||
}
|
||||
|
||||
pub fn projection(&self) -> &MxArrayProjection {
|
||||
&self.projection
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum MxArrayProjection {
|
||||
Unset,
|
||||
Bool(Vec<bool>),
|
||||
Int32(Vec<i32>),
|
||||
Int64(Vec<i64>),
|
||||
Float(Vec<f32>),
|
||||
Double(Vec<f64>),
|
||||
String(Vec<String>),
|
||||
Timestamp(Vec<prost_types::Timestamp>),
|
||||
Raw(Vec<Vec<u8>>),
|
||||
}
|
||||
|
||||
impl MxArrayProjection {
|
||||
fn from_proto(array: &MxArray) -> Self {
|
||||
match array.values.as_ref() {
|
||||
Some(Values::BoolValues(BoolArray { values })) => Self::Bool(values.clone()),
|
||||
Some(Values::Int32Values(Int32Array { values })) => Self::Int32(values.clone()),
|
||||
Some(Values::Int64Values(Int64Array { values })) => Self::Int64(values.clone()),
|
||||
Some(Values::FloatValues(FloatArray { values })) => Self::Float(values.clone()),
|
||||
Some(Values::DoubleValues(DoubleArray { values })) => Self::Double(values.clone()),
|
||||
Some(Values::StringValues(StringArray { values })) => Self::String(values.clone()),
|
||||
Some(Values::TimestampValues(TimestampArray { values })) => {
|
||||
Self::Timestamp(values.clone())
|
||||
}
|
||||
Some(Values::RawValues(RawArray { values })) => Self::Raw(values.clone()),
|
||||
None => Self::Unset,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct MxStatus {
|
||||
raw: MxStatusProxy,
|
||||
}
|
||||
|
||||
impl MxStatus {
|
||||
pub fn from_proto(raw: MxStatusProxy) -> Self {
|
||||
Self { raw }
|
||||
}
|
||||
|
||||
pub fn raw(&self) -> &MxStatusProxy {
|
||||
&self.raw
|
||||
}
|
||||
|
||||
pub fn success(&self) -> i32 {
|
||||
self.raw.success
|
||||
}
|
||||
|
||||
pub fn category(&self) -> Option<MxStatusCategory> {
|
||||
MxStatusCategory::try_from(self.raw.category).ok()
|
||||
}
|
||||
|
||||
pub fn detected_by(&self) -> Option<MxStatusSource> {
|
||||
MxStatusSource::try_from(self.raw.detected_by).ok()
|
||||
}
|
||||
|
||||
pub fn detail(&self) -> i32 {
|
||||
self.raw.detail
|
||||
}
|
||||
|
||||
pub fn raw_category(&self) -> i32 {
|
||||
self.raw.raw_category
|
||||
}
|
||||
|
||||
pub fn raw_detected_by(&self) -> i32 {
|
||||
self.raw.raw_detected_by
|
||||
}
|
||||
|
||||
pub fn diagnostic_text(&self) -> &str {
|
||||
&self.raw.diagnostic_text
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user