Skip to content

Commit

Permalink
remove the apiClient from the functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Dec 19, 2024
1 parent 6221567 commit de9aeea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -557,31 +557,27 @@ class ClientTest {
val time3 = end3.time - start3.time
Log.d("PERF", "Built a client with inboxId in ${time3 / 1000.0}s")

runBlocking { Client.connectToApiBackend(ClientOptions.Api(XMTPEnvironment.DEV, true)) }
val start4 = Date()
val buildClient3 = runBlocking {
Client().build(
fakeWallet.address,
runBlocking {
Client().create(
PrivateKeyBuilder(),
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")
Log.d("PERF", "Create a client after prebuilding 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)
}
}
44 changes: 20 additions & 24 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.xmtp.android.library

import android.content.Context
import com.google.protobuf.api
import kotlinx.coroutines.runBlocking
import org.xmtp.android.library.codecs.ContentCodec
import org.xmtp.android.library.codecs.TextCodec
Expand Down Expand Up @@ -49,7 +48,6 @@ 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

Expand All @@ -62,17 +60,26 @@ class Client() {
registry
}

private val apiClientCache = mutableMapOf<String, XmtpApiClient>()
private val cacheLock = Any()

suspend fun connectToApiBackend(api: ClientOptions.Api): XmtpApiClient {
return connectToBackend(api.env.getUrl(), api.isSecure)
val cacheKey = api.env.getUrl()
return synchronized(cacheLock) {
apiClientCache.getOrPut(cacheKey) {
runBlocking {
connectToBackend(api.env.getUrl(), api.isSecure)
}
}
}
}

suspend fun getOrCreateInboxId(
api: ClientOptions.Api,
address: String,
apiClient: XmtpApiClient? = null,
): String {
var inboxId = getInboxIdForAddress(
api = apiClient ?: connectToApiBackend(api),
api = connectToApiBackend(api),
accountAddress = address.lowercase()
)
if (inboxId.isNullOrBlank()) {
Expand All @@ -89,7 +96,6 @@ class Client() {
accountAddresses: List<String>,
appContext: Context,
api: ClientOptions.Api,
apiClient: XmtpApiClient? = null,
): Map<String, Boolean> {
val accountAddress = "0x0000000000000000000000000000000000000000"
val inboxId = getOrCreateInboxId(api, accountAddress)
Expand All @@ -100,7 +106,7 @@ class Client() {
val dbPath = directoryFile.absolutePath + "/$alias.db3"

val ffiClient = createClient(
api = apiClient ?: connectToApiBackend(api),
api = connectToApiBackend(api),
db = dbPath,
encryptionKey = null,
accountAddress = accountAddress.lowercase(),
Expand All @@ -125,7 +131,6 @@ class Client() {
installationId: String,
inboxId: String,
environment: XMTPEnvironment,
apiClient: XmtpApiClient,
) : this() {
this.address = address.lowercase()
this.preferences = PrivatePreferences(client = this, ffiClient = libXMTPClient)
Expand All @@ -136,27 +141,24 @@ class Client() {
this.installationId = installationId
this.inboxId = inboxId
this.environment = environment
this.apiClient = apiClient
}

private suspend fun initializeV3Client(
address: String,
clientOptions: ClientOptions,
signingKey: SigningKey? = null,
inboxId: String? = null,
apiClient: XmtpApiClient? = null,
): Client {
val accountAddress = address.lowercase()
val recoveredInboxId =
inboxId ?: getOrCreateInboxId(clientOptions.api, accountAddress, apiClient)
inboxId ?: getOrCreateInboxId(clientOptions.api, accountAddress)

val (ffiClient, dbPath, xmtpApiClient) = createFfiClient(
val (ffiClient, dbPath) = createFfiClient(
accountAddress,
recoveredInboxId,
clientOptions,
signingKey,
clientOptions.appContext,
apiClient,
)

return Client(
Expand All @@ -166,18 +168,16 @@ class Client() {
ffiClient.installationId().toHex(),
ffiClient.inboxId(),
clientOptions.api.env,
xmtpApiClient
)
}

// Function to create a V3 client with a signing key
suspend fun create(
account: SigningKey,
options: ClientOptions,
apiClient: XmtpApiClient? = null,
): Client {
return try {
initializeV3Client(account.address, options, account, apiClient = apiClient)
initializeV3Client(account.address, options, account)
} catch (e: Exception) {
throw XMTPException("Error creating V3 client: ${e.message}", e)
}
Expand All @@ -188,10 +188,9 @@ class Client() {
address: String,
options: ClientOptions,
inboxId: String? = null,
apiClient: XmtpApiClient? = null,
): Client {
return try {
initializeV3Client(address, options, inboxId = inboxId, apiClient = apiClient)
initializeV3Client(address, options, inboxId = inboxId)
} catch (e: Exception) {
throw XMTPException("Error creating V3 client: ${e.message}", e)
}
Expand All @@ -203,8 +202,7 @@ class Client() {
options: ClientOptions,
signingKey: SigningKey?,
appContext: Context,
apiClient: XmtpApiClient? = null,
): Triple<FfiXmtpClient, String, XmtpApiClient> {
): Pair<FfiXmtpClient, String> {
val alias = "xmtp-${options.api.env}-$inboxId"

val mlsDbDirectory = options.dbDirectory
Expand All @@ -216,10 +214,8 @@ class Client() {
directoryFile.mkdir()
dbPath = directoryFile.absolutePath + "/$alias.db3"

val xmtpApiClient =
apiClient ?: connectToApiBackend(options.api)
val ffiClient = createClient(
api = xmtpApiClient,
api = connectToApiBackend(options.api),
db = dbPath,
encryptionKey = options.dbEncryptionKey,
accountAddress = accountAddress.lowercase(),
Expand All @@ -239,7 +235,7 @@ class Client() {
?: throw XMTPException("No signer passed but signer was required.")
ffiClient.registerIdentity(signatureRequest)
}
return Triple(ffiClient, dbPath, xmtpApiClient)
return Pair(ffiClient, dbPath)
}

suspend fun revokeAllOtherInstallations(signingKey: SigningKey) {
Expand Down

0 comments on commit de9aeea

Please sign in to comment.