Skip to content

Commit 5dfac25

Browse files
authored
Merge pull request #185 from icerockdev/develop
Release 0.19.0
2 parents a05813a + 45acd5b commit 5dfac25

File tree

34 files changed

+269
-455
lines changed

34 files changed

+269
-455
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ buildscript {
3636
}
3737
3838
dependencies {
39-
classpath "dev.icerock.moko:network-generator:0.18.0"
39+
classpath "dev.icerock.moko:network-generator:0.19.0"
4040
}
4141
}
4242
@@ -53,9 +53,9 @@ project build.gradle
5353
apply plugin: "dev.icerock.mobile.multiplatform-network-generator"
5454
5555
dependencies {
56-
commonMainApi("dev.icerock.moko:network:0.18.0")
57-
commonMainApi("dev.icerock.moko:network-bignum:0.18.0") // kbignum serializer
58-
commonMainApi("dev.icerock.moko:network-errors:0.18.0") // moko-errors integration
56+
commonMainApi("dev.icerock.moko:network:0.19.0")
57+
commonMainApi("dev.icerock.moko:network-bignum:0.19.0") // kbignum serializer
58+
commonMainApi("dev.icerock.moko:network-errors:0.19.0") // moko-errors integration
5959
}
6060
```
6161

gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ kotlin.native.enableDependencyPropagation=false
99
kotlin.mpp.enableGranularSourceSetsMetadata=true
1010
kotlin.mpp.enableCompatibilityMetadataVariant=true
1111

12+
kotlin.native.binary.memoryModel=experimental
13+
1214
android.useAndroidX=true
1315

1416
mobile.multiplatform.iosTargetWarning=false

gradle/libs.versions.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[versions]
2-
kotlinVersion = "1.6.10"
2+
kotlinVersion = "1.6.20"
33

44
# android
55
lifecycleViewModelVersion = "2.3.1"
@@ -15,11 +15,11 @@ kotlinxSerializationVersion = "1.3.2"
1515
coroutinesVersion = "1.6.0-native-mt"
1616

1717
# moko
18-
mokoResourcesVersion = "0.18.0"
18+
mokoResourcesVersion = "0.20.1"
1919
mokoMvvmVersion = "0.12.0"
2020
mokoErrorsVersion = "0.6.0"
2121
mokoTestVersion = "0.6.1"
22-
mokoNetworkVersion = "0.18.0"
22+
mokoNetworkVersion = "0.19.0"
2323

2424
# tests
2525
espressoCoreVersion = "3.2.0"
@@ -29,7 +29,7 @@ androidxTestVersion = "1.3.0"
2929
robolectricVersion = "4.6.1"
3030

3131
# other
32-
ktorClientVersion = "1.6.7"
32+
ktorClientVersion = "2.0.1"
3333
kbignumVersion = "2.4.12"
3434
multidexVersion = "2.0.1"
3535

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

network-generator/src/main/resources/kotlin-ktor-client/api.mustache

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ package {{apiPackage}}
44
{{#imports}}import {{import}}
55
{{/imports}}
66

7+
import io.ktor.client.call.body
78
import io.ktor.client.HttpClient
89
import io.ktor.client.request.HttpRequestBuilder
910
import io.ktor.client.request.request
1011
import io.ktor.client.request.forms.FormDataContent
12+
import io.ktor.client.request.setBody
1113
import io.ktor.http.ContentType
1214
import io.ktor.http.HttpMethod
1315
import io.ktor.http.Parameters
@@ -19,6 +21,8 @@ import kotlinx.serialization.builtins.SetSerializer
1921
import kotlinx.serialization.builtins.serializer
2022
import io.ktor.client.call.ReceivePipelineException
2123
import io.ktor.http.content.TextContent
24+
import io.ktor.http.encodedPath
25+
import io.ktor.http.path
2226

2327
{{#operations}}
2428
{{>classes_modifiers}}interface {{classname}} {
@@ -60,7 +64,7 @@ import io.ktor.http.content.TextContent
6064
takeFrom(_basePath)
6165
encodedPath = encodedPath.let { startingPath ->
6266
path("{{path}}")
63-
return@let startingPath + encodedPath.substring(1)
67+
return@let startingPath + encodedPath
6468
}
6569
{{#hasQueryParams}}
6670
@Suppress("UNNECESSARY_SAFE_CALL")
@@ -79,7 +83,7 @@ import io.ktor.http.content.TextContent
7983
{{#bodyParams}}
8084
@Suppress("SENSELESS_COMPARISON")
8185
if({{paramName}} != null) {
82-
builder.body = TextContent(
86+
builder.setBody(TextContent(
8387
{{#dataType}}
8488
{{#isArray}}
8589
{{#uniqueItems}}
@@ -106,7 +110,7 @@ import io.ktor.http.content.TextContent
106110
{{paramName}}),
107111
{{/dataType}}
108112
ContentType.Application.Json.withoutParameters()
109-
)
113+
))
110114
}
111115
{{/bodyParams}}
112116

@@ -116,7 +120,7 @@ import io.ktor.http.content.TextContent
116120
{{paramName}}?.let { append("{{baseName}}", it.toString()) }
117121
{{/formParams}}
118122
})
119-
builder.body = formData
123+
builder.setBody(formData)
120124
{{/hasFormParams}}
121125
with(builder.headers) {
122126
append("Accept", "application/json")
@@ -131,12 +135,12 @@ import io.ktor.http.content.TextContent
131135
{{#vendorExtensions.x-successResponse}}
132136
val serializer = {{>property_serializer}}
133137
//not primitive type
134-
val result: String = _httpClient.request(builder)
138+
val result: String = _httpClient.request(builder).body()
135139
return _json.decodeFromString(serializer, result)
136140
{{/vendorExtensions.x-successResponse}}
137141
{{/returnType}}
138142
{{^returnType}}
139-
return _httpClient.request(builder)
143+
return _httpClient.request(builder).body()
140144
{{/returnType}}
141145
} catch (pipeline: ReceivePipelineException) {
142146
throw pipeline.cause

network/src/androidMain/kotlin/dev/icerock/moko/network/LanguageProvider.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ package dev.icerock.moko.network
66

77
import android.content.res.Resources
88
import androidx.core.os.ConfigurationCompat
9-
import dev.icerock.moko.network.features.LanguageFeature
9+
import dev.icerock.moko.network.plugins.LanguagePlugin
1010

11-
actual class LanguageProvider : LanguageFeature.LanguageCodeProvider {
11+
actual class LanguageProvider : LanguagePlugin.LanguageCodeProvider {
1212
override fun getLanguageCode(): String? {
1313
return ConfigurationCompat.getLocales(Resources.getSystem().configuration).get(0).language
1414
}

network/src/commonMain/kotlin/dev/icerock/moko/network/HttpExt.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ package dev.icerock.moko.network
77
import dev.icerock.moko.network.exceptions.ResponseException
88
import io.ktor.client.HttpClient
99
import io.ktor.client.call.ReceivePipelineException
10+
import io.ktor.client.call.body
1011
import io.ktor.client.request.request
12+
import io.ktor.client.request.setBody
1113
import io.ktor.client.request.url
1214
import io.ktor.client.utils.EmptyContent
1315
import io.ktor.http.ContentType
@@ -36,8 +38,8 @@ suspend inline fun <reified Value : Any> HttpClient.createRequest(
3638
method = methodType
3739
url(path)
3840
if (contentType != null) contentType(contentType)
39-
this.body = body
40-
}
41+
setBody(body)
42+
}.body()
4143
} catch (e: ReceivePipelineException) {
4244
if (e.cause is ResponseException) {
4345
throw e.cause

network/src/commonMain/kotlin/dev/icerock/moko/network/LanguageProvider.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
package dev.icerock.moko.network
66

7-
import dev.icerock.moko.network.features.LanguageFeature
7+
import dev.icerock.moko.network.plugins.LanguagePlugin
88

99
@Suppress("EmptyDefaultConstructor")
10-
expect class LanguageProvider() : LanguageFeature.LanguageCodeProvider
10+
expect class LanguageProvider() : LanguagePlugin.LanguageCodeProvider

network/src/commonMain/kotlin/dev/icerock/moko/network/features/ExceptionFeature.kt

Lines changed: 0 additions & 52 deletions
This file was deleted.

network/src/commonMain/kotlin/dev/icerock/moko/network/features/DynamicUserAgent.kt renamed to network/src/commonMain/kotlin/dev/icerock/moko/network/plugins/DynamicUserAgent.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
package dev.icerock.moko.network.features
66

77
import io.ktor.client.HttpClient
8-
import io.ktor.client.features.HttpClientFeature
8+
import io.ktor.client.plugins.HttpClientPlugin
99
import io.ktor.client.request.HttpRequestPipeline
1010
import io.ktor.client.request.header
1111
import io.ktor.http.HttpHeaders
@@ -16,7 +16,7 @@ class DynamicUserAgent(
1616
) {
1717
class Config(var agentProvider: () -> String? = { null })
1818

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

2222
override fun prepare(block: Config.() -> Unit): DynamicUserAgent =
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2019 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package dev.icerock.moko.network.plugins
6+
7+
import dev.icerock.moko.network.exceptionfactory.ExceptionFactory
8+
import io.ktor.client.HttpClient
9+
import io.ktor.client.plugins.HttpClientPlugin
10+
import io.ktor.client.plugins.HttpSend
11+
import io.ktor.client.plugins.plugin
12+
import io.ktor.client.statement.bodyAsChannel
13+
import io.ktor.http.isSuccess
14+
import io.ktor.util.AttributeKey
15+
import io.ktor.utils.io.charsets.Charset
16+
import io.ktor.utils.io.core.readText
17+
18+
class ExceptionPlugin(private val exceptionFactory: ExceptionFactory) {
19+
20+
class Config {
21+
var exceptionFactory: ExceptionFactory? = null
22+
fun build() = ExceptionPlugin(
23+
exceptionFactory
24+
?: throw IllegalArgumentException("Exception factory should be contain")
25+
)
26+
}
27+
28+
companion object Plugin : HttpClientPlugin<Config, ExceptionPlugin> {
29+
30+
override val key = AttributeKey<ExceptionPlugin>("ExceptionPlugin")
31+
32+
override fun prepare(block: Config.() -> Unit) = Config().apply(block).build()
33+
34+
override fun install(plugin: ExceptionPlugin, scope: HttpClient) {
35+
scope.plugin(HttpSend).intercept { request ->
36+
val call = execute(request)
37+
if (!call.response.status.isSuccess()) {
38+
val packet = call.response.bodyAsChannel().readRemaining()
39+
val responseString = packet.readText(charset = Charset.forName("UTF-8"))
40+
throw plugin.exceptionFactory.createException(
41+
request = call.request,
42+
response = call.response,
43+
responseBody = responseString
44+
)
45+
}
46+
call
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)