Skip to content

Commit f6e4c6f

Browse files
Implemented event dispatcher and datafile manager default in core with the network service pulled in from the Shared module. Also fixed a broken OptimizelyManager test.
1 parent 67eb3cc commit f6e4c6f

File tree

9 files changed

+51
-36
lines changed

9 files changed

+51
-36
lines changed

OptimizelySDKCore/OptimizelySDKCore/OPTLYBuilder.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ - (NSData *)datafile {
8888

8989
- (id<OPTLYDatafileManager>)datafileManager {
9090
if (!_datafileManager) {
91-
_datafileManager = [[OPTLYDatafileManagerNoOp alloc] init];
91+
_datafileManager = [[OPTLYDatafileManagerDefault alloc] init];
9292
}
9393
return _datafileManager;
9494
}
@@ -102,7 +102,7 @@ - (NSData *)datafile {
102102

103103
- (id<OPTLYEventDispatcher>)eventDispatcher {
104104
if (!_eventDispatcher) {
105-
_eventDispatcher = [[OPTLYEventDispatcherBasic alloc] init];
105+
_eventDispatcher = [[OPTLYEventDispatcherDefault alloc] init];
106106
}
107107
return _eventDispatcher;
108108
}

OptimizelySDKCore/OptimizelySDKCore/OPTLYDatafileManager.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040

4141
@end
4242

43+
@interface OPTLYDatafileManagerDefault : NSObject<OPTLYDatafileManager>
44+
45+
@end
46+
4347
@interface OPTLYDatafileManagerNoOp : NSObject<OPTLYDatafileManager>
4448

4549
@end

OptimizelySDKCore/OptimizelySDKCore/OPTLYDatafileManager.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
***************************************************************************/
1616

1717
#import "OPTLYDatafileManager.h"
18+
#import "OPTLYNetworkService.h"
1819

1920
@implementation OPTLYDatafileManagerUtility
2021

@@ -30,6 +31,16 @@ + (BOOL)conformsToOPTLYDatafileManagerProtocol:(Class)instanceClass {
3031

3132
@end
3233

34+
@implementation OPTLYDatafileManagerDefault
35+
36+
- (void)downloadDatafile:(nonnull NSString *)projectId
37+
completionHandler:(nullable void (^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completion {
38+
OPTLYNetworkService *networkService = [OPTLYNetworkService new];
39+
[networkService downloadProjectConfig:projectId
40+
completionHandler:completion];
41+
}
42+
@end
43+
3344
@implementation OPTLYDatafileManagerNoOp
3445

3546
- (void)downloadDatafile:(nonnull NSString *)projectId

OptimizelySDKCore/OptimizelySDKCore/OPTLYEventDispatcher.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@
4747
@end
4848

4949
/**
50-
* OPTLYEventDispatcherBasic is a very simple implementation of the OPTLYEventDispatcher protocol.
50+
* OPTLYEventDispatcherDefault is a very simple implementation of the OPTLYEventDispatcher protocol.
5151
* It dispatches events without any failure mechanisms (e.g., events are not queued up for a loater
5252
* retry.
5353
*/
54-
@interface OPTLYEventDispatcherBasic : NSObject <OPTLYEventDispatcher>
54+
@interface OPTLYEventDispatcherDefault : NSObject <OPTLYEventDispatcher>
5555
@end
5656

5757
/**

OptimizelySDKCore/OptimizelySDKCore/OPTLYEventDispatcher.m

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
***************************************************************************/
1616

1717
#import <Foundation/Foundation.h>
18-
#import "OPTLYProjectConfig.h"
19-
#import "OPTLYEventDispatcher.h"
2018
#import "OPTLYErrorHandler.h"
19+
#import "OPTLYEventDispatcher.h"
20+
#import "OPTLYProjectConfig.h"
21+
#import "OPTLYNetworkService.h"
2122

2223
static NSString * const kEventDispatcherImpressionEventURL = @"https://logx.optimizely.com/log/decision";
2324
static NSString * const kEventDispatcherConversionEventURL = @"https://logx.optimizely.com/log/event";
@@ -42,7 +43,7 @@ + (BOOL)conformsToOPTLYEventDispatcherProtocol:(Class)instanceClass
4243

4344
@end
4445

45-
@implementation OPTLYEventDispatcherBasic
46+
@implementation OPTLYEventDispatcherDefault
4647

4748
- (void)dispatchImpressionEvent:(nonnull NSDictionary *)params
4849
callback:(nullable void(^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))callback {
@@ -59,34 +60,11 @@ - (void)dispatchConversionEvent:(nonnull NSDictionary *)params
5960

6061
- (void)dispatchEvent:(NSDictionary *)params
6162
toURL:(NSURL *)url
62-
completionHandler:(void(^)(NSData *data, NSURLResponse *response, NSError *error))completion
63-
{
64-
NSURLSession *ephemeralSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]];
65-
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
66-
[request setHTTPMethod:kHTTPRequestMethodPost];
67-
68-
NSError *JSONSerializationError = nil;
69-
NSData *data = [NSJSONSerialization dataWithJSONObject:params
70-
options:kNilOptions
71-
error:&JSONSerializationError];
72-
73-
[request addValue:kHTTPHeaderFieldValueApplicationJSON forHTTPHeaderField:kHTTPHeaderFieldContentType];
74-
75-
if (!JSONSerializationError) {
76-
NSURLSessionUploadTask *uploadTask = [ephemeralSession uploadTaskWithRequest:request
77-
fromData:data
78-
completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
79-
if (completion) {
80-
completion(data, response, error);
81-
}
82-
}];
83-
84-
[uploadTask resume];
85-
}
63+
completionHandler:(void(^)(NSData *data, NSURLResponse *response, NSError *error))completion {
64+
OPTLYNetworkService *networkService = [OPTLYNetworkService new];
65+
[networkService dispatchEvent:params toURL:url completionHandler:completion];
8666
}
8767

88-
89-
9068
@end
9169

9270
@implementation OPTLYEventDispatcherNoOp

OptimizelySDKCore/OptimizelySDKCore/OPTLYNetworkService.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,14 @@ NS_ASSUME_NONNULL_END
4444
lastModified:(nonnull NSString *)lastModifiedDate
4545
completionHandler:(nullable OPTLYHTTPRequestManagerResponse)completion;
4646

47+
/**
48+
* Dispatches an event to a url
49+
* @param params Dictionary of the event parameter values
50+
* @param url The url to dispatch the event
51+
* @param completion The completion handler
52+
*/
53+
- (void)dispatchEvent:(nonnull NSDictionary *)params
54+
toURL:(nonnull NSURL *)url
55+
completionHandler:(nullable void(^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completion;
56+
4757
@end

OptimizelySDKCore/OptimizelySDKCore/OPTLYNetworkService.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ - (void)downloadProjectConfig:(NSString *)projectId completionHandler:(OPTLYHTTP
5353
}];
5454
}
5555

56+
- (void)dispatchEvent:(nonnull NSDictionary *)params
57+
toURL:(nonnull NSURL *)url
58+
completionHandler:(nullable void(^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completion
59+
{
60+
OPTLYHTTPRequestManager *requestManager = [[OPTLYHTTPRequestManager alloc] initWithURL:url];
61+
[requestManager POSTWithParameters:params completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
62+
if (completion) {
63+
completion(data, response, error);
64+
}
65+
}];
66+
}
67+
5668
# pragma mark - Helper Methods
5769

5870
- (NSURL *)projectConfigURLPath:(NSURL *)url

OptimizelySDKShared/OptimizelySDKShared/OPTLYManagerBuilder.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ - (id)initWithBlock:(OPTLYManagerBuilderBlock)block {
4444

4545
- (id<OPTLYDatafileManager>)datafileManager {
4646
if (!_datafileManager) {
47-
_datafileManager = [[OPTLYDatafileManagerNoOp alloc] init];
47+
_datafileManager = [[OPTLYDatafileManagerDefault alloc] init];
4848
}
4949
return _datafileManager;
5050
}
@@ -58,7 +58,7 @@ - (id)initWithBlock:(OPTLYManagerBuilderBlock)block {
5858

5959
- (id<OPTLYEventDispatcher>)eventDispatcher {
6060
if (!_eventDispatcher) {
61-
_eventDispatcher = [[OPTLYEventDispatcherNoOp alloc] init];
61+
_eventDispatcher = [[OPTLYEventDispatcherDefault alloc] init];
6262
}
6363
return _eventDispatcher;
6464
}

OptimizelySDKShared/OptimizelySDKSharedTests/OPTLYManagerTest.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ - (void)testInitializeClientAsync {
122122
OPTLYManager *manager = [OPTLYManager initWithBuilderBlock:^(OPTLYManagerBuilder * _Nullable builder) {
123123
builder.datafile = self.defaultDatafile;
124124
builder.projectId = kProjectId;
125-
builder.datafileManager = [OPTLYDatafileManagerNoOp new];
125+
builder.datafileManager = [OPTLYDatafileManagerDefault new];
126126
}];
127127

128128
// make sure manager is initialized correctly

0 commit comments

Comments
 (0)