Last active
January 21, 2016 15:35
-
-
Save halysongoncalves/fca3e7ee857267487892 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import android.util.Log; | |
import com.google.gson.Gson; | |
import com.google.gson.annotations.SerializedName; | |
import java.io.IOException; | |
import retrofit.Callback; | |
import retrofit.Response; | |
import retrofit.Retrofit; | |
/** | |
* Created by halysongoncalves on 22/12/15. | |
*/ | |
public abstract class CallbackRequest<T> implements Callback<T> { | |
private static final String TAG = CallbackRequest.class.getSimpleName(); | |
@Override | |
public void onResponse(Response<T> response, Retrofit retrofit) { | |
try { | |
switch (response.code()) { | |
case Kind.HTTP_200: | |
http2xx(Kind.HTTP_200, response); | |
break; | |
case Kind.HTTP_201: | |
http2xx(Kind.HTTP_201, response); | |
break; | |
case Kind.HTTP_204: | |
http2xx(Kind.HTTP_200, response); | |
break; | |
case Kind.HTTP_400: | |
http400(new Gson().fromJson(response.errorBody().string(), BadRequestEvent.class), Kind.HTTP_400); | |
break; | |
case Kind.HTTP_401: | |
http401(new Gson().fromJson(response.errorBody().string(), UnauthorizedEvent.class), Kind.HTTP_401); | |
break; | |
case Kind.HTTP_403: | |
unexpectedError(new Gson().fromJson(response.errorBody().string(), UnexpectedErrorEvent.class)); | |
break; | |
default: | |
unexpectedError(new Gson().fromJson(response.errorBody().string(), UnexpectedErrorEvent.class)); | |
break; | |
} | |
} catch (IOException ioExceotion) { | |
Log.e(TAG, "Error retrieving body", ioExceotion); | |
} | |
} | |
@Override | |
public void onFailure(Throwable throwable) { | |
if (throwable instanceof IOException) { | |
networkError(new NetworkErrorEvent()); | |
return; | |
} | |
unexpectedError(new UnexpectedErrorEvent()); | |
} | |
protected abstract void http2xx(int status, Response<T> response); | |
protected abstract void http400(BadRequestEvent badRequestEvent, int status); | |
protected abstract void http401(UnauthorizedEvent unauthorizedEvent, int status); | |
protected abstract void networkError(NetworkErrorEvent networkErrorEvent); | |
protected abstract void unexpectedError(UnexpectedErrorEvent unexpectedErrorEvent); | |
interface Kind { | |
int HTTP_200 = 200; | |
int HTTP_201 = 201; | |
int HTTP_204 = 204; | |
int HTTP_400 = 400; | |
int HTTP_401 = 401; | |
int HTTP_403 = 403; | |
} | |
public static class UnauthorizedEvent { | |
@SerializedName("message") | |
private String message; | |
public UnauthorizedEvent() { | |
} | |
public UnauthorizedEvent(String message) { | |
this.message = message; | |
} | |
public String getMessage() { | |
return message; | |
} | |
public void setMessage(String message) { | |
this.message = message; | |
} | |
} | |
public static class BadRequestEvent { | |
@SerializedName("message") | |
private String message; | |
public BadRequestEvent() { | |
} | |
public BadRequestEvent(String message) { | |
this.message = message; | |
} | |
public String getMessage() { | |
return message; | |
} | |
public void setMessage(String message) { | |
this.message = message; | |
} | |
} | |
public static class NetworkErrorEvent { | |
@SerializedName("message") | |
private String message; | |
public NetworkErrorEvent() { | |
} | |
public NetworkErrorEvent(String message) { | |
this.message = message; | |
} | |
public String getMessage() { | |
return message; | |
} | |
public void setMessage(String message) { | |
this.message = message; | |
} | |
} | |
public static class UnexpectedErrorEvent { | |
@SerializedName("message") | |
private String message; | |
public UnexpectedErrorEvent() { | |
} | |
public UnexpectedErrorEvent(String message) { | |
this.message = message; | |
} | |
public String getMessage() { | |
return message; | |
} | |
public void setMessage(String message) { | |
this.message = message; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment