Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API trait #647

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed to make this a v2 since we already have a get_identity_updates method on the client for the old endpoint.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, get_identity_updates_v2 is getting xmtp v3 identities right?

&self,
request: GetIdentityUpdatesV2Request,
) -> Result<GetIdentityUpdatesV2Response, Error>;

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