From 2b4ba2ff0b87343d910d4dae37f0b8a8c33972af Mon Sep 17 00:00:00 2001 From: Nicholas Molnar <65710+neekolas@users.noreply.github.com> Date: Wed, 10 Apr 2024 12:29:36 -0700 Subject: [PATCH] Add API trait --- xmtp_api_grpc/src/grpc_api_helper.rs | 16 ++++--- xmtp_api_grpc/src/identity.rs | 62 ++++++++++++++++++++++++++++ xmtp_api_grpc/src/lib.rs | 1 + xmtp_proto/src/api_client.rs | 27 ++++++++++++ 4 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 xmtp_api_grpc/src/identity.rs diff --git a/xmtp_api_grpc/src/grpc_api_helper.rs b/xmtp_api_grpc/src/grpc_api_helper.rs index 60aac20d1..d7a3e8ecf 100644 --- a/xmtp_api_grpc/src/grpc_api_helper.rs +++ b/xmtp_api_grpc/src/grpc_api_helper.rs @@ -14,6 +14,7 @@ use xmtp_proto::{ Error, ErrorKind, GroupMessageStream, MutableApiSubscription, WelcomeMessageStream, XmtpApiClient, XmtpApiSubscription, XmtpMlsClient, }, + xmtp::identity::api::v1::identity_api_client::IdentityApiClient as ProtoIdentityApiClient, xmtp::message_api::v1::{ message_api_client::MessageApiClient, BatchQueryRequest, BatchQueryResponse, Envelope, PublishRequest, PublishResponse, QueryRequest, QueryResponse, SubscribeRequest, @@ -45,9 +46,10 @@ async fn create_tls_channel(address: String) -> Result { #[derive(Debug)] pub struct Client { - client: MessageApiClient, - mls_client: ProtoMlsApiClient, - app_version: MetadataValue, + pub(crate) client: MessageApiClient, + pub(crate) mls_client: ProtoMlsApiClient, + pub(crate) identity_client: ProtoIdentityApiClient, + pub(crate) app_version: MetadataValue, } impl Client { @@ -58,12 +60,14 @@ impl Client { let channel = create_tls_channel(host).await?; let client = MessageApiClient::new(channel.clone()); - let mls_client = ProtoMlsApiClient::new(channel); + let mls_client = ProtoMlsApiClient::new(channel.clone()); + let identity_client = ProtoIdentityApiClient::new(channel); Ok(Self { client, mls_client, app_version, + identity_client, }) } else { let channel = Channel::from_shared(host) @@ -73,11 +77,13 @@ impl Client { .map_err(|e| Error::new(ErrorKind::SetupError).with(e))?; let client = MessageApiClient::new(channel.clone()); - let mls_client = ProtoMlsApiClient::new(channel); + let mls_client = ProtoMlsApiClient::new(channel.clone()); + let identity_client = ProtoIdentityApiClient::new(channel); Ok(Self { client, mls_client, + identity_client, app_version, }) } diff --git a/xmtp_api_grpc/src/identity.rs b/xmtp_api_grpc/src/identity.rs new file mode 100644 index 000000000..b3e324bdf --- /dev/null +++ b/xmtp_api_grpc/src/identity.rs @@ -0,0 +1,62 @@ +use tonic::{async_trait, Request}; +use xmtp_proto::{ + api_client::{Error, ErrorKind, XmtpIdentityClient}, + xmtp::identity::api::v1::{ + GetIdentityUpdatesRequest as GetIdentityUpdatesV2Request, + GetIdentityUpdatesResponse as GetIdentityUpdatesV2Response, GetInboxIdsRequest, + GetInboxIdsResponse, PublishIdentityUpdateRequest, PublishIdentityUpdateResponse, + }, +}; + +use crate::Client; + +#[async_trait] +impl XmtpIdentityClient for Client { + async fn publish_identity_update( + &self, + request: PublishIdentityUpdateRequest, + ) -> Result { + let mut tonic_request = Request::new(request); + tonic_request + .metadata_mut() + .insert("x-app-version", self.app_version.clone()); + let client = &mut self.identity_client.clone(); + + let res = client.publish_identity_update(tonic_request).await; + + res.map(|response| response.into_inner()) + .map_err(|err| Error::new(ErrorKind::IdentityError).with(err)) + } + + async fn get_inbox_ids( + &self, + request: GetInboxIdsRequest, + ) -> Result { + let mut tonic_request = Request::new(request); + tonic_request + .metadata_mut() + .insert("x-app-version", self.app_version.clone()); + let client = &mut self.identity_client.clone(); + + let res = client.get_inbox_ids(tonic_request).await; + + res.map(|response| response.into_inner()) + .map_err(|err| Error::new(ErrorKind::IdentityError).with(err)) + } + + async fn get_identity_updates_v2( + &self, + request: GetIdentityUpdatesV2Request, + ) -> Result { + let mut tonic_request = Request::new(request); + tonic_request + .metadata_mut() + .insert("x-app-version", self.app_version.clone()); + let client = &mut self.identity_client.clone(); + + let res = client.get_identity_updates(tonic_request).await; + + res.map(|response| response.into_inner()) + .map_err(|err| Error::new(ErrorKind::IdentityError).with(err)) + } +} diff --git a/xmtp_api_grpc/src/lib.rs b/xmtp_api_grpc/src/lib.rs index 5ed19b55e..9cc8bf750 100644 --- a/xmtp_api_grpc/src/lib.rs +++ b/xmtp_api_grpc/src/lib.rs @@ -1,5 +1,6 @@ pub mod auth_token; pub mod grpc_api_helper; +mod identity; pub const LOCALHOST_ADDRESS: &str = "http://localhost:5556"; pub const DEV_ADDRESS: &str = "https://grpc.dev.xmtp.network:443"; diff --git a/xmtp_proto/src/api_client.rs b/xmtp_proto/src/api_client.rs index ca164fda0..88f5d2321 100644 --- a/xmtp_proto/src/api_client.rs +++ b/xmtp_proto/src/api_client.rs @@ -7,6 +7,11 @@ pub use super::xmtp::message_api::v1::{ BatchQueryRequest, BatchQueryResponse, Envelope, PagingInfo, PublishRequest, PublishResponse, QueryRequest, QueryResponse, SubscribeRequest, }; +use crate::xmtp::identity::api::v1::{ + GetIdentityUpdatesRequest as GetIdentityUpdatesV2Request, + GetIdentityUpdatesResponse as GetIdentityUpdatesV2Response, GetInboxIdsRequest, + GetInboxIdsResponse, PublishIdentityUpdateRequest, PublishIdentityUpdateResponse, +}; use crate::xmtp::mls::api::v1::{ FetchKeyPackagesRequest, FetchKeyPackagesResponse, GetIdentityUpdatesRequest, GetIdentityUpdatesResponse, GroupMessage, QueryGroupMessagesRequest, @@ -24,6 +29,7 @@ pub enum ErrorKind { SubscribeError, BatchQueryError, MlsError, + IdentityError, SubscriptionUpdateError, } @@ -67,6 +73,7 @@ impl fmt::Display for Error { ErrorKind::QueryError => "query error", ErrorKind::SubscribeError => "subscribe error", ErrorKind::BatchQueryError => "batch query error", + ErrorKind::IdentityError => "identity error", ErrorKind::MlsError => "mls error", ErrorKind::SubscriptionUpdateError => "subscription update error", })?; @@ -166,3 +173,23 @@ pub trait XmtpMlsClient: Send + Sync + 'static { request: SubscribeWelcomeMessagesRequest, ) -> Result; } + +// Wasm futures don't have `Send` or `Sync` bounds. +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] +pub trait XmtpIdentityClient: Send + Sync + 'static { + async fn publish_identity_update( + &self, + request: PublishIdentityUpdateRequest, + ) -> Result; + + async fn get_identity_updates_v2( + &self, + request: GetIdentityUpdatesV2Request, + ) -> Result; + + async fn get_inbox_ids( + &self, + request: GetInboxIdsRequest, + ) -> Result; +}