Skip to content

Commit b264a15

Browse files
authored
[2] Use LPRequestFactory (#185)
1 parent 863c6ba commit b264a15

File tree

9 files changed

+115
-44
lines changed

9 files changed

+115
-44
lines changed

Leanplum-SDK/Classes/Features/Actions/LPActionManager.m

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#import "LPUIAlert.h"
3434
#import "LeanplumRequest.h"
3535
#import "LPMessageTemplates.h"
36+
#import "LPRequestFactory.h"
3637

3738
#import <objc/runtime.h>
3839
#import <objc/message.h>
@@ -84,9 +85,12 @@ - (void)leanplum_application:(UIApplication *)application didRegisterForRemoteNo
8485
if (!existingToken || ![existingToken isEqualToString:formattedToken]) {
8586
[[NSUserDefaults standardUserDefaults] setObject:formattedToken forKey:tokenKey];
8687
[[NSUserDefaults standardUserDefaults] synchronize];
87-
88-
[[LeanplumRequest post:LP_METHOD_SET_DEVICE_ATTRIBUTES
89-
params:@{LP_PARAM_DEVICE_PUSH_TOKEN: formattedToken}] send];
88+
89+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
90+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
91+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_SET_DEVICE_ATTRIBUTES
92+
params:@{LP_PARAM_DEVICE_PUSH_TOKEN: formattedToken}];
93+
[req send];
9094
}
9195
LP_END_TRY
9296

@@ -335,7 +339,10 @@ - (void)sendUserNotificationSettingsIfChanged:(UIUserNotificationSettings *)noti
335339
}
336340
[Leanplum onStartResponse:^(BOOL success) {
337341
LP_END_USER_CODE
338-
[[LeanplumRequest post:LP_METHOD_SET_DEVICE_ATTRIBUTES params:params] send];
342+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
343+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
344+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_SET_DEVICE_ATTRIBUTES params:params];
345+
[req send];
339346
LP_BEGIN_USER_CODE
340347
}];
341348
}

Leanplum-SDK/Classes/Features/Inbox/LPInbox.m

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#import "LPKeychainWrapper.h"
3434
#import "LPFileManager.h"
3535
#import "Utils.h"
36+
#import "LPRequestFactory.h"
3637

3738
static NSObject *updatingLock;
3839

@@ -188,7 +189,9 @@ - (void)read
188189
RETURN_IF_NOOP;
189190
LP_TRY
190191
NSDictionary *params = @{LP_PARAM_INBOX_MESSAGE_ID: [self messageId]};
191-
LeanplumRequest *req = [LeanplumRequest post:LP_METHOD_MARK_INBOX_MESSAGE_AS_READ
192+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
193+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
194+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_MARK_INBOX_MESSAGE_AS_READ
192195
params:params];
193196
[req send];
194197
LP_END_TRY
@@ -367,7 +370,9 @@ - (void)removeMessageForId:(NSString *)messageId
367370
[[LPInbox sharedState] updateMessages:_messages unreadCount:unreadCount];
368371

369372
NSDictionary *params = @{LP_PARAM_INBOX_MESSAGE_ID:messageId};
370-
LeanplumRequest *req = [LeanplumRequest post:LP_METHOD_DELETE_INBOX_MESSAGE
373+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
374+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
375+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_DELETE_INBOX_MESSAGE
371376
params:params];
372377
[req send];
373378
LP_END_TRY
@@ -413,7 +418,9 @@ - (void)downloadMessages
413418
{
414419
RETURN_IF_NOOP;
415420
LP_TRY
416-
LeanplumRequest *req = [LeanplumRequest post:LP_METHOD_GET_INBOX_MESSAGES params:nil];
421+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
422+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
423+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_GET_INBOX_MESSAGES params:nil];
417424
[req onResponse:^(id<LPNetworkOperationProtocol> operation, NSDictionary *response) {
418425
LP_TRY
419426
NSDictionary *messagesDict = response[LP_KEY_INBOX_MESSAGES];

Leanplum-SDK/Classes/Features/Variables/LPVarCache.m

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#import "LPAES.h"
3434
#import "Leanplum_SocketIO.h"
3535
#import "Utils.h"
36+
#import "LPRequestFactory.h"
3637

3738
@interface LPVarCache()
3839
@property (strong, nonatomic) NSRegularExpression *varNameRegex;
@@ -655,8 +656,11 @@ - (BOOL)sendContentIfChanged:(BOOL)variables actions:(BOOL)actions
655656
args[LP_PARAM_ACTION_DEFINITIONS] = [LPJSON stringFromJSON:self.actionDefinitions];
656657
}
657658
args[LP_PARAM_FILE_ATTRIBUTES] = [LPJSON stringFromJSON:limitedFileAttributes];
658-
[[LeanplumRequest post:LP_METHOD_SET_VARS
659-
params:args] send];
659+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
660+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
661+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_SET_VARS
662+
params:args];
663+
[req send];
660664
return YES;
661665
} @catch (NSException *e) {
662666
[Leanplum throwError:@"Cannot serialize variable values. "
@@ -708,9 +712,11 @@ - (void)maybeUploadNewFiles
708712
}
709713
}
710714
if (filenames.count > 0) {
711-
[[LeanplumRequest post:LP_METHOD_UPLOAD_FILE
712-
params:@{LP_PARAM_DATA: [LPJSON stringFromJSON:fileData]}]
713-
sendFilesNow:filenames];
715+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
716+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
717+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_UPLOAD_FILE
718+
params:@{LP_PARAM_DATA: [LPJSON stringFromJSON:fileData]}];
719+
[req sendFilesNow:filenames];
714720
}
715721
}
716722

