Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.remek.rosettapos.communication.api
- import retrofit2.Call
- import retrofit2.Callback
- import retrofit2.Response
- /**
- * Callback for the API calls.
- */
- interface ApiCallback<T> : Callback<T> {
- companion object {
- const val UNKNOWN_ERROR_MESSAGE = "Unknown error"
- }
- override fun onResponse(call: Call<T>, response: Response<T>) {
- val responseBody = response.body()
- val error = response.errorBody()?.toString()
- if (response.isSuccessful && responseBody != null) {
- onSuccess(responseBody)
- } else {
- when (response.code()) {
- 401 -> onError(CommunicationException.NotAuthenticatedException("Authentication not successful."))
- else -> onError(CommunicationException.UnknownCommunicationException(error ?: UNKNOWN_ERROR_MESSAGE))
- }
- }
- }
- override fun onFailure(call: Call<T>, t: Throwable) {
- onError(CommunicationException.UnknownCommunicationException(UNKNOWN_ERROR_MESSAGE))
- }
- /**
- * Called after a successful network request.
- *
- * @param response the response object.
- */
- fun onSuccess(response: T)
- /**
- * Called if network communication fails.
- *
- * @param error an exception that may tell more about the error.
- */
- fun onError(error: CommunicationException)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement