A cheap attempt at a native Bluesky client for Android

Bluesky: use mkClient to create API client

Refactor the client creation logic into a new `mkClient` helper function. This function now handles setting up the `HttpClient` with the correct PDS, session data, and labeler headers.

The client is now re-created with the updated list of labelers after fetching them.

+33 -25
+33 -25
app/src/main/java/industries/geesawra/monarch/datalayer/Bluesky.kt
··· 295 295 val message: String?, 296 296 ) 297 297 298 + private fun mkClient( 299 + pds: String, 300 + sessionData: SessionData, 301 + labelers: List<String> = listOf() 302 + ): AuthenticatedXrpcBlueskyApi { 303 + val hc = HttpClient(OkHttp) { 304 + defaultRequest { 305 + url(pds) 306 + headers["atproto-accept-labelers"] = labelers.joinToString() 307 + } 308 + install(HttpTimeout) { 309 + requestTimeoutMillis = 15000 310 + connectTimeoutMillis = 15000 311 + socketTimeoutMillis = 15000 312 + } 313 + } 314 + 315 + return AuthenticatedXrpcBlueskyApi( 316 + hc, 317 + BlueskyAuthPlugin.Tokens(sessionData.accessJwt, sessionData.refreshJwt) 318 + ) 319 + } 320 + 298 321 private suspend fun refreshIfNeeded( 299 322 pdsURL: String, 300 323 token: SessionData, 301 - labelers: List<String>? = listOf() 302 324 ): Result<Unit> { 303 325 return runCatching { 304 326 val httpClient = HttpClient(OkHttp) { 305 327 defaultRequest { 306 328 url(pdsURL) 307 - labelers?.let { 308 - headers["atproto-accept-labelers"] = labelers.joinToString() 309 - } 310 329 } 311 330 install(HttpTimeout) { 312 331 requestTimeoutMillis = 15000 ··· 326 345 when (gs.status) { 327 346 HttpStatusCode.OK -> run { 328 347 this.session = token 329 - val tokens = 330 - BlueskyAuthPlugin.Tokens(token.accessJwt, token.refreshJwt) 331 - this.client = AuthenticatedXrpcBlueskyApi(httpClient, tokens) 332 348 return Result.success(Unit) 333 349 } 334 350 ··· 357 373 } 358 374 359 375 when (rs.status) { 360 - 361 376 HttpStatusCode.OK -> run { 362 377 val body: String = rs.body() 363 378 val rs: RefreshSessionResponse = ··· 417 432 createMutex.unlock() 418 433 return Result.failure(it) 419 434 } 435 + this.pdsURL = pdsURL 420 436 421 - this.pdsURL = pdsURL 437 + this.client = mkClient( 438 + pdsURL, 439 + sessionData, 440 + ) 422 441 423 442 val labelers = this.subscribedLabelers().getOrThrow().keys.mapNotNull { it?.did } 424 - 425 - refreshIfNeeded(pdsURL, sessionData, labelers).onFailure { 426 - createMutex.unlock() 427 - return Result.failure(it) 428 - } 443 + this.client = mkClient( 444 + pdsURL, 445 + sessionData, 446 + labelers 447 + ) 429 448 430 449 createMutex.unlock() 431 450 } ··· 762 781 else -> false 763 782 } 764 783 } as PreferencesUnion.LabelersPref).value.labelers.map { it.did }.toMutableList() 765 - 766 - val otherOnes = (prefs.preferences.filter { 767 - when (it) { 768 - is PreferencesUnion.ContentLabelPref -> true 769 - else -> false 770 - } 771 - }.map { it as PreferencesUnion.ContentLabelPref }).forEach { 772 - it.value.labelerDid?.let { did -> 773 - labelers += did 774 - } 775 - } 776 784 777 785 val res = client!!.getServices( 778 786 GetServicesQueryParams(