Skip to content

Commit

Permalink
[#18] Add Napier, fix network call on the wrong client
Browse files Browse the repository at this point in the history
  • Loading branch information
blyscuit committed Nov 21, 2022
1 parent 1d8bc23 commit aa37084
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 30 deletions.
3 changes: 3 additions & 0 deletions buildSrc/src/main/kotlin/appPackage/Dependency.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ object Dependency {
// Resources
const val MOKO_RESOURCES_GENERATOR = "dev.icerock.moko:resources-generator:${Version.MOKO_RESOURCES}"
const val MOKO_RESOURCES = "dev.icerock.moko:resources:${Version.MOKO_RESOURCES}"

// Napier
const val NAPIER = "io.github.aakira:napier:${Version.NAPIER}"
}
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/appPackage/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ object Version {
const val MULTIPLATFORM_SETTINGS = "1.0.0-RC"
const val SECURITY = "1.1.0-alpha03"
const val MOKO_RESOURCES = "0.20.1"
const val NAPIER = "2.6.1"
}
1 change: 1 addition & 0 deletions shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ kotlin {
implementation(Dependency.MULTIPLATFORM_SETTINGS)
implementation(Dependency.MULTIPLATFORM_SETTINGS_SERIALIZATION)
implementation(Dependency.MULTIPLATFORM_SETTINGS_TEST)
implementation(Dependency.NAPIER)
}
}
val commonTest by getting {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package co.nimblehq.blisskmmic.data.network.core

import co.nimblehq.jsonapi.json.JsonApi
import io.github.aakira.napier.DebugAntilog
import io.github.aakira.napier.Napier
import io.ktor.client.*
import io.ktor.client.engine.*
import io.ktor.client.plugins.auth.*
Expand All @@ -25,6 +27,8 @@ open class NetworkClient {
}

constructor(engine: HttpClientEngine? = null) {
Napier.takeLogarithm()
Napier.base(DebugAntilog())
if (engine == null) {
client = HttpClient(clientConfig())
} else {
Expand Down Expand Up @@ -53,7 +57,14 @@ open class NetworkClient {

private fun clientConfig(): HttpClientConfig<*>.() -> Unit {
return {
install(Logging)
install(Logging) {
level = LogLevel.ALL
logger = object : Logger {
override fun log(message: String) {
Napier.log(io.github.aakira.napier.LogLevel.DEBUG, message = message)
}
}
}
install(ContentNegotiation) {
json(json)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.coroutines.flow.singleOrNull

class TokenizedNetworkClient: NetworkClient {

val localDataSource: LocalDataSource
private val localDataSource: LocalDataSource

constructor(
engine: HttpClientEngine? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,28 @@ package co.nimblehq.blisskmmic.data.network.datasource
import co.nimblehq.blisskmmic.data.model.PaginationMetaApiModel
import co.nimblehq.blisskmmic.data.model.SurveyApiModel
import co.nimblehq.blisskmmic.data.network.core.NetworkClient
import co.nimblehq.blisskmmic.data.network.helpers.requestBuilder
import co.nimblehq.blisskmmic.data.network.target.LoginTargetType
import co.nimblehq.blisskmmic.data.network.target.SurveySelectionTargetType
import co.nimblehq.blisskmmic.data.network.target.SurveyTargetType
import co.nimblehq.blisskmmic.domain.model.PaginationMeta
import co.nimblehq.blisskmmic.domain.model.Survey
import co.nimblehq.blisskmmic.domain.model.TokenApiModel
import kotlinx.coroutines.flow.Flow

interface NetworkDataSource {

fun logIn(target: LoginTargetType): Flow<TokenApiModel>
fun survey(target: SurveyTargetType): Flow<Pair<List<SurveyApiModel>, PaginationMetaApiModel>>
fun survey(target: SurveySelectionTargetType): Flow<Pair<List<SurveyApiModel>, PaginationMetaApiModel>>
}

class NetworkDataSourceImpl(private val networkClient: NetworkClient): NetworkDataSource {

override fun logIn(target: LoginTargetType): Flow<TokenApiModel> {
return networkClient.fetch(target.requestBuilder)
return networkClient.fetch(target.requestBuilder())
}

override fun survey(target: SurveyTargetType): Flow<Pair<List<SurveyApiModel>, PaginationMetaApiModel>> {
return networkClient.fetchWithMeta<List<SurveyApiModel>, PaginationMetaApiModel>(
target.requestBuilder
override fun survey(target: SurveySelectionTargetType):
Flow<Pair<List<SurveyApiModel>, PaginationMetaApiModel>> {
return networkClient.fetchWithMeta(
target.requestBuilder()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,35 @@ package co.nimblehq.blisskmmic.data.network.helpers
import co.nimblehq.blisskmmic.BuildKonfig
import io.ktor.client.request.*
import io.ktor.http.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.encodeToJsonElement
import kotlinx.serialization.json.jsonObject

interface TargetType {
interface TargetType<T> {

val path: String
val method: HttpMethod
val body: Any?
val headers: Map<String, String>?
val body: T?
}

val requestBuilder: HttpRequestBuilder
get() {
val builder = HttpRequestBuilder()
builder.url("$BuildKonfig.BASE_URL$API_VERSION$path")
builder.method = method
builder.contentType(ContentType.Application.Json)
private inline fun <reified T> TargetType<T>.requestBuilder(): HttpRequestBuilder {
val builder = HttpRequestBuilder()
builder.url("$BuildKonfig.BASE_URL$API_VERSION$path")
builder.method = method
builder.contentType(ContentType.Application.Json)
if (method == HttpMethod.Get) {
builder.setQueryParameters(body)
} else {
builder.setBody(body)
return builder
}
return builder
}

private inline fun <reified T> HttpRequestBuilder.setQueryParameters(parameters: T?) {
parameters?.run {
val queryParameters = Json.encodeToJsonElement(this).jsonObject
for ((key, value) in queryParameters) {
parameter(key, value)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import io.ktor.http.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

sealed class SurveyTargetType: TargetType
sealed class SurveyTargetType<T>: TargetType<T>

class SurveySelectionTargetType(page: Int = 1, size: Int = 3): SurveyTargetType() {
class SurveySelectionTargetType(page: Int = 1, size: Int = 3):
SurveyTargetType<SurveySelectionTargetType.SurveySelectionInput>() {

@Serializable
data class SurveySelectionInput(
Expand All @@ -18,7 +19,6 @@ class SurveySelectionTargetType(page: Int = 1, size: Int = 3): SurveyTargetType(
)

override var path = "surveys"
override var method = HttpMethod.Post
override var method = HttpMethod.Get
override var body = SurveySelectionInput(page, size)
override var headers: Map<String, String>? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import io.ktor.http.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

sealed class UserTargetType: TargetType
sealed class UserTargetType<T>: TargetType<T>

class LoginTargetType(email: String, password: String): UserTargetType() {
class LoginTargetType(email: String, password: String):
UserTargetType<LoginTargetType.LoginInput>() {

@Serializable
data class LoginInput(
Expand All @@ -31,5 +32,4 @@ class LoginTargetType(email: String, password: String): UserTargetType() {
BuildKonfig.CLIENT_ID,
BuildKonfig.CLIENT_SECRET
)
override val headers: Map<String, String>? = null
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package co.nimblehq.blisskmmic.di.koin.constants

const val TOKENIZED_NETWORK_CLIENT_KOIN = "TOKENIZED_NETWORK_CLIENT_KOIN"
const val NETWORK_CLIENT_KOIN = "NETWORK_CLIENT_KOIN"
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import co.nimblehq.blisskmmic.data.network.core.NetworkClient
import co.nimblehq.blisskmmic.data.network.core.TokenizedNetworkClient
import co.nimblehq.blisskmmic.data.network.datasource.NetworkDataSource
import co.nimblehq.blisskmmic.data.network.datasource.NetworkDataSourceImpl
import co.nimblehq.blisskmmic.di.koin.constants.NETWORK_CLIENT_KOIN
import co.nimblehq.blisskmmic.di.koin.constants.TOKENIZED_NETWORK_CLIENT_KOIN
import org.koin.core.qualifier.named
import org.koin.dsl.module

val networkModule = module {
single { NetworkClient() }
single<NetworkDataSource> { NetworkDataSourceImpl(get()) }
factory(named(TOKENIZED_NETWORK_CLIENT_KOIN)) { TokenizedNetworkClient(null, get()) }
single<NetworkDataSource>(named(NETWORK_CLIENT_KOIN)) { NetworkDataSourceImpl(get()) }
factory<NetworkDataSource>(named(TOKENIZED_NETWORK_CLIENT_KOIN)) { NetworkDataSourceImpl(TokenizedNetworkClient(null, get())) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package co.nimblehq.blisskmmic.di.koin.modules

import co.nimblehq.blisskmmic.data.repository.SurveyRepositoryImpl
import co.nimblehq.blisskmmic.data.repository.TokenRepositoryImpl
import co.nimblehq.blisskmmic.di.koin.constants.NETWORK_CLIENT_KOIN
import co.nimblehq.blisskmmic.di.koin.constants.TOKENIZED_NETWORK_CLIENT_KOIN
import co.nimblehq.blisskmmic.domain.repository.SurveyRepository
import co.nimblehq.blisskmmic.domain.repository.TokenRepository
import org.koin.core.qualifier.named
import org.koin.dsl.module

val repositoryModule = module {
single<TokenRepository> { TokenRepositoryImpl(get(), get()) }
single<TokenRepository> { TokenRepositoryImpl(get(named(NETWORK_CLIENT_KOIN)), get()) }
factory<SurveyRepository> { SurveyRepositoryImpl(get(named(TOKENIZED_NETWORK_CLIENT_KOIN))) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package co.nimblehq.blisskmmic.di.koin.modules

import co.nimblehq.blisskmmic.domain.usecase.LogInUseCase
import co.nimblehq.blisskmmic.domain.usecase.LogInUseCaseImpl
import co.nimblehq.blisskmmic.domain.usecase.SurveyListUseCase
import co.nimblehq.blisskmmic.domain.usecase.SurveyListUseCaseImpl
import org.koin.dsl.module

val useCaseModule = module {
single<LogInUseCase> { LogInUseCaseImpl(get()) }
factory<SurveyListUseCase> { SurveyListUseCaseImpl(get()) }
}

0 comments on commit aa37084

Please sign in to comment.