Leanplum-SDK/Classes/Internal/Constants.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#import "Constants.h"
2626
#import "LeanplumRequest.h"
2727
#import "Utils.h"
28+
#import "LPRequestFactory.h"
2829

2930
@implementation LPConstantsState
3031

@@ -316,13 +317,16 @@ void leanplumInternalError(NSException *e)
316317
objectForKey:LP_USER_CODE_BLOCKS] intValue];
317318
if (userCodeBlocks <= 0) {
318319
@try {
319-
[[LeanplumRequest post:LP_METHOD_LOG
320+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
321+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
322+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_LOG
320323
params:@{
321324
LP_PARAM_TYPE: LP_VALUE_SDK_ERROR,
322325
LP_PARAM_MESSAGE: [e description],
323326
@"stackTrace": [[e callStackSymbols] description] ?: @"",
324327
LP_PARAM_VERSION_NAME: versionName
325-
}] send];
328+
}];
329+
[req send];
326330
} @catch (NSException *e) {
327331
// This empty try/catch is needed to prevent crash <-> loop.
328332
}

Leanplum-SDK/Classes/Internal/Leanplum.m

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#import "Utils.h"
4646
#import "LPAppIconManager.h"
4747
#import "LPUIEditorWrapper.h"
48+
#import "LPRequestFactory.h"
4849

4950
static NSString *leanplum_deviceId = nil;
5051
static NSString *registrationEmail = nil;
@@ -857,7 +858,9 @@ + (void)startWithUserId:(NSString *)userId
857858
}
858859

859860
// Issue start API call.
860-
LeanplumRequest *req = [LeanplumRequest post:LP_METHOD_START params:params];
861+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
862+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
863+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_START params:params];
861864
[req onResponse:^(id<LPNetworkOperationProtocol> operation, NSDictionary *response) {
862865
LP_TRY
863866
state.hasStarted = YES;
@@ -936,11 +939,14 @@ + (void)startWithUserId:(NSString *)userId
936939
// Report latency for 0.1% of users.
937940
NSTimeInterval latency = [[NSDate date] timeIntervalSinceDate:startTime];
938941
if (arc4random() % 1000 == 0) {
939-
[[LeanplumRequest post:LP_METHOD_LOG
942+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
943+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
944+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_LOG
940945
params:@{
941946
LP_PARAM_TYPE: LP_VALUE_SDK_START_LATENCY,
942947
@"startLatency": [@(latency) description]
943-
}] send];
948+
}];
949+
[req send];
944950
}
945951
}
946952

@@ -1032,8 +1038,10 @@ + (void)startWithUserId:(NSString *)userId
10321038
LP_TRY
10331039
BOOL exitOnSuspend = [[[[NSBundle mainBundle] infoDictionary]
10341040
objectForKey:@"UIApplicationExitsOnSuspend"] boolValue];
1035-
[[LeanplumRequest post:LP_METHOD_STOP params:nil]
1036-
sendIfConnectedSync:exitOnSuspend];
1041+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
1042+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
1043+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_STOP params:nil];
1044+
[req sendIfConnectedSync:exitOnSuspend];
10371045
LP_END_TRY
10381046
}];
10391047

