Retrofit Response Evaluator for Android
The ResponseEvaluator
maps a retrofit2.Response<T>
object to a kotlin sealed class APIResult<T>
.
This gives you a conveniant way to differentiate the different api results using kotlins when-statement.
Usage
// apiResponse is of type retrofit2.Response and comes
// from your retrofit api interface
val apiResult = ResponseEvaluator.evaluate(apiResponse)
when(apiResult){
is APIResult.Success -> {
apiResult.response.body()?.let { responseObject ->
// ...
}
}
is APIResult.Failure.ClientError -> {}
is APIResult.Failure.ServerError -> {}
else -> {}
}
// or fully specified
when(apiResult){
is APIResult.Success.Ok -> {}
is APIResult.Success.Created -> {}
is APIResult.Success.Accepted -> {}
is APIResult.Success.Other -> {}
is APIResult.Redirect -> {}
is APIResult.Failure.ClientError.BadRequest -> {}
is APIResult.Failure.ClientError.Conflict -> {}
is APIResult.Failure.ClientError.Forbidden -> {}
is APIResult.Failure.ClientError.Gone -> {}
is APIResult.Failure.ClientError.NotFound -> {}
is APIResult.Failure.ClientError.Other -> {}
is APIResult.Failure.ClientError.Unauthorized -> {}
is APIResult.Failure.ServerError.BadGateway -> {}
is APIResult.Failure.ServerError.GatewayTimeout -> {}
is APIResult.Failure.ServerError.InternalError -> {}
is APIResult.Failure.ServerError.Unavailable -> {}
is APIResult.Failure.ServerError.Other -> {}
is APIResult.Unknown -> {}
}
Installation
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the dependency to your module level build.gradle:
dependencies {
implementation 'com.github.bornholdtleegmbh:response-evaluator:1.2.1'
}