Skip to content

Instantly share code, notes, and snippets.

@Miha-x64
Last active February 9, 2022 10:02
Show Gist options
  • Save Miha-x64/ec0d224320ec97edaa76ea557a44aa9a to your computer and use it in GitHub Desktop.
Save Miha-x64/ec0d224320ec97edaa76ea557a44aa9a to your computer and use it in GitHub Desktop.
A wrapper around retrofit2.Callback
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