Skip to content

Commit 6c3d25d

Browse files
feat: Remove the existing code and added new method 'getUserAudience' (#542)
1 parent 12eef85 commit 6c3d25d

File tree

26 files changed

+501
-260
lines changed

26 files changed

+501
-260
lines changed

android-core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ dependencies {
159159
testImplementation project(':testutils')
160160
testImplementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
161161
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
162+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_version"
162163
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$kotlin_version"
163164

164165
androidTestImplementation project(':testutils')

android-core/src/androidTest/kotlin/com.mparticle/networking/MParticleBaseClientImplTest.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() {
2424
startMParticle(MParticleOptions.builder(mContext).credentials(apiKey, "secret"))
2525
val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl
2626
for (endpoint in MParticleBaseClientImpl.Endpoint.values()) {
27-
defaultUrls[endpoint] = baseClientImpl.getUrl(endpoint, endpoint.name, UploadSettings(apiKey, "secret", NetworkOptions.builder().build(), "", ""))
27+
defaultUrls[endpoint] =
28+
baseClientImpl.getUrl(endpoint, endpoint.name, null, UploadSettings(apiKey, "secret", NetworkOptions.builder().build(), "", ""))
2829
}
2930
MParticle.setInstance(null)
3031
}
@@ -219,11 +220,11 @@ class MParticleBaseClientImplTest : BaseCleanInstallEachTest() {
219220
val uploadSettings = UploadSettings(apiKey, "secret", options.networkOptions, "", "")
220221
val baseClientImpl = AccessUtils.getApiClient() as MParticleBaseClientImpl
221222
for (endpoint in MParticleBaseClientImpl.Endpoint.values()) {
222-
val generatedUrl = baseClientImpl.getUrl(endpoint, endpoint.name, uploadSettings)
223+
val generatedUrl = baseClientImpl.getUrl(endpoint, endpoint.name, null, uploadSettings)
223224
assertEquals(defaultUrls[endpoint].toString(), generatedUrl.defaultUrl.toString())
224225
}
225226
for (endpoint in MParticleBaseClientImpl.Endpoint.values()) {
226-
val generatedUrl = baseClientImpl.getUrl(endpoint, endpoint.name, uploadSettings)
227+
val generatedUrl = baseClientImpl.getUrl(endpoint, endpoint.name, null, uploadSettings)
227228
Assert.assertNotEquals(defaultUrls[endpoint].toString(), generatedUrl.toString())
228229
Assert.assertFalse(generatedUrl === generatedUrl.defaultUrl)
229230
assertEquals(

android-core/src/androidTest/kotlin/com.mparticle/networking/NetworkOptionsTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class NetworkOptionsTest : BaseCleanInstallEachTest() {
2323
startMParticle(MParticleOptions.builder(mContext).credentials(apiKey, "secret"))
2424
setClients()
2525
for (endpoint in MParticleBaseClientImpl.Endpoint.values()) {
26-
defaultUrls[endpoint] = mpClient.getUrl(endpoint, endpoint.name, UploadSettings(apiKey, "secret", NetworkOptions.builder().build(), "", ""))
26+
defaultUrls[endpoint] =
27+
mpClient.getUrl(endpoint, endpoint.name, null, UploadSettings(apiKey, "secret", NetworkOptions.builder().build(), "", ""))
2728
}
2829
MParticle.setInstance(null)
2930
}

android-core/src/main/java/com/mparticle/MParticle.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -960,11 +960,6 @@ public int getSessionTimeout() {
960960
return mConfigManager.getSessionTimeout() / 1000;
961961
}
962962

963-
public void getUserSegments(long timeout, @NonNull String endpointId, @NonNull SegmentListener listener) {
964-
if (mMessageManager != null && mMessageManager.mUploadHandler != null) {
965-
mMessageManager.mUploadHandler.fetchSegments(timeout, endpointId, listener);
966-
}
967-
}
968963

969964
/**
970965
* Instrument a WebView so that communication can be facilitated between this Native SDK instance and any

android-core/src/main/java/com/mparticle/identity/MParticleUser.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import com.mparticle.MParticle;
88
import com.mparticle.UserAttributeListenerType;
9+
import com.mparticle.audience.AudienceResponse;
10+
import com.mparticle.audience.AudienceTask;
911
import com.mparticle.consent.ConsentState;
1012

1113
import java.util.Map;
@@ -129,4 +131,10 @@ public interface MParticleUser {
129131
*/
130132
long getLastSeenTime();
131133

134+
/**
135+
* Get a list of the audiences that this given MPID is currently within.
136+
*
137+
* @return an AudienceTask<AudienceResponse> which provides an asynchronous result
138+
*/
139+
AudienceTask<AudienceResponse> getUserAudiences();
132140
}

android-core/src/main/java/com/mparticle/identity/MParticleUserDelegate.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
import com.mparticle.MParticle;
77
import com.mparticle.UserAttributeListenerType;
8+
import com.mparticle.audience.AudienceResponse;
9+
import com.mparticle.audience.AudienceTask;
10+
import com.mparticle.audience.BaseAudienceTask;
811
import com.mparticle.consent.ConsentState;
912
import com.mparticle.internal.AppStateManager;
1013
import com.mparticle.internal.ConfigManager;
@@ -15,7 +18,6 @@
1518
import com.mparticle.internal.Logger;
1619
import com.mparticle.internal.MPUtility;
1720
import com.mparticle.internal.MessageManager;
18-
import com.mparticle.segmentation.SegmentListener;
1921

2022
import org.json.JSONArray;
2123
import org.json.JSONException;
@@ -225,11 +227,6 @@ public boolean removeUserAttribute(String key, long userMpId) {
225227
return true;
226228
}
227229

228-
public void getSegments(long timeout, String endpointId, SegmentListener listener) {
229-
if (mMessageManager != null && mMessageManager.mUploadHandler != null) {
230-
mMessageManager.mUploadHandler.fetchSegments(timeout, endpointId, listener);
231-
}
232-
}
233230

234231
static void setUserIdentities(MParticleUserDelegate userDelegate, Map<MParticle.IdentityType, String> identities, long mpid) {
235232
if (identities != null) {
@@ -303,4 +300,15 @@ public long getLastSeenTime(Long mpid) {
303300
return mConfigManager.getUserStorage(mpid).getLastSeenTime();
304301
}
305302
}
303+
304+
public AudienceTask<AudienceResponse> getUserAudiences(long mpId) {
305+
if (mMessageManager != null && mMessageManager.mUploadHandler != null) {
306+
return mMessageManager.mUploadHandler.fetchUserAudiences(mpId);
307+
} else {
308+
BaseAudienceTask task = new BaseAudienceTask();
309+
task.setFailed(new AudienceResponse(IdentityApi.UNKNOWN_ERROR,
310+
"Error while fetching user audiences"));
311+
return task;
312+
}
313+
}
306314
}

android-core/src/main/java/com/mparticle/identity/MParticleUserImpl.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
import com.mparticle.MParticle;
99
import com.mparticle.UserAttributeListenerType;
1010
import com.mparticle.consent.ConsentState;
11+
import com.mparticle.audience.AudienceResponse;
12+
import com.mparticle.audience.AudienceTask;
1113
import com.mparticle.internal.listeners.ApiClass;
12-
import com.mparticle.segmentation.SegmentListener;
1314

1415
import java.util.Map;
1516

@@ -111,9 +112,6 @@ public boolean setUserTag(@NonNull String tag) {
111112
return setUserAttribute(tag, null);
112113
}
113114

114-
public void getSegments(long timeout, String endpointId, SegmentListener listener) {
115-
mUserDelegate.getSegments(timeout, endpointId, listener);
116-
}
117115

118116
MParticleUser setUserDelegate(MParticleUserDelegate mParticleUserDelegate) {
119117
mUserDelegate = mParticleUserDelegate;
@@ -145,4 +143,9 @@ public long getLastSeenTime() {
145143
return mUserDelegate.getLastSeenTime(getId());
146144
}
147145

146+
@Override
147+
public AudienceTask<AudienceResponse> getUserAudiences() {
148+
return mUserDelegate.getUserAudiences(getId());
149+
}
150+
148151
}

android-core/src/main/java/com/mparticle/internal/ConfigManager.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public class ConfigManager {
5757
public static final String KEY_EMBEDDED_KITS = "eks";
5858
static final String KEY_UPLOAD_INTERVAL = "uitl";
5959
static final String KEY_SESSION_TIMEOUT = "stl";
60+
public static final String KEY_FLAGS = "flags";
61+
public static final String KEY_AUDIENCE_API = "AudienceAPI";
6062
public static final String VALUE_APP_DEFINED = "appdefined";
6163
public static final String VALUE_CUE_CATCH = "forcecatch";
6264
public static final String PREFERENCES_FILE = "mp_preferences";
@@ -88,6 +90,7 @@ public class ConfigManager {
8890
private boolean directUrlRouting = false;
8991
private UserStorage mUserStorage;
9092
private String mLogUnhandledExceptions = VALUE_APP_DEFINED;
93+
private boolean audienceAPIFlag = false;
9194

9295
private boolean mSendOoEvents;
9396
private JSONObject mProviderPersistence;
@@ -413,6 +416,13 @@ private synchronized void updateCoreConfig(JSONObject responseJSON, boolean newC
413416
editor.putBoolean(KEY_DIRECT_URL_ROUTING, directUrlRouting);
414417
}
415418

419+
if (responseJSON.has(KEY_FLAGS)) {
420+
JSONObject items = responseJSON.getJSONObject(KEY_FLAGS);
421+
if (items.has(KEY_AUDIENCE_API)) {
422+
audienceAPIFlag = items.getBoolean(KEY_AUDIENCE_API);
423+
}
424+
}
425+
416426
mRampValue = responseJSON.optInt(KEY_RAMP, -1);
417427

418428
if (responseJSON.has(KEY_OPT_OUT)) {
@@ -1303,6 +1313,10 @@ private void triggerMpidChangeListenerCallbacks(long mpid, long previousMpid) {
13031313
}
13041314
}
13051315

1316+
public boolean isAudienceFeatureFlagEnabled() {
1317+
return audienceAPIFlag;
1318+
}
1319+
13061320
public ConsentState getConsentState(long mpid) {
13071321
String serializedConsent = getUserStorage(mpid).getSerializedConsentState();
13081322
return ConsentState.withConsentState(serializedConsent).build();

android-core/src/main/java/com/mparticle/internal/MParticleApiClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import androidx.annotation.NonNull;
44
import androidx.annotation.Nullable;
55

6+
import com.mparticle.audience.BaseAudienceTask;
67
import com.mparticle.internal.database.UploadSettings;
78
import com.mparticle.networking.MParticleBaseClient;
89

@@ -21,7 +22,7 @@ public interface MParticleApiClient extends MParticleBaseClient {
2122

2223
int sendMessageBatch(@NonNull String message, @NonNull UploadSettings uploadSettings) throws IOException, MParticleApiClientImpl.MPThrottleException, MParticleApiClientImpl.MPRampException;
2324

24-
JSONObject fetchAudiences();
25+
void fetchUserAudience(BaseAudienceTask task,long mpId);
2526

2627
JSONObject getCookies();
2728

android-core/src/main/java/com/mparticle/internal/MParticleApiClientImpl.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
import com.mparticle.MParticle;
99
import com.mparticle.SdkListener;
10+
import com.mparticle.audience.AudienceResponse;
11+
import com.mparticle.audience.BaseAudienceTask;
12+
import com.mparticle.identity.IdentityApi;
1013
import com.mparticle.internal.database.UploadSettings;
1114
import com.mparticle.internal.listeners.InternalListenerManager;
1215
import com.mparticle.networking.MPConnection;
@@ -30,6 +33,7 @@
3033
import java.util.Calendar;
3134
import java.util.Date;
3235
import java.util.Iterator;
36+
import java.util.Objects;
3337
import java.util.Set;
3438

3539
/**
@@ -198,34 +202,35 @@ public void fetchConfig(boolean force) throws IOException, MPConfigException {
198202
}
199203
}
200204

201-
public JSONObject fetchAudiences() {
202-
203-
JSONObject response = null;
205+
public void fetchUserAudience(BaseAudienceTask task, long mpId) {
206+
JSONObject jsonResponse = null;
204207
try {
205-
Logger.debug("Starting Segment Network request");
206-
MPConnection connection = getUrl(Endpoint.AUDIENCE).openConnection();
207-
connection.setConnectTimeout(mConfigManager.getConnectionTimeout());
208-
connection.setReadTimeout(mConfigManager.getConnectionTimeout());
208+
MPConnection connection = getUrl(Endpoint.AUDIENCE,mpId).openConnection();
209+
Logger.verbose("Audience API request: \n" + connection.getURL().toString());
209210
connection.setRequestProperty("User-Agent", mUserAgent);
210-
211211
addMessageSignature(connection, null);
212+
connection.setRequestProperty("x-mp-key", mApiKey);
212213
makeUrlRequest(Endpoint.AUDIENCE, connection, true);
213214
if (connection.getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN) {
214-
Logger.error("Segment call forbidden: is Segmentation enabled for your account?");
215+
Logger.error("Your workspace is not enabled to retrieve user audiences");
216+
}
217+
jsonResponse = MPUtility.getJsonResponse(connection);
218+
Logger.verbose("Audience API response: \n Status code: " +connection.getResponseCode()+ " JSON response: "+ jsonResponse);
219+
if (jsonResponse != null && connection.getResponseCode() == 200) {
220+
task.setSuccessful(new AudienceResponse(connection.getResponseCode(), jsonResponse));
221+
} else {
222+
task.setFailed(new AudienceResponse(connection.getResponseCode(), "mParticle Audience API failed."));
215223
}
216-
response = MPUtility.getJsonResponse(connection);
217-
parseCookies(response);
218-
219224
} catch (Exception e) {
220-
Logger.error("Segment call failed: " + e.getMessage());
225+
Logger.error("mParticle Audience API failed. " + e);
226+
task.setFailed(new AudienceResponse(IdentityApi.UNKNOWN_ERROR, Objects.requireNonNull(e.getMessage())));
221227
}
222-
return response;
223228
}
224229

225230
public int sendMessageBatch(@NonNull String message, @NonNull UploadSettings uploadSettings) throws IOException, MPThrottleException, MPRampException {
226231
checkThrottleTime(Endpoint.EVENTS);
227232
checkRampValue();
228-
MPUrl eventUrl = getUrl(Endpoint.EVENTS, null, uploadSettings);
233+
MPUrl eventUrl = getUrl(Endpoint.EVENTS, null,null, uploadSettings);
229234
MPConnection connection = eventUrl.openConnection();
230235
connection.setConnectTimeout(mConfigManager.getConnectionTimeout());
231236
connection.setReadTimeout(mConfigManager.getConnectionTimeout());
@@ -289,7 +294,7 @@ public AliasNetworkResponse sendAliasRequest(@NonNull String message, @NonNull U
289294
checkThrottleTime(Endpoint.ALIAS);
290295
Logger.verbose("Identity alias request:\n" + message);
291296

292-
MPUrl aliasUrl = getUrl(Endpoint.ALIAS, null, uploadSettings);
297+
MPUrl aliasUrl = getUrl(Endpoint.ALIAS, null, null, uploadSettings);
293298
MPConnection connection = aliasUrl.openConnection();
294299
connection.setConnectTimeout(mConfigManager.getConnectionTimeout());
295300
connection.setReadTimeout(mConfigManager.getConnectionTimeout());

0 commit comments

Comments
 (0)