mirror of
https://github.com/mihonapp/mihon.git
synced 2024-12-25 02:18:24 +01:00
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.
This commit is contained in:
parent
9672ea8b1b
commit
8c437ceecf
@ -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 eu.kanade.tachiyomi.network.GET
|
||||
import eu.kanade.tachiyomi.network.HttpException
|
||||
import eu.kanade.tachiyomi.network.awaitSuccess
|
||||
import eu.kanade.tachiyomi.network.parseAs
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.jsonObject
|
||||
import kotlinx.serialization.json.jsonPrimitive
|
||||
import logcat.LogPriority
|
||||
import mihon.domain.extensionrepo.model.ExtensionRepo
|
||||
import okhttp3.OkHttpClient
|
||||
import tachiyomi.core.common.util.lang.withIOContext
|
||||
import tachiyomi.core.common.util.system.logcat
|
||||
import uy.kohesive.injekt.injectLazy
|
||||
|
||||
class ExtensionRepoService(
|
||||
@ -20,6 +18,7 @@ class ExtensionRepoService(
|
||||
|
||||
private val json: Json by injectLazy()
|
||||
|
||||
@Suppress("TooGenericExceptionCaught")
|
||||
suspend fun fetchRepoDetails(
|
||||
repo: String,
|
||||
): ExtensionRepo? {
|
||||
@ -27,31 +26,16 @@ class ExtensionRepoService(
|
||||
val url = "$repo/repo.json".toUri()
|
||||
|
||||
try {
|
||||
val response = with(json) {
|
||||
with(json) {
|
||||
client.newCall(GET(url.toString()))
|
||||
.awaitSuccess()
|
||||
.parseAs<JsonObject>()
|
||||
.parseAs<ExtensionRepoMetaDto>()
|
||||
.toExtensionRepo(baseUrl = repo)
|
||||
}
|
||||
response["meta"]
|
||||
?.jsonObject
|
||||
?.let { jsonToExtensionRepo(baseUrl = repo, it) }
|
||||
} catch (_: HttpException) {
|
||||
} catch (e: Exception) {
|
||||
logcat(LogPriority.ERROR, e) { "Failed to fetch repo details" }
|
||||
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…
Reference in New Issue
Block a user