@@ -1042,7 +1050,10 @@ + (void)startWithUserId:(NSString *)userId
10421050
RETURN_IF_NOOP;
10431051
LP_TRY
10441052
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive) {
1045-
[[LeanplumRequest post:LP_METHOD_HEARTBEAT params:nil] sendIfDelayed];
1053+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
1054+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
1055+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_HEARTBEAT params:nil];
1056+
[req sendIfDelayed];
10461057
}
10471058
LP_END_TRY
10481059
} repeats:YES];
@@ -1135,7 +1146,9 @@ + (void)pause
11351146
backgroundTask = [application beginBackgroundTaskWithExpirationHandler:finishTaskHandler];
11361147

11371148
// Send pause event.
1138-
LeanplumRequest *request = [LeanplumRequest post:LP_METHOD_PAUSE_SESSION params:nil];
1149+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
1150+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
1151+
LeanplumRequest *request = [reqFactory createPostForApiMethod:LP_METHOD_PAUSE_SESSION params:nil];
11391152
[request onResponse:^(id<LPNetworkOperationProtocol> operation, id json) {
11401153
finishTaskHandler();
11411154
}];
@@ -1147,7 +1160,10 @@ + (void)pause
11471160

11481161
+ (void)resume
11491162
{
1150-
[[LeanplumRequest post:LP_METHOD_RESUME_SESSION params:nil] sendIfDelayed];
1163+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
1164+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
1165+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_RESUME_SESSION params:nil];
1166+
[req sendIfDelayed];
11511167
}
11521168

11531169
+ (void)trackCrashes
@@ -1886,7 +1902,10 @@ + (void)track:(NSString *)event withValue:(double)value andInfo:(NSString *)info
18861902
+ (void)trackInternal:(NSString *)event withArgs:(NSDictionary *)args
18871903
andParameters:(NSDictionary *)params
18881904
{
1889-
[[LeanplumRequest post:LP_METHOD_TRACK params:args] send];
1905+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
1906+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
1907+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_TRACK params:args];
1908+
[req send];
18901909

18911910
// Perform event actions.
18921911
NSString *messageId = args[LP_PARAM_MESSAGE_ID];
@@ -2016,10 +2035,13 @@ + (void)setUserIdInternal:(NSString *)userId withAttributes:(NSDictionary *)attr
20162035
attributes = @{};
20172036
}
20182037

2019-
[[LeanplumRequest post:LP_METHOD_SET_USER_ATTRIBUTES params:@{
2038+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
2039+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
2040+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_SET_USER_ATTRIBUTES params:@{
20202041
LP_PARAM_USER_ATTRIBUTES: attributes ? [LPJSON stringFromJSON:attributes] : @"",
20212042
LP_PARAM_NEW_USER_ID: userId ? userId : @""
2022-
}] send];
2043+
}];
2044+
[req send];
20232045

20242046
if (userId.length) {
20252047
[LeanplumRequest setUserId:userId];
@@ -2089,9 +2111,12 @@ + (void)setTrafficSourceInfo:(NSDictionary *)info
20892111

20902112
+ (void)setTrafficSourceInfoInternal:(NSDictionary *)info
20912113
{
2092-
[[LeanplumRequest post:LP_METHOD_SET_TRAFFIC_SOURCE_INFO params:@{
2114+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
2115+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
2116+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_SET_TRAFFIC_SOURCE_INFO params:@{
20932117
LP_PARAM_TRAFFIC_SOURCE: info
2094-
}] send];
2118+
}];
2119+
[req send];
20952120
}
20962121

20972122
+ (void)advanceTo:(NSString *)state
@@ -2141,7 +2166,10 @@ + (void)advanceTo:(NSString *)state withInfo:(NSString *)info andParameters:(NSD
21412166
+ (void)advanceToInternal:(NSString *)state withArgs:(NSDictionary *)args
21422167
andParameters:(NSDictionary *)params
21432168
{
2144-
[[LeanplumRequest post:LP_METHOD_ADVANCE params:args] send];
2169+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
2170+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
2171+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_ADVANCE params:args];
2172+
[req send];
21452173
LPContextualValues *contextualValues = [[LPContextualValues alloc] init];
21462174
contextualValues.parameters = params;
21472175
[self maybePerformActions:@[@"state"]
@@ -2167,7 +2195,10 @@ + (void)pauseState
21672195

21682196
+ (void)pauseStateInternal
21692197
{
2170-
[[LeanplumRequest post:LP_METHOD_PAUSE_STATE params:@{}] send];
2198+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
2199+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
2200+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_PAUSE_STATE params:@{}];
2201+
[req send];
21712202
}
21722203

21732204
+ (void)resumeState
@@ -2186,7 +2217,10 @@ + (void)resumeState
21862217

21872218
+ (void)resumeStateInternal
21882219
{
2189-
[[LeanplumRequest post:LP_METHOD_RESUME_STATE params:@{}] send];
2220+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
2221+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
2222+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_RESUME_STATE params:@{}];
2223+
[req send];
21902224
}
21912225

