Skip to content

Commit

Permalink
Setup network module
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrohfp committed Jun 2, 2024
1 parent d5905fd commit 2e631c2
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 2 additions & 3 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
alias(libs.plugins.jetbrainsCompose)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.detekt)
alias(libs.plugins.ksp)
}

kotlin {
Expand Down Expand Up @@ -102,6 +103,4 @@ tasks.register<io.gitlab.arturbosch.detekt.Detekt>("detektCommon") {
include("**/*.kts")
exclude("**/resources/**")
exclude("**/build/**")
}


}
8 changes: 8 additions & 0 deletions core/network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions core/network/src/commonMain/kotlin/di/networkModule.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
17 changes: 16 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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]
Expand All @@ -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" }
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" }

0 comments on commit 2e631c2

Please sign in to comment.