Skip to content

Commit 4e05af6

Browse files
authored
fix: possible content convert exception (#294)
1 parent b48372d commit 4e05af6

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

pluto-plugins/plugins/network/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ dependencies {
5656
Add interceptor in your HttpClient
5757
```kotlin
5858
val client = HttpClient {
59-
...
60-
}.apply {
61-
addPlutoKtorInterceptor()
59+
install(PlutoKtorInterceptor)
6260
}
6361
```
6462
<br>
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
package com.pluto.plugins.network.ktor
22

33
import io.ktor.client.HttpClient
4+
import io.ktor.client.plugins.HttpClientPlugin
5+
import io.ktor.util.AttributeKey
46

7+
@Deprecated("install the PlutoKtorInterceptor plugin instead")
58
@SuppressWarnings("EmptyFunctionBlock")
69
fun HttpClient.addPlutoKtorInterceptor() {
710
}
11+
12+
class PlutoKtorInterceptor private constructor() {
13+
companion object : HttpClientPlugin<Unit, PlutoKtorInterceptor> {
14+
override val key: AttributeKey<PlutoKtorInterceptor>
15+
get() = AttributeKey("PlutoKtorInterceptor")
16+
17+
override fun prepare(block: Unit.() -> Unit): PlutoKtorInterceptor {
18+
return PlutoKtorInterceptor()
19+
}
20+
21+
override fun install(plugin: PlutoKtorInterceptor, scope: HttpClient) {
22+
// no-op
23+
}
24+
}
25+
}

pluto-plugins/plugins/network/interceptor-ktor/lib/src/main/kotlin/com/pluto/plugins/network/ktor/PlutoKtorHelper.kt

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ import com.pluto.plugins.network.intercept.NetworkInterceptor
44
import com.pluto.plugins.network.ktor.internal.KtorRequestConverter.convert
55
import com.pluto.plugins.network.ktor.internal.KtorResponseConverter.convert
66
import io.ktor.client.HttpClient
7+
import io.ktor.client.call.save
8+
import io.ktor.client.plugins.HttpClientPlugin
79
import io.ktor.client.plugins.HttpSend
810
import io.ktor.client.plugins.plugin
911
import io.ktor.client.request.url
12+
import io.ktor.util.AttributeKey
1013
import io.ktor.utils.io.errors.IOException
1114

15+
private val saveAttributeKey = AttributeKey<Unit>("ResponseBodySaved")
16+
17+
@Deprecated("install the PlutoKtorInterceptor plugin instead")
1218
fun HttpClient.addPlutoKtorInterceptor() {
13-
// todo add ktor settings block here
1419
plugin(HttpSend).intercept { requestUnBuilt ->
1520
val request = requestUnBuilt.build()
1621
val networkInterceptor = NetworkInterceptor.intercept(request.convert(), NetworkInterceptor.Option(NAME))
@@ -21,10 +26,35 @@ fun HttpClient.addPlutoKtorInterceptor() {
2126
networkInterceptor.onError(e)
2227
throw e
2328
}
29+
val res = if (callResult.attributes.contains(saveAttributeKey)) {
30+
callResult
31+
} else {
32+
val newCall = callResult.save()
33+
newCall.attributes.put(saveAttributeKey, Unit)
34+
newCall
35+
}
2436
networkInterceptor.onResponse(callResult.response.convert())
25-
callResult
37+
res
38+
}
39+
}
40+
41+
class PlutoKtorInterceptor {
42+
companion object : HttpClientPlugin<Unit, PlutoKtorInterceptor> {
43+
44+
override val key: AttributeKey<PlutoKtorInterceptor>
45+
get() = AttributeKey("PlutoKtorInterceptor")
46+
47+
override fun prepare(block: Unit.() -> Unit): PlutoKtorInterceptor {
48+
return PlutoKtorInterceptor()
49+
}
50+
51+
override fun install(plugin: PlutoKtorInterceptor, scope: HttpClient) {
52+
scope.addPlutoKtorInterceptor()
53+
}
54+
2655
}
2756
}
2857

58+
2959
private const val NAME = "Ktor"
3060

sample/src/main/java/com/sampleapp/functions/network/internal/ktor/KtorNetwork.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.sampleapp.functions.network.internal.ktor
22

3-
import com.pluto.plugins.network.ktor.addPlutoKtorInterceptor
3+
import com.pluto.plugins.network.ktor.PlutoKtorInterceptor
44
import io.ktor.client.HttpClient
55
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
66
import io.ktor.client.plugins.defaultRequest
@@ -17,6 +17,5 @@ val Client = HttpClient {
1717
install(ContentNegotiation) {
1818
json()
1919
}
20-
}.apply {
21-
addPlutoKtorInterceptor()
20+
install(PlutoKtorInterceptor)
2221
}

0 commit comments

Comments
 (0)