21922226
+ (void)forceContentUpdate
@@ -2212,9 +2246,10 @@ + (void)forceContentUpdate:(LeanplumVariablesChangedBlock)block
22122246
params[LP_PARAM_INCLUDE_VARIANT_DEBUG_INFO] = @(YES);
22132247
}
22142248

2215-
2216-
LeanplumRequest* req = [LeanplumRequest
2217-
post:LP_METHOD_GET_VARS
2249+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
2250+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
2251+
LeanplumRequest* req = [reqFactory
2252+
createPostForApiMethod:LP_METHOD_GET_VARS
22182253
params:params];
22192254
[req onResponse:^(id<LPNetworkOperationProtocol> operation, NSDictionary *response) {
22202255
LP_TRY
@@ -2501,10 +2536,13 @@ + (void)maybeSendLog:(NSString *)message {
25012536
threadDict[LP_IS_LOGGING] = @YES;
25022537

25032538
@try {
2504-
[[LeanplumRequest post:LP_METHOD_LOG params:@{
2539+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
2540+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
2541+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_LOG params:@{
25052542
LP_PARAM_TYPE: LP_VALUE_SDK_LOG,
25062543
LP_PARAM_MESSAGE: message
2507-
}] sendEventually];
2544+
}];
2545+
[req sendEventually];
25082546
} @catch (NSException *exception) {
25092547
NSLog(@"Leanplum: Unable to send log: %@", exception);
25102548
} @finally {
@@ -2591,7 +2629,9 @@ + (void)setUserLocationAttributeWithLatitude:(double)latitude
25912629
params[LP_KEY_COUNTRY] = country;
25922630
}
25932631

2594-
LeanplumRequest *req = [LeanplumRequest post:LP_METHOD_SET_USER_ATTRIBUTES params:params];
2632+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
2633+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
2634+
LeanplumRequest *req = [reqFactory createPostForApiMethod:LP_METHOD_SET_USER_ATTRIBUTES params:params];
25952635
[req onResponse:^(id<LPNetworkOperationProtocol> operation, id json) {
25962636
if (response) {
25972637
response(YES);

Leanplum-SDK/Classes/Managers/LPAppIconManager.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#import "LeanplumRequest.h"
2727
#import "LeanplumInternal.h"
2828
#import "Utils.h"
29+
#import "LPRequestFactory.h"
2930

3031
@implementation LPAppIconManager
3132

@@ -62,8 +63,9 @@ + (void)uploadAppIconsOnDevMode
6263
withIconBundle:obj
6364
iconName:key];
6465
}];
65-
66-
LeanplumRequest *request = [LeanplumRequest post:LP_METHOD_UPLOAD_FILE
66+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
67+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
68+
LeanplumRequest *request = [reqFactory createPostForApiMethod:LP_METHOD_UPLOAD_FILE
6769
params:@{@"data":
6870
[LPJSON stringFromJSON:requestParam]}];
6971
[request onResponse:^(id<LPNetworkOperationProtocol> operation, id json) {

Leanplum-SDK/Classes/Managers/LPFileManager.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#import <objc/message.h>
3333
#import <objc/runtime.h>
3434
#include <unistd.h>
35+
#import "LPRequestFactory.h"
3536

3637
typedef enum {
3738
kLeanplumFileOperationGet = 0,
@@ -565,7 +566,9 @@ + (BOOL)maybeDownloadFile:(NSString *)value
565566
return NO;
566567
}
567568
if ([self shouldDownloadFile:value defaultValue:defaultValue]) {
568-
LeanplumRequest *downloadRequest = [LeanplumRequest get:LP_METHOD_DOWNLOAD_FILE params:nil];
569+
LPRequestFactory *reqFactory = [[LPRequestFactory alloc]
570+
initWithFeatureFlagManager:[LPFeatureFlagManager sharedManager]];
571+
LeanplumRequest *downloadRequest = [reqFactory createGetForApiMethod:LP_METHOD_DOWNLOAD_FILE params:nil];
569572
[downloadRequest onResponse:^(id<LPNetworkOperationProtocol> operation, id json) {
570573
if (complete) {
571574
complete();

0 commit comments

Comments
 (0)