Refactor response parsing helper function
(cherry picked from commit 8f98055e9ec8a40a5d41bad3e02f409119b7abe8) # Conflicts: # app/src/main/java/eu/kanade/tachiyomi/data/updater/github/GithubUpdateChecker.kt # app/src/main/java/eu/kanade/tachiyomi/extension/api/ExtensionGithubApi.kt
This commit is contained in:
parent
df1daa6b7f
commit
959559a89f
@ -5,7 +5,7 @@ import eu.kanade.tachiyomi.data.updater.UpdateResult
|
|||||||
import eu.kanade.tachiyomi.network.GET
|
import eu.kanade.tachiyomi.network.GET
|
||||||
import eu.kanade.tachiyomi.network.NetworkHelper
|
import eu.kanade.tachiyomi.network.NetworkHelper
|
||||||
import eu.kanade.tachiyomi.network.await
|
import eu.kanade.tachiyomi.network.await
|
||||||
import eu.kanade.tachiyomi.network.withResponse
|
import eu.kanade.tachiyomi.network.parseAs
|
||||||
import exh.syDebugVersion
|
import exh.syDebugVersion
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
@ -30,7 +30,8 @@ class GithubUpdateChecker {
|
|||||||
networkService.client
|
networkService.client
|
||||||
.newCall(GET("https://api.github.com/repos/$repo/releases/latest"))
|
.newCall(GET("https://api.github.com/repos/$repo/releases/latest"))
|
||||||
.await()
|
.await()
|
||||||
.withResponse<GithubRelease, UpdateResult> {
|
.parseAs<GithubRelease>()
|
||||||
|
.let {
|
||||||
// Check if latest version is different from current version
|
// Check if latest version is different from current version
|
||||||
// SY -->
|
// SY -->
|
||||||
val newVersion = it.version
|
val newVersion = it.version
|
||||||
|
@ -8,7 +8,7 @@ import eu.kanade.tachiyomi.extension.util.ExtensionLoader
|
|||||||
import eu.kanade.tachiyomi.network.GET
|
import eu.kanade.tachiyomi.network.GET
|
||||||
import eu.kanade.tachiyomi.network.NetworkHelper
|
import eu.kanade.tachiyomi.network.NetworkHelper
|
||||||
import eu.kanade.tachiyomi.network.await
|
import eu.kanade.tachiyomi.network.await
|
||||||
import eu.kanade.tachiyomi.network.withResponse
|
import eu.kanade.tachiyomi.network.parseAs
|
||||||
import exh.source.BlacklistedSources
|
import exh.source.BlacklistedSources
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
@ -29,15 +29,15 @@ internal class ExtensionGithubApi {
|
|||||||
networkService.client
|
networkService.client
|
||||||
.newCall(GET("${REPO_URL_PREFIX}index.min.json"))
|
.newCall(GET("${REPO_URL_PREFIX}index.min.json"))
|
||||||
.await()
|
.await()
|
||||||
.withResponse<JsonArray, List<Extension.Available>> {
|
.parseAs<JsonArray>()
|
||||||
parseResponse(it)
|
.let { parseResponse(it) }
|
||||||
}
|
|
||||||
} /* SY --> */ + preferences.extensionRepos().get().flatMap { repoPath ->
|
} /* SY --> */ + preferences.extensionRepos().get().flatMap { repoPath ->
|
||||||
val url = "$BASE_URL$repoPath/repo/"
|
val url = "$BASE_URL$repoPath/repo/"
|
||||||
networkService.client
|
networkService.client
|
||||||
.newCall(GET("${url}index.min.json"))
|
.newCall(GET("${url}index.min.json"))
|
||||||
.await()
|
.await()
|
||||||
.withResponse<JsonArray, List<Extension.Available>> {
|
.parseAs<JsonArray>()
|
||||||
|
.let {
|
||||||
parseResponse(it, url)
|
parseResponse(it, url)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -110,10 +110,9 @@ fun OkHttpClient.newCallWithProgress(request: Request, listener: ProgressListene
|
|||||||
return progressClient.newCall(request)
|
return progressClient.newCall(request)
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified T, R> Response.withResponse(block: (T) -> R): R {
|
inline fun <reified T> Response.parseAs(): T {
|
||||||
this.use {
|
this.use {
|
||||||
val responseBody = it.body?.string().orEmpty()
|
val responseBody = it.body?.string().orEmpty()
|
||||||
val response = Injekt.get<Json>().decodeFromString<T>(responseBody)
|
return Injekt.get<Json>().decodeFromString<T>(responseBody)
|
||||||
return block(response)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user