Skip to content

Commit

Permalink
Compatibility Layer stubs for d14n and ApiClientWrapper (#1642)
Browse files Browse the repository at this point in the history
* compat layer

* add compat layer for d14n

* dont forget client trait
  • Loading branch information
insipx authored Feb 18, 2025
1 parent 7d6b8b1 commit c598672
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions xmtp_api_d14n/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ license.workspace = true
version.workspace = true

[dependencies]
async-trait.workspace = true
derive_builder = "0.20"
once_cell.workspace = true
parking_lot.workspace = true
prost.workspace = true
prost-types.workspace = true
xmtp_common.workspace = true
xmtp_proto.workspace = true

[dev-dependencies]
Expand Down
120 changes: 120 additions & 0 deletions xmtp_api_d14n/src/compat/d14n.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//! Compatibility layer for d14n and previous xmtp_api crate
//TODO: Remove once d14n integration complete
#![allow(unused)]
use xmtp_common::RetryableError;
use xmtp_proto::api_client::{XmtpIdentityClient, XmtpMlsClient, XmtpMlsStreams};
use xmtp_proto::traits::ApiError;
use xmtp_proto::traits::Client;

use xmtp_proto::xmtp::identity::api::v1::{
GetIdentityUpdatesRequest, GetIdentityUpdatesResponse, GetInboxIdsRequest, GetInboxIdsResponse,
PublishIdentityUpdateRequest, PublishIdentityUpdateResponse,
VerifySmartContractWalletSignaturesRequest, VerifySmartContractWalletSignaturesResponse,
};
use xmtp_proto::XmtpApiError;
use xmtp_proto::{
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,
},
xmtp::mls::api::v1::{
mls_api_client::MlsApiClient as ProtoMlsApiClient, FetchKeyPackagesRequest,
FetchKeyPackagesResponse, QueryGroupMessagesRequest, QueryGroupMessagesResponse,
QueryWelcomeMessagesRequest, QueryWelcomeMessagesResponse, SendGroupMessagesRequest,
SendWelcomeMessagesRequest, SubscribeGroupMessagesRequest, SubscribeWelcomeMessagesRequest,
UploadKeyPackageRequest,
},
};

use crate::endpoints::{GetInboxIds, PublishClientEnvelopes, QueryEnvelopes};
pub struct D14nClient<C, P, E> {
message_client: C,
payer_client: P,
_marker: E,
}

#[async_trait::async_trait]
impl<C, P, E> XmtpMlsClient for D14nClient<C, P, E>
where
E: std::error::Error + RetryableError + Send + Sync + 'static,
P: Send + Sync + Client,
C: Send + Sync + Client,
{
type Error = ApiError<E>;
async fn upload_key_package(
&self,
request: UploadKeyPackageRequest,
) -> Result<(), Self::Error> {
todo!()
}
async fn fetch_key_packages(
&self,
request: FetchKeyPackagesRequest,
) -> Result<FetchKeyPackagesResponse, Self::Error> {
todo!()
}
async fn send_group_messages(
&self,
request: SendGroupMessagesRequest,
) -> Result<(), Self::Error> {
todo!()
}
async fn send_welcome_messages(
&self,
request: SendWelcomeMessagesRequest,
) -> Result<(), Self::Error> {
todo!()
}
async fn query_group_messages(
&self,
request: QueryGroupMessagesRequest,
) -> Result<QueryGroupMessagesResponse, Self::Error> {
todo!()
}
async fn query_welcome_messages(
&self,
request: QueryWelcomeMessagesRequest,
) -> Result<QueryWelcomeMessagesResponse, Self::Error> {
todo!()
}
}

#[async_trait::async_trait]
impl<C, P, E> XmtpIdentityClient for D14nClient<C, P, E>
where
E: std::error::Error + RetryableError + Send + Sync + 'static,
P: Send + Sync + Client,
C: Send + Sync + Client,
{
type Error = ApiError<E>;

async fn publish_identity_update(
&self,
request: PublishIdentityUpdateRequest,
) -> Result<PublishIdentityUpdateResponse, Self::Error> {
todo!()
}

async fn get_identity_updates_v2(
&self,
request: GetIdentityUpdatesRequest,
) -> Result<GetIdentityUpdatesResponse, Self::Error> {
todo!()
}

async fn get_inbox_ids(
&self,
request: GetInboxIdsRequest,
) -> Result<GetInboxIdsResponse, Self::Error> {
todo!()
}

async fn verify_smart_contract_wallet_signatures(
&self,
request: VerifySmartContractWalletSignaturesRequest,
) -> Result<VerifySmartContractWalletSignaturesResponse, Self::Error> {
todo!()
}
}
6 changes: 6 additions & 0 deletions xmtp_api_d14n/src/compat/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
mod v3;
pub use v3::*;
*/
mod d14n;
pub use d14n::*;
81 changes: 81 additions & 0 deletions xmtp_api_d14n/src/compat/v3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use xmtp_proto::api_client::{XmtpApiClient, XmtpIdentityClient, XmtpMlsClient, XmtpMlsStreams};
use xmtp_proto::traits::ApiError;

pub struct V3Client<C> {
client: C,
}

