From 2d19729869d1c88a46c0d1a4582d29a80d7ab9cb Mon Sep 17 00:00:00 2001 From: stevenyomi <95685115+stevenyomi@users.noreply.github.com> Date: Wed, 19 Oct 2022 00:09:23 +0800 Subject: [PATCH] Clean up OkHttp methods and parse from Okio directly (#8238) --- .../myanimelist/MyAnimeListInterceptor.kt | 7 +---- core/build.gradle.kts | 1 + .../tachiyomi/network/OkHttpExtensions.kt | 26 ++++++++++++------- gradle/kotlinx.versions.toml | 3 ++- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListInterceptor.kt index cf26d57e8e..da1768178d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListInterceptor.kt @@ -1,17 +1,12 @@ package eu.kanade.tachiyomi.data.track.myanimelist import eu.kanade.tachiyomi.network.parseAs -import kotlinx.serialization.json.Json import okhttp3.Interceptor import okhttp3.Response -import okhttp3.internal.closeQuietly -import uy.kohesive.injekt.injectLazy import java.io.IOException class MyAnimeListInterceptor(private val myanimelist: MyAnimeList, private var token: String?) : Interceptor { - private val json: Json by injectLazy() - private var oauth: OAuth? = null override fun intercept(chain: Interceptor.Chain): Response { @@ -31,7 +26,7 @@ class MyAnimeListInterceptor(private val myanimelist: MyAnimeList, private var t if (oauthResponse.isSuccessful) { oauthResponse.parseAs() } else { - oauthResponse.closeQuietly() + oauthResponse.close() null } } diff --git a/core/build.gradle.kts b/core/build.gradle.kts index 8ec7f65fa5..f769da7c3c 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -37,6 +37,7 @@ dependencies { api(kotlinx.coroutines.core) api(kotlinx.serialization.json) + api(kotlinx.serialization.json.okio) api(libs.injekt.core) diff --git a/core/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt b/core/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt index 34b95adeb3..384269df07 100644 --- a/core/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt +++ b/core/src/main/java/eu/kanade/tachiyomi/network/OkHttpExtensions.kt @@ -2,23 +2,27 @@ package eu.kanade.tachiyomi.network import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.suspendCancellableCoroutine -import kotlinx.serialization.decodeFromString +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.KSerializer import kotlinx.serialization.json.Json +import kotlinx.serialization.json.okio.decodeFromBufferedSource +import kotlinx.serialization.serializer import okhttp3.Call import okhttp3.Callback import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response -import okhttp3.internal.closeQuietly import rx.Observable import rx.Producer import rx.Subscription import uy.kohesive.injekt.Injekt -import uy.kohesive.injekt.api.fullType +import uy.kohesive.injekt.api.get import java.io.IOException import java.util.concurrent.atomic.AtomicBoolean import kotlin.coroutines.resumeWithException +import kotlin.reflect.KType +import kotlin.reflect.typeOf val jsonMime = "application/json; charset=utf-8".toMediaType() @@ -72,7 +76,7 @@ suspend fun Call.await(): Response { } continuation.resume(response) { - response.body.closeQuietly() + response.body.close() } } @@ -118,11 +122,15 @@ fun OkHttpClient.newCachelessCallWithProgress(request: Request, listener: Progre } inline fun Response.parseAs(): T { - // Avoiding Injekt.get() due to compiler issues - val json = Injekt.getInstance(fullType().type) - this.use { - val responseBody = it.body.string() - return json.decodeFromString(responseBody) + return internalParseAs(this, typeOf()) +} + +@Suppress("UNCHECKED_CAST") +@OptIn(ExperimentalSerializationApi::class) +fun internalParseAs(response: Response, type: KType): T { + val deserializer = serializer(type) as KSerializer + return response.body.source().use { + Injekt.get().decodeFromBufferedSource(deserializer, it) } } diff --git a/gradle/kotlinx.versions.toml b/gradle/kotlinx.versions.toml index b3ee527612..b4a2fbc3b5 100644 --- a/gradle/kotlinx.versions.toml +++ b/gradle/kotlinx.versions.toml @@ -12,6 +12,7 @@ coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", ve coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines_version" } serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "serialization_version" } +serialization-json-okio = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json-okio", version.ref = "serialization_version" } serialization-protobuf = { module = "org.jetbrains.kotlinx:kotlinx-serialization-protobuf", version.ref = "serialization_version" } serialization-gradle = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "kotlin_version" } serialization-xml-core = { module = "io.github.pdvrieze.xmlutil:core-android", version.ref = "xml_serialization_version" } @@ -19,7 +20,7 @@ serialization-xml = { module = "io.github.pdvrieze.xmlutil:serialization-android [bundles] coroutines = ["coroutines-core", "coroutines-android"] -serialization = ["serialization-json", "serialization-protobuf", "serialization-xml-core", "serialization-xml"] +serialization = ["serialization-json", "serialization-json-okio", "serialization-protobuf", "serialization-xml-core", "serialization-xml"] [plugins] android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin_version" } \ No newline at end of file