Stylianos Gakis
02/06/2024, 11:09 PMHttpResponse.
• I can not do this.body<MyTypeHere>, since the backend is not also using kotlinx.serialization in order to give me a classDiscriminator so that I can just do a sealed class.
• I could make one type with everything nullable so that I can get the body, and then try and manually check what is null and what is not so that I can map it to my own class
• Something else? I see that calling body twice isn't allowed either, otherwise I would've tried to map it to one, and if that failed I'd try the other.hfhbd
02/06/2024, 11:16 PMStylianos Gakis
02/07/2024, 1:27 AMAleksei Tirman [JB]
02/07/2024, 7:27 AMbody and the bodyAsText methods multiple times.Stylianos Gakis
02/07/2024, 7:47 AMStylianos Gakis
02/07/2024, 9:12 AMbody uses the serializer passed into the ktor client itself in order to do the serialization/deserialization. Is there a way for me to take that out of the HttpResponse so that I don't need to pass my Json instance down to where I need to do this deserialization?Aleksei Tirman [JB]
02/07/2024, 9:13 AMContentNegotiation plugin.Stylianos Gakis
02/07/2024, 9:14 AMbodyNullable if I call call.bodyNullable, it was just not available on HttpResponse itself. I think I will just use that and if it's null try the next thing instead. This way I just use the ktor pipeline itself without having to extract the serializer or anythingStylianos Gakis
02/07/2024, 1:18 PMbodyNullable actually throws a JsonConvertException (which is ContentConvertException : Exception) when it does not find the right type actually, instead of just returning null ☠️ Ouch. And the docs for bodyNullable mention that it might throw NoTransformationFoundException (which is UnsupportedOperationException : RuntimeException) or DoubleReceiveException, so I am guessing the docs above it are wrong and should mention that too?
What I am doing now which works but I really do not love 🤷 is this
internal suspend fun HttpResponse.foo(): FooResult {
val success = try {
body<Success>()
} catch (e: JsonConvertException) {
null
}
if (success != null) {
return SuccessResult
}
val failure = try {
body<Failure>()
} catch (e: JsonConvertException) {
null
}
if (failure != null) {
return FooResult.TypedResponse(failure)
}
return FooResult.OtherFailure
}Ciaran Sloan
02/07/2024, 3:21 PMStylianos Gakis
02/07/2024, 3:30 PMinstall(ContentNegotiation) {
json(Json { isLenient = true; ignoreUnknownKeys = true; ... })
}
so that I can do MyJsonConfig.decodeFromString... right? But if I do that, then I could totally use this polymorphic serializer, and then only have to wrap this in one try catch in case the response object can not map to either of my 2 responses.Ciaran Sloan
02/07/2024, 3:31 PMCiaran Sloan
02/07/2024, 3:40 PMCiaran Sloan
02/07/2024, 3:40 PMdecodeFromString when using the serializerStylianos Gakis
02/07/2024, 3:42 PMJson.decodeFromString(PaymentSerializer, bodyAsText()).Json there is not something that is going to be configured with the exact same configuration as the Json that I am providing to my ktor ContentNegotiation, and optimally I would not want to have two different ways of doing deserialization or serializationbody<>() call, since that one would then use the right Json configuration, right?Ciaran Sloan
02/07/2024, 3:42 PMStylianos Gakis
02/07/2024, 3:42 PMCiaran Sloan
02/07/2024, 3:43 PMwhen over the sealed class, that returns, to handle each case of possible responseCiaran Sloan
02/07/2024, 3:44 PMStylianos Gakis
02/07/2024, 3:53 PMCiaran Sloan
02/07/2024, 3:53 PM