Skip to content

Commit 3a395dd

Browse files
committed
code review - refactor check for email and userId.
1 parent 3fe68f7 commit 3a395dd

File tree

2 files changed

+27
-95
lines changed

2 files changed

+27
-95
lines changed

.idea/misc.xml

Lines changed: 0 additions & 58 deletions
This file was deleted.

iterableapi/src/main/java/com/iterable/iterableapi/IterableApi.java

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,7 @@ public void track(String eventName, String campaignId, String templateId) {
278278
public void track(String eventName, String campaignId, String templateId, JSONObject dataFields) {
279279
JSONObject requestJSON = new JSONObject();
280280
try {
281-
if (_email != null) {
282-
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
283-
} else {
284-
requestJSON.put(IterableConstants.KEY_USER_ID, _userId);
285-
}
281+
addEmailOrUserIdToJson(requestJSON);
286282
requestJSON.put(IterableConstants.KEY_EVENTNAME, eventName);
287283

288284
requestJSON.put(IterableConstants.KEY_CAMPAIGNID, campaignId);
@@ -342,19 +338,19 @@ public void sendPush(String email, int campaignId, Date sendAt, JSONObject dataF
342338
}
343339

344340
public void updateEmail(String newEmail) {
345-
JSONObject requestJSON = new JSONObject();
341+
if (_email != null) {
342+
JSONObject requestJSON = new JSONObject();
346343

347-
try {
348-
requestJSON.put(IterableConstants.KEY_CURRENT_EMAIL, _email);
349-
requestJSON.put(IterableConstants.KEY_NEW_EMAIL, newEmail);
350-
}
351-
catch (JSONException e) {
352-
e.printStackTrace();
353-
}
344+
try {
345+
requestJSON.put(IterableConstants.KEY_CURRENT_EMAIL, _email);
346+
requestJSON.put(IterableConstants.KEY_NEW_EMAIL, newEmail);
347+
}
348+
catch (JSONException e) {
349+
e.printStackTrace();
350+
}
354351

355-
sendRequest(IterableConstants.ENDPOINT_UPDATEEMAIL, requestJSON);
352+
sendRequest(IterableConstants.ENDPOINT_UPDATEEMAIL, requestJSON);
356353

357-
if (_email != null) {
358354
_email = newEmail;
359355
} else {
360356
IterableLogger.w(TAG, "updateEmail should not be called with a userId. " +
@@ -366,12 +362,7 @@ public void updateUser(JSONObject dataFields) {
366362
JSONObject requestJSON = new JSONObject();
367363

368364
try {
369-
if (_email != null) {
370-
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
371-
} else {
372-
requestJSON.put(IterableConstants.KEY_USER_ID, _userId);
373-
}
374-
365+
addEmailOrUserIdToJson(requestJSON);
375366
requestJSON.put(IterableConstants.KEY_DATAFIELDS, dataFields);
376367
}
377368
catch (JSONException e) {
@@ -425,11 +416,7 @@ protected void trackPushOpen(int campaignId, int templateId, String messageId) {
425416
JSONObject requestJSON = new JSONObject();
426417

427418
try {
428-
if (_email != null) {
429-
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
430-
} else {
431-
requestJSON.put(IterableConstants.KEY_USER_ID, _userId);
432-
}
419+
addEmailOrUserIdToJson(requestJSON);
433420
requestJSON.put(IterableConstants.KEY_CAMPAIGNID, campaignId);
434421
requestJSON.put(IterableConstants.KEY_TEMPLATE_ID, templateId);
435422
requestJSON.put(IterableConstants.KEY_MESSAGE_ID, messageId);
@@ -448,12 +435,7 @@ protected void trackPushOpen(int campaignId, int templateId, String messageId) {
448435
protected void disablePush(String token) {
449436
JSONObject requestJSON = new JSONObject();
450437
try {
451-
requestJSON.put(IterableConstants.KEY_TOKEN, token);
452-
if (_email != null) {
453-
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
454-
} else {
455-
requestJSON.put(IterableConstants.KEY_USER_ID, _userId);
456-
}
438+
requestJSON.put(IterableConstants.KEY_TOKEN, token);addEmailOrUserIdToJson(requestJSON);
457439
}
458440
catch (JSONException e) {
459441
e.printStackTrace();
@@ -495,11 +477,7 @@ private void registerDeviceToken(String applicationName, String token, JSONObjec
495477

496478
JSONObject requestJSON = new JSONObject();
497479
try {
498-
if (_email != null) {
499-
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
500-
} else {
501-
requestJSON.put(IterableConstants.KEY_USER_ID, _userId);
502-
}
480+
addEmailOrUserIdToJson(requestJSON);
503481

504482
if (dataFields == null) {
505483
dataFields = new JSONObject();
@@ -529,6 +507,18 @@ private void sendRequest(String resourcePath, JSONObject json) {
529507
new IterableRequest().execute(request);
530508
}
531509

510+
private void addEmailOrUserIdToJson(JSONObject requestJSON) {
511+
try {
512+
if (_email != null) {
513+
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
514+
} else {
515+
requestJSON.put(IterableConstants.KEY_USER_ID, _userId);
516+
}
517+
} catch (JSONException e) {
518+
e.printStackTrace();
519+
}
520+
}
521+
532522
//---------------------------------------------------------------------------------------
533523
//endregion
534524

0 commit comments

Comments
 (0)