Inject http client via Koin
This commit is contained in:
parent
5bb5a38981
commit
7c693fb5e8
4 changed files with 27 additions and 26 deletions
|
|
@ -11,7 +11,7 @@ class ManagerApplication : Application() {
|
||||||
|
|
||||||
startKoin {
|
startKoin {
|
||||||
androidContext(this@ManagerApplication)
|
androidContext(this@ManagerApplication)
|
||||||
modules(appModule, serviceModule, repositoryModule, viewModelModule, managerModule)
|
modules(httpModule, serviceModule, repositoryModule, viewModelModule, managerModule)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,14 +0,0 @@
|
||||||
package com.aliucord.manager.di
|
|
||||||
|
|
||||||
import kotlinx.serialization.json.Json
|
|
||||||
import org.koin.core.module.dsl.singleOf
|
|
||||||
import org.koin.dsl.module
|
|
||||||
|
|
||||||
val appModule = module {
|
|
||||||
fun provideJson(): Json = Json {
|
|
||||||
ignoreUnknownKeys = true
|
|
||||||
isLenient = true
|
|
||||||
}
|
|
||||||
|
|
||||||
singleOf(::provideJson)
|
|
||||||
}
|
|
||||||
25
app/src/main/kotlin/com/aliucord/manager/di/HttpModule.kt
Normal file
25
app/src/main/kotlin/com/aliucord/manager/di/HttpModule.kt
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.aliucord.manager.di
|
||||||
|
|
||||||
|
import io.ktor.client.*
|
||||||
|
import io.ktor.client.engine.cio.*
|
||||||
|
import io.ktor.client.plugins.contentnegotiation.*
|
||||||
|
import io.ktor.serialization.kotlinx.json.*
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
import org.koin.core.module.dsl.singleOf
|
||||||
|
import org.koin.dsl.module
|
||||||
|
|
||||||
|
val httpModule = module {
|
||||||
|
fun provideJson() = Json {
|
||||||
|
ignoreUnknownKeys = true
|
||||||
|
isLenient = true
|
||||||
|
}
|
||||||
|
|
||||||
|
fun provideHttpClient(json: Json) = HttpClient(CIO) {
|
||||||
|
install(ContentNegotiation) {
|
||||||
|
json(json)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
singleOf(::provideJson)
|
||||||
|
singleOf(::provideHttpClient)
|
||||||
|
}
|
||||||
|
|
@ -4,24 +4,14 @@ import com.aliucord.manager.network.dto.Commit
|
||||||
import com.aliucord.manager.network.dto.GithubUser
|
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.engine.cio.*
|
|
||||||
import io.ktor.client.plugins.contentnegotiation.*
|
|
||||||
import io.ktor.client.request.*
|
import io.ktor.client.request.*
|
||||||
import io.ktor.client.statement.*
|
import io.ktor.client.statement.*
|
||||||
import io.ktor.serialization.kotlinx.json.*
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import kotlinx.serialization.json.Json
|
|
||||||
|
|
||||||
class GithubService(
|
class GithubService(
|
||||||
private val json: Json
|
private val httpClient: HttpClient
|
||||||
) {
|
) {
|
||||||
private val httpClient = HttpClient(CIO) {
|
|
||||||
install(ContentNegotiation) {
|
|
||||||
json(json)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun getCommits(page: Int): List<Commit> = withContext(Dispatchers.IO) {
|
suspend fun getCommits(page: Int): List<Commit> = withContext(Dispatchers.IO) {
|
||||||
httpClient.get(commitsUrl) {
|
httpClient.get(commitsUrl) {
|
||||||
parameter("page", page)
|
parameter("page", page)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue