Matija Sokol
11/22/2023, 3:58 PMEither<Left, Right>
Aleksei Tirman [JB]
11/22/2023, 5:40 PMMatija Sokol
11/22/2023, 6:05 PMEither
):
sealed class Result<out T> {
data class Success<out T>(val data: T) : Result<T>()
data class Error(val error: ErrorEntity) : Result<Nothing>()
}
And what I want is to return Result
type when calling HttpClient
methods:
suspend fun fetchData(): Result<String> = httpClient.get(url).body()
What I found is that maybe I can do this by creating plugin but I'm not sure if I'm on the right track:
val ConverterPlugin = createClientPlugin("ConverterPlugin", ::ConverterPluginConfig) {
transformResponseBody { response, content, requestedType ->
}
}
Aleksei Tirman [JB]
11/22/2023, 6:07 PMMatija Sokol
11/22/2023, 8:01 PMsuspend fun fetchData(): Result<String> = try {
Result.success(httpClient.get(url).body())
} catch (t: Throwable) {
Result.failure(t)
}
MV
11/23/2023, 6:34 PMMatija Sokol
11/23/2023, 6:52 PMrunCatching
. Actually, I would prefer runCatching
if Kotlin Result
type is used, but for custom models I would go with try-catch
.