Skip to content

Commit

Permalink
Add API trait
Browse files Browse the repository at this point in the history
  • Loading branch information
neekolas committed Apr 11, 2024
1 parent 121a867 commit 2b4ba2f
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 5 deletions.
16 changes: 11 additions & 5 deletions xmtp_api_grpc/src/grpc_api_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -45,9 +46,10 @@ async fn create_tls_channel(address: String) -> Result<Channel, Error> {

#[derive(Debug)]
pub struct Client {
client: MessageApiClient<Channel>,
mls_client: ProtoMlsApiClient<Channel>,
app_version: MetadataValue<tonic::metadata::Ascii>,
pub(crate) client: MessageApiClient<Channel>,
pub(crate) mls_client: ProtoMlsApiClient<Channel>,
pub(crate) identity_client: ProtoIdentityApiClient<Channel>,
pub(crate) app_version: MetadataValue<tonic::metadata::Ascii>,
}

impl Client {
Expand All @@ -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)
Expand All @@ -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,
})
}
Expand Down
62 changes: 62 additions & 0 deletions xmtp_api_grpc/src/identity.rs
Original file line number Diff line number Diff line change
@@ -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<PublishIdentityUpdateResponse, Error> {
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<GetInboxIdsResponse, Error> {
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<GetIdentityUpdatesV2Response, Error> {
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))
}
}
1 change: 1 addition & 0 deletions xmtp_api_grpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
27 changes: 27 additions & 0 deletions xmtp_proto/src/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -24,6 +29,7 @@ pub enum ErrorKind {
SubscribeError,
BatchQueryError,
MlsError,
IdentityError,
SubscriptionUpdateError,
}

Expand Down Expand Up @@ -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",
})?;
Expand Down Expand Up @@ -166,3 +173,23 @@ pub trait XmtpMlsClient: Send + Sync + 'static {
request: SubscribeWelcomeMessagesRequest,
) -> Result<WelcomeMessageStream, Error>;
}

// 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<PublishIdentityUpdateResponse, Error>;

async fn get_identity_updates_v2(
&self,
request: GetIdentityUpdatesV2Request,
) -> Result<GetIdentityUpdatesV2Response, Error>;

async fn get_inbox_ids(
&self,
request: GetInboxIdsRequest,
) -> Result<GetInboxIdsResponse, Error>;
}

0 comments on commit 2b4ba2f

Please sign in to comment.