Skip to content

Commit

Permalink
Merge pull request #183 from Dorofeev/#178-ktor-2.0
Browse files Browse the repository at this point in the history
#178 update to ktor 2.0.1
  • Loading branch information
Alex009 authored Aug 1, 2022
2 parents d6a3e50 + 5793821 commit 9ce910e
Show file tree
Hide file tree
Showing 33 changed files with 265 additions and 451 deletions.
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ kotlin.native.enableDependencyPropagation=false
kotlin.mpp.enableGranularSourceSetsMetadata=true
kotlin.mpp.enableCompatibilityMetadataVariant=true

kotlin.native.binary.memoryModel=experimental

android.useAndroidX=true

mobile.multiplatform.iosTargetWarning=false
Expand Down
9 changes: 5 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
kotlinVersion = "1.6.10"
kotlinVersion = "1.6.20"

# android
lifecycleViewModelVersion = "2.3.1"
Expand All @@ -15,11 +15,11 @@ kotlinxSerializationVersion = "1.3.2"
coroutinesVersion = "1.6.0-native-mt"

# moko
mokoResourcesVersion = "0.18.0"
mokoResourcesVersion = "0.20.1"
mokoMvvmVersion = "0.12.0"
mokoErrorsVersion = "0.6.0"
mokoTestVersion = "0.6.1"
mokoNetworkVersion = "0.18.0"
mokoNetworkVersion = "0.19.0"

# tests
espressoCoreVersion = "3.2.0"
Expand All @@ -29,7 +29,7 @@ androidxTestVersion = "1.3.0"
robolectricVersion = "4.6.1"

# other
ktorClientVersion = "1.6.7"
ktorClientVersion = "2.0.1"
kbignumVersion = "2.4.12"
multidexVersion = "2.0.1"

Expand All @@ -48,6 +48,7 @@ coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version
ktorClientOkHttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktorClientVersion" }
ktorClient = { module = "io.ktor:ktor-client-core", version.ref = "ktorClientVersion" }
ktorClientLogging = { module = "io.ktor:ktor-client-logging", version.ref = "ktorClientVersion" }
ktorClientWebSocket = { module = "io.ktor:ktor-client-websockets", version.ref = "ktorClientVersion" }
ktorClientMock = { module = "io.ktor:ktor-client-mock", version.ref = "ktorClientVersion" }
ktorClientIos = { module = "io.ktor:ktor-client-ios", version.ref = "ktorClientVersion" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ package {{apiPackage}}
{{#imports}}import {{import}}
{{/imports}}

import io.ktor.client.call.body
import io.ktor.client.HttpClient
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.client.request.request
import io.ktor.client.request.forms.FormDataContent
import io.ktor.client.request.setBody
import io.ktor.http.ContentType
import io.ktor.http.HttpMethod
import io.ktor.http.Parameters
Expand All @@ -19,6 +21,8 @@ import kotlinx.serialization.builtins.SetSerializer
import kotlinx.serialization.builtins.serializer
import io.ktor.client.call.ReceivePipelineException
import io.ktor.http.content.TextContent
import io.ktor.http.encodedPath
import io.ktor.http.path

{{#operations}}
{{>classes_modifiers}}interface {{classname}} {
Expand Down Expand Up @@ -60,7 +64,7 @@ import io.ktor.http.content.TextContent
takeFrom(_basePath)
encodedPath = encodedPath.let { startingPath ->
path("{{path}}")
return@let startingPath + encodedPath.substring(1)
return@let startingPath + encodedPath
}
{{#hasQueryParams}}
@Suppress("UNNECESSARY_SAFE_CALL")
Expand All @@ -79,7 +83,7 @@ import io.ktor.http.content.TextContent
{{#bodyParams}}
@Suppress("SENSELESS_COMPARISON")
if({{paramName}} != null) {
builder.body = TextContent(
builder.setBody(TextContent(
{{#dataType}}
{{#isArray}}
{{#uniqueItems}}
Expand All @@ -106,7 +110,7 @@ import io.ktor.http.content.TextContent
{{paramName}}),
{{/dataType}}
ContentType.Application.Json.withoutParameters()
)
))
}
{{/bodyParams}}

Expand All @@ -116,7 +120,7 @@ import io.ktor.http.content.TextContent
{{paramName}}?.let { append("{{baseName}}", it.toString()) }
{{/formParams}}
})
builder.body = formData
builder.setBody(formData)
{{/hasFormParams}}
with(builder.headers) {
append("Accept", "application/json")
Expand All @@ -131,12 +135,12 @@ import io.ktor.http.content.TextContent
{{#vendorExtensions.x-successResponse}}
val serializer = {{>property_serializer}}
//not primitive type
val result: String = _httpClient.request(builder)
val result: String = _httpClient.request(builder).body()
return _json.decodeFromString(serializer, result)
{{/vendorExtensions.x-successResponse}}
{{/returnType}}
{{^returnType}}
return _httpClient.request(builder)
return _httpClient.request(builder).body()
{{/returnType}}
} catch (pipeline: ReceivePipelineException) {
throw pipeline.cause
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ package dev.icerock.moko.network

import android.content.res.Resources
import androidx.core.os.ConfigurationCompat
import dev.icerock.moko.network.features.LanguageFeature
import dev.icerock.moko.network.plugins.LanguagePlugin

actual class LanguageProvider : LanguageFeature.LanguageCodeProvider {
actual class LanguageProvider : LanguagePlugin.LanguageCodeProvider {
override fun getLanguageCode(): String? {
return ConfigurationCompat.getLocales(Resources.getSystem().configuration).get(0).language
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ package dev.icerock.moko.network
import dev.icerock.moko.network.exceptions.ResponseException
import io.ktor.client.HttpClient
import io.ktor.client.call.ReceivePipelineException
import io.ktor.client.call.body
import io.ktor.client.request.request
import io.ktor.client.request.setBody
import io.ktor.client.request.url
import io.ktor.client.utils.EmptyContent
import io.ktor.http.ContentType
Expand Down Expand Up @@ -36,8 +38,8 @@ suspend inline fun <reified Value : Any> HttpClient.createRequest(
method = methodType
url(path)
if (contentType != null) contentType(contentType)
this.body = body
}
setBody(body)
}.body()
} catch (e: ReceivePipelineException) {
if (e.cause is ResponseException) {
throw e.cause
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package dev.icerock.moko.network

import dev.icerock.moko.network.features.LanguageFeature
import dev.icerock.moko.network.plugins.LanguagePlugin

@Suppress("EmptyDefaultConstructor")
expect class LanguageProvider() : LanguageFeature.LanguageCodeProvider
expect class LanguageProvider() : LanguagePlugin.LanguageCodeProvider

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package dev.icerock.moko.network.features

import io.ktor.client.HttpClient
import io.ktor.client.features.HttpClientFeature
import io.ktor.client.plugins.HttpClientPlugin
import io.ktor.client.request.HttpRequestPipeline
import io.ktor.client.request.header
import io.ktor.http.HttpHeaders
Expand All @@ -16,7 +16,7 @@ class DynamicUserAgent(
) {
class Config(var agentProvider: () -> String? = { null })

companion object Feature : HttpClientFeature<Config, DynamicUserAgent> {
companion object Feature : HttpClientPlugin<Config, DynamicUserAgent> {
override val key: AttributeKey<DynamicUserAgent> = AttributeKey("DynamicUserAgent")

override fun prepare(block: Config.() -> Unit): DynamicUserAgent =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2019 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.network.plugins

import dev.icerock.moko.network.exceptionfactory.ExceptionFactory
import io.ktor.client.HttpClient
import io.ktor.client.plugins.HttpClientPlugin
import io.ktor.client.plugins.HttpSend
import io.ktor.client.plugins.plugin
import io.ktor.client.statement.bodyAsChannel
import io.ktor.http.isSuccess
import io.ktor.util.AttributeKey
import io.ktor.utils.io.charsets.Charset
import io.ktor.utils.io.core.readText

class ExceptionPlugin(private val exceptionFactory: ExceptionFactory) {

class Config {
var exceptionFactory: ExceptionFactory? = null
fun build() = ExceptionPlugin(
exceptionFactory
?: throw IllegalArgumentException("Exception factory should be contain")
)
}

companion object Plugin : HttpClientPlugin<Config, ExceptionPlugin> {

override val key = AttributeKey<ExceptionPlugin>("ExceptionPlugin")

override fun prepare(block: Config.() -> Unit) = Config().apply(block).build()

override fun install(plugin: ExceptionPlugin, scope: HttpClient) {
scope.plugin(HttpSend).intercept { request ->
val call = execute(request)
if (!call.response.status.isSuccess()) {
val packet = call.response.bodyAsChannel().readRemaining()
val responseString = packet.readText(charset = Charset.forName("UTF-8"))
throw plugin.exceptionFactory.createException(
request = call.request,
response = call.response,
responseBody = responseString
)
}
call
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.network.features
package dev.icerock.moko.network.plugins

import io.ktor.client.HttpClient
import io.ktor.client.features.HttpClientFeature
import io.ktor.client.plugins.HttpClientPlugin
import io.ktor.client.request.HttpRequestPipeline
import io.ktor.client.request.header
import io.ktor.util.AttributeKey

class LanguageFeature private constructor(
class LanguagePlugin private constructor(
private val languageHeaderName: String,
private val languageProvider: LanguageFeature.LanguageCodeProvider
private val languageProvider: LanguagePlugin.LanguageCodeProvider
) {
class Config {
var languageHeaderName: String? = null
var languageCodeProvider: LanguageCodeProvider? = null
fun build() = LanguageFeature(
fun build() = LanguagePlugin(
languageHeaderName ?: throw IllegalArgumentException("HeaderName should be contain"),
languageCodeProvider
?: throw IllegalArgumentException("LanguageCodeProvider should be contain")
)
}

companion object Feature : HttpClientFeature<Config, LanguageFeature> {
override val key = AttributeKey<LanguageFeature>("LanguageFeature")
companion object Plugin : HttpClientPlugin<Config, LanguagePlugin> {
override val key = AttributeKey<LanguagePlugin>("LanguagePlugin")

override fun prepare(block: Config.() -> Unit) = Config().apply(block).build()

override fun install(feature: LanguageFeature, scope: HttpClient) {
override fun install(plugin: LanguagePlugin, scope: HttpClient) {
scope.requestPipeline.intercept(HttpRequestPipeline.State) {
feature.languageProvider.getLanguageCode()?.apply {
context.header(feature.languageHeaderName, this)
plugin.languageProvider.getLanguageCode()?.apply {
context.header(plugin.languageHeaderName, this)
}
}
}
Expand Down
Loading

0 comments on commit 9ce910e

Please sign in to comment.