Skip to content

Commit 03da734

Browse files
committed
bring back a basic event dispatcher in core so that it works out of the box
1 parent 297dce4 commit 03da734

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

OptimizelySDKCore/OptimizelySDKCore/OPTLYBuilder.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ - (NSData *)datafile {
8787

8888
- (id<OPTLYEventDispatcher>)eventDispatcher {
8989
if (!_eventDispatcher) {
90-
_eventDispatcher = [[OPTLYEventDispatcherNoOp alloc] init];
90+
_eventDispatcher = [[OPTLYEventDispatcherBasic alloc] init];
9191
}
9292
return _eventDispatcher;
9393
}

OptimizelySDKCore/OptimizelySDKCore/OPTLYEventDispatcher.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,8 @@
3838
+ (BOOL)conformsToOPTLYEventDispatcherProtocol:(nonnull Class)instanceClass;
3939
@end
4040

41+
@interface OPTLYEventDispatcherBasic : NSObject <OPTLYEventDispatcher>
42+
@end
43+
4144
@interface OPTLYEventDispatcherNoOp : NSObject<OPTLYEventDispatcher>
4245
@end

OptimizelySDKCore/OptimizelySDKCore/OPTLYEventDispatcher.m

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,41 @@ + (BOOL)conformsToOPTLYEventDispatcherProtocol:(Class)instanceClass
3434

3535
@end
3636

37+
static NSString * const kHTTPRequestMethodPost = @"POST";
38+
static NSString * const kHTTPHeaderFieldContentType = @"Content-Type";
39+
static NSString * const kHTTPHeaderFieldValueApplicationJSON = @"application/json";
40+
41+
@implementation OPTLYEventDispatcherBasic
42+
43+
- (void)dispatchEvent:(NSDictionary *)params
44+
toURL:(NSURL *)url
45+
completionHandler:(void(^)(NSURLResponse *response, NSError *error))completion
46+
{
47+
NSURLSession *ephemeralSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration]];
48+
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
49+
[request setHTTPMethod:kHTTPRequestMethodPost];
50+
51+
NSError *JSONSerializationError = nil;
52+
NSData *data = [NSJSONSerialization dataWithJSONObject:params
53+
options:kNilOptions
54+
error:&JSONSerializationError];
55+
56+
[request addValue:kHTTPHeaderFieldValueApplicationJSON forHTTPHeaderField:kHTTPHeaderFieldContentType];
57+
58+
if (!JSONSerializationError) {
59+
NSURLSessionUploadTask *uploadTask = [ephemeralSession uploadTaskWithRequest:request
60+
fromData:data
61+
completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
62+
if (completion) {
63+
completion(response, error);
64+
}
65+
}];
66+
67+
[uploadTask resume];
68+
}
69+
}
70+
71+
@end
3772

3873
@implementation OPTLYEventDispatcherNoOp
3974

0 commit comments

Comments
 (0)