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

Tim Malseed

10/12/2021, 7:39 AM
If I wanted to implement some kind response mapper, where I wrap an already-deserialized response object, with some details about the actual HttpResponse itself, how would I go about doing that? It seems like a response pipeline interceptor of some kind. I had a read of the docs, but it wasn't obvious to me what this would look like
a

Aleksei Tirman [JB]

10/12/2021, 10:04 AM
Could you just wrap deserialized response object in the code where you make an HTTP request? The problem is that you need to specify which single type of object you expect to receive.
t

Tim Malseed

10/12/2021, 10:09 AM
Ideally, I'd have access to the response data (http status code, error body, etc.), as well as the deserialized response object. I understand this might mean re-implementing the deserialization logic in my pipeline interceptor, but at least if I could reuse the existing deserialization pipeline logic.. somehow
The main reason I want this is so I can map error types to data models. But I realise now that maybe Ktor already does this.. I haven't actually looked at the exception types thrown when a HTTP request fails
a

Aleksei Tirman [JB]

10/12/2021, 10:30 AM
FYI When you intercept the response pipeline you can rewrap your object in the new response container to push your data in a pipeline:
Copy code
client.responsePipeline.intercept(HttpResponsePipeline.State) { container ->
    proceedWith(HttpResponseContainer(typeInfo<NetworkResult>(), NetworkResult(container.response)))
}
👍 2
today i learned 1
2 Views