From 4a227c5c541d34fcebbbc26adfd9ece64152b16e Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Tue, 17 Dec 2024 20:53:45 -0800 Subject: [PATCH] allow storing and passing the api client --- .../org/xmtp/android/library/ClientTest.kt | 22 ++++++++++++++++++ .../java/org/xmtp/android/library/Client.kt | 23 ++++++++++++++----- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt b/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt index c30f8cc8..1354e7bc 100644 --- a/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt +++ b/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt @@ -557,10 +557,32 @@ class ClientTest { val time3 = end3.time - start3.time Log.d("PERF", "Built a client with inboxId in ${time3 / 1000.0}s") + val start4 = Date() + val buildClient3 = runBlocking { + Client().build( + fakeWallet.address, + options = ClientOptions( + ClientOptions.Api(XMTPEnvironment.DEV, true), + appContext = context, + dbEncryptionKey = key + ), + inboxId = client.inboxId, + apiClient = client.apiClient + ) + } + val end4 = Date() + val time4 = end4.time - start4.time + Log.d("PERF", "Built a client with inboxId and apiClient in ${time4 / 1000.0}s") + + assert(time2 < time1) assert(time3 < time1) assert(time3 < time2) + assert(time4 < time1) + assert(time4 < time2) + assert(time4 < time3) assertEquals(client.inboxId, buildClient1.inboxId) assertEquals(client.inboxId, buildClient2.inboxId) + assertEquals(client.inboxId, buildClient3.inboxId) } } diff --git a/library/src/main/java/org/xmtp/android/library/Client.kt b/library/src/main/java/org/xmtp/android/library/Client.kt index 334ee4bf..fb046737 100644 --- a/library/src/main/java/org/xmtp/android/library/Client.kt +++ b/library/src/main/java/org/xmtp/android/library/Client.kt @@ -10,6 +10,7 @@ import uniffi.xmtpv3.FfiConversationType import uniffi.xmtpv3.FfiDeviceSyncKind import uniffi.xmtpv3.FfiSignatureRequest import uniffi.xmtpv3.FfiXmtpClient +import uniffi.xmtpv3.XmtpApiClient import uniffi.xmtpv3.connectToBackend import uniffi.xmtpv3.createClient import uniffi.xmtpv3.generateInboxId @@ -47,6 +48,7 @@ class Client() { lateinit var conversations: Conversations lateinit var environment: XMTPEnvironment lateinit var dbPath: String + lateinit var apiClient: XmtpApiClient val libXMTPVersion: String = getVersionInfo() private lateinit var ffiClient: FfiXmtpClient @@ -114,6 +116,7 @@ class Client() { installationId: String, inboxId: String, environment: XMTPEnvironment, + apiClient: XmtpApiClient, ) : this() { this.address = address.lowercase() this.preferences = PrivatePreferences(client = this, ffiClient = libXMTPClient) @@ -124,6 +127,7 @@ class Client() { this.installationId = installationId this.inboxId = inboxId this.environment = environment + this.apiClient = apiClient } private suspend fun initializeV3Client( @@ -131,16 +135,18 @@ class Client() { clientOptions: ClientOptions, signingKey: SigningKey? = null, inboxId: String? = null, + apiClient: XmtpApiClient? = null, ): Client { val accountAddress = address.lowercase() val recoveredInboxId = inboxId ?: getOrCreateInboxId(clientOptions.api, accountAddress) - val (ffiClient, dbPath) = createFfiClient( + val (ffiClient, dbPath, apiClient) = createFfiClient( accountAddress, recoveredInboxId, clientOptions, signingKey, clientOptions.appContext, + apiClient, ) return Client( @@ -149,7 +155,8 @@ class Client() { dbPath, ffiClient.installationId().toHex(), ffiClient.inboxId(), - clientOptions.api.env + clientOptions.api.env, + apiClient ) } @@ -170,9 +177,10 @@ class Client() { address: String, options: ClientOptions, inboxId: String? = null, + apiClient: XmtpApiClient? = null, ): Client { return try { - initializeV3Client(address, options, inboxId = inboxId) + initializeV3Client(address, options, inboxId = inboxId, apiClient = apiClient) } catch (e: Exception) { throw XMTPException("Error creating V3 client: ${e.message}", e) } @@ -184,7 +192,8 @@ class Client() { options: ClientOptions, signingKey: SigningKey?, appContext: Context, - ): Pair { + apiClient: XmtpApiClient? = null, + ): Triple { val alias = "xmtp-${options.api.env}-$inboxId" val mlsDbDirectory = options.dbDirectory @@ -196,8 +205,10 @@ class Client() { directoryFile.mkdir() dbPath = directoryFile.absolutePath + "/$alias.db3" + val xmtpApiClient = + apiClient ?: connectToBackend(options.api.env.getUrl(), options.api.isSecure) val ffiClient = createClient( - api = connectToBackend(options.api.env.getUrl(), options.api.isSecure), + api = xmtpApiClient, db = dbPath, encryptionKey = options.dbEncryptionKey, accountAddress = accountAddress.lowercase(), @@ -217,7 +228,7 @@ class Client() { ?: throw XMTPException("No signer passed but signer was required.") ffiClient.registerIdentity(signatureRequest) } - return Pair(ffiClient, dbPath) + return Triple(ffiClient, dbPath, xmtpApiClient) } suspend fun revokeAllOtherInstallations(signingKey: SigningKey) {