#[async_trait::async_trait]
impl<C> XmtpMlsClient for V3Client<C> {
type Error = ApiError<Box<dyn XmtpApiError>>;

async fn upload_key_package(
&self,
request: UploadKeyPackageRequest,
) -> Result<(), Self::Error> {
todo!()
}
async fn fetch_key_packages(
&self,
request: FetchKeyPackagesRequest,
) -> Result<FetchKeyPackagesResponse, Self::Error> {
todo!()
}
async fn send_group_messages(
&self,
request: SendGroupMessagesRequest,
) -> Result<(), Self::Error> {
todo!()
}
async fn send_welcome_messages(
&self,
request: SendWelcomeMessagesRequest,
) -> Result<(), Self::Error> {
todo!()
}
async fn query_group_messages(
&self,
request: QueryGroupMessagesRequest,
) -> Result<QueryGroupMessagesResponse, Self::Error> {
todo!()
}
async fn query_welcome_messages(
&self,
request: QueryWelcomeMessagesRequest,
) -> Result<QueryWelcomeMessagesResponse, Self::Error> {
todo!()
}
}

#[async_trait::async_trait]
impl<C> XmtpIdentityClient for V3Client<C> {
type Error = ApiError<Box<dyn XmtpApiError>>;

async fn publish_identity_update(
&self,
request: PublishIdentityUpdateRequest,
) -> Result<PublishIdentityUpdateResponse, Self::Error> {
todo!()
}

async fn get_identity_updates_v2(
&self,
request: GetIdentityUpdatesV2Request,
) -> Result<GetIdentityUpdatesV2Response, Self::Error> {
todo!()
}

async fn get_inbox_ids(
&self,
request: GetInboxIdsRequest,
) -> Result<GetInboxIdsResponse, Self::Error> {
todo!()
}

async fn verify_smart_contract_wallet_signatures(
&self,
request: VerifySmartContractWalletSignaturesRequest,
) -> Result<VerifySmartContractWalletSignaturesResponse, Self::Error> {
todo!()
}
}
20 changes: 13 additions & 7 deletions xmtp_api_d14n/src/endpoints/d14n/get_inbox_ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ use derive_builder::Builder;
use prost::Message;
use std::borrow::Cow;
use xmtp_proto::traits::{BodyError, Endpoint};
use xmtp_proto::xmtp::mls::api::v1::FetchKeyPackagesRequest;
use xmtp_proto::xmtp::xmtpv4::message_api::FILE_DECRIPTOR_SET;
use xmtp_proto::xmtp::xmtpv4::message_api::{GetInboxIdsRequest, GetInboxIdsResponse};
use xmtp_proto::xmtp::xmtpv4::message_api::FILE_DESCRIPTOR_SET;
use xmtp_proto::xmtp::xmtpv4::message_api::{
get_inbox_ids_request, GetInboxIdsRequest, GetInboxIdsResponse,
};

#[derive(Debug, Builder, Default)]
#[builder(setter(strip_option))]
pub struct GetInboxIds {
#[builder(setter(into))]
envelopes: Vec<ClientEnvelope>,
addresses: Vec<String>,
}

impl GetInboxIds {
Expand All @@ -27,12 +28,17 @@ impl Endpoint for GetInboxIds {
}

fn grpc_endpoint(&self) -> Cow<'static, str> {
crate::path_and_query::<PublishClientEnvelopesRequest>(FILE_DESCRIPTOR_SET)
crate::path_and_query::<GetInboxIdsRequest>(FILE_DESCRIPTOR_SET)
}

fn body(&self) -> Result<Vec<u8>, BodyError> {
Ok(PublishClientEnvelopesRequest {
envelopes: self.envelopes.clone(),
Ok(GetInboxIdsRequest {
requests: self
.addresses
.iter()
.cloned()
.map(|i| get_inbox_ids_request::Request { address: i })
.collect(),
}
.encode_to_vec())
}
Expand Down
3 changes: 3 additions & 0 deletions xmtp_api_d14n/src/endpoints/d14n/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ pub use publish_client_envelopes::*;

mod query_envelopes;
pub use query_envelopes::*;

mod get_inbox_ids;
pub use get_inbox_ids::*;
3 changes: 3 additions & 0 deletions xmtp_api_d14n/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ pub use endpoints::*;

mod proto_cache;
pub(crate) use proto_cache::*;

pub mod compat;
pub use compat::*;
2 changes: 1 addition & 1 deletion xmtp_proto/src/gen/xmtp.xmtpv4.message_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,4 +888,4 @@ pub const FILE_DESCRIPTOR_SET: &[u8] = &[
];
include!("xmtp.xmtpv4.message_api.serde.rs");
include!("xmtp.xmtpv4.message_api.tonic.rs");
// @@protoc_insertion_point(module)
// @@protoc_insertion_point(module)
21 changes: 20 additions & 1 deletion xmtp_proto/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::borrow::Cow;
use thiserror::Error;
use xmtp_common::{retry_async, retryable, BoxedRetry, RetryableError};

use crate::{ApiEndpoint, Code, XmtpApiError};

pub trait Endpoint {
type Output: prost::Message + Default;

Expand All @@ -23,11 +25,11 @@ where
inner: S,
}
*/

#[allow(async_fn_in_trait)]
pub trait Client {
type Error: std::error::Error + Send + Sync + 'static;
type Stream: futures::Stream;
// TODO: probably need type: Stream here

async fn request(
&self,
Expand Down Expand Up @@ -104,6 +106,23 @@ where
Conversion(#[from] crate::ConversionError),
}

impl<E> XmtpApiError for ApiError<E>
where
E: std::error::Error + Send + Sync + RetryableError + 'static,
{
fn api_call(&self) -> Option<ApiEndpoint> {
None
}

fn code(&self) -> Option<Code> {
None
}

fn grpc_message(&self) -> Option<&str> {
None
}
}

impl<E> RetryableError for ApiError<E>
where
E: RetryableError + std::error::Error + Send + Sync + 'static,
Expand Down

0 comments on commit c598672

Please sign in to comment.