diff --git a/pluto-plugins/plugins/network/README.md b/pluto-plugins/plugins/network/README.md
index 26f8a334..1f9896f8 100644
--- a/pluto-plugins/plugins/network/README.md
+++ b/pluto-plugins/plugins/network/README.md
@@ -56,9 +56,7 @@ dependencies {
Add interceptor in your HttpClient
```kotlin
val client = HttpClient {
-...
-}.apply {
- addPlutoKtorInterceptor()
+ install(PlutoKtorInterceptor)
}
```
diff --git a/pluto-plugins/plugins/network/interceptor-ktor/lib-no-op/src/main/java/com/pluto/plugins/network/ktor/PlutoKtorInterceptor.kt b/pluto-plugins/plugins/network/interceptor-ktor/lib-no-op/src/main/java/com/pluto/plugins/network/ktor/PlutoKtorInterceptor.kt
index c61d049d..04474952 100644
--- a/pluto-plugins/plugins/network/interceptor-ktor/lib-no-op/src/main/java/com/pluto/plugins/network/ktor/PlutoKtorInterceptor.kt
+++ b/pluto-plugins/plugins/network/interceptor-ktor/lib-no-op/src/main/java/com/pluto/plugins/network/ktor/PlutoKtorInterceptor.kt
@@ -1,7 +1,25 @@
package com.pluto.plugins.network.ktor
import io.ktor.client.HttpClient
+import io.ktor.client.plugins.HttpClientPlugin
+import io.ktor.util.AttributeKey
+@Deprecated("install the PlutoKtorInterceptor plugin instead")
@SuppressWarnings("EmptyFunctionBlock")
fun HttpClient.addPlutoKtorInterceptor() {
}
+
+class PlutoKtorInterceptor private constructor() {
+ companion object : HttpClientPlugin {
+ override val key: AttributeKey
+ get() = AttributeKey("PlutoKtorInterceptor")
+
+ override fun prepare(block: Unit.() -> Unit): PlutoKtorInterceptor {
+ return PlutoKtorInterceptor()
+ }
+
+ override fun install(plugin: PlutoKtorInterceptor, scope: HttpClient) {
+ // no-op
+ }
+ }
+}
diff --git a/pluto-plugins/plugins/network/interceptor-ktor/lib/src/main/kotlin/com/pluto/plugins/network/ktor/PlutoKtorHelper.kt b/pluto-plugins/plugins/network/interceptor-ktor/lib/src/main/kotlin/com/pluto/plugins/network/ktor/PlutoKtorHelper.kt
index 348c7b67..73a37f3c 100644
--- a/pluto-plugins/plugins/network/interceptor-ktor/lib/src/main/kotlin/com/pluto/plugins/network/ktor/PlutoKtorHelper.kt
+++ b/pluto-plugins/plugins/network/interceptor-ktor/lib/src/main/kotlin/com/pluto/plugins/network/ktor/PlutoKtorHelper.kt
@@ -4,13 +4,18 @@ import com.pluto.plugins.network.intercept.NetworkInterceptor
import com.pluto.plugins.network.ktor.internal.KtorRequestConverter.convert
import com.pluto.plugins.network.ktor.internal.KtorResponseConverter.convert
import io.ktor.client.HttpClient
+import io.ktor.client.call.save
+import io.ktor.client.plugins.HttpClientPlugin
import io.ktor.client.plugins.HttpSend
import io.ktor.client.plugins.plugin
import io.ktor.client.request.url
+import io.ktor.util.AttributeKey
import io.ktor.utils.io.errors.IOException
+private val saveAttributeKey = AttributeKey("ResponseBodySaved")
+
+@Deprecated("install the PlutoKtorInterceptor plugin instead")
fun HttpClient.addPlutoKtorInterceptor() {
- // todo add ktor settings block here
plugin(HttpSend).intercept { requestUnBuilt ->
val request = requestUnBuilt.build()
val networkInterceptor = NetworkInterceptor.intercept(request.convert(), NetworkInterceptor.Option(NAME))
@@ -21,10 +26,35 @@ fun HttpClient.addPlutoKtorInterceptor() {
networkInterceptor.onError(e)
throw e
}
+ val res = if (callResult.attributes.contains(saveAttributeKey)) {
+ callResult
+ } else {
+ val newCall = callResult.save()
+ newCall.attributes.put(saveAttributeKey, Unit)
+ newCall
+ }
networkInterceptor.onResponse(callResult.response.convert())
- callResult
+ res
+ }
+}
+
+class PlutoKtorInterceptor {
+ companion object : HttpClientPlugin {
+
+ override val key: AttributeKey
+ get() = AttributeKey("PlutoKtorInterceptor")
+
+ override fun prepare(block: Unit.() -> Unit): PlutoKtorInterceptor {
+ return PlutoKtorInterceptor()
+ }
+
+ override fun install(plugin: PlutoKtorInterceptor, scope: HttpClient) {
+ scope.addPlutoKtorInterceptor()
+ }
+
}
}
+
private const val NAME = "Ktor"
diff --git a/sample/src/main/java/com/sampleapp/functions/network/internal/ktor/KtorNetwork.kt b/sample/src/main/java/com/sampleapp/functions/network/internal/ktor/KtorNetwork.kt
index f7859136..c564f5c1 100644
--- a/sample/src/main/java/com/sampleapp/functions/network/internal/ktor/KtorNetwork.kt
+++ b/sample/src/main/java/com/sampleapp/functions/network/internal/ktor/KtorNetwork.kt
@@ -1,6 +1,6 @@
package com.sampleapp.functions.network.internal.ktor
-import com.pluto.plugins.network.ktor.addPlutoKtorInterceptor
+import com.pluto.plugins.network.ktor.PlutoKtorInterceptor
import io.ktor.client.HttpClient
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.defaultRequest
@@ -17,6 +17,5 @@ val Client = HttpClient {
install(ContentNegotiation) {
json()
}
-}.apply {
- addPlutoKtorInterceptor()
+ install(PlutoKtorInterceptor)
}