Skip to content

Commit c1b3675

Browse files
authored
Configurable API endpoint (#488)
* Initial testing * Rewrite ApiConfig Persist api host changes Add unit test for changing api host * Rename servlet to path Fix tests * Update socket host and reconnect * Remove old files * Clean up * Move userId and deviceId to User * Move params to constants * rename dispatch once * small fixes * Make the host part of the network operation Small fixes * Remove unused * Prevent retry loop
1 parent 74e5ddf commit c1b3675

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+708
-465
lines changed

LeanplumSDK/LeanplumSDK.xcodeproj/project.pbxproj

Lines changed: 24 additions & 28 deletions
Large diffs are not rendered by default.

LeanplumSDK/LeanplumSDK/Classes/Features/Actions/LPActionManager.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#import "LPMessageTemplates.h"
3434
#import "LPRequestFactory.h"
3535
#import "LPRequestSender.h"
36-
#import "LPAPIConfig.h"
3736
#import "LPCountAggregator.h"
3837

3938
#import <objc/runtime.h>

LeanplumSDK/LeanplumSDK/Classes/Features/Variables/LPVarCache.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
#import "LPUtils.h"
3535
#import "LPRequestFactory.h"
3636
#import "LPRequestSender.h"
37-
#import "LPAPIConfig.h"
3837
#import "LPCountAggregator.h"
3938
#import "LPFileTransferManager.h"
39+
#import <Leanplum/Leanplum-Swift.h>
4040

4141
@interface LPVarCache()
4242
@property (strong, nonatomic) NSRegularExpression *varNameRegex;
@@ -431,10 +431,10 @@ - (void)loadDiffs
431431
BOOL loggingEnabled = [unarchiver decodeBoolForKey:LP_KEY_LOGGING_ENABLED];
432432
localCaps = [unarchiver decodeObjectForKey:LEANPLUM_DEFAULTS_LOCAL_CAPS_KEY];
433433
if (deviceId) {
434-
[[LPAPIConfig sharedConfig] setDeviceId:deviceId];
434+
[[Leanplum user] setDeviceId:deviceId];
435435
}
436436
if (userId) {
437-
[[LPAPIConfig sharedConfig] setUserId:userId];
437+
[[Leanplum user] setUserId:userId];
438438
}
439439
if (loggingEnabled) {
440440
[LPConstantsState sharedState].loggingEnabled = YES;
@@ -476,8 +476,8 @@ - (void)saveDiffs
476476
[archiver encodeObject:self.variantDebugInfo forKey:LP_KEY_VARIANT_DEBUG_INFO];
477477
[archiver encodeObject:self.regions forKey:LP_KEY_REGIONS];
478478
[archiver encodeObject:[LPConstantsState sharedState].sdkVersion forKey:LP_PARAM_SDK_VERSION];
479-
[archiver encodeObject:[LPAPIConfig sharedConfig].deviceId forKey:LP_PARAM_DEVICE_ID];
480-
[archiver encodeObject:[LPAPIConfig sharedConfig].userId forKey:LP_PARAM_USER_ID];
479+
[archiver encodeObject:[[Leanplum user] deviceId] forKey:LP_PARAM_DEVICE_ID];
480+
[archiver encodeObject:[[Leanplum user] userId] forKey:LP_PARAM_USER_ID];
481481
[archiver encodeBool:[LPConstantsState sharedState].loggingEnabled forKey:LP_KEY_LOGGING_ENABLED];
482482
[archiver encodeObject:self.varsJson forKey:LEANPLUM_DEFAULTS_VARS_JSON_KEY];
483483
[archiver encodeObject:self.varsSignature forKey:LEANPLUM_DEFAULTS_VARS_SIGNATURE_KEY];
@@ -740,7 +740,7 @@ - (NSMutableDictionary *)userAttributes
740740
{
741741
if (!_userAttributes) {
742742
@try {
743-
NSString *token = [[LPAPIConfig sharedConfig] token];
743+
NSString *token = [[ApiConfig shared] token];
744744
if (token) {
745745
NSData *encryptedValue = [[NSUserDefaults standardUserDefaults] dataForKey:LEANPLUM_DEFAULTS_ATTRIBUTES_KEY];
746746
if (encryptedValue) {

LeanplumSDK/LeanplumSDK/Classes/Internal/LPConstants.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,6 @@ NS_ASSUME_NONNULL_BEGIN
7070

7171
NS_SWIFT_NAME(Leanplum.Constants)
7272
@interface LPConstantsState : NSObject {
73-
NSString *_apiHostName;
74-
NSString *_apiServlet;
75-
BOOL _apiSSL;
76-
NSString *_socketHost;
77-
int _socketPort;
7873
int _networkTimeoutSeconds;
7974
int _networkTimeoutSecondsForDownloads;
8075
int _syncNetworkTimeoutSeconds;
@@ -89,16 +84,11 @@ NS_SWIFT_NAME(Leanplum.Constants)
8984
int _userCodeBlocks;
9085
}
9186

92-
@property(strong, nonatomic) NSString *apiHostName;
93-
@property(strong, nonatomic) NSString *socketHost;
94-
@property(assign, nonatomic) int socketPort;
95-
@property(assign, nonatomic) BOOL apiSSL;
9687
@property(assign, nonatomic) int networkTimeoutSeconds;
9788
@property(assign, nonatomic) int networkTimeoutSecondsForDownloads;
9889
@property(assign, nonatomic) int syncNetworkTimeoutSeconds;
9990
@property(assign, nonatomic) BOOL isDevelopmentModeEnabled;
10091
@property(assign, nonatomic) BOOL loggingEnabled;
101-
@property(strong, nonatomic) NSString *apiServlet;
10292
@property(assign, nonatomic) BOOL isTestMode;
10393
@property(assign, nonatomic) BOOL isInPermanentFailureState;
10494
@property(strong, nonatomic) NSString *client;
@@ -189,6 +179,9 @@ OBJC_EXPORT NSString *LP_PARAM_INBOX_MESSAGE_ID;
189179
OBJC_EXPORT NSString *LP_PARAM_RICH_PUSH_ENABLED;
190180
OBJC_EXPORT NSString *LP_PARAM_UUID;
191181
OBJC_EXPORT NSString *LP_PARAM_CURRENCY_CODE;
182+
OBJC_EXPORT NSString *LP_PARAM_API_HOST;
183+
OBJC_EXPORT NSString *LP_PARAM_API_PATH;
184+
OBJC_EXPORT NSString *LP_PARAM_DEV_SERVER_HOST;
192185

193186
OBJC_EXPORT NSString *LP_KEY_VARS;
194187
OBJC_EXPORT NSString *LP_KEY_VARS_SIGNATURE;

LeanplumSDK/LeanplumSDK/Classes/Internal/LPConstants.m

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ + (LPConstantsState *)sharedState {
4040

4141
- (id)init {
4242
if (self = [super init]) {
43-
_apiHostName = @"api.leanplum.com";
44-
_apiServlet = @"api";
45-
_apiSSL = YES;
46-
_socketHost = @"dev.leanplum.com";
47-
_socketPort = 443;
4843
_networkTimeoutSeconds = 10;
4944
_networkTimeoutSecondsForDownloads = 15;
5045
_syncNetworkTimeoutSeconds = 5;
@@ -145,6 +140,9 @@ - (id)init {
145140
NSString *LP_PARAM_RICH_PUSH_ENABLED = @"richPushEnabled";
146141
NSString *LP_PARAM_UUID = @"uuid";
147142
NSString *LP_PARAM_CURRENCY_CODE = @"currencyCode";
143+
NSString *LP_PARAM_API_HOST = @"apiHost";
144+
NSString *LP_PARAM_API_PATH = @"apiPath";
145+
NSString *LP_PARAM_DEV_SERVER_HOST = @"devServerHost";
148146

149147
NSString *LP_KEY_REASON = @"reason";
150148
NSString *LP_KEY_STACK_TRACE = @"stackTrace";
@@ -216,9 +214,6 @@ - (id)init {
216214
NSString *LP_VALUE_SDK_COUNT = @"sdkCount";
217215
NSString *LP_VALUE_SDK_START_LATENCY = @"sdkStartLatency";
218216

219-
NSString *LP_KEYCHAIN_SERVICE_NAME = @"com.leanplum.storage";
220-
NSString *LP_KEYCHAIN_USERNAME = @"defaultUser";
221-
222217
NSString *LP_PATH_DOCUMENTS = @"Leanplum_Resources";
223218
NSString *LP_PATH_BUNDLE = @"Leanplum_Bundle";
224219
NSString *LP_SWIZZLING_ENABLED = @"LeanplumSwizzlingEnabled";

LeanplumSDK/LeanplumSDK/Classes/Internal/Leanplum.m

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
#import "LPRequestFactory.h"
4747
#import "LPFileTransferManager.h"
4848
#import "LPRequestSender.h"
49-
#import "LPAPIConfig.h"
5049
#import "LPOperationQueue.h"
5150
#import "LPDeferMessageManager.h"
5251
#include <sys/sysctl.h>
@@ -132,6 +131,16 @@ + (NotificationsManager*)notificationsManager
132131
return managerInstance;
133132
}
134133

134+
+ (User*)user
135+
{
136+
static User *userInstance = nil;
137+
static dispatch_once_t onceUserInstanceToken;
138+
dispatch_once(&onceUserInstanceToken, ^{
139+
userInstance = [User new];
140+
});
141+
return userInstance;
142+
}
143+
135144
+ (void)throwError:(NSString *)reason
136145
{
137146
if ([LPConstantsState sharedState].isDevelopmentModeEnabled) {
@@ -153,24 +162,24 @@ + (void)_initPush
153162
}
154163

155164
+ (void)setApiHostName:(NSString *)hostName
156-
withServletName:(NSString *)servletName
165+
withPath:(NSString *)apiPath
157166
usingSsl:(BOOL)ssl
158167
{
159168
if ([LPUtils isNullOrEmpty:hostName]) {
160-
[self throwError:@"[Leanplum setApiHostName:withServletName:usingSsl:] Empty hostname "
169+
[self throwError:@"[Leanplum setApiHostName:withPath:usingSsl:] Empty hostname "
161170
@"parameter provided."];
162171
return;
163172
}
164-
if ([LPUtils isNullOrEmpty:servletName]) {
165-
[self throwError:@"[Leanplum setApiHostName:withServletName:usingSsl:] Empty servletName "
173+
if ([LPUtils isNullOrEmpty:apiPath]) {
174+
[self throwError:@"[Leanplum setApiHostName:withPath:usingSsl:] Empty apiPath "
166175
@"parameter provided."];
167176
return;
168177
}
169178

170179
LP_TRY
171-
[LPConstantsState sharedState].apiHostName = hostName;
172-
[LPConstantsState sharedState].apiServlet = servletName;
173-
[LPConstantsState sharedState].apiSSL = ssl;
180+
[ApiConfig shared].apiHostName = hostName;
181+
[ApiConfig shared].apiPath = apiPath;
182+
[ApiConfig shared].apiSSL = ssl;
174183
LP_END_TRY
175184
}
176185

@@ -182,8 +191,12 @@ + (void)setSocketHostName:(NSString *)hostName withPortNumber:(int)port
182191
return;
183192
}
184193

185-
[LPConstantsState sharedState].socketHost = hostName;
186-
[LPConstantsState sharedState].socketPort = port;
194+
if (![[ApiConfig shared].socketHost isEqualToString:hostName] ||
195+
[ApiConfig shared].socketPort != port) {
196+
[ApiConfig shared].socketHost = hostName;
197+
[ApiConfig shared].socketPort = port;
198+
[[LeanplumSocket sharedSocket] connectToNewSocket];
199+
}
187200
}
188201

189202
+ (void)setClient:(NSString *)client withVersion:(NSString *)version
@@ -325,7 +338,7 @@ + (void)setAppId:(NSString *)appId withDevelopmentKey:(NSString *)accessKey
325338

326339
LP_TRY
327340
[LPConstantsState sharedState].isDevelopmentModeEnabled = YES;
328-
[[LPAPIConfig sharedConfig] setAppId:appId withAccessKey:accessKey];
341+
[[ApiConfig shared] setAppId:appId accessKey:accessKey];
329342
LP_END_TRY
330343
}
331344

@@ -350,7 +363,7 @@ + (void)setAppId:(NSString *)appId withProductionKey:(NSString *)accessKey
350363

351364
LP_TRY
352365
[LPConstantsState sharedState].isDevelopmentModeEnabled = NO;
353-
[[LPAPIConfig sharedConfig] setAppId:appId withAccessKey:accessKey];
366+
[[ApiConfig shared] setAppId:appId accessKey:accessKey];
354367
LP_END_TRY
355368
}
356369

@@ -377,10 +390,10 @@ + (void)setDeviceId:(NSString *)deviceId
377390
LP_TRY
378391
// If Leanplum start has been called already, changing the deviceId results in a new device
379392
// Ensure the id is updated and the new device has all attributes set
380-
if ([LPInternalState sharedState].hasStarted && ![[LPAPIConfig sharedConfig].deviceId isEqualToString:deviceId]) {
393+
if ([LPInternalState sharedState].hasStarted && ![[Leanplum user].deviceId isEqualToString:deviceId]) {
381394
[self setDeviceIdInternal:deviceId];
382395
} else {
383-
[[LPAPIConfig sharedConfig] setDeviceId:deviceId];
396+
[[Leanplum user] setDeviceId:deviceId];
384397
}
385398
LP_END_TRY
386399
}
@@ -398,7 +411,7 @@ +(void)setDeviceIdInternal:(NSString *)deviceId
398411
LP_PARAM_DEVICE_ID: deviceId
399412
} mutableCopy];
400413

401-
NSString *pushToken = [Leanplum notificationsManager].pushToken;
414+
NSString *pushToken = [Leanplum user].pushToken;
402415
if (pushToken) {
403416
params[LP_PARAM_DEVICE_PUSH_TOKEN] = pushToken;
404417
}
@@ -411,20 +424,19 @@ +(void)setDeviceIdInternal:(NSString *)deviceId
411424
}
412425

413426
//Clean UserDefaults before changing deviceId because it is used to generate key
414-
[Leanplum notificationsManager].pushToken = nil;
427+
[Leanplum user].pushToken = nil;
415428
[[Leanplum notificationsManager] removeNotificationSettings];
416429

417-
// Change the LPAPIConfig after getting the push token and settings
430+
// Change the User deviceId after getting the push token and settings
418431
// and after cleaning UserDefaults
419-
// The LPAPIConfig value is used in retrieving them
420-
[[LPAPIConfig sharedConfig] setDeviceId:deviceId];
432+
// The User userId and deviceId are used in retrieving them
433+
[[Leanplum user] setDeviceId:deviceId];
421434
[[LPVarCache sharedCache] saveDiffs];
422435

423436
// Update the token and settings now that the key is different
424-
[Leanplum notificationsManager].pushToken = pushToken;
437+
[Leanplum user].pushToken = pushToken;
425438
[[Leanplum notificationsManager] saveNotificationSettings:settings];
426439

427-
428440
LPRequest *request = [LPRequestFactory setDeviceAttributesWithParams:params];
429441
[[LPRequestSender sharedInstance] send:request];
430442
}];
@@ -783,7 +795,7 @@ + (void)startWithUserId:(NSString *)userId
783795
{
784796
[[Leanplum notificationsManager].proxy setupNotificationSwizzling];
785797

786-
if ([LPAPIConfig sharedConfig].appId == nil) {
798+
if ([ApiConfig shared].appId == nil) {
787799
[self throwError:@"Please provide your app ID using one of the [Leanplum setAppId:] "
788800
@"methods."];
789801
return;
@@ -852,7 +864,6 @@ + (void)startWithUserId:(NSString *)userId
852864
});
853865
state.actionManager = [LPActionManager sharedManager];
854866

855-
[[LPAPIConfig sharedConfig] loadToken];
856867
[[LPVarCache sharedCache] setSilent:YES];
857868
[[LPVarCache sharedCache] loadDiffs];
858869
[[LPVarCache sharedCache] setSilent:NO];
@@ -871,7 +882,7 @@ + (void)startWithUserId:(NSString *)userId
871882
}];
872883

873884
// Set device ID.
874-
NSString *deviceId = [LPAPIConfig sharedConfig].deviceId;
885+
NSString *deviceId = [Leanplum user].deviceId;
875886
// This is the device ID set when the MAC address is used on iOS 7.
876887
// This is to allow apps who upgrade to the new ID to forget the old one.
877888
if ([deviceId isEqualToString:@"0f607264fc6318a92b9e13c65db7cd3c"]) {
@@ -882,17 +893,17 @@ + (void)startWithUserId:(NSString *)userId
882893
if (!deviceId) {
883894
deviceId = [[UIDevice currentDevice] leanplum_uniqueGlobalDeviceIdentifier];
884895
}
885-
[[LPAPIConfig sharedConfig] setDeviceId:deviceId];
896+
[[Leanplum user] setDeviceId:deviceId];
886897
}
887898

888899
// Set user ID.
889900
if (!userId) {
890-
userId = [LPAPIConfig sharedConfig].userId;
901+
userId = [Leanplum user].userId;
891902
if (!userId) {
892-
userId = [LPAPIConfig sharedConfig].deviceId;
903+
userId = [Leanplum user].deviceId;
893904
}
894905
}
895-
[[LPAPIConfig sharedConfig] setUserId:userId];
906+
[[Leanplum user] setUserId:userId];
896907

897908
// Setup parameters.
898909
NSString *versionName = [self appVersion];
@@ -944,7 +955,7 @@ + (void)startWithUserId:(NSString *)userId
944955
params[LP_PARAM_INBOX_MESSAGES] = [self.inbox messagesIds];
945956

946957
// Push token.
947-
NSString *pushToken = [[Leanplum notificationsManager] pushToken];
958+
NSString *pushToken = [[Leanplum user] pushToken];
948959
if (pushToken) {
949960
params[LP_PARAM_DEVICE_PUSH_TOKEN] = pushToken;
950961
}
@@ -971,8 +982,9 @@ + (void)startWithUserId:(NSString *)userId
971982
NSString *varsJson = [LPJSON stringFromJSON:[response valueForKey:LP_KEY_VARS]];
972983
NSString *varsSignature = response[LP_KEY_VARS_SIGNATURE];
973984
NSArray *localCaps = response[LP_KEY_LOCAL_CAPS];
974-
[[LPAPIConfig sharedConfig] setToken:token];
975-
[[LPAPIConfig sharedConfig] saveToken];
985+
if (token) {
986+
[[ApiConfig shared] setToken:token];
987+
}
976988
[[LPVarCache sharedCache] applyVariableDiffs:values
977989
messages:messages
978990
variants:variants
@@ -1027,8 +1039,8 @@ + (void)startWithUserId:(NSString *)userId
10271039
[[LPVarCache sharedCache] setDevModeValuesFromServer:valuesFromCode
10281040
fileAttributes:fileAttributes
10291041
actionDefinitions:actionDefinitions];
1030-
[[LeanplumSocket sharedSocket] connectToAppId:[LPAPIConfig sharedConfig].appId
1031-
deviceId:[LPAPIConfig sharedConfig].deviceId];
1042+
[[LeanplumSocket sharedSocket] connectToAppId:[ApiConfig shared].appId
1043+
deviceId:[Leanplum user].deviceId];
10321044
if ([response[LP_KEY_IS_REGISTERED] boolValue]) {
10331045
[Leanplum onHasStartedAndRegisteredAsDeveloper];
10341046
}
@@ -2206,13 +2218,13 @@ + (void)setUserIdInternal:(NSString *)userId withAttributes:(NSDictionary *)attr
22062218

22072219
LPRequest *request = [LPRequestFactory setUserAttributesWithParams:@{
22082220
LP_PARAM_USER_ATTRIBUTES: attributes ? [LPJSON stringFromJSON:attributes] : @"",
2209-
LP_PARAM_USER_ID: [LPAPIConfig sharedConfig].userId ?: @"",
2221+
LP_PARAM_USER_ID: [Leanplum user].userId ?: @"",
22102222
LP_PARAM_NEW_USER_ID: userId ?: @""
22112223
}];
22122224
[[LPRequestSender sharedInstance] send:request];
22132225

22142226
if (userId.length) {
2215-
[[LPAPIConfig sharedConfig] setUserId:userId];
2227+
[[Leanplum user] setUserId:userId];
22162228
if ([LPInternalState sharedState].hasStarted) {
22172229
[[LPVarCache sharedCache] saveDiffs];
22182230
}
@@ -2648,7 +2660,7 @@ + (NSString *)deviceId
26482660
[self throwError:@"[Leanplum start] must be called before calling deviceId"];
26492661
return nil;
26502662
}
2651-
return [LPAPIConfig sharedConfig].deviceId;
2663+
return [Leanplum user].deviceId;
26522664
LP_END_TRY
26532665
return nil;
26542666
}
@@ -2660,7 +2672,7 @@ + (NSString *)userId
26602672
[self throwError:@"[Leanplum start] must be called before calling userId"];
26612673
return nil;
26622674
}
2663-
return [LPAPIConfig sharedConfig].userId;
2675+
return [Leanplum user].userId;
26642676
LP_END_TRY
26652677
return nil;
26662678
}

LeanplumSDK/LeanplumSDK/Classes/Internal/LeanplumInternal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ NS_ASSUME_NONNULL_BEGIN
3636
@class LeanplumSocket;
3737
@class LPRegisterDevice;
3838
@class NotificationsManager;
39+
@class User;
3940

4041
/**
4142
* Keys for the plist file name
@@ -57,6 +58,8 @@ typedef void (^LeanplumStartIssuedBlock)(void);
5758
typedef void (^LeanplumEventsChangedBlock)(void);
5859
typedef void (^LeanplumHandledBlock)(BOOL success);
5960

61+
@property (class, readonly) User* user;
62+
6063
+ (NotificationsManager*)notificationsManager;
6164

6265
+ (void)throwError:(NSString *)reason;

0 commit comments

Comments
 (0)