Skip to content

Commit 49b6553

Browse files
committed
fix it all for batch query
1 parent b6e4646 commit 49b6553

File tree

1 file changed

+11
-28
lines changed

1 file changed

+11
-28
lines changed

library/src/main/java/org/xmtp/android/library/ApiClient.kt

+11-28
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import uniffi.xmtpv3.FfiPagingInfo
2828
import uniffi.xmtpv3.FfiPublishRequest
2929
import uniffi.xmtpv3.FfiSortDirection
3030
import uniffi.xmtpv3.FfiV2ApiClient
31+
import uniffi.xmtpv3.FfiV2BatchQueryRequest
32+
import uniffi.xmtpv3.FfiV2BatchQueryResponse
3133
import uniffi.xmtpv3.FfiV2QueryRequest
3234
import uniffi.xmtpv3.FfiV2QueryResponse
3335
import java.io.Closeable
@@ -57,15 +59,6 @@ data class GRPCApiClient(
5759
) :
5860
ApiClient, Closeable {
5961
companion object {
60-
val AUTHORIZATION_HEADER_KEY: Metadata.Key<String> =
61-
Metadata.Key.of("authorization", Metadata.ASCII_STRING_MARSHALLER)
62-
63-
val CLIENT_VERSION_HEADER_KEY: Metadata.Key<String> =
64-
Metadata.Key.of("X-Client-Version", Metadata.ASCII_STRING_MARSHALLER)
65-
66-
val APP_VERSION_HEADER_KEY: Metadata.Key<String> =
67-
Metadata.Key.of("X-App-Version", Metadata.ASCII_STRING_MARSHALLER)
68-
6962
fun makeQueryRequest(
7063
topic: String,
7164
pagination: Pagination? = null,
@@ -144,17 +137,8 @@ data class GRPCApiClient(
144137
override suspend fun batchQuery(
145138
requests: List<QueryRequest>,
146139
): BatchQueryResponse {
147-
val batchRequest = BatchQueryRequest.newBuilder().addAllRequests(requests).build()
148-
val headers = Metadata()
149-
150-
authToken?.let { token ->
151-
headers.put(AUTHORIZATION_HEADER_KEY, "Bearer $token")
152-
}
153-
headers.put(CLIENT_VERSION_HEADER_KEY, Constants.VERSION)
154-
if (appVersion != null) {
155-
headers.put(APP_VERSION_HEADER_KEY, appVersion)
156-
}
157-
return rustV2Client.batchQuery(batchRequest, headers = headers)
140+
val batchRequest = requests.map { queryRequestToFFi(it) }
141+
return batchQueryResponseFromFFi(rustV2Client.batchQuery(FfiV2BatchQueryRequest(requests = batchRequest)))
158142
}
159143

160144
override suspend fun publish(envelopes: List<Envelope>) {
@@ -165,13 +149,6 @@ data class GRPCApiClient(
165149
}
166150

167151
override suspend fun subscribe(request: Flow<SubscribeRequest>): Flow<Envelope> {
168-
val headers = Metadata()
169-
170-
headers.put(CLIENT_VERSION_HEADER_KEY, Constants.VERSION)
171-
if (appVersion != null) {
172-
headers.put(APP_VERSION_HEADER_KEY, appVersion)
173-
}
174-
175152
return rustV2Client.subscribe2(request, headers)
176153
}
177154

@@ -189,7 +166,7 @@ data class GRPCApiClient(
189166

190167
private fun envelopeFromFFi(envelope: FfiEnvelope): Envelope {
191168
return Envelope.newBuilder().also {
192-
it.contentTopic = envelope.contentTopic,
169+
it.contentTopic = envelope.contentTopic
193170
it.timestampNs = envelope.timestampNs.toLong()
194171
it.message = envelope.message.toByteString()
195172
}.build()
@@ -213,6 +190,12 @@ data class GRPCApiClient(
213190
}.build()
214191
}
215192

193+
private fun batchQueryResponseFromFFi(response: FfiV2BatchQueryResponse): BatchQueryResponse {
194+
return BatchQueryResponse.newBuilder().also { queryResponse ->
195+
queryResponse.addAllResponses(response.responses.map { queryResponseFromFFi(it) })
196+
}.build()
197+
}
198+
216199
private fun pagingInfoFromFFi(info: FfiPagingInfo): PagingInfo {
217200
return PagingInfo.newBuilder().also {
218201
it.limit = info.limit.toInt()

0 commit comments

Comments
 (0)