Remove last Gson usage at MangaDex (#7656)

* Remove last Gson usage at MangaDex.

* Use ImageReportDto instead of buildJsonObject.
This commit is contained in:
Alessandro Jean 2021-06-15 12:11:03 -03:00 committed by GitHub
parent 1175b0d1c7
commit 6991d96d59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 14 deletions

View File

@ -6,7 +6,7 @@ ext {
extName = 'MangaDex' extName = 'MangaDex'
pkgNameSuffix = 'all.mangadex' pkgNameSuffix = 'all.mangadex'
extClass = '.MangaDexFactory' extClass = '.MangaDexFactory'
extVersionCode = 119 extVersionCode = 120
libVersion = '1.2' libVersion = '1.2'
containsNsfw = true containsNsfw = true
} }

View File

@ -1,15 +1,19 @@
package eu.kanade.tachiyomi.extension.all.mangadex package eu.kanade.tachiyomi.extension.all.mangadex
import android.util.Log import android.util.Log
import com.google.gson.Gson import eu.kanade.tachiyomi.extension.all.mangadex.dto.ImageReportDto
import eu.kanade.tachiyomi.lib.ratelimit.RateLimitInterceptor import eu.kanade.tachiyomi.lib.ratelimit.RateLimitInterceptor
import eu.kanade.tachiyomi.network.POST import eu.kanade.tachiyomi.network.POST
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import okhttp3.Headers import okhttp3.Headers
import okhttp3.Interceptor import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.RequestBody import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response import okhttp3.Response
import okhttp3.internal.closeQuietly import okhttp3.internal.closeQuietly
import uy.kohesive.injekt.injectLazy
/** /**
* Rate limit requests ignore covers though * Rate limit requests ignore covers though
@ -33,7 +37,8 @@ class MdAtHomeReportInterceptor(
private val headers: Headers private val headers: Headers
) : Interceptor { ) : Interceptor {
private val gson: Gson by lazy { Gson() } private val json: Json by injectLazy()
private val mdAtHomeUrlRegex = private val mdAtHomeUrlRegex =
Regex("""^https://[\w\d]+\.[\w\d]+\.mangadex(\b-test\b)?\.network.*${'$'}""") Regex("""^https://[\w\d]+\.[\w\d]+\.mangadex(\b-test\b)?\.network.*${'$'}""")
@ -43,22 +48,24 @@ class MdAtHomeReportInterceptor(
return chain.proceed(chain.request()).let { response -> return chain.proceed(chain.request()).let { response ->
val url = originalRequest.url.toString() val url = originalRequest.url.toString()
if (url.contains(mdAtHomeUrlRegex)) { if (url.contains(mdAtHomeUrlRegex)) {
val cachedImage = response.header("X-Cache", "") == "HIT" val byteSize = response.peekBody(Long.MAX_VALUE).bytes().size
val jsonString = gson.toJson( val duration = response.receivedResponseAtMillis - response.sentRequestAtMillis
mapOf( val cache = response.header("X-Cache", "") == "HIT"
"url" to url, val result = ImageReportDto(
"success" to response.isSuccessful, url,
"bytes" to response.peekBody(Long.MAX_VALUE).bytes().size, response.isSuccessful,
"duration" to response.receivedResponseAtMillis - response.sentRequestAtMillis, byteSize,
"cached" to cachedImage, cache,
) duration
) )
val jsonString = json.encodeToString(result)
val postResult = client.newCall( val postResult = client.newCall(
POST( POST(
MDConstants.atHomePostUrl, MDConstants.atHomePostUrl,
headers, headers,
RequestBody.create(null, jsonString) jsonString.toRequestBody("application/json".toMediaType())
) )
) )
try { try {