Skip to content

Commit f73a939

Browse files
committed
refactor(appstore): move http to be a suspending function
1 parent 318b027 commit f73a939

File tree

1 file changed

+15
-11
lines changed
  • appstore/data/src/main/kotlin/com/matejdro/micropebble/appstore/data

1 file changed

+15
-11
lines changed

appstore/data/src/main/kotlin/com/matejdro/micropebble/appstore/data/ApiClientImpl.kt

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ import com.matejdro.micropebble.common.util.joinUrls
1111
import dev.zacsweers.metro.AppScope
1212
import dev.zacsweers.metro.ContributesBinding
1313
import dev.zacsweers.metro.Inject
14+
import dispatch.core.withIO
1415
import io.ktor.client.HttpClient
1516
import io.ktor.client.call.body
17+
import io.ktor.client.engine.ProxyBuilder.http
1618
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
1719
import io.ktor.client.request.get
20+
import io.ktor.http.parameters
1821
import io.ktor.serialization.kotlinx.json.json
1922
import kotlinx.coroutines.Dispatchers
2023
import kotlinx.coroutines.runBlocking
24+
import kotlinx.coroutines.withContext
2125
import 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

Comments
 (0)