refactor: move parsing Version to GithubService

This commit is contained in:
Diamond 2022-09-17 12:51:08 -07:00
parent 69ff9b531c
commit 7c7313ca1a
No known key found for this signature in database
GPG key ID: DCBE5952BB3B6420
3 changed files with 10 additions and 8 deletions

View file

@ -11,7 +11,6 @@ import org.koin.dsl.module
val httpModule = module {
fun provideJson() = Json {
ignoreUnknownKeys = true
isLenient = true
}
fun provideHttpClient(json: Json) = HttpClient(CIO) {

View file

@ -7,7 +7,6 @@ import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
class GithubRepository(
private val json: Json,
private val service: GithubService
) {
suspend fun getCommits(page: Int) = service.getCommits(page)
@ -15,5 +14,5 @@ class GithubRepository(
suspend fun getContributors() = service.getContributors()
.sortedByDescending(GithubUser::contributions)
suspend fun getVersion() = json.decodeFromString<Version>(service.getVersion())
suspend fun getVersion() = service.getVersion()
}

View file

@ -1,16 +1,18 @@
package com.aliucord.manager.network.service
import com.aliucord.manager.network.dto.Commit
import com.aliucord.manager.network.dto.GithubUser
import com.aliucord.manager.network.dto.*
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
class GithubService(
private val httpClient: HttpClient
private val httpClient: HttpClient,
private val json: Json,
) {
suspend fun getCommits(page: Int): List<Commit> = withContext(Dispatchers.IO) {
httpClient.get(COMMITS_URL) {
@ -18,8 +20,10 @@ class GithubService(
}.body()
}
suspend fun getVersion(): String = withContext(Dispatchers.IO) {
httpClient.get(DATA_URL).bodyAsText()
suspend fun getVersion(): Version = withContext(Dispatchers.IO) {
// Return content type is plain text so manually decoding
val res = httpClient.get(DATA_URL).bodyAsText()
json.decodeFromString(res)
}
suspend fun getContributors(): List<GithubUser> = withContext(Dispatchers.IO) {