Skip to content

Commit 2b4ba2f

Browse files
committed
Add API trait
1 parent 121a867 commit 2b4ba2f

File tree

4 files changed

+101
-5
lines changed

4 files changed

+101
-5
lines changed

xmtp_api_grpc/src/grpc_api_helper.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use xmtp_proto::{
1414
Error, ErrorKind, GroupMessageStream, MutableApiSubscription, WelcomeMessageStream,
1515
XmtpApiClient, XmtpApiSubscription, XmtpMlsClient,
1616
},
17+
xmtp::identity::api::v1::identity_api_client::IdentityApiClient as ProtoIdentityApiClient,
1718
xmtp::message_api::v1::{
1819
message_api_client::MessageApiClient, BatchQueryRequest, BatchQueryResponse, Envelope,
1920
PublishRequest, PublishResponse, QueryRequest, QueryResponse, SubscribeRequest,
@@ -45,9 +46,10 @@ async fn create_tls_channel(address: String) -> Result<Channel, Error> {
4546

4647
#[derive(Debug)]
4748
pub struct Client {
48-
client: MessageApiClient<Channel>,
49-
mls_client: ProtoMlsApiClient<Channel>,
50-
app_version: MetadataValue<tonic::metadata::Ascii>,
49+
pub(crate) client: MessageApiClient<Channel>,
50+
pub(crate) mls_client: ProtoMlsApiClient<Channel>,
51+
pub(crate) identity_client: ProtoIdentityApiClient<Channel>,
52+
pub(crate) app_version: MetadataValue<tonic::metadata::Ascii>,
5153
}
5254

5355
impl Client {
@@ -58,12 +60,14 @@ impl Client {
5860
let channel = create_tls_channel(host).await?;
5961

6062
let client = MessageApiClient::new(channel.clone());
61-
let mls_client = ProtoMlsApiClient::new(channel);
63+
let mls_client = ProtoMlsApiClient::new(channel.clone());
64+
let identity_client = ProtoIdentityApiClient::new(channel);
6265

6366
Ok(Self {
6467
client,
6568
mls_client,
6669
app_version,
70+
identity_client,
6771
})
6872
} else {
6973
let channel = Channel::from_shared(host)
@@ -73,11 +77,13 @@ impl Client {
7377
.map_err(|e| Error::new(ErrorKind::SetupError).with(e))?;
7478

7579
let client = MessageApiClient::new(channel.clone());
76-
let mls_client = ProtoMlsApiClient::new(channel);
80+
let mls_client = ProtoMlsApiClient::new(channel.clone());
81+
let identity_client = ProtoIdentityApiClient::new(channel);
7782

7883
Ok(Self {
7984
client,
8085
mls_client,
86+
identity_client,
8187
app_version,
8288
})
8389
}

xmtp_api_grpc/src/identity.rs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use tonic::{async_trait, Request};
2+
use xmtp_proto::{
3+
api_client::{Error, ErrorKind, XmtpIdentityClient},
4+
xmtp::identity::api::v1::{
5+
GetIdentityUpdatesRequest as GetIdentityUpdatesV2Request,
6+
GetIdentityUpdatesResponse as GetIdentityUpdatesV2Response, GetInboxIdsRequest,
7+
GetInboxIdsResponse, PublishIdentityUpdateRequest, PublishIdentityUpdateResponse,
8+
},
9+
};
10+
11+
use crate::Client;
12+
13+
#[async_trait]
14+
impl XmtpIdentityClient for Client {
15+
async fn publish_identity_update(
16+
&self,
17+
request: PublishIdentityUpdateRequest,
18+
) -> Result<PublishIdentityUpdateResponse, Error> {
19+
let mut tonic_request = Request::new(request);
20+
tonic_request
21+
.metadata_mut()
22+
.insert("x-app-version", self.app_version.clone());
23+
let client = &mut self.identity_client.clone();
24+
25+
let res = client.publish_identity_update(tonic_request).await;
26+
27+
res.map(|response| response.into_inner())
28+
.map_err(|err| Error::new(ErrorKind::IdentityError).with(err))
29+
}
30+
31+
async fn get_inbox_ids(
32+
&self,
33+
request: GetInboxIdsRequest,
34+
) -> Result<GetInboxIdsResponse, Error> {
35+
let mut tonic_request = Request::new(request);
36+
tonic_request
37+
.metadata_mut()
38+
.insert("x-app-version", self.app_version.clone());
39+
let client = &mut self.identity_client.clone();
40+
41+
let res = client.get_inbox_ids(tonic_request).await;
42+
43+
res.map(|response| response.into_inner())
44+
.map_err(|err| Error::new(ErrorKind::IdentityError).with(err))
45+
}
46+
47+
async fn get_identity_updates_v2(
48+
&self,
49+
request: GetIdentityUpdatesV2Request,
50+
) -> Result<GetIdentityUpdatesV2Response, Error> {
51+
let mut tonic_request = Request::new(request);
52+
tonic_request
53+
.metadata_mut()
54+
.insert("x-app-version", self.app_version.clone());
55+
let client = &mut self.identity_client.clone();
56+
57+
let res = client.get_identity_updates(tonic_request).await;
58+
59+
res.map(|response| response.into_inner())
60+
.map_err(|err| Error::new(ErrorKind::IdentityError).with(err))
61+
}
62+
}

xmtp_api_grpc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod auth_token;
22
pub mod grpc_api_helper;
3+
mod identity;
34

45
pub const LOCALHOST_ADDRESS: &str = "http://localhost:5556";
56
pub const DEV_ADDRESS: &str = "https://grpc.dev.xmtp.network:443";

xmtp_proto/src/api_client.rs

+27
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ pub use super::xmtp::message_api::v1::{
77
BatchQueryRequest, BatchQueryResponse, Envelope, PagingInfo, PublishRequest, PublishResponse,
88
QueryRequest, QueryResponse, SubscribeRequest,
99
};
10+
use crate::xmtp::identity::api::v1::{
11+
GetIdentityUpdatesRequest as GetIdentityUpdatesV2Request,
12+
GetIdentityUpdatesResponse as GetIdentityUpdatesV2Response, GetInboxIdsRequest,
13+
GetInboxIdsResponse, PublishIdentityUpdateRequest, PublishIdentityUpdateResponse,
14+
};
1015
use crate::xmtp::mls::api::v1::{
1116
FetchKeyPackagesRequest, FetchKeyPackagesResponse, GetIdentityUpdatesRequest,
1217
GetIdentityUpdatesResponse, GroupMessage, QueryGroupMessagesRequest,
@@ -24,6 +29,7 @@ pub enum ErrorKind {
2429
SubscribeError,
2530
BatchQueryError,
2631
MlsError,
32+
IdentityError,
2733
SubscriptionUpdateError,
2834
}
2935

@@ -67,6 +73,7 @@ impl fmt::Display for Error {
6773
ErrorKind::QueryError => "query error",
6874
ErrorKind::SubscribeError => "subscribe error",
6975
ErrorKind::BatchQueryError => "batch query error",
76+
ErrorKind::IdentityError => "identity error",
7077
ErrorKind::MlsError => "mls error",
7178
ErrorKind::SubscriptionUpdateError => "subscription update error",
7279
})?;
@@ -166,3 +173,23 @@ pub trait XmtpMlsClient: Send + Sync + 'static {
166173
request: SubscribeWelcomeMessagesRequest,
167174
) -> Result<WelcomeMessageStream, Error>;
168175
}
176+
177+
// Wasm futures don't have `Send` or `Sync` bounds.
178+
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
179+
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
180+
pub trait XmtpIdentityClient: Send + Sync + 'static {
181+
async fn publish_identity_update(
182+
&self,
183+
request: PublishIdentityUpdateRequest,
184+
) -> Result<PublishIdentityUpdateResponse, Error>;
185+
186+
async fn get_identity_updates_v2(
187+
&self,
188+
request: GetIdentityUpdatesV2Request,
189+
) -> Result<GetIdentityUpdatesV2Response, Error>;
190+
191+
async fn get_inbox_ids(
192+
&self,
193+
request: GetInboxIdsRequest,
194+
) -> Result<GetInboxIdsResponse, Error>;
195+
}

0 commit comments

Comments
 (0)