Retrofit Call Adapter that converts return types of retrofit API definitions into any custom result class.
implementation("app.tourandot:upshot:0.0.3")
-
Define a custom result type:
sealed class NetworkResult<out T> { data class Success<T>(val value: T) : NetworkResult<T>() object NetworkFailure : NetworkResult<Nothing>() data class ApiError(val errorCode: Int) : NetworkResult<Nothing>() }
-
Map to the desired type:
object NetworkResultMapper : UpshotMapper<NetworkResult> { override fun mapSuccess(convertedResponse: Any?) = NetworkResult.Success(convertedResponse!!) override fun mapApiError(errorCode: Int) = NetworkResult.ApiError(errorCode) override fun mapNetworkError(exception: IOException) = NetworkResult.NetworkFailure }
-
Register with retrofit:
retrofitBuilder.addCallAdapterFactory( UpshotCallAdapterFactory.createWithMapper(NetworkResultMapper) )
-
Use the result type in a retrofit service:
interface SwampService { @GET("/friends") suspend fun friends(): NetworkResult<List<Friend>> }
Currently only supports suspend fun
API definitions