Skip to content

Commit b9c0bba

Browse files
authored
E2-1628 refactor request class part 1 (#276)
* 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
1 parent 34d39d5 commit b9c0bba

33 files changed

+1540
-1251
lines changed

AndroidSDKCore/src/main/java/com/leanplum/Leanplum.java

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
import com.leanplum.internal.Log;
5050
import com.leanplum.internal.OsHandler;
5151
import com.leanplum.internal.Registration;
52-
import com.leanplum.internal.Request;
52+
import com.leanplum.internal.RequestOld;
5353
import com.leanplum.internal.Util;
5454
import com.leanplum.internal.Util.DeviceIdInfo;
5555
import com.leanplum.internal.VarCache;
@@ -59,11 +59,9 @@
5959
import com.leanplum.utils.SharedPreferencesUtil;
6060

6161
import org.json.JSONArray;
62-
import org.json.JSONException;
6362
import org.json.JSONObject;
6463

6564
import java.util.ArrayList;
66-
import java.util.Arrays;
6765
import java.util.Date;
6866
import java.util.HashMap;
6967
import java.util.HashSet;
@@ -115,6 +113,7 @@ public class Leanplum {
115113
private static Runnable pushStartCallback;
116114

117115
private static CountAggregator countAggregator = new CountAggregator();
116+
private static FeatureFlagManager featureFlagManager = FeatureFlagManager.INSTANCE;
118117

119118
private Leanplum() {
120119
}
@@ -234,7 +233,7 @@ public static void setAppIdForDevelopmentMode(String appId, String accessKey) {
234233
}
235234

236235
Constants.isDevelopmentModeEnabled = true;
237-
Request.setAppId(appId, accessKey);
236+
RequestOld.setAppId(appId, accessKey);
238237
}
239238

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

257256
Constants.isDevelopmentModeEnabled = false;
258-
Request.setAppId(appId, accessKey);
257+
RequestOld.setAppId(appId, accessKey);
259258
}
260259

