Skip to content

Commit

Permalink
E2-1628 refactor request class part 1 (#276)
Browse files Browse the repository at this point in the history
* add LPRequesting interface

* add LPRequest, LPRequesting, RequestFactory

* add apiconfig

* minor cleanup

* rename old Request class to RequestOld, add new Request class

* add some methods, change some to private

* add countaggregator and featureflag to recFactory

* minor change to getInstance in reqFactory
  • Loading branch information
gg4race authored Oct 31, 2018
1 parent 34d39d5 commit b9c0bba
Show file tree
Hide file tree
Showing 33 changed files with 1,540 additions and 1,251 deletions.
91 changes: 47 additions & 44 deletions AndroidSDKCore/src/main/java/com/leanplum/Leanplum.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import com.leanplum.internal.Log;
import com.leanplum.internal.OsHandler;
import com.leanplum.internal.Registration;
import com.leanplum.internal.Request;
import com.leanplum.internal.RequestOld;
import com.leanplum.internal.Util;
import com.leanplum.internal.Util.DeviceIdInfo;
import com.leanplum.internal.VarCache;
Expand All @@ -59,11 +59,9 @@
import com.leanplum.utils.SharedPreferencesUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -115,6 +113,7 @@ public class Leanplum {
private static Runnable pushStartCallback;

private static CountAggregator countAggregator = new CountAggregator();
private static FeatureFlagManager featureFlagManager = FeatureFlagManager.INSTANCE;

private Leanplum() {
}
Expand Down Expand Up @@ -234,7 +233,7 @@ public static void setAppIdForDevelopmentMode(String appId, String accessKey) {
}

Constants.isDevelopmentModeEnabled = true;
Request.setAppId(appId, accessKey);
RequestOld.setAppId(appId, accessKey);
}

/**
Expand All @@ -255,7 +254,7 @@ public static void setAppIdForProductionMode(String appId, String accessKey) {
}

Constants.isDevelopmentModeEnabled = false;
Request.setAppId(appId, accessKey);
RequestOld.setAppId(appId, accessKey);
}

/**
Expand Down Expand Up @@ -330,7 +329,7 @@ public static String getDeviceId() {
Log.e("Leanplum.start() must be called before calling getDeviceId.");
return null;
}
return Request.deviceId();
return RequestOld.deviceId();
}

/**
Expand Down Expand Up @@ -566,7 +565,7 @@ static synchronized void start(final Context context, final String userId,
LeanplumInternal.getUserAttributeChanges().add(validAttributes);
}

Request.loadToken();
RequestOld.loadToken();
VarCache.setSilent(true);
VarCache.loadDiffs();
VarCache.setSilent(false);
Expand All @@ -577,12 +576,12 @@ static synchronized void start(final Context context, final String userId,
@Override
public void updateCache() {
triggerVariablesChanged();
if (Request.numPendingDownloads() == 0) {
if (RequestOld.numPendingDownloads() == 0) {
triggerVariablesChangedAndNoDownloadsPending();
}
}
});
Request.onNoPendingDownloads(new Request.NoPendingDownloadsCallback() {
RequestOld.onNoPendingDownloads(new RequestOld.NoPendingDownloadsCallback() {
@Override
public void noPendingDownloads() {
triggerVariablesChangedAndNoDownloadsPending();
Expand Down Expand Up @@ -629,7 +628,7 @@ private static void startHelper(
LeanplumEventDataManager.init(context);
checkAndStartNotificationsModules();
Boolean limitAdTracking = null;
String deviceId = Request.deviceId();
String deviceId = RequestOld.deviceId();
if (deviceId == null) {
if (!userSpecifiedDeviceId && Constants.defaultDeviceId != null) {
deviceId = Constants.defaultDeviceId;
Expand All @@ -640,16 +639,16 @@ private static void startHelper(
deviceId = deviceIdInfo.id;
limitAdTracking = deviceIdInfo.limitAdTracking;
}
Request.setDeviceId(deviceId);
RequestOld.setDeviceId(deviceId);
}

if (userId == null) {
userId = Request.userId();
userId = RequestOld.userId();
if (userId == null) {
userId = Request.deviceId();
userId = RequestOld.deviceId();
}
}
Request.setUserId(userId);
RequestOld.setUserId(userId);

// Setup parameters.
String versionName = Util.getVersionName();
Expand Down Expand Up @@ -702,8 +701,8 @@ private static void startHelper(
Util.initializePreLeanplumInstall(params);

// Issue start API call.
final Request request = Request.post(Constants.Methods.START, params);
request.onApiResponse(new Request.ApiResponseCallback() {
final RequestOld request = RequestOld.post(Constants.Methods.START, params);
request.onApiResponse(new RequestOld.ApiResponseCallback() {
@Override
public void response(List<Map<String, Object>> requests, JSONObject response, int countOfEvents) {
Leanplum.handleApiResponse(response, requests, request, countOfEvents);
Expand All @@ -720,7 +719,7 @@ public void response(List<Map<String, Object>> requests, JSONObject response, in
}

private static void handleApiResponse(JSONObject response, List<Map<String, Object>> requests,
final Request request, int countOfUnsentRequests) {
final RequestOld request, int countOfUnsentRequests) {
JSONObject lastStartResponse = null;

// Find and handle the last start response.
Expand Down Expand Up @@ -748,21 +747,21 @@ private static void handleApiResponse(JSONObject response, List<Map<String, Obje

@VisibleForTesting
public static JSONObject parseLastStartResponse(JSONObject response, List<Map<String, Object>> requests) {
final int responseCount = Request.numResponses(response);
final int responseCount = RequestOld.numResponses(response);
for (int i = requests.size() - 1; i >= 0; i--) {
Map<String, Object> currentRequest = requests.get(i);
if (Constants.Methods.START.equals(currentRequest.get(Constants.Params.ACTION))) {
if (currentRequest.containsKey(Request.REQUEST_ID_KEY)) {
for (int j = Request.numResponses(response) - 1; j >= 0; j--) {
JSONObject currentResponse = Request.getResponseAt(response, j);
if (currentRequest.get(Request.REQUEST_ID_KEY)
.equals(currentResponse.optString(Request.REQUEST_ID_KEY))) {
if (currentRequest.containsKey(RequestOld.REQUEST_ID_KEY)) {
for (int j = RequestOld.numResponses(response) - 1; j >= 0; j--) {
JSONObject currentResponse = RequestOld.getResponseAt(response, j);
if (currentRequest.get(RequestOld.REQUEST_ID_KEY)
.equals(currentResponse.optString(RequestOld.REQUEST_ID_KEY))) {
return currentResponse;
}
}
}
if (i < responseCount) {
return Request.getResponseAt(response, i);
return RequestOld.getResponseAt(response, i);
}
}
}
Expand All @@ -773,7 +772,7 @@ private static void handleStartResponse(final JSONObject response) {
Util.executeAsyncTask(false, new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
boolean success = Request.isResponseSuccess(response);
boolean success = RequestOld.isResponseSuccess(response);
Leanplum.countAggregator().incrementCount("on_start_response");
if (!success) {
try {
Expand Down Expand Up @@ -831,8 +830,8 @@ protected Void doInBackground(Void... params) {
}

String token = response.optString(Constants.Keys.TOKEN, null);
Request.setToken(token);
Request.saveToken();
RequestOld.setToken(token);
RequestOld.saveToken();

applyContentInResponse(response, true);

Expand Down Expand Up @@ -1026,7 +1025,7 @@ public void run() {
}

private static void pauseInternal() {
Request.post(Constants.Methods.PAUSE_SESSION, null).sendIfConnected();
RequestOld.post(Constants.Methods.PAUSE_SESSION, null).sendIfConnected();
pauseHeartbeat();
LeanplumInternal.setIsPaused(true);
}
Expand Down Expand Up @@ -1060,7 +1059,7 @@ public void run() {
}

private static void resumeInternal() {
Request request = Request.post(Constants.Methods.RESUME_SESSION, null);
RequestOld request = RequestOld.post(Constants.Methods.RESUME_SESSION, null);
if (LeanplumInternal.hasStartedInBackground()) {
LeanplumInternal.setStartedInBackground(false);
request.sendIfConnected();
Expand Down Expand Up @@ -1102,7 +1101,7 @@ private static void createHeartbeatExecutor() {
heartbeatExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
try {
Request.post(Constants.Methods.HEARTBEAT, null).sendIfDelayed();
RequestOld.post(Constants.Methods.HEARTBEAT, null).sendIfDelayed();
} catch (Throwable t) {
Util.handleException(t);
}
Expand Down Expand Up @@ -1140,7 +1139,7 @@ public void run() {
}

private static void stopInternal() {
Request.post(Constants.Methods.STOP, null).sendIfConnected();
RequestOld.post(Constants.Methods.STOP, null).sendIfConnected();
}

/**
Expand All @@ -1156,7 +1155,7 @@ public static boolean hasStarted() {
*/
public static String getUserId() {
if (hasStarted()) {
return Request.userId();
return RequestOld.userId();
} else {
Log.e("Leanplum.start() must be called before calling getUserId()");
}
Expand Down Expand Up @@ -1281,7 +1280,7 @@ public static void addVariablesChangedAndNoDownloadsPendingHandler(
noDownloadsHandlers.add(handler);
}
if (VarCache.hasReceivedDiffs()
&& Request.numPendingDownloads() == 0) {
&& RequestOld.numPendingDownloads() == 0) {
handler.variablesChanged();
}
}
Expand Down Expand Up @@ -1369,7 +1368,7 @@ public static void addOnceVariablesChangedAndNoDownloadsPendingHandler(
}

if (VarCache.hasReceivedDiffs()
&& Request.numPendingDownloads() == 0) {
&& RequestOld.numPendingDownloads() == 0) {
handler.variablesChanged();
} else {
synchronized (onceNoDownloadsHandlers) {
Expand Down Expand Up @@ -1536,9 +1535,9 @@ public void run() {

private static void setUserAttributesInternal(String userId,
HashMap<String, Object> requestArgs) {
Request.post(Constants.Methods.SET_USER_ATTRIBUTES, requestArgs).send();
RequestOld.post(Constants.Methods.SET_USER_ATTRIBUTES, requestArgs).send();
if (userId != null && userId.length() > 0) {
Request.setUserId(userId);
RequestOld.setUserId(userId);
if (LeanplumInternal.hasStarted()) {
VarCache.saveDiffs();
}
Expand Down Expand Up @@ -1586,7 +1585,7 @@ public void run() {
try {
HashMap<String, Object> params = new HashMap<>();
params.put(Constants.Params.DEVICE_PUSH_TOKEN, registrationId);
Request.post(Constants.Methods.SET_DEVICE_ATTRIBUTES, params).send();
RequestOld.post(Constants.Methods.SET_DEVICE_ATTRIBUTES, params).send();
} catch (Throwable t) {
Util.handleException(t);
}
Expand Down Expand Up @@ -1637,7 +1636,7 @@ public void run() {
}

private static void setTrafficSourceInfoInternal(HashMap<String, Object> params) {
Request.post(Constants.Methods.SET_TRAFFIC_SOURCE_INFO, params).send();
RequestOld.post(Constants.Methods.SET_TRAFFIC_SOURCE_INFO, params).send();
}

/**
Expand Down Expand Up @@ -1908,7 +1907,7 @@ public void run() {
*/
private static void advanceToInternal(String state, Map<String, ?> params,
Map<String, Object> requestParams) {
Request.post(Constants.Methods.ADVANCE, requestParams).send();
RequestOld.post(Constants.Methods.ADVANCE, requestParams).send();

ContextualValues contextualValues = new ContextualValues();
contextualValues.parameters = params;
Expand Down Expand Up @@ -1988,7 +1987,7 @@ public void run() {
}

private static void pauseStateInternal() {
Request.post(Constants.Methods.PAUSE_STATE, new HashMap<String, Object>()).send();
RequestOld.post(Constants.Methods.PAUSE_STATE, new HashMap<String, Object>()).send();
}

/**
Expand Down Expand Up @@ -2024,7 +2023,7 @@ public void run() {
}

private static void resumeStateInternal() {
Request.post(Constants.Methods.RESUME_STATE, new HashMap<String, Object>()).send();
RequestOld.post(Constants.Methods.RESUME_STATE, new HashMap<String, Object>()).send();
}

/**
Expand Down Expand Up @@ -2058,8 +2057,8 @@ public static void forceContentUpdate(final VariablesChangedCallback callback) {
params.put(Constants.Params.INBOX_MESSAGES, LeanplumInbox.getInstance().messagesIds());
params.put(Constants.Params.INCLUDE_VARIANT_DEBUG_INFO, LeanplumInternal.getIsVariantDebugInfoEnabled());

Request req = Request.post(Constants.Methods.GET_VARS, params);
req.onResponse(new Request.ResponseCallback() {
RequestOld req = RequestOld.post(Constants.Methods.GET_VARS, params);
req.onResponse(new RequestOld.ResponseCallback() {
@Override
public void response(JSONObject response) {
try {
Expand All @@ -2086,7 +2085,7 @@ public void response(JSONObject response) {
}
}
});
req.onError(new Request.ErrorCallback() {
req.onError(new RequestOld.ErrorCallback() {
@Override
public void error(Exception e) {
if (callback != null) {
Expand Down Expand Up @@ -2284,4 +2283,8 @@ private static Set<String> toSet(JSONArray array) {
public static CountAggregator countAggregator() {
return countAggregator;
}

public static FeatureFlagManager featureFlagManager() {
return featureFlagManager;
}
}
18 changes: 9 additions & 9 deletions AndroidSDKCore/src/main/java/com/leanplum/LeanplumInbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import com.leanplum.internal.JsonConverter;
import com.leanplum.internal.Log;
import com.leanplum.internal.OsHandler;
import com.leanplum.internal.Request;
import com.leanplum.internal.RequestOld;
import com.leanplum.internal.Util;
import com.leanplum.utils.SharedPreferencesUtil;

Expand Down Expand Up @@ -255,7 +255,7 @@ void removeMessage(String messageId) {

Map<String, Object> params = new HashMap<>();
params.put(Constants.Params.INBOX_MESSAGE_ID, messageId);
Request req = Request.post(Constants.Methods.DELETE_INBOX_MESSAGE, params);
RequestOld req = RequestOld.post(Constants.Methods.DELETE_INBOX_MESSAGE, params);
req.send();
}

Expand Down Expand Up @@ -287,12 +287,12 @@ void load() {
Context context = Leanplum.getContext();
SharedPreferences defaults = context.getSharedPreferences(
"__leanplum__", Context.MODE_PRIVATE);
if (Request.token() == null) {
if (RequestOld.token() == null) {
update(new HashMap<String, LeanplumInboxMessage>(), 0, false);
return;
}
int unreadCount = 0;
AESCrypt aesContext = new AESCrypt(Request.appId(), Request.token());
AESCrypt aesContext = new AESCrypt(RequestOld.appId(), RequestOld.token());
String newsfeedString = aesContext.decodePreference(
defaults, Constants.Defaults.INBOX_KEY, "{}");
Map<String, Object> newsfeed = JsonConverter.fromJson(newsfeedString);
Expand Down Expand Up @@ -322,7 +322,7 @@ void save() {
if (Constants.isNoop()) {
return;
}
if (Request.token() == null) {
if (RequestOld.token() == null) {
return;
}
Context context = Leanplum.getContext();
Expand All @@ -337,7 +337,7 @@ void save() {
messages.put(messageId, data);
}
String messagesJson = JsonConverter.toJson(messages);
AESCrypt aesContext = new AESCrypt(Request.appId(), Request.token());
AESCrypt aesContext = new AESCrypt(RequestOld.appId(), RequestOld.token());
editor.putString(Constants.Defaults.INBOX_KEY, aesContext.encrypt(messagesJson));
SharedPreferencesUtil.commitChanges(editor);
}
Expand All @@ -347,8 +347,8 @@ void downloadMessages() {
return;
}

final Request req = Request.post(Constants.Methods.GET_INBOX_MESSAGES, null);
req.onResponse(new Request.ResponseCallback() {
final RequestOld req = RequestOld.post(Constants.Methods.GET_INBOX_MESSAGES, null);
req.onResponse(new RequestOld.ResponseCallback() {
@Override
public void response(JSONObject response) {
try {
Expand Down Expand Up @@ -411,7 +411,7 @@ public void variablesChanged() {
}
}
});
req.onError(new Request.ErrorCallback() {
req.onError(new RequestOld.ErrorCallback() {
@Override
public void error(Exception e) {
triggerInboxSyncedWithStatus(false);
Expand Down
Loading

0 comments on commit b9c0bba

Please sign in to comment.