Panagiotis Triantafyllou

refresh token fixes

......@@ -186,6 +186,8 @@ import retrofit2.Response;
*/
public class WarplyManager {
private static final int MAX_RETRIES = 3;
public static void getProducts(WarplyProductsRequest request, final CallbackReceiver<ProductList> receiver) {
// String productCategory = "";
// int resStringId = getWarplyContext().getResources().getIdentifier("products_category", "string", getWarplyContext().getPackageName());
......@@ -1568,7 +1570,7 @@ public class WarplyManager {
//TODO: check to see if we need merchants global else delete request
ListenableFuture<MerchantList> futureMerchants = getMerchantsRetro(service);
ListenableFuture<CouponsetsList> futureCouponsets = getCouponsetsRetro(service);
ListenableFuture<CouponList> futureCoupons = getCouponsUniversalRetro(service);
ListenableFuture<CouponList> futureCoupons = getCouponsUniversalRetro(service, 0);
ListenableFuture<List<Object>> allResultsFuture = Futures.allAsList(futureMerchants, futureCouponsets, futureCoupons);
ListenableFuture<CouponList> mergedResultFuture = Futures.transformAsync(
......@@ -1603,7 +1605,7 @@ public class WarplyManager {
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(3));
ListenableFuture<ArrayList<Campaign>> futureCampaigns = getCampaignsRetro(service);
ListenableFuture<ArrayList<Campaign>> futurePersonalizedCampaigns = getCampaignsPersonalizedRetro(service);
ListenableFuture<ArrayList<Campaign>> futurePersonalizedCampaigns = getCampaignsPersonalizedRetro(service, 0);
ListenableFuture<JSONObject> futureCampaignAvailability = getCampaignAvailability(service);
ListenableFuture<List<Object>> allResultsFuture = Futures.allAsList(futureCampaigns, futurePersonalizedCampaigns, futureCampaignAvailability);
......@@ -1835,7 +1837,7 @@ public class WarplyManager {
return future;
}
private static /*void*/ ListenableFuture<ArrayList<Campaign>> getCampaignsPersonalizedRetro(ApiService service/*, final CallbackReceiver<ArrayList<Campaign>> receiver*/) {
private static /*void*/ ListenableFuture<ArrayList<Campaign>> getCampaignsPersonalizedRetro(ApiService service, int tries/*, final CallbackReceiver<ArrayList<Campaign>> receiver*/) {
SettableFuture<ArrayList<Campaign>> future = SettableFuture.create();
String timeStamp = DateFormat.format("yyyy-MM-dd hh:mm:ss", System.currentTimeMillis()).toString();
......@@ -1963,17 +1965,25 @@ public class WarplyManager {
public void onSuccess(JSONObject result) {
int status = result.optInt("status", 2);
if (status == 1)
getCampaignsPersonalizedRetro(service/*, receiver*/);
getCampaignsPersonalizedRetro(service, tries/*, receiver*/);
else {
// receiver.onFailure(status);
future.set(new ArrayList<Campaign>());
if (tries < MAX_RETRIES) {
getCampaignsPersonalizedRetro(service, (tries + 1));
} else {
future.set(new ArrayList<Campaign>());
}
}
}
@Override
public void onFailure(int errorCode) {
// receiver.onFailure(errorCode);
future.set(new ArrayList<Campaign>());
if (tries < MAX_RETRIES) {
getCampaignsPersonalizedRetro(service, (tries + 1));
} else {
future.set(new ArrayList<Campaign>());
}
}
});
} else {
......@@ -4285,7 +4295,7 @@ public class WarplyManager {
});
}
private static ListenableFuture<CouponList> getCouponsUniversalRetro(ApiService service) {
private static ListenableFuture<CouponList> getCouponsUniversalRetro(ApiService service, int tries) {
SettableFuture<CouponList> future = SettableFuture.create();
String timeStamp = DateFormat.format("yyyy-MM-dd hh:mm:ss", System.currentTimeMillis()).toString();
......@@ -4392,21 +4402,32 @@ public class WarplyManager {
future.set(new CouponList());
}
} else if (response.code() == 401) {
// refreshToken(new WarplyRefreshTokenRequest(), new CallbackReceiver<JSONObject>() {
// @Override
// public void onSuccess(JSONObject result) {
// int status = result.optInt("status", 2);
// if (status == 1)
// getCouponsUniversalRetro(service, callback);
// else
// callback.onFailure(call, new Throwable());
// }
//
// @Override
// public void onFailure(int errorCode) {
// callback.onFailure(call, new Throwable());
// }
// });
refreshToken(new WarplyRefreshTokenRequest(), new CallbackReceiver<JSONObject>() {
@Override
public void onSuccess(JSONObject result) {
int status = result.optInt("status", 2);
if (status == 1)
getCouponsUniversalRetro(service, tries/*, receiver*/);
else {
// receiver.onFailure(status);
if (tries < MAX_RETRIES) {
getCouponsUniversalRetro(service, (tries + 1));
} else {
future.set(new CouponList());
}
}
}
@Override
public void onFailure(int errorCode) {
// receiver.onFailure(errorCode);
if (tries < MAX_RETRIES) {
getCouponsUniversalRetro(service, (tries + 1));
} else {
future.set(new CouponList());
}
}
});
} else {
LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
dynatraceEvent.setEventName("custom_error_user_coupons_loyalty");
......