Skip to content

Commit b69dc2b

Browse files
authored
switch to new request class on feature flag (#201)
* switch to new request on feature flag * actually return LPRequesting * fix naming; * refactor into own method
1 parent 5fe95eb commit b69dc2b

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

Leanplum-SDK/Classes/Internal/FeatureFlag/LPFeatureFlagManager.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
//
77

88
#import <Foundation/Foundation.h>
9+
#import "LPFeatureFlags.h"
910

1011
@interface LPFeatureFlagManager : NSObject
1112

12-
+ (instancetype)sharedManager;
13+
+ (_Nonnull instancetype)sharedManager;
1314

1415
-(void)refreshEnabledFeatureFlags:(nullable NSArray<NSString *> *)featureFlags;
1516
-(BOOL)isFeatureFlagEnabled:(nonnull NSString *)featureFlagName;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// LPFeatureFlags.h
3+
// Pods
4+
//
5+
// Created by Mayank Sanganeria on 8/30/18.
6+
//
7+
8+
#ifndef LPFeatureFlags_h
9+
#define LPFeatureFlags_h
10+
11+
#define LP_FEATURE_FLAG_REQUEST_REFACTOR @"request_refactor"
12+
13+
#endif /* LPFeatureFlags_h */

Leanplum-SDK/Classes/Managers/Networking/LPRequest.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424

2525
#import <Foundation/Foundation.h>
2626
#import "Leanplum.h"
27+
#import "LPRequesting.h"
2728
#import "LPNetworkFactory.h"
2829

29-
@interface LPRequest : NSObject
30+
@interface LPRequest : NSObject <LPRequesting>
3031

3132
@property (nonatomic, strong) NSString *apiMethod;
3233
@property (nonatomic, strong) NSDictionary *params;

Leanplum-SDK/Classes/Managers/Networking/LPRequestFactory.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
// under the License.
2424

2525
#import "LPRequestFactory.h"
26+
#import "LPRequest.h"
2627
#import "LeanplumRequest.h"
2728

2829
@interface LPRequestFactory()
@@ -42,11 +43,21 @@ -(instancetype)initWithFeatureFlagManager:(LPFeatureFlagManager *)featureFlagMan
4243
}
4344

4445
- (id<LPRequesting>)createGetForApiMethod:(NSString *)apiMethod params:(NSDictionary *)params {
46+
if ([self shouldReturnLPRequestClass]) {
47+
return [LPRequest get:apiMethod params:params];
48+
}
4549
return [LeanplumRequest get:apiMethod params:params];
4650
}
4751

4852
- (id<LPRequesting>)createPostForApiMethod:(NSString *)apiMethod params:(NSDictionary *)params {
53+
if ([self shouldReturnLPRequestClass]) {
54+
return [LPRequest get:apiMethod params:params];
55+
}
4956
return [LeanplumRequest post:apiMethod params:params];
5057
}
5158

59+
-(BOOL)shouldReturnLPRequestClass {
60+
return [self.featureFlagManager isFeatureFlagEnabled:LP_FEATURE_FLAG_REQUEST_REFACTOR];
61+
}
62+
5263
@end

Leanplum-SDK/Classes/Managers/Networking/LPRequestSender.m

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ - (void)sendRequest:(id<LPRequesting>)request
9696
}
9797
}
9898

99-
- (void)sendNow:(BOOL)async request:(id<LPRequesting>)request
99+
- (void)sendNow:(id<LPRequesting>)request sync:(BOOL)sync
100100
{
101101
if ([request isKindOfClass:[LeanplumRequest class]]) {
102102
LeanplumRequest *oldLeanplumRequest = request;
@@ -114,7 +114,7 @@ - (void)sendNow:(BOOL)async request:(id<LPRequesting>)request
114114
}
115115

116116
[self sendEventually:request];
117-
[self sendRequests:async];
117+
[self sendRequests:sync];
118118
}
119119
}
120120

@@ -167,7 +167,7 @@ - (void)sendIfConnected:(id<LPRequesting>)request sync:(BOOL)sync
167167
} else {
168168
if ([[Leanplum_Reachability reachabilityForInternetConnection] isReachable]) {
169169
if (sync) {
170-
[self sendNowSyncRequest:request];
170+
[self sendNowSync:request];
171171
} else {
172172
[self sendNow:request];
173173
}
@@ -183,12 +183,12 @@ - (void)sendIfConnected:(id<LPRequesting>)request sync:(BOOL)sync
183183

184184
- (void)sendNow:(id<LPRequesting>)request
185185
{
186-
[self sendNow:YES request:request];
186+
[self sendNow:request sync:NO];
187187
}
188188

189-
- (void)sendNowSyncRequest:(id<LPRequesting>)request
189+
- (void)sendNowSync:(id<LPRequesting>)request
190190
{
191-
[self sendNow:NO request:request];
191+
[self sendNow:request sync:YES];
192192
}
193193

194194
// Wait 1 second for potential other API calls, and then sends the call synchronously
@@ -315,7 +315,7 @@ - (NSMutableDictionary *)createArgsDictionaryForRequest:(LPRequest *)request
315315
return args;
316316
}
317317

318-
- (void)sendRequests:(BOOL)async
318+
- (void)sendRequests:(BOOL)sync
319319
{
320320
NSBlockOperation *requestOperation = [NSBlockOperation new];
321321
__weak NSBlockOperation *weakOperation = requestOperation;
@@ -348,7 +348,7 @@ - (void)sendRequests:(BOOL)async
348348
LP_PARAM_TIME: timestamp
349349
} mutableCopy];
350350
[self attachApiKeys:multiRequestArgs];
351-
int timeout = async ? constants.networkTimeoutSeconds : constants.syncNetworkTimeoutSeconds;
351+
int timeout = sync ? constants.syncNetworkTimeoutSeconds : constants.networkTimeoutSeconds;
352352

353353
NSTimeInterval uiTimeoutInterval = timeout;
354354
timeout = 5 * timeout; // let slow operations complete
@@ -384,7 +384,7 @@ - (void)sendRequests:(BOOL)async
384384

385385
// Send another request if the last request had maximum events per api call.
386386
if (requestsToSend.count == MAX_EVENTS_PER_API_CALL) {
387-
[self sendRequests:async];
387+
[self sendRequests:sync];
388388
}
389389
LP_END_TRY
390390

@@ -465,7 +465,7 @@ - (void)sendRequests:(BOOL)async
465465

466466
// Send. operationBlock will run synchronously.
467467
// Adding to OperationQueue puts it in the background.
468-
if (async) {
468+
if (!sync) {
469469
[requestOperation addExecutionBlock:operationBlock];
470470
[[self sendNowQueue] addOperation:requestOperation];
471471
} else {

0 commit comments

Comments
 (0)