Skip to content

Instantly share code, notes, and snippets.

@ankitdbst
Forked from pallavahooja/APIHelper.java
Last active July 12, 2019 05:19
Show Gist options
  • Save ankitdbst/b6cd3f510276b5b67d6c7a852a51f5cf to your computer and use it in GitHub Desktop.
Save ankitdbst/b6cd3f510276b5b67d6c7a852a51f5cf to your computer and use it in GitHub Desktop.
Retrofit Retry
package app.goplus.in.v2.network;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* Created by pallavahooja on 16/05/16.
*/
public class APIHelper {
public static final int DEFAULT_RETRIES = 3;
public static <T> void enqueueWithRetry(Call<T> call, final int retryCount,final Callback<T> callback) {
call.enqueue(new RetryableCallback<T>(call, retryCount) {
@Override
public void onFinalResponse(Call<T> call, Response<T> response) {
callback.onResponse(call, response);
}
@Override
public void onFinalFailure(Call<T> call, Throwable t) {
callback.onFailure(call, t);
}
});
}
public static <T> void enqueueWithRetry(Call<T> call, final Callback<T> callback) {
enqueueWithRetry(call, DEFAULT_RETRIES, callback);
}
public static boolean isCallSuccess(Response response) {
int code = response.code();
return (code >= 200 && code < 400);
}
}
Call<BaseResponse> call = authService.initiateSignup(new RequestInitiateSignup(number, referral));
APIHelper.enqueueWithRetry(call, 5, new Callback<BaseResponse>() {
@Override
public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
if (!response.isSuccessful()) {
if (response.code() == 403) {
// Refresh token or redirect to login screen
}
// Failure handling
}
// Success handling
}
@Override
public void onFailure(Call<BaseResponse> call, Throwable t) {
}
}
package app.goplus.in.v2.network;
import android.util.Log;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* Created by pallavahooja on 16/05/16.
*/
public abstract class RetryableCallback<T> implements Callback<T> {
private int totalRetries = 3;
private static final String TAG = RetryableCallback.class.getSimpleName();
private final Call<T> call;
private int retryCount = 0;
public RetryableCallback(Call<T> call, int totalRetries) {
this.call = call;
this.totalRetries = totalRetries;
}
@Override
public void onResponse(Call<T> call, Response<T> response) {
if (!APIHelper.isCallSuccess(response)) {
if (response.code() == 403) {
onFinalResponse(call, response);
} else {
if (retryCount++ < totalRetries) {
Log.v(TAG, "Retrying API Call - (" + retryCount + " / " + totalRetries + ")");
retry();
} else {
onFinalResponse(call, response);
}
}
}
else {
onFinalResponse(call,response);
}
}
@Override
public void onFailure(Call<T> call, Throwable t) {
Log.e(TAG, t.getMessage());
if (retryCount++ < totalRetries) {
Log.v(TAG, "Retrying API Call - (" + retryCount + " / " + totalRetries + ")");
retry();
} else {
onFinalFailure(call, t);
}
}
public void onFinalResponse(Call<T> call, Response<T> response) {
}
public void onFinalFailure(Call<T> call, Throwable t) {
}
private void retry() {
call.clone().enqueue(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment