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

max.denissov

07/23/2019, 6:13 AM
Hello! Who may help with information about how to implement a “response wrapper” pattern in the ktor-client. For example, has a model object User.
Copy code
data User(name: String)
Response from server with a wrapper: Success response:
Copy code
{
  "data": {
    "name": "Mark Watney"
  },
  "error": null
}
Business error respose:
Copy code
{

 	"data": null
 	"error": { ... }
 }
In android I can use something like an interceptor or an adapter in library like Retrofit, but what about ktor-client?
m

mbonnin

07/23/2019, 6:43 AM
Ktor has a JsonFeature that can parse the response for you
m

max.denissov

07/23/2019, 6:50 AM
There is no problem with json, a problem with a common data/error response. Now I am seeing Feature in ktor-client, maybe it will help me intercept server response and unwrap it.
m

mbonnin

07/23/2019, 7:22 AM
Why don't you do
@Serializable Wrapper<T>(val data: T, val error: Error)
N
m

max.denissov

07/23/2019, 7:24 AM
m

mbonnin

07/23/2019, 7:38 AM
I see. Upvoted.
2 Views