Skip to content

Instantly share code, notes, and snippets.

@pallavahooja
Last active November 9, 2020 16:48
Show Gist options
  • Save pallavahooja/1aaff98c1a790db7ea96e0015d1ac2d3 to your computer and use it in GitHub Desktop.
Save pallavahooja/1aaff98c1a790db7ea96e0015d1ac2d3 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) {
}
@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 (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);
}
}
@pallavahooja
Copy link
Author

Thoughts and suggestions on better design welcome

@GlobeTechGit
Copy link

Very nice, thank you

@guille150
Copy link

Muchas gracias! está perfecto!!

@GitMoDu
Copy link

GitMoDu commented Apr 23, 2020

Functional and concise, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment