https://kotlinlang.org logo
#ktor
Title
# ktor
j

Javier

06/07/2021, 12:56 PM
Is it possible a feature/plugin depends on another?
e

El Zhang

06/07/2021, 1:26 PM
For ktor client's plugin I used/created, I don't think there's a direct approach to impl this. But if u look into their implementations, can see the interceptor pipelines - base on the lifecycle of those pipeline, u can define the running sequence and thus make dependent relationship indirectly.
j

Javier

06/07/2021, 1:30 PM
I need to transform the type with my feature before JsonFeature get it.
Copy code
@Serializable
data class User

@Serializable
data class SomeError

client.get<Either<SomeError, User>>(...)
The problem is JsonFeature should be able to parse SomeError or User, so I have to unwrap it from my Either before, if not, JsonFeature will try it with the Either class and it will fail
e

El Zhang

06/07/2021, 1:36 PM
Probably u can use HttpResponsePipeline.Receive in your custom plugin to unwrap before JsonFeature works.
Copy code
scope.responsePipeline.intercept(HttpResponsePipeline.Receive) { (info, body) ->
    if (body !is ByteReadChannel) { return @intercept}
    // Do something you want and get the final result in String (others types I did not try)
    val result: String = decrypt(body.readRemaining().readText())
    proceedWith(HttpResponseContainer(info, ByteReadChannel(result)))
}
j

Javier

06/07/2021, 1:37 PM
I will try, thank you 🙂
e

El Zhang

06/07/2021, 1:39 PM
Updated my code, I used it in my project to decrypt encoded Json Strings before JsonFeature.
j

Javier

06/07/2021, 1:40 PM
But it let changing the type that JsonFeature will receive?
e

El Zhang

06/07/2021, 1:42 PM
Well.. from the JsonFeature it does not aware our interceptor, so it reads http response from the ByteReadChannel (Kinda byte stream or something similar) always.
5 Views