I am trying to write a custom, "Ktor-client Conver...
# ktor
r
I am trying to write a custom, "Ktor-client Converter for wrapped responses", in a generic class internally like here https://medium.com/hackernoon/retrofit-converter-for-wrapped-responses-8919298a549c , but unable to do so, Example: Suppose an api gives following response, @Serializable @SerialName("DataResponseContainer") data class DataResponseContainer<out T>(val data: T?, val statusCode: Int?, val message: String?) Api Response format:: { "data" : {...} "status_code" : 0 "error_message" : null } for which we write the client as follows, httpClient.get("splash").body<DataResponseContainer<SplashEntity>>() now, i need only, "data" object, so i want to write it like, httpClient.get("splash").body<SplashEntity>() httpClient.get("login").body<LoginEntity>() Also, i tried to write custom plugin but i don't know how to do it, val DataTransformationPlugin = createClientPlugin("DataTransformationPlugin") { transformResponseBody { response, content, requestedType -> val mapper = ObjectMapper() if(response.status.isSuccess()) { try { val user: ResponseWrapper<*> = mapper.readValue(response.bodyAsText(), ResponseWrapper::class.java) //Now i want to deserialize it to get only "data" in called body } catch (e: Exception) { content } } else content } } Response Modal: data class SplashEntity(val lang: String, val type: String) data class LoginEntity(val id: Int, val role: String) Response for splash: { data: { "lang": "en", "type": "owner", }, "statusCode": 200, "message": "" } Response for login: { data: { "id": 1244, "role": "admin", }, "statusCode": 200, "message": "" }
🧵 2
@AdamW like this?