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 { val httpModule = module {
fun provideJson() = Json { fun provideJson() = Json {
ignoreUnknownKeys = true ignoreUnknownKeys = true
isLenient = true
} }
fun provideHttpClient(json: Json) = HttpClient(CIO) { fun provideHttpClient(json: Json) = HttpClient(CIO) {

View file

@ -7,7 +7,6 @@ import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
class GithubRepository( class GithubRepository(
private val json: Json,
private val service: GithubService private val service: GithubService
) { ) {
suspend fun getCommits(page: Int) = service.getCommits(page) suspend fun getCommits(page: Int) = service.getCommits(page)
@ -15,5 +14,5 @@ class GithubRepository(
suspend fun getContributors() = service.getContributors() suspend fun getContributors() = service.getContributors()
.sortedByDescending(GithubUser::contributions) .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 package com.aliucord.manager.network.service
import com.aliucord.manager.network.dto.Commit import com.aliucord.manager.network.dto.*
import com.aliucord.manager.network.dto.GithubUser
import io.ktor.client.* import io.ktor.client.*
import io.ktor.client.call.* import io.ktor.client.call.*
import io.ktor.client.request.* import io.ktor.client.request.*
import io.ktor.client.statement.* import io.ktor.client.statement.*
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
class GithubService( class GithubService(
private val httpClient: HttpClient private val httpClient: HttpClient,
private val json: Json,
) { ) {
suspend fun getCommits(page: Int): List<Commit> = withContext(Dispatchers.IO) { suspend fun getCommits(page: Int): List<Commit> = withContext(Dispatchers.IO) {
httpClient.get(COMMITS_URL) { httpClient.get(COMMITS_URL) {
@ -18,8 +20,10 @@ class GithubService(
}.body() }.body()
} }
suspend fun getVersion(): String = withContext(Dispatchers.IO) { suspend fun getVersion(): Version = withContext(Dispatchers.IO) {
httpClient.get(DATA_URL).bodyAsText() // 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) { suspend fun getContributors(): List<GithubUser> = withContext(Dispatchers.IO) {