Refactor the ExtensionRepoService to use DTOs (#573)
* Refactor the ExtensionRepoService to use DTOs Slightly refactored the `ExtensionRepoService` so it uses a DTO with `parseAs` to avoid parsing the JSON response by hand. The default Json instance Injekt provides here has `ignoreUnknownKeys` enabled, so the `ExtensionRepoMetaDto` only specifies the meta key of the response content. The extension function `toExtensionRepo` allows for mapping the new DTO to the `domain` `ExtensionRepo` data class. * Implement feedback - Removed SerialName of the ExtensionRepoMetaDto property and renamed it `meta`, same as the incoming attribute. - Added a more general catch clause that also logs the occurring Exception Detekt likes to complain about TooGenericExceptionCaught, hence the Suppress annotation on the function. (cherry picked from commit 8c437ceecf3c5d8d944a70439d3549e21d751736)
This commit is contained in:
parent
268b483182
commit
9ee0034c9a
@ -0,0 +1,27 @@
|
|||||||
|
package mihon.domain.extensionrepo.service
|
||||||
|
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
import mihon.domain.extensionrepo.model.ExtensionRepo
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class ExtensionRepoMetaDto(
|
||||||
|
val meta: ExtensionRepoDto,
|
||||||
|
)
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class ExtensionRepoDto(
|
||||||
|
val name: String,
|
||||||
|
val shortName: String?,
|
||||||
|
val website: String,
|
||||||
|
val signingKeyFingerprint: String,
|
||||||
|
)
|
||||||
|
|
||||||
|
fun ExtensionRepoMetaDto.toExtensionRepo(baseUrl: String): ExtensionRepo {
|
||||||
|
return ExtensionRepo(
|
||||||
|
baseUrl = baseUrl,
|
||||||
|
name = meta.name,
|
||||||
|
shortName = meta.shortName,
|
||||||
|
website = meta.website,
|
||||||
|
signingKeyFingerprint = meta.signingKeyFingerprint,
|
||||||
|
)
|
||||||
|
}
|
@ -2,16 +2,14 @@ package mihon.domain.extensionrepo.service
|
|||||||
|
|
||||||
import androidx.core.net.toUri
|
import androidx.core.net.toUri
|
||||||
import eu.kanade.tachiyomi.network.GET
|
import eu.kanade.tachiyomi.network.GET
|
||||||
import eu.kanade.tachiyomi.network.HttpException
|
|
||||||
import eu.kanade.tachiyomi.network.awaitSuccess
|
import eu.kanade.tachiyomi.network.awaitSuccess
|
||||||
import eu.kanade.tachiyomi.network.parseAs
|
import eu.kanade.tachiyomi.network.parseAs
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import kotlinx.serialization.json.JsonObject
|
import logcat.LogPriority
|
||||||
import kotlinx.serialization.json.jsonObject
|
|
||||||
import kotlinx.serialization.json.jsonPrimitive
|
|
||||||
import mihon.domain.extensionrepo.model.ExtensionRepo
|
import mihon.domain.extensionrepo.model.ExtensionRepo
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import tachiyomi.core.common.util.lang.withIOContext
|
import tachiyomi.core.common.util.lang.withIOContext
|
||||||
|
import tachiyomi.core.common.util.system.logcat
|
||||||
import uy.kohesive.injekt.injectLazy
|
import uy.kohesive.injekt.injectLazy
|
||||||
|
|
||||||
class ExtensionRepoService(
|
class ExtensionRepoService(
|
||||||
@ -20,6 +18,7 @@ class ExtensionRepoService(
|
|||||||
|
|
||||||
private val json: Json by injectLazy()
|
private val json: Json by injectLazy()
|
||||||
|
|
||||||
|
@Suppress("TooGenericExceptionCaught")
|
||||||
suspend fun fetchRepoDetails(
|
suspend fun fetchRepoDetails(
|
||||||
repo: String,
|
repo: String,
|
||||||
): ExtensionRepo? {
|
): ExtensionRepo? {
|
||||||
@ -27,31 +26,16 @@ class ExtensionRepoService(
|
|||||||
val url = "$repo/repo.json".toUri()
|
val url = "$repo/repo.json".toUri()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
val response = with(json) {
|
with(json) {
|
||||||
client.newCall(GET(url.toString()))
|
client.newCall(GET(url.toString()))
|
||||||
.awaitSuccess()
|
.awaitSuccess()
|
||||||
.parseAs<JsonObject>()
|
.parseAs<ExtensionRepoMetaDto>()
|
||||||
|
.toExtensionRepo(baseUrl = repo)
|
||||||
}
|
}
|
||||||
response["meta"]
|
} catch (e: Exception) {
|
||||||
?.jsonObject
|
logcat(LogPriority.ERROR, e) { "Failed to fetch repo details" }
|
||||||
?.let { jsonToExtensionRepo(baseUrl = repo, it) }
|
|
||||||
} catch (_: HttpException) {
|
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun jsonToExtensionRepo(baseUrl: String, obj: JsonObject): ExtensionRepo? {
|
|
||||||
return try {
|
|
||||||
ExtensionRepo(
|
|
||||||
baseUrl = baseUrl,
|
|
||||||
name = obj["name"]!!.jsonPrimitive.content,
|
|
||||||
shortName = obj["shortName"]?.jsonPrimitive?.content,
|
|
||||||
website = obj["website"]!!.jsonPrimitive.content,
|
|
||||||
signingKeyFingerprint = obj["signingKeyFingerprint"]!!.jsonPrimitive.content,
|
|
||||||
)
|
|
||||||
} catch (_: NullPointerException) {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user