From 2e631c27594bcb7cd2349c28dcfe5416c2db3ee7 Mon Sep 17 00:00:00 2001 From: Pedro Henrique Date: Sun, 2 Jun 2024 01:07:17 -0300 Subject: [PATCH] Setup network module --- build.gradle.kts | 2 + composeApp/build.gradle.kts | 5 +-- core/network/build.gradle.kts | 8 ++++ .../src/commonMain/kotlin/di/networkModule.kt | 37 +++++++++++++++++++ gradle/libs.versions.toml | 17 ++++++++- 5 files changed, 65 insertions(+), 4 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index d12f6ec..322faac 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,4 +7,6 @@ plugins { alias(libs.plugins.compose.compiler) apply false alias(libs.plugins.kotlinMultiplatform) apply false alias(libs.plugins.detekt) apply false + alias(libs.plugins.ksp) apply false + alias(libs.plugins.ktorfit) apply false } \ No newline at end of file diff --git a/composeApp/build.gradle.kts b/composeApp/build.gradle.kts index ff02fe0..5335c52 100644 --- a/composeApp/build.gradle.kts +++ b/composeApp/build.gradle.kts @@ -7,6 +7,7 @@ plugins { alias(libs.plugins.jetbrainsCompose) alias(libs.plugins.compose.compiler) alias(libs.plugins.detekt) + alias(libs.plugins.ksp) } kotlin { @@ -102,6 +103,4 @@ tasks.register("detektCommon") { include("**/*.kts") exclude("**/resources/**") exclude("**/build/**") -} - - +} \ No newline at end of file diff --git a/core/network/build.gradle.kts b/core/network/build.gradle.kts index 4749832..896de01 100644 --- a/core/network/build.gradle.kts +++ b/core/network/build.gradle.kts @@ -5,6 +5,8 @@ plugins { alias(libs.plugins.kotlinMultiplatform) alias(libs.plugins.androidLibrary) alias(libs.plugins.detekt) + alias(libs.plugins.ksp) + alias(libs.plugins.ktorfit) } kotlin { @@ -29,6 +31,12 @@ kotlin { commonMain.dependencies { implementation(libs.koin.core) implementation(libs.koin.compose) + + implementation(libs.ktor.core) + implementation(libs.ktor.logging) + implementation(libs.ktor.serialization) + implementation(libs.ktor.negotiation) + api(libs.ktorfit) } } } diff --git a/core/network/src/commonMain/kotlin/di/networkModule.kt b/core/network/src/commonMain/kotlin/di/networkModule.kt index 2d0fd37..7fab0a4 100644 --- a/core/network/src/commonMain/kotlin/di/networkModule.kt +++ b/core/network/src/commonMain/kotlin/di/networkModule.kt @@ -1,7 +1,44 @@ package di +import de.jensklingenberg.ktorfit.Ktorfit +import io.ktor.client.HttpClient +import io.ktor.client.plugins.contentnegotiation.ContentNegotiation +import io.ktor.client.plugins.logging.LogLevel +import io.ktor.client.plugins.logging.Logging +import io.ktor.serialization.kotlinx.json.json +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.json.Json import org.koin.dsl.module +const val BASE_URL = "https://example.com" + +@OptIn(ExperimentalSerializationApi::class) val networkModule = module { + single { + HttpClient { + install(Logging) { + level = LogLevel.ALL + } + + install(ContentNegotiation) { + json(Json { + explicitNulls = false + ignoreUnknownKeys = true + isLenient = true + prettyPrint = true + encodeDefaults = true + classDiscriminator = "#class" + }) + } + } + } + + single { + val httpClient: HttpClient = get() + Ktorfit.Builder() + .httpClient(httpClient) + .baseUrl(BASE_URL) + .build() + } } \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3227902..8ca418b 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -16,6 +16,10 @@ kotlin = "2.0.0" koin = "3.5.6" koin-compose = "1.1.5" detekt = "1.23.2" +ktor = "2.3.7" +okhttp = "4.12.0" +ksp = "2.0.0-1.0.21" +ktorfit = "2.0.0" [libraries] kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" } @@ -32,6 +36,15 @@ androidx-activity-compose = { module = "androidx.activity:activity-compose", ver koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin" } koin-compose = { module = "io.insert-koin:koin-compose", version.ref = "koin-compose" } +ktor-darwin-ios = { module = "io.ktor:ktor-client-darwin", version.ref = "ktor" } +ktor-ios = { module = "io.ktor:ktor-client-ios", version.ref = "ktor" } +ktor-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" } +ktor-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" } +ktor-logging = { module = "io.ktor:ktor-client-logging", version.ref = "ktor" } +ktor-serialization = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" } +ktor-negotiation = { module = "io.ktor:ktor-client-content-negotiation", version.ref = "ktor" } +ktorfit = { group = "de.jensklingenberg.ktorfit", name = "ktorfit-lib", version.ref = "ktorfit" } + detekt-formatting = { group = "io.gitlab.arturbosch.detekt", name = "detekt-formatting", version.ref = "detekt"} [plugins] @@ -40,4 +53,6 @@ androidLibrary = { id = "com.android.library", version.ref = "agp" } jetbrainsCompose = { id = "org.jetbrains.compose", version.ref = "compose-plugin" } compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" } -kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } \ No newline at end of file +kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } +ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" } +ktorfit = { id = "de.jensklingenberg.ktorfit", version.ref = "ktorfit" } \ No newline at end of file