Skip to content

Commit 0eadcb0

Browse files
update to have sdk key use a different cdn path in the datafile config (#280)
* update to have sdk key use a different cdn path in the datafile config * update per comments and move valid key string test to datafile config * rename local variables and getters to avoid confusion. going to cdn
1 parent f5d7805 commit 0eadcb0

File tree

9 files changed

+64
-27
lines changed

9 files changed

+64
-27
lines changed

OptimizelyDemoApp/AppDelegate.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
7373
// ---- Create the Datafile Manager ----
7474
let datafileManager = OPTLYDatafileManagerDefault.init(builder: OPTLYDatafileManagerBuilder.init(block: { (builder) in
7575
// builder!.datafileFetchInterval = TimeInterval(self.datafileManagerDownloadInterval)
76-
builder!.datafileConfig = OPTLYDatafileConfig(projectId: nil, withSDKKey:self.projectId)!;
77-
}))
76+
builder!.datafileConfig = OPTLYDatafileConfig(projectId: self.projectId, withSDKKey:nil)!;
77+
}
7878

7979
// ---- Create the Manager ----
8080
let optimizelyManager = OPTLYManager.init(builder: OPTLYManagerBuilder.init(block: { (builder) in

OptimizelySDKCore/OptimizelySDKCoreTests/OPTLYNetworkServiceTest.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ - (void)testDownloadProjectConfigRequestRetrievesProperDatafileVersion {
6464
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"testDownloadProjectConfigRequestRetrievesProperDatafileVersion"];
6565
[self stub200Response];
6666

67-
NSString *filePath = [NSString stringWithFormat:OPTLY_DATAFILE_URL, kProjectId];
67+
NSString *cdnPath = [OPTLYDatafileConfig defaultProjectIdCdnPath:kProjectId];
6868

69-
[self.network downloadProjectConfig:[NSURL URLWithString:filePath]
69+
[self.network downloadProjectConfig:[NSURL URLWithString:cdnPath]
7070
backoffRetry:NO
7171
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
7272
NSDictionary *datafile = [NSJSONSerialization JSONObjectWithData:data
@@ -83,9 +83,9 @@ - (void)testDownloadProjectConfigRequestRetrievesProperDatafileVersion {
8383
- (void)testDownloadProjectConfigWithLastModifiedRequestRetrievesProperDatafileVersion {
8484
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"testDownloadProjectConfigWithLastModifiedRequestRetrievesProperDatafileVersion"];
8585
[self stub200Response];
86-
NSString *filePath = [NSString stringWithFormat:OPTLY_DATAFILE_URL, kProjectId];
86+
NSString *cdnPath = [OPTLYDatafileConfig defaultProjectIdCdnPath:kProjectId];
8787

88-
[self.network downloadProjectConfig:[NSURL URLWithString:filePath]
88+
[self.network downloadProjectConfig:[NSURL URLWithString:cdnPath]
8989
backoffRetry:NO
9090
lastModified:kLastModifiedDate
9191
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
@@ -101,7 +101,7 @@ - (void)testDownloadProjectConfigWithLastModifiedRequestRetrievesProperDatafileV
101101

102102
# pragma mark - Helper Methods
103103
- (id<OHHTTPStubsDescriptor>)stub200Response {
104-
NSString *filePath = [NSString stringWithFormat:OPTLY_DATAFILE_URL, kProjectId];
104+
NSString *filePath = [OPTLYDatafileConfig defaultProjectIdCdnPath:kProjectId];
105105

106106
NSURL *hostURL = [NSURL URLWithString:filePath];
107107
NSString *hostName = [hostURL host];

OptimizelySDKShared/OptimizelySDKShared/OPTLYDatafileConfig.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,22 @@
1515
***************************************************************************/
1616
#import <Foundation/Foundation.h>
1717

18-
extern NSString * const OPTLY_DATAFILE_URL;
18+
extern NSString * const DEFAULT_HOST;
19+
extern NSString * const OPTLY_PROJECTID_SUFFIX;
20+
extern NSString * const OPTLY_ENVIRONMENTS_SUFFIX;
1921

2022
@interface OPTLYDatafileConfig : NSObject
2123
- (nullable id)initWithProjectId:(NSString *)projectId withSDKKey:(NSString *)sdkKey;
2224
- (NSURL *) URLForKey;
2325
- (NSString *) key;
2426
@end
27+
28+
@interface OPTLYDatafileConfig(OPTLYHelpers)
29+
+ (NSString *)defaultProjectIdCdnPath:(NSString *)projectId;
30+
+ (NSString *)defaultSdkKeyCdnPath:(NSString *)sdkKey;
31+
/*
32+
* Test if string s can be an Optimizely SDK key string.
33+
*/
34+
+ (BOOL)isValidKeyString:(NSString*)s;
35+
36+
@end

OptimizelySDKShared/OptimizelySDKShared/OPTLYDatafileConfig.m

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,67 @@
1717
#import "OPTLYDatafileConfig.h"
1818
#import "OPTLYManagerBase.h"
1919

20-
NSString * const OPTLY_DATAFILE_URL = @"https://cdn.optimizely.com/json/%@.json";
20+
NSString * const DEFAULT_HOST = @"https://cdn.optimizely.com";
21+
NSString * const OPTLY_PROJECTID_SUFFIX = @"/json/%@.json";
22+
NSString * const OPTLY_ENVIRONMENTS_SUFFIX = @"/datafiles/%@.json";
2123

2224
@interface OPTLYDatafileConfig()
25+
- (nullable id)initWithProjectId:(NSString *)projectId withSDKKey:(NSString *)sdkKey withHost:(NSString *)host;
2326
@property(nonatomic, strong, nullable) NSString* projectId;
2427
@property(nonatomic, strong, nullable) NSString* sdkKey;
28+
@property(nonatomic, strong) NSString* host;
29+
@property(nonatomic, strong) NSURL* datafileUrl;
30+
@end
31+
32+
@implementation OPTLYDatafileConfig(OPTLYHelpers)
33+
+ (NSString *)defaultProjectIdCdnPath:(NSString *)projectId {
34+
NSString *datafileFormat = [DEFAULT_HOST stringByAppendingString:OPTLY_PROJECTID_SUFFIX];
35+
return [NSString stringWithFormat:datafileFormat, projectId];
36+
}
37+
+ (NSString *)defaultSdkKeyCdnPath:(NSString *)sdkKey {
38+
NSString *datafileFormat = [DEFAULT_HOST stringByAppendingString:OPTLY_ENVIRONMENTS_SUFFIX];
39+
return [NSString stringWithFormat:datafileFormat, sdkKey];
40+
}
41+
+ (BOOL)isValidKeyString:(NSString*)s {
42+
return ((s != nil) && ![s isEqualToString:@""] && ![s containsString:@" "]);
43+
}
2544
@end
2645

2746
@implementation OPTLYDatafileConfig
2847

29-
- (instancetype)initWithProjectId:(NSString *)projectId withSDKKey:(NSString *)sdkKey {
30-
if (![OPTLYManagerBase isValidKeyString:projectId] && ![OPTLYManagerBase isValidKeyString:sdkKey]) {
48+
- (instancetype)initWithProjectId:(NSString *)projectId withSDKKey:(NSString *)sdkKey withHost:(NSString *)host {
49+
self.host = host;
50+
51+
if (![OPTLYDatafileConfig isValidKeyString:projectId] && ![OPTLYDatafileConfig isValidKeyString:sdkKey]) {
3152
// One of projectId and sdkKey needs to be a valid key string.
3253
return nil;
3354
}
3455
if (self = [super init]) {
3556
self.projectId = projectId;
3657
self.sdkKey = sdkKey;
58+
if (self.sdkKey != nil) {
59+
NSString *datafileFormat = [self.host stringByAppendingString:OPTLY_ENVIRONMENTS_SUFFIX];
60+
NSString *datafile = [NSString stringWithFormat:datafileFormat, self.sdkKey];
61+
self.datafileUrl = [NSURL URLWithString:datafile];
62+
}
63+
else {
64+
NSString *datafileFormat = [self.host stringByAppendingString:OPTLY_PROJECTID_SUFFIX];
65+
NSString *datafile = [NSString stringWithFormat:datafileFormat, self.projectId];
66+
self.datafileUrl = [NSURL URLWithString:datafile];
67+
}
3768
}
3869
return self;
3970
}
71+
72+
- (instancetype)initWithProjectId:(NSString *)projectId withSDKKey:(NSString *)sdkKey {
73+
return [self initWithProjectId:projectId withSDKKey:sdkKey withHost:DEFAULT_HOST];
74+
}
4075
- (NSString*)key {
4176
return (_sdkKey != nil) ? _sdkKey : _projectId;
4277
}
4378

4479
- (NSURL *)URLForKey {
45-
NSString *filePath = [NSString stringWithFormat:OPTLY_DATAFILE_URL, [self key]];
46-
return [NSURL URLWithString:filePath];
80+
return _datafileUrl;
4781
}
4882

4983
+ (BOOL)areNilOrEqual:(NSString*)x y:(NSString*)y {

OptimizelySDKShared/OptimizelySDKShared/OPTLYManagerBase.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,4 @@ typedef void (^OPTLYManagerBuilderBlock)(OPTLYManagerBuilder * _Nullable builder
121121
* Gets the cached Optimizely client.
122122
*/
123123
- (nullable OPTLYClient *)getOptimizely;
124-
125-
/*
126-
* Test if string s can be an Optimizely SDK key string.
127-
*/
128-
+ (BOOL)isValidKeyString:(NSString*)s;
129124
@end

OptimizelySDKShared/OptimizelySDKShared/OPTLYManagerBase.m

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,4 @@ - (OPTLYClient *)initializeClientWithManagerSettingsAndDatafile:(NSData *)datafi
181181
- (NSString *)description {
182182
return [NSString stringWithFormat:@"projectId: %@ \nclientEngine: %@\nclientVersion: %@\ndatafile:%@\nlogger:%@\nerrorHandler:%@\ndatafileManager:%@\neventDispatcher:%@\nuserProfile:%@", self.projectId, self.clientEngine, self.clientVersion, self.datafile, self.logger, self.errorHandler, self.datafileManager, self.eventDispatcher, self.userProfileService];
183183
}
184-
185-
+ (BOOL)isValidKeyString:(NSString*)s {
186-
return ((s != nil) && ![s isEqualToString:@""] && ![s containsString:@" "]);
187-
}
188184
@end

OptimizelySDKShared/OptimizelySDKShared/OPTLYManagerBuilder.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ - (id)initWithBlock:(OPTLYManagerBuilderBlock)block {
8080
}
8181

8282
// check the project id
83-
if (![OPTLYManagerBase isValidKeyString:_projectId] && ![OPTLYManagerBase isValidKeyString:_sdkKey]) {
83+
if (![OPTLYDatafileConfig isValidKeyString:_projectId] && ![OPTLYDatafileConfig isValidKeyString:_sdkKey]) {
8484
[_logger logMessage:OPTLYLoggerMessagesManagerMustBeInitializedWithProjectId
8585
withLevel:OptimizelyLogLevelError];
8686
return nil;

OptimizelySDKShared/OptimizelySDKSharedTests/OPTLYDatafileManagerTest.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ @interface OPTLYDatafileManagerTest : XCTestCase
2828
@implementation OPTLYDatafileManagerTest
2929

3030
- (void)testProjectConfigURLPathReturnsExpectedURL {
31-
NSString *expectedURLString = [NSString stringWithFormat:OPTLY_DATAFILE_URL, kProjectId];
31+
NSString *expectedURLString = [OPTLYDatafileConfig defaultProjectIdCdnPath:kProjectId];
3232
NSURL *expectedURL = [NSURL URLWithString:expectedURLString];
3333

3434
NSURL *cdnURL = [[[OPTLYDatafileConfig alloc] initWithProjectId:kProjectId withSDKKey:nil] URLForKey];

OptimizelySDKShared/OptimizelySDKSharedTests/OPTLYManagerTest.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,10 @@ - (void)testInitializeWithCallbackDownloadErrorNoDatafile
417417
#pragma mark - isValidKeyString
418418

419419
-(void)testIsValidKeyString {
420-
XCTAssertTrue([OPTLYManagerBase isValidKeyString:@"12121212" ]);
420+
XCTAssertTrue([OPTLYDatafileConfig isValidKeyString:@"12121212" ]);
421421
NSString *strTest = nil;
422-
XCTAssertFalse([OPTLYManagerBase isValidKeyString:strTest]);
423-
XCTAssertFalse([OPTLYManagerBase isValidKeyString:@""]);
422+
XCTAssertFalse([OPTLYDatafileConfig isValidKeyString:strTest]);
423+
XCTAssertFalse([OPTLYDatafileConfig isValidKeyString:@""]);
424424
}
425425

426426
# pragma mark - Helper Methods

0 commit comments

Comments
 (0)