Last active
February 9, 2022 10:02
-
-
Save Miha-x64/ec0d224320ec97edaa76ea557a44aa9a to your computer and use it in GitHub Desktop.
A wrapper around retrofit2.Callback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class RetrofitCallback<T> : retrofit2.Callback<T> { | |
final override fun onResponse(call: Call<T>, response: Response<T>) = | |
if (response.isSuccessful) onSuccess(response.body()) | |
else | |
onError(when (response.code()) { | |
401 -> Failure.Unauthorized() | |
404 -> Failure.NotFound() | |
409 -> Failure.Conflict() | |
500 -> Failure.InternalServerError() | |
else -> Failure.Generic() | |
}) | |
final override fun onFailure(call: Call<T>, t: Throwable) = | |
onError(t) | |
protected abstract fun onSuccess(t: T) | |
protected abstract fun onError(t: Throwable) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment