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

feat: expose batch_query on xmtp_networking and bindings_swift #127

Merged
merged 1 commit into from
May 31, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ void* __swift_bridge__$Vec_RustSubscription$get_mut(void* vec_ptr, uintptr_t ind
uintptr_t __swift_bridge__$Vec_RustSubscription$len(void* vec_ptr);
void* __swift_bridge__$Vec_RustSubscription$as_ptr(void* vec_ptr);

typedef struct QueryRequest QueryRequest;
void __swift_bridge__$QueryRequest$_free(void* self);

void* __swift_bridge__$Vec_QueryRequest$new(void);
void __swift_bridge__$Vec_QueryRequest$drop(void* vec_ptr);
void __swift_bridge__$Vec_QueryRequest$push(void* vec_ptr, void* item_ptr);
void* __swift_bridge__$Vec_QueryRequest$pop(void* vec_ptr);
void* __swift_bridge__$Vec_QueryRequest$get(void* vec_ptr, uintptr_t index);
void* __swift_bridge__$Vec_QueryRequest$get_mut(void* vec_ptr, uintptr_t index);
uintptr_t __swift_bridge__$Vec_QueryRequest$len(void* vec_ptr);
void* __swift_bridge__$Vec_QueryRequest$as_ptr(void* vec_ptr);

typedef struct QueryResponse QueryResponse;
void __swift_bridge__$QueryResponse$_free(void* self);

Expand All @@ -53,6 +65,18 @@ void* __swift_bridge__$Vec_QueryResponse$get_mut(void* vec_ptr, uintptr_t index)
uintptr_t __swift_bridge__$Vec_QueryResponse$len(void* vec_ptr);
void* __swift_bridge__$Vec_QueryResponse$as_ptr(void* vec_ptr);

typedef struct BatchQueryResponse BatchQueryResponse;
void __swift_bridge__$BatchQueryResponse$_free(void* self);

void* __swift_bridge__$Vec_BatchQueryResponse$new(void);
void __swift_bridge__$Vec_BatchQueryResponse$drop(void* vec_ptr);
void __swift_bridge__$Vec_BatchQueryResponse$push(void* vec_ptr, void* item_ptr);
void* __swift_bridge__$Vec_BatchQueryResponse$pop(void* vec_ptr);
void* __swift_bridge__$Vec_BatchQueryResponse$get(void* vec_ptr, uintptr_t index);
void* __swift_bridge__$Vec_BatchQueryResponse$get_mut(void* vec_ptr, uintptr_t index);
uintptr_t __swift_bridge__$Vec_BatchQueryResponse$len(void* vec_ptr);
void* __swift_bridge__$Vec_BatchQueryResponse$as_ptr(void* vec_ptr);

typedef struct RustClient RustClient;
void __swift_bridge__$RustClient$_free(void* self);

Expand All @@ -73,7 +97,9 @@ struct __private__ResultPtrAndPtr __swift_bridge__$RustSubscription$get_messages
void __swift_bridge__$RustSubscription$close(void* self);
void* __swift_bridge__$QueryResponse$envelopes(void* self);
struct __swift_bridge__$Option$PagingInfo __swift_bridge__$QueryResponse$paging_info(void* self);
void* __swift_bridge__$BatchQueryResponse$responses(void* self);
void __swift_bridge__$create_client(void* callback_wrapper, void __swift_bridge__$create_client$async(void* callback_wrapper, struct __private__ResultPtrAndPtr ret), void* host, bool is_secure);
void __swift_bridge__$RustClient$batch_query(void* callback_wrapper, void __swift_bridge__$RustClient$batch_query$async(void* callback_wrapper, struct __private__ResultPtrAndPtr ret), void* self, void* requests);
void __swift_bridge__$RustClient$query(void* callback_wrapper, void __swift_bridge__$RustClient$query$async(void* callback_wrapper, struct __private__ResultPtrAndPtr ret), void* self, void* topic, struct __private__OptionU64 start_time_ns, struct __private__OptionU64 end_time_ns, struct __swift_bridge__$Option$PagingInfo paging_info);
void __swift_bridge__$RustClient$publish(void* callback_wrapper, void __swift_bridge__$RustClient$publish$async(void* callback_wrapper, struct __private__ResultPtrAndPtr ret), void* self, void* token, void* envelopes);
void __swift_bridge__$RustClient$subscribe(void* callback_wrapper, void __swift_bridge__$RustClient$subscribe$async(void* callback_wrapper, struct __private__ResultPtrAndPtr ret), void* self, void* topics);
Expand Down
40 changes: 39 additions & 1 deletion bindings_swift/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use types::BatchQueryResponse;
use types::QueryResponse;
use xmtp_crypto::{hashes, k256_helper};
use xmtp_networking::grpc_api_helper;
use xmtp_proto::xmtp::message_api::v1::{Envelope as EnvelopeProto, PagingInfo};
use xmtp_proto::xmtp::message_api::v1::{Envelope as EnvelopeProto, PagingInfo, QueryRequest};
pub mod types;

#[swift_bridge::bridge]
Expand Down Expand Up @@ -42,6 +43,10 @@ mod ffi {
fn close(&mut self);
}

extern "Rust" {
type QueryRequest;
}

extern "Rust" {
type QueryResponse;

Expand All @@ -50,11 +55,22 @@ mod ffi {

}

extern "Rust" {
type BatchQueryResponse;

fn responses(&self) -> Vec<QueryResponse>;
}

extern "Rust" {
type RustClient;

async fn create_client(host: String, is_secure: bool) -> Result<RustClient, String>;

async fn batch_query(
&mut self,
requests: Vec<QueryRequest>,
) -> Result<BatchQueryResponse, String>;

async fn query(
&mut self,
topic: String,
Expand Down Expand Up @@ -110,6 +126,19 @@ async fn create_client(host: String, is_secure: bool) -> Result<RustClient, Stri
}

impl RustClient {
async fn batch_query(
&mut self,
requests: Vec<QueryRequest>,
) -> Result<BatchQueryResponse, String> {
let result = self
.client
.batch_query(requests)
.await
.map_err(|e| format!("{}", e))?;

Ok(BatchQueryResponse::from(result))
}

async fn query(
&mut self,
topic: String,
Expand Down Expand Up @@ -307,6 +336,15 @@ mod tests {
}
}

#[tokio::test]
async fn test_batch_query() {
let mut client = super::create_client(ADDRESS.to_string(), false)
.await
.unwrap();
let result = client.batch_query(vec![]).await.unwrap();
assert_eq!(result.responses().len(), 0);
}

// Try a query on a test topic, and make sure we get a response
#[tokio::test]
async fn test_publish_query() {
Expand Down
30 changes: 28 additions & 2 deletions bindings_swift/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use xmtp_proto::xmtp::message_api::v1::{
cursor as cursor_proto, Cursor, Envelope as EnvelopeProto, IndexCursor, PagingInfo,
QueryResponse as ProtoQueryResponse, SortDirection,
cursor as cursor_proto, BatchQueryResponse as ProtoBatchQueryResponse, Cursor,
Envelope as EnvelopeProto, IndexCursor, PagingInfo, QueryResponse as ProtoQueryResponse,
SortDirection,
};

impl From<crate::ffi::SortDirection> for SortDirection {
Expand Down Expand Up @@ -87,6 +88,7 @@ impl From<EnvelopeProto> for crate::Envelope {
}
}

#[derive(Clone)]
pub struct QueryResponse {
_envelopes: Vec<crate::Envelope>,
_paging_info: Option<crate::ffi::PagingInfo>,
Expand Down Expand Up @@ -119,3 +121,27 @@ impl From<ProtoQueryResponse> for QueryResponse {
}
}
}

pub struct BatchQueryResponse {
_responses: Vec<crate::QueryResponse>,
}

impl BatchQueryResponse {
pub fn responses(&self) -> Vec<crate::QueryResponse> {
self._responses.clone()
}
}

impl From<ProtoBatchQueryResponse> for BatchQueryResponse {
fn from(batch_query_response: ProtoBatchQueryResponse) -> Self {
let responses = batch_query_response
.responses
.into_iter()
.map(crate::QueryResponse::from)
.collect();

BatchQueryResponse {
_responses: responses,
}
}
}
19 changes: 17 additions & 2 deletions xmtp_networking/src/grpc_api_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use tokio_rustls::rustls::{ClientConfig, OwnedTrustAnchor, RootCertStore};
use tonic::Status;
use tonic::{metadata::MetadataValue, transport::Channel, Request, Streaming};
use xmtp_proto::xmtp::message_api::v1::{
message_api_client::MessageApiClient, Envelope, PagingInfo, PublishRequest, PublishResponse,
QueryRequest, QueryResponse, SubscribeRequest,
message_api_client::MessageApiClient, BatchQueryRequest, BatchQueryResponse, Envelope,
PagingInfo, PublishRequest, PublishResponse, QueryRequest, QueryResponse, SubscribeRequest,
};

fn tls_config() -> Result<ClientConfig, tonic::Status> {
Expand Down Expand Up @@ -173,6 +173,21 @@ impl Client {
Err(e) => Err(e),
}
}

pub async fn batch_query(
&self,
requests: Vec<QueryRequest>,
) -> Result<BatchQueryResponse, tonic::Status> {
let request = BatchQueryRequest { requests };
let res = match &self.client {
InnerApiClient::Plain(c) => c.clone().batch_query(request).await,
InnerApiClient::Tls(c) => c.clone().batch_query(request).await,
};
match res {
Ok(response) => Ok(response.into_inner()),
Err(e) => Err(e),
}
}
}

pub struct Subscription {
Expand Down
11 changes: 11 additions & 0 deletions xmtp_networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ mod tests {
assert_eq!(result.envelopes.len(), 0);
}

#[tokio::test]
async fn grpc_batch_query_test() {
let client = Client::create("http://localhost:5556".to_string(), false)
.await
.unwrap();

let result = client.batch_query(vec![]).await.unwrap();

assert_eq!(result.responses.len(), 0);
}

#[tokio::test]
async fn publish_test() {
let client = Client::create("http://localhost:5556".to_string(), false)
Expand Down