Hey guys, Do you have any good example of <Kotlin:...
# multiplatform
v
Hey guys, Do you have any good example of Kotlin: Sealed Classes for better handling of API Response in multiplatform?
t
Copy code
sealed class ApiResult<out R, out E>
data class ApiSuccess<out R>(val value: R) : ApiResult<R, Nothing>()
data class ApiFailure<out E>(
    val status: ApiErrorStatus,
    val details: E?
) : ApiResult<Nothing, E>()
This is ours. It helps when you have domain specific error response bodies in addition to success bodies.
For example, we have an api that changes an order state. It requires the clients expected state as well as the new state to catch concurrency errors. The details of what the actual state was when it doesn’t match the expected state comes back in a domain specific error body.
Notably we don’t have a loading state, because that’s more of a View/Presenter layer concern, and these objects are just used in the repository/data layer.
m
Why does the original approach not work for KMP?
t
How does it not work for KMP?
m
The OP was asking for good examples of using sealed classes in multiplatform. The article linked seems like it would work with multiplatform as-is. I was confused about the basis of the question.
t
Oh for sure, I missed that myself. It seems like it would in fact work
So @Vivek Modi why doesn’t it work? Or more precisely, how doesn’t it work?
v
Hey @Tim Oltjenbruns great explanation. As you see in the link, there is function called
fetchResult
inside that code is checking every time
response.code()
and route according to domain specific
Success
or
Error
body. Is there any better way it will automatically route on specific domain rather than checking every time.
t
Well that’s dependent on what you use to make HTTP calls
The example you linked looks like it uses Retrofit?
v
@Tim Oltjenbruns @mike.holler I am using Ktor for api call.
t
So the implementation will be different then.
v
So do you have any sample project?
t
I do not have an example using Ktor. For my KMP I use Retrofit on Android and another library for iOS
You may want to ask in the Ktor channel
m
#ktor
🙌 1
v
ok sure thanks