I am trying to write a custom, "Ktor-client Converter for wrapped responses", in a generic class internally like here Medium Link
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" : {...}
"statusCode" : 0
"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": ""
}
Actual, issue was the deserialization of body, for reference, you can find the repo here: https://github.com/TheReprator/AccountBook_Backend/blob/master/lib/testModule/src/main/kotlin/dev/reprator/testModule/KoinClientModule.kt