Skip to content

Commit

Permalink
allow storing and passing the api client
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Dec 18, 2024
1 parent fbe0420 commit 4a227c5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
23 changes: 17 additions & 6 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -124,23 +127,26 @@ 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)

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

return Client(
Expand All @@ -149,7 +155,8 @@ class Client() {
dbPath,
ffiClient.installationId().toHex(),
ffiClient.inboxId(),
clientOptions.api.env
clientOptions.api.env,
apiClient
)
}

Expand All @@ -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)
}
Expand All @@ -184,7 +192,8 @@ class Client() {
options: ClientOptions,
signingKey: SigningKey?,
appContext: Context,
): Pair<FfiXmtpClient, String> {
apiClient: XmtpApiClient? = null,
): Triple<FfiXmtpClient, String, XmtpApiClient> {
val alias = "xmtp-${options.api.env}-$inboxId"

val mlsDbDirectory = options.dbDirectory
Expand All @@ -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(),
Expand All @@ -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) {
Expand Down

0 comments on commit 4a227c5

Please sign in to comment.