Skip to content

Commit

Permalink
KMM-81:
Browse files Browse the repository at this point in the history
- Implemented Ktor type-safe requests
- Bumped dependencies
  • Loading branch information
maltsev-gorskij committed Feb 13, 2023
1 parent c1100d1 commit b2bcafd
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 deletions.
6 changes: 6 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("com.github.ben-manes.versions")
id("io.gitlab.arturbosch.detekt")
Expand Down Expand Up @@ -53,6 +55,10 @@ task<Delete>("disableGitHooks") {
)
}

tasks.withType<KotlinCompile> {
kotlinOptions.useK2 = true
}

dependencies {
detekt(libs.detekt.cli)
}
Expand Down
6 changes: 4 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ datetime = "0.4.0"
android_plugin = "7.4.1"
ktor = "2.2.3"
kotlin_dsl = "2.3.3"
koin = "3.3.2"
koin = "3.3.3"
koin_compose = "3.4.1"
gradle_versions = "0.44.0"
crypto = "1.1.0-alpha04"
Expand Down Expand Up @@ -36,6 +36,7 @@ ktor-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
ktor-content = { module = "io.ktor:ktor-client-content-negotiation", version.ref = "ktor" }
ktor-serialization = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" }
ktor-logging = { module = "io.ktor:ktor-client-logging", version.ref = "ktor" }
ktor-resources = { module = "io.ktor:ktor-client-resources", version.ref = "ktor" }
ktor-android = { module = "io.ktor:ktor-client-android", version.ref = "ktor" }
ktor-ios = { module = "io.ktor:ktor-client-darwin", version.ref = "ktor" }

Expand Down Expand Up @@ -63,5 +64,6 @@ ktor-common = [
"ktor-core",
"ktor-content",
"ktor-serialization",
"ktor-logging"
"ktor-logging",
"ktor-resources"
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ru.lyrian.kotlinmultiplatformsandbox.core.constants
internal object KtorConstants {
const val LOG_TAG = "HTTP Client"
const val SPACEX_API_BASE_URL = "https://api.spacexdata.com/v5/"
const val SPACEX_LAUNCHES_PAGING_ENDPOINT = "launches/query"
const val CONNECTION_TIMEOUT = 10_000L
const val REQUEST_TIMEOUT = 10_000L
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.ktor.client.plugins.defaultRequest
import io.ktor.client.plugins.logging.LogLevel
import io.ktor.client.plugins.logging.Logger
import io.ktor.client.plugins.logging.Logging
import io.ktor.client.plugins.resources.Resources
import io.ktor.http.ContentType
import io.ktor.http.contentType
import io.ktor.serialization.kotlinx.json.json
Expand Down Expand Up @@ -41,6 +42,8 @@ internal val apiClientModule = module {
connectTimeoutMillis = KtorConstants.CONNECTION_TIMEOUT
}

install(Resources)

if (BuildInfo.isDebug) {
install(Logging) {
logger = object : Logger {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.lyrian.kotlinmultiplatformsandbox.core.data.data_source.api.di

import io.ktor.resources.Resource

@Resource("/launches")
class Launches {
@Resource("/query")
class Query(val parent: Launches = Launches())
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package ru.lyrian.kotlinmultiplatformsandbox.feature.launches.data.data_source.n

import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.post
import io.ktor.client.plugins.resources.post
import io.ktor.client.request.setBody
import ru.lyrian.kotlinmultiplatformsandbox.core.constants.KtorConstants
import ru.lyrian.kotlinmultiplatformsandbox.core.data.data_source.api.di.Launches

internal class LaunchesListApi(private val httpClient: HttpClient) {
internal suspend fun getLaunchesByPage(pagingRequest: PagingRequest): RocketLaunchesPagingResponse {
val response = httpClient
.post(KtorConstants.SPACEX_LAUNCHES_PAGING_ENDPOINT) {
internal suspend fun getLaunchesByPage(pagingRequest: PagingRequest): RocketLaunchesPagingResponse =
httpClient
.post(Launches.Query()) {
setBody(pagingRequest)
}
return response.body()
}
.body()
}

0 comments on commit b2bcafd

Please sign in to comment.