Skip to content

Commit bcaebb4

Browse files
add new sdkKey cdn location and allow the datafileconfig to be passed… (#207)
* add new sdkKey cdn location and allow the datafileconfig to be passed in overriding host if need be * update to have only one url string
1 parent e171053 commit bcaebb4

File tree

5 files changed

+50
-18
lines changed

5 files changed

+50
-18
lines changed

android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/OptimizelyManagerTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import com.optimizely.ab.android.datafile_handler.DatafileService;
3434
import com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler;
3535
import com.optimizely.ab.android.event_handler.DefaultEventHandler;
36-
import com.optimizely.ab.android.sdk.test.R;
3736
import com.optimizely.ab.android.shared.DatafileConfig;
3837
import com.optimizely.ab.android.shared.ServiceScheduler;
3938
import com.optimizely.ab.android.user_profile.DefaultUserProfileService;
@@ -72,7 +71,7 @@
7271
public class OptimizelyManagerTest {
7372

7473
private String testProjectId = "7595190003";
75-
private String testSdkKey = "123-2232323-343423423-435345345";
74+
private String testSdkKey = "EQRZ12XAR22424";
7675
private ListeningExecutorService executor;
7776
private Logger logger;
7877
private OptimizelyManager optimizelyManager;
@@ -95,7 +94,7 @@ public void setup() {
9594
executor = MoreExecutors.newDirectExecutorService();
9695
DatafileHandler datafileHandler = mock(DefaultDatafileHandler.class);
9796
EventHandler eventHandler = mock(DefaultEventHandler.class);
98-
optimizelyManager = new OptimizelyManager(testProjectId, null,logger, 3600L, datafileHandler, null, 3600L,
97+
optimizelyManager = new OptimizelyManager(testProjectId, null, null, logger, 3600L, datafileHandler, null, 3600L,
9998
eventHandler, null);
10099
}
101100

@@ -146,7 +145,7 @@ public void initializeSyncWithEnvironment() {
146145
Logger logger = mock(Logger.class);
147146
DatafileHandler datafileHandler = mock(DefaultDatafileHandler.class);
148147
EventHandler eventHandler = mock(DefaultEventHandler.class);
149-
OptimizelyManager optimizelyManager = new OptimizelyManager(testProjectId, testSdkKey,logger, 3600L, datafileHandler, null, 3600L,
148+
OptimizelyManager optimizelyManager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, 3600L, datafileHandler, null, 3600L,
150149
eventHandler, null);
151150
/*
152151
* Scenario#1: when datafile is not Empty
@@ -156,7 +155,7 @@ public void initializeSyncWithEnvironment() {
156155

157156
assertEquals(optimizelyManager.isDatafileCached(InstrumentationRegistry.getTargetContext()), false);
158157

159-
assertEquals(optimizelyManager.getDatafileUrl(), "https://cdn.optimizely.com/json/123-2232323-343423423-435345345.json" );
158+
assertEquals(optimizelyManager.getDatafileUrl(), String.format((DatafileConfig.defaultHost + DatafileConfig.environmentUrlSuffix), testSdkKey));
160159

161160
assertNotNull(optimizelyManager.getOptimizely());
162161
assertNotNull(optimizelyManager.getDatafileHandler());
@@ -203,7 +202,7 @@ public void initializeAsyncWithEnvironment() {
203202
Logger logger = mock(Logger.class);
204203
DatafileHandler datafileHandler = mock(DefaultDatafileHandler.class);
205204
EventHandler eventHandler = mock(DefaultEventHandler.class);
206-
final OptimizelyManager optimizelyManager = new OptimizelyManager(testProjectId, testSdkKey,logger, 3600L, datafileHandler, null, 3600L,
205+
final OptimizelyManager optimizelyManager = new OptimizelyManager(testProjectId, testSdkKey, null, logger, 3600L, datafileHandler, null, 3600L,
207206
eventHandler, null);
208207

209208
/*
@@ -235,7 +234,7 @@ public void onStart(OptimizelyClient optimizely) {
235234

236235
assertEquals(optimizelyManager.isDatafileCached(InstrumentationRegistry.getTargetContext()), false);
237236

238-
assertEquals(optimizelyManager.getDatafileUrl(), "https://cdn.optimizely.com/json/123-2232323-343423423-435345345.json" );
237+
assertEquals(optimizelyManager.getDatafileUrl(), String.format((DatafileConfig.defaultHost + DatafileConfig.environmentUrlSuffix), testSdkKey) );
239238

240239

241240
}

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyManager.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public class OptimizelyManager {
7676

7777
OptimizelyManager(@Nullable String projectId,
7878
@Nullable String sdkKey,
79+
@Nullable DatafileConfig datafileConfig,
7980
@NonNull Logger logger,
8081
long datafileDownloadInterval,
8182
@NonNull DatafileHandler datafileHandler,
@@ -89,7 +90,12 @@ public class OptimizelyManager {
8990
}
9091
this.projectId = projectId;
9192
this.sdkKey = sdkKey;
92-
this.datafileConfig = new DatafileConfig(this.projectId, this.sdkKey);
93+
if (datafileConfig == null) {
94+
this.datafileConfig = new DatafileConfig(this.projectId, this.sdkKey);
95+
}
96+
else {
97+
this.datafileConfig = datafileConfig;
98+
}
9399
this.logger = logger;
94100
this.datafileDownloadInterval = datafileDownloadInterval;
95101
this.datafileHandler = datafileHandler;
@@ -605,6 +611,7 @@ public static class Builder {
605611
@Nullable private ErrorHandler errorHandler = null;
606612
@Nullable private UserProfileService userProfileService = null;
607613
@Nullable private String sdkKey = null;
614+
@Nullable private DatafileConfig datafileConfig = null;
608615

609616
@Deprecated
610617
Builder(@Nullable String projectId) {
@@ -698,6 +705,11 @@ public Builder withUserProfileService(UserProfileService userProfileService) {
698705
return this;
699706
}
700707

708+
public Builder withDatafileConfig(DatafileConfig datafileConfig) {
709+
this.datafileConfig = datafileConfig;
710+
return this;
711+
}
712+
701713
/**
702714
* Get a new {@link Builder} instance to create {@link OptimizelyManager} with.
703715
* @param context the application context used to create default service if not provided.
@@ -752,6 +764,7 @@ public OptimizelyManager build(Context context) {
752764
}
753765

754766
return new OptimizelyManager(projectId, sdkKey,
767+
datafileConfig,
755768
logger,
756769
datafileDownloadInterval,
757770
datafileHandler,

datafile-handler/src/androidTest/java/com/optimizely/ab/android/datafile_handler/DatafileServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,6 @@ public void testGetDatafileEnvironmentUrl(){
220220
// and url by accidentally changing those constants.
221221
// us to update this test.
222222
String datafileUrl = new DatafileConfig("1", "2").getUrl();
223-
assertEquals("https://cdn.optimizely.com/json/2.json", datafileUrl);
223+
assertEquals("https://cdn.optimizely.com/datafiles/2.json", datafileUrl);
224224
}
225225
}

shared/src/main/java/com/optimizely/ab/android/shared/DatafileConfig.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,45 @@
2828
* if you change environments or projects and no longer want updates from the previous.
2929
*/
3030
public class DatafileConfig {
31-
public static String projectUrl = "https://cdn.optimizely.com/json/%s.json";
32-
public static String delimitor = "::::";
31+
public static String defaultHost = "https://cdn.optimizely.com";
32+
public static String projectUrlSuffix = "/json/%s.json";
33+
public static String environmentUrlSuffix = "/datafiles/%s.json";
34+
public static String delimiter = "::::";
3335

3436
private final String projectId;
3537
private final String sdkKey;
38+
private final String host;
39+
private final String datafileUrlString;
3640

3741
/**
3842
* Constructor used to construct a DatafileConfig to get cache key, url,
3943
* for the appropriate environment. One or the other can be null. But, not both.
4044
* @param projectId project id string.
4145
* @param sdkKey the environment url.
46+
* @param host used to override the DatafileConfig.defaultHost used for datafile synchronization.
4247
*/
43-
public DatafileConfig(String projectId, String sdkKey) {
48+
public DatafileConfig(String projectId, String sdkKey, String host) {
4449
assert(projectId != null || sdkKey != null);
4550
this.projectId = projectId;
4651
this.sdkKey = sdkKey;
52+
this.host = host;
53+
54+
if (sdkKey != null) {
55+
this.datafileUrlString = String.format((this.host + environmentUrlSuffix), sdkKey);
56+
}
57+
else {
58+
this.datafileUrlString = String.format((this.host + projectUrlSuffix), projectId);
59+
}
60+
}
61+
62+
/**
63+
* Constructor used to construct a DatafileConfig to get cache key, url,
64+
* for the appropriate environment. One or the other can be null. But, not both.
65+
* @param projectId project id string.
66+
* @param sdkKey the environment url.
67+
*/
68+
public DatafileConfig(String projectId, String sdkKey) {
69+
this(projectId, sdkKey, defaultHost);
4770
}
4871

4972
/**
@@ -60,7 +83,7 @@ public String getKey() {
6083
* @return url of current project configuration.
6184
*/
6285
public String getUrl() {
63-
return String.format(projectUrl, getKey());
86+
return datafileUrlString;
6487
}
6588

6689
public String toJSONString() {
@@ -101,7 +124,7 @@ public static DatafileConfig fromJSONString(String jsonString) {
101124
*/
102125
@Override
103126
public String toString() {
104-
return projectId != null ? projectId : "null" + delimitor + (sdkKey != null? sdkKey : "null");
127+
return projectId != null ? projectId : "null" + delimiter + (sdkKey != null? sdkKey : "null");
105128
}
106129

107130
public boolean equals(Object o) {

test-app/src/main/java/com/optimizely/ab/android/test_app/MyApplication.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ public void onCreate() {
7171
// This app is built against a real Optimizely project with real experiments set. Automated
7272
// espresso tests are run against this project id. Changing it will make the Optimizely
7373
// must match the project id of the compiled in Optimizely data file in rest/raw/data_file.json.
74-
OptimizelyManager.Builder builder = OptimizelyManager.builder();
75-
if (BuildConfig.DEBUG) {
76-
builder = builder.withSDKKey(PROJECT_ID);
77-
}
74+
OptimizelyManager.Builder builder = OptimizelyManager.builder(PROJECT_ID);
7875
optimizelyManager = builder.withEventDispatchInterval(60L * 10L)
7976
.withDatafileDownloadInterval(60L * 10L)
8077
.build(getApplicationContext());

0 commit comments

Comments
 (0)