Skip to content

Commit 5407ab3

Browse files
mikeproeng37kellyroach-optimizely
authored andcommitted
Add environments support to SDK with SDK key initialization (#264)
* iOS Environment refactor to use OPTLYDatafileConfig Test Plan: all tests pass on iOS and tvOS. Reviewers: ali, kelly.roach, thomas.zurkan Differential Revision: https://phabricator.optimizely.com/D19924 * Add unit test for initializing with SDK and fix a typo. * Undo disabling of MxP. * projectConfigURLPath: declaration removed * Improve initWithProjectId:withSDKKey: * Use BOOL values YES/NO * Add parentheses and formatting to improve code readability * Add areNilOrEqual:y: and refactor isEqual: for code clarity * Improved OPTLYDatafileConfig.m hash method * NSString+OPTLYCategory.m+.h dropped in favor of adding static method to OPTLYManagerBase.m+.h * Refer to 6 identical strings by SSOT OPTLY_DATAFILE_URL global
1 parent fcc6126 commit 5407ab3

File tree

29 files changed

+467
-299
lines changed

29 files changed

+467
-299
lines changed

OptimizelyDemoApp/AppDelegate.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
3636

3737
// customizable settings
3838
let datafileName = "demoTestDatafile" // default parameter for initializing Optimizely from saved datafile
39-
var projectId = "8182362857" // project name: X Mobile - Sample App
39+
var projectId = "10657931863" // project name: X Mobile - Sample App
4040
var experimentKey = "background_experiment"
4141
var eventKey = "sample_conversion"
4242
let attributes = ["sample_attribute_key":"sample_attribute_value"]
@@ -73,12 +73,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
7373
// ---- Create the Datafile Manager ----
7474
let datafileManager = OPTLYDatafileManagerDefault.init{(builder) in
7575
// builder!.datafileFetchInterval = TimeInterval(self.datafileManagerDownloadInterval)
76-
builder!.projectId = self.projectId
76+
builder!.datafileConfig = OPTLYDatafileConfig(projectId: nil, withSDKKey:self.projectId)!;
7777
}
7878

7979
// ---- Create the Manager ----
8080
let optimizelyManager = OPTLYManager.init {(builder) in
81-
builder!.projectId = self.projectId
81+
builder!.projectId = nil;
82+
builder!.sdkKey = self.projectId
8283
builder!.datafileManager = datafileManager!
8384
builder!.eventDispatcher = eventDispatcher
8485
}
@@ -120,6 +121,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
120121
})
121122
#endif
122123
let variation = optimizelyClient?.activate((self?.experimentKey)!, userId: (self?.userId)!, attributes: (self?.attributes))
124+
123125
self?.setRootViewController(optimizelyClient: optimizelyClient, bucketedVariation:variation)
124126
})
125127

OptimizelySDKCore/OptimizelySDKCore/OPTLYLoggerMessages.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@
167167
NSString *const OPTLYLoggerMessagesManagerErrorHandlerDoesNotConformToOPTLYErrorHandlerProtocol = @"[MANAGER] Error handler does not conform to the OPTLYErrorHandler protocol.";
168168
NSString *const OPTLYLoggerMessagesManagerEventDispatcherDoesNotConformToOPTLYEventDispatcherProtocol = @"[MANAGER] Event dispatcher does not conform to the OPTLYEventDispatcher protocol.";
169169
NSString *const OPTLYLoggerMessagesManagerLoggerDoesNotConformToOPTLYLoggerProtocol = @"[MANAGER] Logger does not conform to the OPTLYLogger protocol.";
170-
NSString *const OPTLYLoggerMessagesManagerMustBeInitializedWithProjectId = @"[MANAGER] An Optimizely Manager instance must be initialized with a project ID.";
170+
NSString *const OPTLYLoggerMessagesManagerMustBeInitializedWithProjectId = @"[MANAGER] An Optimizely Manager instance must be initialized with a project ID or SDK Key.";
171171
NSString *const OPTLYLoggerMessagesManagerProjectIdCannotBeEmptyString = @"[MANAGER] The project ID for the Optimizely Manager instance cannot be an empty string";
172-
NSString *const OPTLYLoggerMessagesManagerInit = @"[MANAGER] Initializing client for project %@.";
173-
NSString *const OPTLYLoggerMessagesManagerInitWithCallback = @"[MANAGER] Initializing client with callback for project %@.";
172+
NSString *const OPTLYLoggerMessagesManagerInit = @"[MANAGER] Initializing client for projectId %@ with SDK Key %@.";
173+
NSString *const OPTLYLoggerMessagesManagerInitWithCallback = @"[MANAGER] Initializing client with callback for projectId %@ with SDK Key %@.";
174174
NSString *const OPTLYLoggerMessagesManagerInitWithCallbackErrorDatafileDownload = @"[MANAGER] Error downloading datafile: %@.";
175175
NSString *const OPTLYLoggerMessagesManagerInitWithCallbackNoDatafileUpdates = @"[MANAGER] Not downloading new datafile — no updates have been made.";
176176
NSString *const OPTLYLoggerMessagesManagerBundledDataLoaded = @"[MANAGER] The bundled datafile was loaded.";

OptimizelySDKCore/OptimizelySDKCore/OPTLYNetworkService.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2016-2017, Optimizely, Inc. and contributors *
2+
* Copyright 2016-2018, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -30,24 +30,24 @@ NS_ASSUME_NONNULL_END
3030
/**
3131
* Download the project config file from remote server
3232
*
33-
* @param projectId The project ID of the datafile to download
33+
* @param projectUrl The project URL of the datafile to download
3434
* @param backoffRetry Indicates if the exponential backoff retry should be enabled
3535
* @param completion The completion block of type OPTLYHTTPRequestManagerResponse
3636
*/
37-
- (void)downloadProjectConfig:(nonnull NSString *)projectId
37+
- (void)downloadProjectConfig:(nonnull NSURL *)projectUrl
3838
backoffRetry:(BOOL)backoffRetry
3939
completionHandler:(nullable OPTLYHTTPRequestManagerResponse)completion;
4040

4141
/**
4242
* Download the project config file from remote server only if it
4343
* has been modified.
4444
*
45-
* @param projectId The project ID of the exponential the datafile to download
45+
* @param projectURL The project URL of the datafile to download
4646
* @param backoffRetry Indicates if backoff retry should be enabled
4747
* @param lastModifiedDate The date the datafile was last modified
4848
* @param completion The completion block of type OPTLYHTTPRequestManagerResponse
4949
*/
50-
- (void)downloadProjectConfig:(nonnull NSString *)projectId
50+
- (void)downloadProjectConfig:(nonnull NSURL *)projectURL
5151
backoffRetry:(BOOL)backoffRetry
5252
lastModified:(nonnull NSString *)lastModifiedDate
5353
completionHandler:(nullable OPTLYHTTPRequestManagerResponse)completion;
@@ -64,10 +64,4 @@ NS_ASSUME_NONNULL_END
6464
toURL:(nonnull NSURL *)url
6565
completionHandler:(nullable OPTLYHTTPRequestManagerResponse)completion;
6666

67-
/**
68-
* Returns the URL path for the datafile of a particular project.
69-
* @param projectId The project ID of the datafile whose URL path we are looking for.
70-
*/
71-
+ (NSURL * _Nonnull)projectConfigURLPath:(nonnull NSString *)projectId;
72-
7367
@end

OptimizelySDKCore/OptimizelySDKCore/OPTLYNetworkService.m

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
// ---- Datafile Download URLs ----
2121
// TODO: Move this to the Datafile manager and parameterize the URL for the datafile download
22-
NSString * const OPTLYNetworkServiceCDNServerURL = @"https://cdn.optimizely.com/json/";
2322
NSString * const OPTLYNetworkServiceS3ServerURL = @"https://optimizely.s3.amazonaws.com/";
2423

2524
// ---- The total backoff and retry interval is: pow(2, attempts) * interval ----
@@ -44,38 +43,35 @@ - (instancetype) init
4443
return self;
4544
}
4645

47-
- (void)downloadProjectConfig:(nonnull NSString *)projectId
46+
- (void)downloadProjectConfig:(nonnull NSURL *)datafileConfigURL
4847
backoffRetry:(BOOL)backoffRetry
4948
lastModified:(nonnull NSString *)lastModifiedDate
5049
completionHandler:(nullable OPTLYHTTPRequestManagerResponse)completion
5150
{
52-
NSURL *cdnConfigFilePathURL = [OPTLYNetworkService projectConfigURLPath:projectId];
5351
if (backoffRetry) {
5452
[self.requestManager GETIfModifiedSince:lastModifiedDate
55-
url:cdnConfigFilePathURL
53+
url:datafileConfigURL
5654
backoffRetryInterval:OPTLYNetworkServiceDatafileDownloadMaxBackoffRetryTimeInterval_ms
5755
retries:OPTLYNetworkServiceDatafileDownloadMaxBackoffRetryAttempts
5856
completionHandler:completion];
5957
} else {
6058
[self.requestManager GETIfModifiedSince:lastModifiedDate
61-
url:cdnConfigFilePathURL
59+
url:datafileConfigURL
6260
completionHandler:completion];
6361
}
6462
}
6563

66-
- (void)downloadProjectConfig:(NSString *)projectId
64+
- (void)downloadProjectConfig:(nonnull NSURL *)datafileConfigURL
6765
backoffRetry:(BOOL)backoffRetry
6866
completionHandler:(OPTLYHTTPRequestManagerResponse)completion
6967
{
70-
NSURL *cdnConfigFilePathURL = [OPTLYNetworkService projectConfigURLPath:projectId];
71-
7268
if (backoffRetry) {
7369
[self.requestManager GETWithBackoffRetryInterval:OPTLYNetworkServiceDatafileDownloadMaxBackoffRetryTimeInterval_ms
74-
url:cdnConfigFilePathURL
70+
url:datafileConfigURL
7571
retries:OPTLYNetworkServiceDatafileDownloadMaxBackoffRetryAttempts
7672
completionHandler:completion];
7773
} else {
78-
[self.requestManager GETWithURL:cdnConfigFilePathURL
74+
[self.requestManager GETWithURL:datafileConfigURL
7975
completion:completion];
8076
}
8177
}
@@ -98,14 +94,4 @@ - (void)dispatchEvent:(nonnull NSDictionary *)params
9894
}
9995
}
10096

101-
# pragma mark - Helper Methods
102-
103-
+ (NSURL *)projectConfigURLPath:(NSString *)projectId
104-
{
105-
NSURL *cdnURL = [NSURL URLWithString:OPTLYNetworkServiceCDNServerURL];
106-
NSString *filePath = [NSString stringWithFormat:@"%@%@.json", cdnURL.absoluteString, projectId];
107-
return [NSURL URLWithString:filePath];
108-
}
109-
110-
11197
@end

OptimizelySDKCore/OptimizelySDKCoreTests/OPTLYNetworkServiceTest.m

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
#import <XCTest/XCTest.h>
1818
#import <OHHTTPStubs/OHHTTPStubs.h>
19+
#import <OptimizelySDKShared/OPTLYDatafileConfig.h>
1920
#import "OPTLYNetworkService.h"
2021
#import "OPTLYTestHelper.h"
2122

2223
static NSString *const kDatafileVersion = @"3";
2324

24-
static NSString *const kExpectedCDNURLTemplate = @"https://cdn.optimizely.com/json/%@.json";
2525
static NSString *const kDatamodelDatafileName = @"optimizely_6372300739";
2626
static NSString *const kLastModifiedDate = @"Mon, 28 Nov 2016 06:10:59 GMT";
2727
static NSString *const kProjectId = @"6372300739";
@@ -64,7 +64,9 @@ - (void)testDownloadProjectConfigRequestRetrievesProperDatafileVersion {
6464
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"testDownloadProjectConfigRequestRetrievesProperDatafileVersion"];
6565
[self stub200Response];
6666

67-
[self.network downloadProjectConfig:kProjectId
67+
NSString *filePath = [NSString stringWithFormat:OPTLY_DATAFILE_URL, kProjectId];
68+
69+
[self.network downloadProjectConfig:[NSURL URLWithString:filePath]
6870
backoffRetry:NO
6971
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
7072
NSDictionary *datafile = [NSJSONSerialization JSONObjectWithData:data
@@ -81,8 +83,9 @@ - (void)testDownloadProjectConfigRequestRetrievesProperDatafileVersion {
8183
- (void)testDownloadProjectConfigWithLastModifiedRequestRetrievesProperDatafileVersion {
8284
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"testDownloadProjectConfigWithLastModifiedRequestRetrievesProperDatafileVersion"];
8385
[self stub200Response];
84-
85-
[self.network downloadProjectConfig:kProjectId
86+
NSString *filePath = [NSString stringWithFormat:OPTLY_DATAFILE_URL, kProjectId];
87+
88+
[self.network downloadProjectConfig:[NSURL URLWithString:filePath]
8689
backoffRetry:NO
8790
lastModified:kLastModifiedDate
8891
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
@@ -96,19 +99,11 @@ - (void)testDownloadProjectConfigWithLastModifiedRequestRetrievesProperDatafileV
9699
[self waitForExpectationsWithTimeout:2 handler:nil];
97100
}
98101

99-
- (void)testProjectConfigURLPathReturnsExpectedUrl {
100-
NSString *expectedURLString = [NSString stringWithFormat:kExpectedCDNURLTemplate, kProjectId];
101-
NSURL *expectedURL = [NSURL URLWithString:expectedURLString];
102-
103-
NSURL *cdnURL = [OPTLYNetworkService projectConfigURLPath:kProjectId];
104-
105-
XCTAssertEqualObjects(cdnURL, expectedURL, @"Unexpected CDN URL: %@", cdnURL);
106-
107-
}
108-
109102
# pragma mark - Helper Methods
110103
- (id<OHHTTPStubsDescriptor>)stub200Response {
111-
NSURL *hostURL = [NSURL URLWithString:OPTLYNetworkServiceCDNServerURL];
104+
NSString *filePath = [NSString stringWithFormat:OPTLY_DATAFILE_URL, kProjectId];
105+
106+
NSURL *hostURL = [NSURL URLWithString:filePath];
112107
NSString *hostName = [hostURL host];
113108

114109
return [OHHTTPStubs stubRequestsPassingTest:^BOOL (NSURLRequest *request) {

OptimizelySDKDatafileManager/OptimizelySDKDatafileManager/OPTLYDatafileManager.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2016, Optimizely, Inc. and contributors *
2+
* Copyright 2016-2018, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -17,19 +17,23 @@
1717
#import <Foundation/Foundation.h>
1818
#ifdef UNIVERSAL
1919
#import "OPTLYDatafileManager.h"
20+
#import "OPTLYDatafileConfig.h"
2021
#else
2122
#import <OptimizelySDKShared/OPTLYDatafileManagerBasic.h>
23+
#import <OptimizelySDKShared/OPTLYDatafileConfig.h>
2224
#endif
25+
2326
#import "OPTLYDatafileManagerBuilder.h"
2427

28+
2529
@protocol OPTLYDatafileManager, OPTLYErrorHandler, OPTLYLogger;
2630

2731
@interface OPTLYDatafileManagerDefault : NSObject<OPTLYDatafileManager>
2832

2933
/// The time interval to regularly fetch the datafile.
3034
@property (nonatomic, readonly) NSTimeInterval datafileFetchInterval;
3135
/// The project ID of the datafile this datafile manager will monitor
32-
@property (nonatomic, readonly, strong, nonnull) NSString *projectId;
36+
@property (nonatomic, readonly, strong, nonnull) OPTLYDatafileConfig *datafileConfig;
3337
/// The error handler to be used for the manager, client, and all subcomponents
3438
@property (nonatomic, readonly, strong, nullable) id<OPTLYErrorHandler> errorHandler;
3539
/// A logger for the OPTLYDatafileManager to log messages.

0 commit comments

Comments
 (0)