Panagiotis Triantafyllou

added campaigns availability request

......@@ -100,6 +100,17 @@ public interface ApiService {
@Header(WarpConstants.HEADER_SIGNATURE) String signature);
@Headers("Content-Type: application/json")
@POST("/api/mobile/v2/{appUuid}/context/")
Call<ResponseBody> getCampaignsAvailability(@Path("appUuid") String appUuid,
@Body RequestBody request,
@Header(WarpConstants.HEADER_DATE) String timeStamp,
@Header(WarpConstants.HEADER_LOYALTY_BUNDLE_ID) String bundleId,
@Header(WarpConstants.HEADER_UNIQUE_DEVICE_ID) String deviceId,
@Header(WarpConstants.HEADER_CHANNEL) String channel,
@Header(WarpConstants.HEADER_WEB_ID) String webId,
@Header(WarpConstants.HEADER_SIGNATURE) String signature);
@Headers("Content-Type: application/json")
@POST("/oauth/{appUuid}/context")
Call<ResponseBody> getCampaignsPersonalized(@Path("appUuid") String appUuid,
@Body RequestBody request,
......
......@@ -1565,7 +1565,7 @@ public class WarplyManager {
request.getSignature()));
}
private static ArrayList<Campaign> mergeCampaignResults(ArrayList<Campaign> resultCampaigns, ArrayList<Campaign> resultPersonalizedCampaigns) {
private static ArrayList<Campaign> mergeCampaignResults(ArrayList<Campaign> resultCampaigns, ArrayList<Campaign> resultPersonalizedCampaigns, JSONObject resultCampaignsAvailability) {
ArrayList<Campaign> newCampaignList = new ArrayList<Campaign>();
ArrayList<Campaign> campaignLoyaltyList = new ArrayList<>();
newCampaignList.clear();
......@@ -1619,14 +1619,16 @@ public class WarplyManager {
ListenableFuture<ArrayList<Campaign>> futureCampaigns = getCampaignsRetro(service);
ListenableFuture<ArrayList<Campaign>> futurePersonalizedCampaigns = getCampaignsPersonalizedRetro(service);
ListenableFuture<JSONObject> futureCampaignAvailability = getCampaignAvailability(service);
ListenableFuture<List<ArrayList<Campaign>>> allResultsFuture = Futures.allAsList(futureCampaigns, futurePersonalizedCampaigns);
ListenableFuture<List<Object>> allResultsFuture = Futures.allAsList(futureCampaigns, futurePersonalizedCampaigns, futureCampaignAvailability);
ListenableFuture<ArrayList<Campaign>> mergedResultFuture = Futures.transformAsync(
allResultsFuture,
results -> {
ArrayList<Campaign> resultCampaigns = results.get(0);
ArrayList<Campaign> resultPersonalizedCampaigns = results.get(1);
return executorService.submit(() -> mergeCampaignResults(resultCampaigns, resultPersonalizedCampaigns));
ArrayList<Campaign> resultCampaigns = (ArrayList<Campaign>) results.get(0);
ArrayList<Campaign> resultPersonalizedCampaigns = (ArrayList<Campaign>) results.get(1);
JSONObject resultCampaignsAvailability = (JSONObject) results.get(2);
return executorService.submit(() -> mergeCampaignResults(resultCampaigns, resultPersonalizedCampaigns, resultCampaignsAvailability));
},
executorService
);
......@@ -1650,8 +1652,8 @@ public class WarplyManager {
// getCampaignsPersonalizedRetro(service, new CallbackReceiver<ArrayList<Campaign>>() {
// @Override
// public void onSuccess(ArrayList<Campaign> resultPersonalized) {
// final ExecutorService executorShops = Executors.newFixedThreadPool(1);
// executorShops.submit(() -> {
// final ExecutorService executorCampaigns = Executors.newFixedThreadPool(1);
// executorCampaigns.submit(() -> {
// ArrayList<Campaign> newCampaignList = new ArrayList<Campaign>();
// newCampaignList.clear();
// newCampaignList.addAll(result);
......@@ -1686,7 +1688,7 @@ public class WarplyManager {
// Set<Campaign> set = new LinkedHashSet<>(campaignLoyaltyList);
// campaignLoyaltyList.clear();
// campaignLoyaltyList.addAll(set);
// executorShops.shutdownNow();
// executorCampaigns.shutdownNow();
// new Handler(Looper.getMainLooper()).post(() -> receiver.onSuccess(campaignLoyaltyList));//resultPersonalized
// });
// }
......@@ -1759,9 +1761,9 @@ public class WarplyManager {
ArrayList<NewCampaign> tempCampaigns = new ArrayList<>();
ArrayList<Campaign> mCampaignsList = new ArrayList<>();
final ExecutorService executorShops = Executors.newFixedThreadPool(1);
final ExecutorService executorCampaigns = Executors.newFixedThreadPool(1);
JSONArray finalCampaignsJBody = jCampaignsBody;
executorShops.submit(() -> {
executorCampaigns.submit(() -> {
for (int i = 0; i < finalCampaignsJBody.length(); ++i) {
tempCampaigns.add(new NewCampaign(finalCampaignsJBody.optJSONObject(i)));
}
......@@ -1809,7 +1811,7 @@ public class WarplyManager {
mCampaignsList.add(camp);
}
executorShops.shutdownNow();
executorCampaigns.shutdownNow();
// receiver.onSuccess(mCampaignsList);
future.set(mCampaignsList);
});
......@@ -1853,6 +1855,7 @@ public class WarplyManager {
Map<String, Object> jsonParamsCampaigns = new ArrayMap<>();
Map<String, Object> jsonParams = new ArrayMap<>();
jsonParams.put("action", "retrieve");
jsonParams.put("filters", new JSONObject());
jsonParams.put("language", WarplyProperty.getLanguage(Warply.getWarplyContext()));
jsonParamsCampaigns.put("campaigns", jsonParams);
......@@ -1897,9 +1900,9 @@ public class WarplyManager {
ArrayList<NewCampaign> tempCampaigns = new ArrayList<>();
ArrayList<Campaign> mCampaignsList = new ArrayList<>();
final ExecutorService executorShops = Executors.newFixedThreadPool(1);
final ExecutorService executorPersonalizedCampaigns = Executors.newFixedThreadPool(1);
JSONArray finalCampaignsJBody = jCampaignsBody;
executorShops.submit(() -> {
executorPersonalizedCampaigns.submit(() -> {
for (int i = 0; i < finalCampaignsJBody.length(); ++i) {
tempCampaigns.add(new NewCampaign(finalCampaignsJBody.optJSONObject(i)));
}
......@@ -1947,7 +1950,7 @@ public class WarplyManager {
mCampaignsList.add(camp);
}
executorShops.shutdownNow();
executorPersonalizedCampaigns.shutdownNow();
// receiver.onSuccess(mCampaignsList);
future.set(mCampaignsList);
});
......@@ -2000,6 +2003,93 @@ public class WarplyManager {
return future;
}
private static ListenableFuture<JSONObject> getCampaignAvailability(ApiService service) {
SettableFuture<JSONObject> future = SettableFuture.create();
String timeStamp = DateFormat.format("yyyy-MM-dd hh:mm:ss", System.currentTimeMillis()).toString();
String apiKey = WarpUtils.getApiKey(Warply.getWarplyContext());
String webId = WarpUtils.getWebId(Warply.getWarplyContext());
Map<String, Object> jsonParamsCampaignsAvailability = new ArrayMap<>();
Map<String, Object> jsonParams = new ArrayMap<>();
jsonParams.put("action", "availability");
Map<String, Object> jsonParamsFilters = new ArrayMap<>();
jsonParams.put("uuids", null);
jsonParams.put("availability_enabled", true);
jsonParams.put("filters", jsonParamsFilters);
jsonParams.put("language", WarplyProperty.getLanguage(Warply.getWarplyContext()));
jsonParamsCampaignsAvailability.put("coupon", jsonParams);
RequestBody campaignsAvailabilityRequest = RequestBody.create(MediaType.get("application/json; charset=utf-8"), (new JSONObject(jsonParamsCampaignsAvailability)).toString());
Call<ResponseBody> campaignsAvailabilityCall = service.getCampaignsAvailability(
WarplyProperty.getAppUuid(Warply.getWarplyContext()),
campaignsAvailabilityRequest,
timeStamp,
"android:" + Warply.getWarplyContext().getPackageName(),
new WarplyDeviceInfoCollector(Warply.getWarplyContext()).getUniqueDeviceId(),
"mobile",
webId,
WarpUtils.produceSignature(apiKey + timeStamp)
);
campaignsAvailabilityCall.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
if (response.code() == 200 && response.body() != null) {
JSONObject jobjCampaignsAvailabilityResponse = null;
try {
jobjCampaignsAvailabilityResponse = new JSONObject(response.body().string());
} catch (Exception e) {
e.printStackTrace();
}
if (jobjCampaignsAvailabilityResponse != null && jobjCampaignsAvailabilityResponse.has("status") && jobjCampaignsAvailabilityResponse.optString("status", "2").equals("1")) {
LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
dynatraceEvent.setEventName("custom_success_available_coupons");
EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
final ExecutorService executorCampaignsAvailability = Executors.newFixedThreadPool(1);
final JSONObject finalJobjCampaignsAvailabilityResponse = jobjCampaignsAvailabilityResponse;
executorCampaignsAvailability.submit(() -> {
JSONObject jCampaignsAvailabilityBody = null;
try {
jCampaignsAvailabilityBody = finalJobjCampaignsAvailabilityResponse.optJSONObject("context").optJSONObject("MAPP_COUPON").optJSONObject("availability");
} catch (Exception e) {
e.printStackTrace();
}
if (jCampaignsAvailabilityBody != null) {
executorCampaignsAvailability.shutdownNow();
future.set(jCampaignsAvailabilityBody);
}
});
} else {
LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
dynatraceEvent.setEventName("custom_error_available_coupons");
EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
future.set(new JSONObject());
}
} else {
LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
dynatraceEvent.setEventName("custom_error_available_coupons");
EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
future.set(new JSONObject());
}
}
@Override
public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
dynatraceEvent.setEventName("custom_error_available_coupons");
EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
future.set(new JSONObject());
}
});
return future;
}
public static void getCampaignsPersonalized(WarplyGetCampaignsRequest request, final CallbackReceiver<ArrayList<Campaign>> receiver) {
WarpUtils.log("************* WARPLY Get Campaigns Request ********************");
WarpUtils.log("[WARP Trace] WARPLY Get Campaigns Request is active");
......