I'm building an android app in Kotlin, and the response comes in two different structures for success and error. My question is, can I combine those two responses together and create a data class, or should I create a sealed class for that? Success response:
{
"access_token": "xxxxxxxxxxxxxxxxxxxx",
"token_type": "bearer"
}
Error response:
{
"detail": [
{
"loc": [
"body"
],
"msg": "Invalid credentials",
"type": "invalid-credentials"
}
]
}
So can I write it all like this in a single data class or use a sealed class?
data class Auth (
val access_token: String,
val token_type: String,
val details: Details
)
Yes try combining all the data into a single data class. Just make all the variables optional.
data class Auth (
val access_token: String?,
val token_type: String?,
val detail: List<Detail>?
)
Edit: On how to access the nested data. Pretty Simple.
data class Detail (
val loc: List<String>,
val msg: String,
val type: String
)
Then when an error occurs you access the data with something like val msg = auth.detail?.msg