@@ -11,13 +11,17 @@ import com.matejdro.micropebble.common.util.joinUrls
1111import dev.zacsweers.metro.AppScope
1212import dev.zacsweers.metro.ContributesBinding
1313import dev.zacsweers.metro.Inject
14+ import dispatch.core.withIO
1415import io.ktor.client.HttpClient
1516import io.ktor.client.call.body
17+ import io.ktor.client.engine.ProxyBuilder.http
1618import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
1719import io.ktor.client.request.get
20+ import io.ktor.http.parameters
1821import io.ktor.serialization.kotlinx.json.json
1922import kotlinx.coroutines.Dispatchers
2023import kotlinx.coroutines.runBlocking
24+ import kotlinx.coroutines.withContext
2125import kotlinx.serialization.json.Json
2226
2327@Inject
@@ -28,35 +32,35 @@ class ApiClientImpl : ApiClient {
2832 ignoreUnknownKeys = true
2933 }
3034
31- private val http by lazy {
32- runBlocking( Dispatchers . IO ) {
33- HttpClient {
34- install( ContentNegotiation ) {
35- json(json)
36- }
35+ private var client : HttpClient ? = null
36+
37+ private suspend fun getHttp () = client ? : withContext( Dispatchers . IO ) {
38+ HttpClient {
39+ install( ContentNegotiation ) {
40+ json(json)
3741 }
3842 }
39- }
43+ }. also { client = it }
4044
4145 override suspend fun fetchAppListing (updateSource : AppstoreSource , installSource : AppInstallSource ) = runCatching {
42- http .get(updateSource.url.joinUrls(" /v1/apps/id/${installSource.storeId} " )).body<ApplicationList >().data.first()
46+ getHttp() .get(updateSource.url.joinUrls(" /v1/apps/id/${installSource.storeId} " )).body<ApplicationList >().data.first()
4347 }.getOrNull()
4448
4549 override suspend fun fetchAppListing (updateSource : AppstoreSource , appstoreId : String ) =
46- http .get(updateSource.url.joinUrls(" /v1/apps/id/$appstoreId " )).body<AppstoreCollectionPage >().apps.first()
50+ getHttp() .get(updateSource.url.joinUrls(" /v1/apps/id/$appstoreId " )).body<AppstoreCollectionPage >().apps.first()
4751
4852 override suspend fun fetchHomePage (
4953 source : AppstoreSource ,
5054 type : ApplicationType ,
5155 platformFilter : String? ,
52- ) = http .get(source.url.joinUrls(type.apiEndpoint)) {
56+ ) = getHttp() .get(source.url.joinUrls(type.apiEndpoint)) {
5357 url {
5458 platformFilter?.let { parameters[" hardware" ] = it }
5559 }
5660 }.body<AppstoreHomePage >()
5761
5862 override suspend fun fetchCollection (platformFilter : String? , endpoint : String , offset : Int , limit : Int ) =
59- http .get(endpoint) {
63+ getHttp() .get(endpoint) {
6064 url {
6165 platformFilter?.let { parameters[" hardware" ] = it }
6266 parameters[" offset" ] = offset.toString()
0 commit comments