Skip to content

Feat: custom odpEventManager and odpSegmentManager #447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
import com.optimizely.ab.notification.NotificationCenter;
import com.optimizely.ab.notification.UpdateConfigNotification;
import com.optimizely.ab.odp.ODPApiManager;
import com.optimizely.ab.odp.ODPEventManager;
import com.optimizely.ab.odp.ODPManager;
import com.optimizely.ab.odp.ODPSegmentManager;
import com.optimizely.ab.optimizelydecision.OptimizelyDecideOption;

import org.slf4j.Logger;
Expand Down Expand Up @@ -740,6 +742,8 @@ public static class Builder {
@Nullable private String sdkKey = null;
@Nullable private DatafileConfig datafileConfig = null;
@Nullable private List<OptimizelyDecideOption> defaultDecideOptions = null;
@Nullable private ODPEventManager odpEventManager;
@Nullable private ODPSegmentManager odpSegmentManager;

private int odpSegmentCacheSize = 100;
private int odpSegmentCacheTimeoutInSecs = 600;
Expand Down Expand Up @@ -947,6 +951,32 @@ public Builder withTimeoutForODPEventDispatch(int interval) {
return this;
}

/**
* Provide an optional custom {@link ODPEventManager} instance.
*
* A Default {@link ODPEventManager} implementation is automatically used if none provided.
*
* @param odpEventManager The implementation of {@link ODPEventManager}
* @return ODPManager builder
*/
public Builder withODPEventManager(ODPEventManager odpEventManager) {
this.odpEventManager = odpEventManager;
return this;
}

/**
* Provide an optional custom {@link ODPSegmentManager} instance.
*
* A Default {@link ODPSegmentManager} implementation is automatically used if none provided.
*
* @param odpSegmentManager The implementation of {@link ODPSegmentManager}
* @return this {@link Builder} instance
*/
public Builder withODPSegmentManager(ODPSegmentManager odpSegmentManager) {
this.odpSegmentManager = odpSegmentManager;
return this;
}

/**
* Disable ODP integration.
* @return this {@link Builder} instance
Expand Down Expand Up @@ -1059,6 +1089,8 @@ public OptimizelyManager build(Context context) {
.withSegmentCacheSize(odpSegmentCacheSize)
.withSegmentCacheTimeout(odpSegmentCacheTimeoutInSecs)
.withUserCommonData(commonData)
.withSegmentManager(odpSegmentManager)
.withEventManager(odpEventManager)
.withUserCommonIdentifiers(commonIdentifiers)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,17 @@ public void setDatafile(String datafile) {
public ProjectConfig getConfig() {
return currentProjectConfig;
}

@Override
public ProjectConfig getCachedConfig() {
return currentProjectConfig;
}

@Override
public String getSDKKey() {
if (currentProjectConfig != null) {
return currentProjectConfig.getSdkKey();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class DefaultODPApiManager(private val context: Context, timeoutForSegmentFetch:
userKey: String,
userValue: String,
segmentsToCheck: Set<String>,
): String? {
): List<String>? {
return segmentClient.fetchQualifiedSegments(
apiKey,
apiEndpoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ package com.optimizely.ab.android.odp

import androidx.annotation.VisibleForTesting
import com.optimizely.ab.android.shared.Client
import com.optimizely.ab.odp.parser.ResponseJsonParser
import com.optimizely.ab.odp.parser.ResponseJsonParserFactory
import org.slf4j.Logger
import java.io.IOException
import java.net.HttpURLConnection
import java.net.URL

Expand All @@ -28,7 +31,7 @@ open class ODPSegmentClient(private val client: Client, private val logger: Logg
apiKey: String,
apiEndpoint: String,
payload: String
): String? {
): List<String>? {

val request: Client.Request<String> = Client.Request {
var urlConnection: HttpURLConnection? = null
Expand Down Expand Up @@ -74,8 +77,15 @@ open class ODPSegmentClient(private val client: Client, private val logger: Logg
}
}
}

return client.execute(request, REQUEST_BACKOFF_TIMEOUT, REQUEST_RETRIES_POWER)
val response = client.execute(request, REQUEST_BACKOFF_TIMEOUT, REQUEST_RETRIES_POWER)
val parser: ResponseJsonParser = ResponseJsonParserFactory.getParser()
try {
return parser.parseQualifiedSegments(response)
} catch (e: java.lang.Exception) {
logger.error("Audience segments fetch failed (Error Parsing Response)")
logger.debug(e.message)
}
return null
}

companion object {
Expand Down