Refactor response parsing helper function

This commit is contained in:
arkon 2020-12-27 10:22:24 -05:00
parent 84ae61f72c
commit 8f98055e9e
3 changed files with 8 additions and 9 deletions

View file

@ -5,7 +5,7 @@ import eu.kanade.tachiyomi.data.updater.UpdateResult
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.NetworkHelper
import eu.kanade.tachiyomi.network.await
import eu.kanade.tachiyomi.network.withResponse
import eu.kanade.tachiyomi.network.parseAs
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import uy.kohesive.injekt.injectLazy
@ -27,7 +27,8 @@ class GithubUpdateChecker {
networkService.client
.newCall(GET("https://api.github.com/repos/$repo/releases/latest"))
.await()
.withResponse<GithubRelease, UpdateResult> {
.parseAs<GithubRelease>()
.let {
// Check if latest version is different from current version
if (isNewVersion(it.version)) {
GithubUpdateResult.NewUpdate(it)

View file

@ -8,7 +8,7 @@ import eu.kanade.tachiyomi.extension.util.ExtensionLoader
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.NetworkHelper
import eu.kanade.tachiyomi.network.await
import eu.kanade.tachiyomi.network.withResponse
import eu.kanade.tachiyomi.network.parseAs
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.JsonArray
@ -28,9 +28,8 @@ internal class ExtensionGithubApi {
networkService.client
.newCall(GET("${REPO_URL_PREFIX}index.min.json"))
.await()
.withResponse<JsonArray, List<Extension.Available>> {
parseResponse(it)
}
.parseAs<JsonArray>()
.let { parseResponse(it) }
}
}

View file

@ -110,10 +110,9 @@ fun OkHttpClient.newCallWithProgress(request: Request, listener: ProgressListene
return progressClient.newCall(request)
}
inline fun <reified T, R> Response.withResponse(block: (T) -> R): R {
inline fun <reified T> Response.parseAs(): T {
this.use {
val responseBody = it.body?.string().orEmpty()
val response = Injekt.get<Json>().decodeFromString<T>(responseBody)
return block(response)
return Injekt.get<Json>().decodeFromString<T>(responseBody)
}
}