Skip to content

Instantly share code, notes, and snippets.

@uzzu
Created April 2, 2019 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uzzu/9bbc6451b2578f7d1d525cda47cdde71 to your computer and use it in GitHub Desktop.
Save uzzu/9bbc6451b2578f7d1d525cda47cdde71 to your computer and use it in GitHub Desktop.
import io.ktor.client.HttpClient
import io.ktor.client.features.HttpClientFeature
import io.ktor.client.response.HttpResponse
import io.ktor.client.response.HttpResponseContainer
import io.ktor.client.response.HttpResponsePipeline
import io.ktor.util.AttributeKey
internal class WrappedHttpResponseTransform {
companion object Feature : HttpClientFeature<Unit, WrappedHttpResponseTransform> {
override val key: AttributeKey<WrappedHttpResponseTransform> = AttributeKey("WrappedHttpResponseTransform")
override fun prepare(block: Unit.() -> Unit): WrappedHttpResponseTransform = WrappedHttpResponseTransform()
override fun install(feature: WrappedHttpResponseTransform, scope: HttpClient) {
scope.responsePipeline.intercept(HttpResponsePipeline.Transform) { (info, response) ->
if (response !is HttpResponse) {
return@intercept
}
proceedWith(HttpResponseContainer(info, WrappedHttpResponse(response)))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment