EitherCallAdapterFactory
A CallAdapter.Factory which supports suspend + Either as the return type
Adding this to Retrofit will enable you to return Either or ResponseE from your service methods. ResponseE is similar to retrofit2.Response but uses Either for the response body.
import arrow.core.Either
import arrow.retrofit.adapter.either.ResponseE
import retrofit2.http.GET
data class User(val name: String)
data class ErrorBody(val msg: String)
interface MyService {
@GET("/user/me")
suspend fun getUser(): Either<ErrorBody, User>
@GET("/user/me")
suspend fun getUserResponseE(): ResponseE<ErrorBody, User>
}
Content copied to clipboard
Using Either or ResponseE as the return type means that 200 status code and HTTP errors return a value, other exceptions will throw.
If you want an adapter that never throws but instead wraps all errors (including no network, timeout, malformed JSON) in a dedicated type then define CallError as your error type argument. Note that this adapter only supports Either as the response wrapper (it does not support ResponseE):
import arrow.core.Either
import arrow.retrofit.adapter.either.networkhandling.CallError
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
data class User(val name: String)
interface MyService {
@GET("/user/me")
suspend fun getUser(): Either<CallError, User>
// Set the expected response type as Unit if you expect a null response body
// (e.g. for 204 No Content response)
@POST("/")
suspend fun postSomething(@Body something: String): Either<CallError, Unit>
}
Content copied to clipboard
Functions
Link copied to clipboard
open operator override fun get(returnType: Type, annotations: Array<Annotation>, retrofit: Retrofit): CallAdapter<*, *>?