261260
/**
@@ -330,7 +329,7 @@ public static String getDeviceId() {
330329
Log.e("Leanplum.start() must be called before calling getDeviceId.");
331330
return null;
332331
}
333-
return Request.deviceId();
332+
return RequestOld.deviceId();
334333
}
335334

336335
/**
@@ -566,7 +565,7 @@ static synchronized void start(final Context context, final String userId,
566565
LeanplumInternal.getUserAttributeChanges().add(validAttributes);
567566
}
568567

569-
Request.loadToken();
568+
RequestOld.loadToken();
570569
VarCache.setSilent(true);
571570
VarCache.loadDiffs();
572571
VarCache.setSilent(false);
@@ -577,12 +576,12 @@ static synchronized void start(final Context context, final String userId,
577576
@Override
578577
public void updateCache() {
579578
triggerVariablesChanged();
580-
if (Request.numPendingDownloads() == 0) {
579+
if (RequestOld.numPendingDownloads() == 0) {
581580
triggerVariablesChangedAndNoDownloadsPending();
582581
}
583582
}
584583
});
585-
Request.onNoPendingDownloads(new Request.NoPendingDownloadsCallback() {
584+
RequestOld.onNoPendingDownloads(new RequestOld.NoPendingDownloadsCallback() {
586585
@Override
587586
public void noPendingDownloads() {
588587
triggerVariablesChangedAndNoDownloadsPending();
@@ -629,7 +628,7 @@ private static void startHelper(
629628
LeanplumEventDataManager.init(context);
630629
checkAndStartNotificationsModules();
631630
Boolean limitAdTracking = null;
632-
String deviceId = Request.deviceId();
631+
String deviceId = RequestOld.deviceId();
633632
if (deviceId == null) {
634633
if (!userSpecifiedDeviceId && Constants.defaultDeviceId != null) {
635634
deviceId = Constants.defaultDeviceId;
@@ -640,16 +639,16 @@ private static void startHelper(
640639
deviceId = deviceIdInfo.id;
641640
limitAdTracking = deviceIdInfo.limitAdTracking;
642641
}
643-
Request.setDeviceId(deviceId);
642+
RequestOld.setDeviceId(deviceId);
644643
}
645644

646645
if (userId == null) {
647-
userId = Request.userId();
646+
userId = RequestOld.userId();
648647
if (userId == null) {
649-
userId = Request.deviceId();
648+
userId = RequestOld.deviceId();
650649
}
651650
}
652-
Request.setUserId(userId);
651+
RequestOld.setUserId(userId);
653652

654653
// Setup parameters.
655654
String versionName = Util.getVersionName();
@@ -702,8 +701,8 @@ private static void startHelper(
702701
Util.initializePreLeanplumInstall(params);
703702

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

722721
private static void handleApiResponse(JSONObject response, List<Map<String, Object>> requests,
723-
final Request request, int countOfUnsentRequests) {
722+
final RequestOld request, int countOfUnsentRequests) {
724723
JSONObject lastStartResponse = null;
725724

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

749748
@VisibleForTesting
750749
public static JSONObject parseLastStartResponse(JSONObject response, List<Map<String, Object>> requests) {
751-
final int responseCount = Request.numResponses(response);
750+
final int responseCount = RequestOld.numResponses(response);
752751
for (int i = requests.size() - 1; i >= 0; i--) {
753752
Map<String, Object> currentRequest = requests.get(i);
754753
if (Constants.Methods.START.equals(currentRequest.get(Constants.Params.ACTION))) {
755-
if (currentRequest.containsKey(Request.REQUEST_ID_KEY)) {
756-
for (int j = Request.numResponses(response) - 1; j >= 0; j--) {
757-
JSONObject currentResponse = Request.getResponseAt(response, j);
758-
if (currentRequest.get(Request.REQUEST_ID_KEY)
759-
.equals(currentResponse.optString(Request.REQUEST_ID_KEY))) {
754+
if (currentRequest.containsKey(RequestOld.REQUEST_ID_KEY)) {
755+
for (int j = RequestOld.numResponses(response) - 1; j >= 0; j--) {
756+
JSONObject currentResponse = RequestOld.getResponseAt(response, j);
757+
if (currentRequest.get(RequestOld.REQUEST_ID_KEY)
758+
.equals(currentResponse.optString(RequestOld.REQUEST_ID_KEY))) {
760759
return currentResponse;
761760
}
762761
}
763762
}
764763
if (i < responseCount) {
765-
return Request.getResponseAt(response, i);
764+
return RequestOld.getResponseAt(response, i);
766765
}
767766
}
768767
}
@@ -773,7 +772,7 @@ private static void handleStartResponse(final JSONObject response) {
773772
Util.executeAsyncTask(false, new AsyncTask<Void, Void, Void>() {
774773
@Override
775774
protected Void doInBackground(Void... params) {
776-
boolean success = Request.isResponseSuccess(response);
775+
boolean success = RequestOld.isResponseSuccess(response);
777776
Leanplum.countAggregator().incrementCount("on_start_response");
778777
if (!success) {
779778
try {
@@ -831,8 +830,8 @@ protected Void doInBackground(Void... params) {
831830
}
832831

833832
String token = response.optString(Constants.Keys.TOKEN, null);
834-
Request.setToken(token);
835-
Request.saveToken();
833+
RequestOld.setToken(token);
834+
RequestOld.saveToken();
836835

837836
applyContentInResponse(response, true);
838837

@@ -1026,7 +1025,7 @@ public void run() {
10261025
}
10271026

10281027
private static void pauseInternal() {
1029-
Request.post(Constants.Methods.PAUSE_SESSION, null).sendIfConnected();
1028+
RequestOld.post(Constants.Methods.PAUSE_SESSION, null).sendIfConnected();
10301029
pauseHeartbeat();
10311030
LeanplumInternal.setIsPaused(true);
10321031
}
@@ -1060,7 +1059,7 @@ public void run() {
10601059
}
10611060

10621061
private static void resumeInternal() {
1063-
Request request = Request.post(Constants.Methods.RESUME_SESSION, null);
1062+
RequestOld request = RequestOld.post(Constants.Methods.RESUME_SESSION, null);
10641063
if (LeanplumInternal.hasStartedInBackground()) {
10651064
LeanplumInternal.setStartedInBackground(false);
10661065
request.sendIfConnected();
@@ -1102,7 +1101,7 @@ private static void createHeartbeatExecutor() {
11021101
heartbeatExecutor.scheduleAtFixedRate(new Runnable() {
11031102
public void run() {
11041103
try {
1105-
Request.post(Constants.Methods.HEARTBEAT, null).sendIfDelayed();
1104+
RequestOld.post(Constants.Methods.HEARTBEAT, null).sendIfDelayed();
11061105
} catch (Throwable t) {
11071106
Util.handleException(t);
11081107
}
@@ -1140,7 +1139,7 @@ public void run() {
11401139
}
11411140

11421141
private static void stopInternal() {
1143-
Request.post(Constants.Methods.STOP, null).sendIfConnected();
1142+
RequestOld.post(Constants.Methods.STOP, null).sendIfConnected();
11441143
}
11451144

11461145
/**
@@ -1156,7 +1155,7 @@ public static boolean hasStarted() {
11561155
*/
11571156
public static String getUserId() {
11581157
if (hasStarted()) {
1159-
return Request.userId();
1158+
return RequestOld.userId();
11601159
} else {
11611160
Log.e("Leanplum.start() must be called before calling getUserId()");
11621161
}
@@ -1281,7 +1280,7 @@ public static void addVariablesChangedAndNoDownloadsPendingHandler(
12811280
noDownloadsHandlers.add(handler);
12821281
}
12831282
if (VarCache.hasReceivedDiffs()
1284-
&& Request.numPendingDownloads() == 0) {
1283+
&& RequestOld.numPendingDownloads() == 0) {
12851284
handler.variablesChanged();
12861285
}
12871286
}
@@ -1369,7 +1368,7 @@ public static void addOnceVariablesChangedAndNoDownloadsPendingHandler(
13691368
}
13701369

13711370
if (VarCache.hasReceivedDiffs()
1372-
&& Request.numPendingDownloads() == 0) {
1371+
&& RequestOld.numPendingDownloads() == 0) {
13731372
handler.variablesChanged();
13741373
} else {
13751374
synchronized (onceNoDownloadsHandlers) {
@@ -1536,9 +1535,9 @@ public void run() {
15361535

15371536
private static void setUserAttributesInternal(String userId,
15381537
HashMap<String, Object> requestArgs) {
1539-
Request.post(Constants.Methods.SET_USER_ATTRIBUTES, requestArgs).send();
1538+
RequestOld.post(Constants.Methods.SET_USER_ATTRIBUTES, requestArgs).send();
15401539
if (userId != null && userId.length() > 0) {
1541-
Request.setUserId(userId);
1540+
RequestOld.setUserId(userId);
15421541
if (LeanplumInternal.hasStarted()) {
15431542
VarCache.saveDiffs();
15441543
}
@@ -1586,7 +1585,7 @@ public void run() {
15861585
try {
15871586
HashMap<String, Object> params = new HashMap<>();
15881587
params.put(Constants.Params.DEVICE_PUSH_TOKEN, registrationId);
1589-
Request.post(Constants.Methods.SET_DEVICE_ATTRIBUTES, params).send();
1588+
RequestOld.post(Constants.Methods.SET_DEVICE_ATTRIBUTES, params).send();
15901589
} catch (Throwable t) {
15911590
Util.handleException(t);
15921591
}
@@ -1637,7 +1636,7 @@ public void run() {
16371636
}
16381637

16391638
private static void setTrafficSourceInfoInternal(HashMap<String, Object> params) {
1640-
Request.post(Constants.Methods.SET_TRAFFIC_SOURCE_INFO, params).send();
1639+
RequestOld.post(Constants.Methods.SET_TRAFFIC_SOURCE_INFO, params).send();
16411640
}
16421641

16431642
/**
@@ -1908,7 +1907,7 @@ public void run() {
19081907
*/
19091908
private static void advanceToInternal(String state, Map<String, ?> params,
19101909
Map<String, Object> requestParams) {
1911-
Request.post(Constants.Methods.ADVANCE, requestParams).send();
1910+
RequestOld.post(Constants.Methods.ADVANCE, requestParams).send();
19121911

19131912
ContextualValues contextualValues = new ContextualValues();
19141913
contextualValues.parameters = params;
@@ -1988,7 +1987,7 @@ public void run() {
19881987
}
19891988

19901989
private static void pauseStateInternal() {
1991-
Request.post(Constants.Methods.PAUSE_STATE, new HashMap<String, Object>()).send();
1990+
RequestOld.post(Constants.Methods.PAUSE_STATE, new HashMap<String, Object>()).send();
19921991
}
19931992

19941993
/**
@@ -2024,7 +2023,7 @@ public void run() {
20242023
}
20252024

20262025
private static void resumeStateInternal() {
2027-
Request.post(Constants.Methods.RESUME_STATE, new HashMap<String, Object>()).send();
2026+
RequestOld.post(Constants.Methods.RESUME_STATE, new HashMap<String, Object>()).send();
20282027
}
20292028

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

2061-
Request req = Request.post(Constants.Methods.GET_VARS, params);
2062-
req.onResponse(new Request.ResponseCallback() {
2060+
RequestOld req = RequestOld.post(Constants.Methods.GET_VARS, params);
2061+
req.onResponse(new RequestOld.ResponseCallback() {
20632062
@Override
20642063
public void response(JSONObject response) {
20652064
try {
@@ -2086,7 +2085,7 @@ public void response(JSONObject response) {
20862085
}
20872086
}
20882087
});
2089-
req.onError(new Request.ErrorCallback() {
2088+
req.onError(new RequestOld.ErrorCallback() {
20902089
@Override
20912090
public void error(Exception e) {
20922091
if (callback != null) {
@@ -2284,4 +2283,8 @@ private static Set<String> toSet(JSONArray array) {
22842283
public static CountAggregator countAggregator() {
22852284
return countAggregator;
22862285
}
2286+
2287+
public static FeatureFlagManager featureFlagManager() {
2288+
return featureFlagManager;
2289+
}
22872290
}

AndroidSDKCore/src/main/java/com/leanplum/LeanplumInbox.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import com.leanplum.internal.JsonConverter;
3434
import com.leanplum.internal.Log;
3535
import com.leanplum.internal.OsHandler;
36-
import com.leanplum.internal.Request;
36+
import com.leanplum.internal.RequestOld;
3737
import com.leanplum.internal.Util;
3838
import com.leanplum.utils.SharedPreferencesUtil;
3939

@@ -255,7 +255,7 @@ void removeMessage(String messageId) {
255255

256256
Map<String, Object> params = new HashMap<>();
257257
params.put(Constants.Params.INBOX_MESSAGE_ID, messageId);
258-
Request req = Request.post(Constants.Methods.DELETE_INBOX_MESSAGE, params);
258+
RequestOld req = RequestOld.post(Constants.Methods.DELETE_INBOX_MESSAGE, params);
259259
req.send();
260260
}
261261

@@ -287,12 +287,12 @@ void load() {
287287
Context context = Leanplum.getContext();
288288
SharedPreferences defaults = context.getSharedPreferences(
289289
"__leanplum__", Context.MODE_PRIVATE);
290-
if (Request.token() == null) {
290+
if (RequestOld.token() == null) {
291291
update(new HashMap<String, LeanplumInboxMessage>(), 0, false);
292292
return;
293293
}
294294
int unreadCount = 0;
295-
AESCrypt aesContext = new AESCrypt(Request.appId(), Request.token());
295+
AESCrypt aesContext = new AESCrypt(RequestOld.appId(), RequestOld.token());
296296
String newsfeedString = aesContext.decodePreference(
297297
defaults, Constants.Defaults.INBOX_KEY, "{}");
298298
Map<String, Object> newsfeed = JsonConverter.fromJson(newsfeedString);
@@ -322,7 +322,7 @@ void save() {
322322
if (Constants.isNoop()) {
323323
return;
324324
}
325-
if (Request.token() == null) {
325+
if (RequestOld.token() == null) {
326326
return;
327327
}
328328
Context context = Leanplum.getContext();
@@ -337,7 +337,7 @@ void save() {
337337
messages.put(messageId, data);
338338
}
339339
String messagesJson = JsonConverter.toJson(messages);
340-
AESCrypt aesContext = new AESCrypt(Request.appId(), Request.token());
340+
AESCrypt aesContext = new AESCrypt(RequestOld.appId(), RequestOld.token());
341341
editor.putString(Constants.Defaults.INBOX_KEY, aesContext.encrypt(messagesJson));
342342
SharedPreferencesUtil.commitChanges(editor);
343343
}
@@ -347,8 +347,8 @@ void downloadMessages() {
347347
return;
348348
}
349349

350-
final Request req = Request.post(Constants.Methods.GET_INBOX_MESSAGES, null);
351-
req.onResponse(new Request.ResponseCallback() {
350+
final RequestOld req = RequestOld.post(Constants.Methods.GET_INBOX_MESSAGES, null);
351+
req.onResponse(new RequestOld.ResponseCallback() {
352352
@Override
353353
public void response(JSONObject response) {
354354
try {
@@ -411,7 +411,7 @@ public void variablesChanged() {
411411
}
412412
}
413413
});
414-
req.onError(new Request.ErrorCallback() {
414+
req.onError(new RequestOld.ErrorCallback() {
415415
@Override
416416
public void error(Exception e) {
417417
triggerInboxSyncedWithStatus(false);

0 commit comments

Comments
 (0)