Skip to content

Commit

Permalink
fix: possible content convert exception
Browse files Browse the repository at this point in the history
  • Loading branch information
epicadk committed Nov 25, 2023
1 parent b48372d commit 7a068a9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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<Unit, PlutoKtorInterceptor> {
override val key: AttributeKey<PlutoKtorInterceptor>
get() = AttributeKey("PlutoKtorInterceptor")

override fun prepare(block: Unit.() -> Unit): PlutoKtorInterceptor {
return PlutoKtorInterceptor()
}

override fun install(plugin: PlutoKtorInterceptor, scope: HttpClient) {
// no-op
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Unit>("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))
Expand All @@ -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<Unit, PlutoKtorInterceptor> {

override val key: AttributeKey<PlutoKtorInterceptor>
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"

Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,6 +17,5 @@ val Client = HttpClient {
install(ContentNegotiation) {
json()
}
}.apply {
addPlutoKtorInterceptor()
install(PlutoKtorInterceptor)
}

0 comments on commit 7a068a9

Please sign in to comment.