Skip to content

Commit

Permalink
feat: Direct routing to correct pod
Browse files Browse the repository at this point in the history
  • Loading branch information
einsteinx2 committed Sep 26, 2023
1 parent 63fc562 commit 50685bf
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 18 deletions.
17 changes: 12 additions & 5 deletions UnitTests/MPNetworkCommunication+Tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

@class MPURL;

extern NSString * _Nonnull const kMPURLHostEventSubdomain;
extern NSString * _Nonnull const kMPURLHostIdentitySubdomain;

@interface MPNetworkCommunication(Tests)

- (MPURL *)configURL;
- (MPURL *)eventURL;
- (MPURL *)aliasURL;
- (MPURL *)modifyURL;
- (MPURL *)identifyURL;
- (nonnull NSString *)defaultHostWithSubdomain:(nonnull NSString *)subdomain apiKey:(nonnull NSString *)apiKey enableDirectRouting:(BOOL)enableDirectRouting;
- (nonnull NSString *)defaultEventHost;
- (nonnull NSString *)defaultIdentityHost;

- (nonnull MPURL *)configURL;
- (nonnull MPURL *)eventURL;
- (nonnull MPURL *)aliasURL;
- (nonnull MPURL *)modifyURL;
- (nonnull MPURL *)identifyURL;

@end
27 changes: 27 additions & 0 deletions UnitTests/MPNetworkCommunicationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -838,4 +838,31 @@ - (void)testMaxAgeForCacheCapitalization {
XCTAssertEqualObjects([networkCommunication maxAgeForCache:test5], @16);
}

- (void)testPodURLRouting {
// NOTE: All keys are fake and randomly generated just for this test
NSArray *testKeys = @[
@[@"4u8wmsug0pf5tbf58lgjiouma3qukrgbu", @"nativesdks.us1.mparticle.com", @"identity.us1.mparticle.com"],
@[@"us1-1vc4gbp24cdtx6e31s58icnymzy83f1uf", @"nativesdks.us1.mparticle.com", @"identity.us1.mparticle.com"],
@[@"us2-v2p8lr3w2g90vtpaumbq21zy05cl50qm3", @"nativesdks.us2.mparticle.com", @"identity.us2.mparticle.com"],
@[@"eu1-bkabfno0b8zpv5bwi2zm2mfa1kfml19al", @"nativesdks.eu1.mparticle.com", @"identity.eu1.mparticle.com"],
@[@"au1-iermuj83dbeoshm0n32f10feotclq6i4a", @"nativesdks.au1.mparticle.com", @"identity.au1.mparticle.com"],
@[@"st1-k77ivhkbbqf4ce0s3y12zpcthyn1ixfyu", @"nativesdks.st1.mparticle.com", @"identity.st1.mparticle.com"],
@[@"us3-w1y2y8yj8q58d5bx9u2dvtxzl4cpa7cuf", @"nativesdks.us3.mparticle.com", @"identity.us3.mparticle.com"]
];
NSString *oldEventHost = @"nativesdks.mparticle.com";
NSString *oldIdentityHost = @"identity.mparticle.com";

MPNetworkCommunication *networkCommunication = [[MPNetworkCommunication alloc] init];
for (NSArray *test in testKeys) {
NSString *key = test[0];
NSString *eventHost = test[1];
NSString *identityHost = test[2];

XCTAssertEqualObjects(eventHost, [networkCommunication defaultHostWithSubdomain:kMPURLHostEventSubdomain apiKey:key enableDirectRouting:YES]);
XCTAssertEqualObjects(identityHost, [networkCommunication defaultHostWithSubdomain:kMPURLHostIdentitySubdomain apiKey:key enableDirectRouting:YES]);
XCTAssertEqualObjects(oldEventHost, [networkCommunication defaultHostWithSubdomain:kMPURLHostEventSubdomain apiKey:key enableDirectRouting:NO]);
XCTAssertEqualObjects(oldIdentityHost, [networkCommunication defaultHostWithSubdomain:kMPURLHostIdentitySubdomain apiKey:key enableDirectRouting:NO]);
}
}

@end
2 changes: 0 additions & 2 deletions mParticle-Apple-SDK/Network/MPNetworkCommunication.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@


extern NSString * _Nonnull const kMPURLScheme;
extern NSString * _Nonnull const kMPURLHost;
extern NSString * _Nonnull const kMPURLHostConfig;

typedef NS_ENUM(NSInteger, MPNetworkError) {
MPNetworkErrorTimeout = 1,
Expand Down
47 changes: 36 additions & 11 deletions mParticle-Apple-SDK/Network/MPNetworkCommunication.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
NSString *const kMPIdentityKey = @"identity";

NSString *const kMPURLScheme = @"https";
NSString *const kMPURLHost = @"nativesdks.mparticle.com";
NSString *const kMPURLHostConfig = @"config2.mparticle.com";
NSString *const kMPURLHostIdentity = @"identity.mparticle.com";
NSString *const kMPURLHostEventSubdomain = @"nativesdks";
NSString *const kMPURLHostIdentitySubdomain = @"identity";

static NSObject<MPConnectorFactoryProtocol> *factory = nil;

Expand Down Expand Up @@ -114,6 +114,31 @@ - (instancetype)init {
}

#pragma mark Private accessors

- (NSString *)defaultHostWithSubdomain:(NSString *)subdomain apiKey:(NSString *)apiKey enableDirectRouting:(BOOL)enableDirectRouting {
if (enableDirectRouting) {
NSArray *splitKey = [apiKey componentsSeparatedByString:@"-"];
if (splitKey.count <= 1) {
// Handle case with no prefix, default to US1 (old keys)
return [NSString stringWithFormat:@"%@.us1.mparticle.com", subdomain];
}
return [NSString stringWithFormat:@"%@.%@.mparticle.com", subdomain, splitKey[0]];
}

// Handle feature flag disabled (old behavior)
return [NSString stringWithFormat:@"%@.mparticle.com", subdomain];
}

- (NSString *)defaultEventHost {
MPStateMachine *stateMachine = [MParticle sharedInstance].stateMachine;
return [self defaultHostWithSubdomain:kMPURLHostEventSubdomain apiKey:stateMachine.apiKey enableDirectRouting:stateMachine.enableDirectRouting];
}

- (NSString *)defaultIdentityHost {
MPStateMachine *stateMachine = [MParticle sharedInstance].stateMachine;
return [self defaultHostWithSubdomain:kMPURLHostIdentitySubdomain apiKey:stateMachine.apiKey enableDirectRouting:stateMachine.enableDirectRouting];
}

- (MPURL *)configURL {
if (_configURL) {
return _configURL;
Expand Down Expand Up @@ -165,8 +190,8 @@ - (MPURL *)eventURL {
}

MPStateMachine *stateMachine = [MParticle sharedInstance].stateMachine;
NSString *eventHost = [MParticle sharedInstance].networkOptions.eventsHost ?: kMPURLHost;
NSString *urlString = [NSString stringWithFormat:urlFormat, kMPURLScheme, kMPURLHost, kMPEventsVersion, stateMachine.apiKey, kMPEventsURL];
NSString *eventHost = [MParticle sharedInstance].networkOptions.eventsHost ?: self.defaultEventHost;
NSString *urlString = [NSString stringWithFormat:urlFormat, kMPURLScheme, self.defaultEventHost, kMPEventsVersion, stateMachine.apiKey, kMPEventsURL];
NSURL *defaultURL = [NSURL URLWithString:urlString];

if ([MParticle sharedInstance].networkOptions.overridesEventsSubdirectory) {
Expand Down Expand Up @@ -213,8 +238,8 @@ - (MPURL *)logoutURL {
}

- (MPURL *)identityURL:(NSString *)pathComponent {
NSString *identityHost = [MParticle sharedInstance].networkOptions.identityHost ?: kMPURLHostIdentity;
NSString *urlString = [NSString stringWithFormat:identityURLFormat, kMPURLScheme, kMPURLHostIdentity, kMPIdentityVersion, pathComponent];
NSString *identityHost = [MParticle sharedInstance].networkOptions.identityHost ?: self.defaultIdentityHost;
NSString *urlString = [NSString stringWithFormat:identityURLFormat, kMPURLScheme, self.defaultIdentityHost, kMPIdentityVersion, pathComponent];
NSURL *defaultURL = [NSURL URLWithString:urlString];

if ([MParticle sharedInstance].networkOptions.overridesIdentitySubdirectory) {
Expand All @@ -237,8 +262,8 @@ - (MPURL *)identityURL:(NSString *)pathComponent {

- (MPURL *)modifyURL {
NSString *pathComponent = @"modify";
NSString *identityHost = [MParticle sharedInstance].networkOptions.identityHost ?: kMPURLHostIdentity;
NSString *urlString = [NSString stringWithFormat:modifyURLFormat, kMPURLScheme, kMPURLHostIdentity, kMPIdentityVersion, [MPPersistenceController mpId], pathComponent];
NSString *identityHost = [MParticle sharedInstance].networkOptions.identityHost ?: self.defaultIdentityHost;
NSString *urlString = [NSString stringWithFormat:modifyURLFormat, kMPURLScheme, self.defaultIdentityHost, kMPIdentityVersion, [MPPersistenceController mpId], pathComponent];
NSURL *defaultURL = [NSURL URLWithString:urlString];

if ([MParticle sharedInstance].networkOptions.overridesIdentitySubdirectory) {
Expand Down Expand Up @@ -267,13 +292,13 @@ - (MPURL *)aliasURL {
NSString *pathComponent = @"alias";
MPStateMachine *stateMachine = [MParticle sharedInstance].stateMachine;

NSString *eventHost = [MParticle sharedInstance].networkOptions.aliasHost ?: kMPURLHost;
NSString *urlString = [NSString stringWithFormat:aliasURLFormat, kMPURLScheme, kMPURLHost, kMPIdentityVersion, kMPIdentityKey, stateMachine.apiKey, pathComponent];
NSString *eventHost = [MParticle sharedInstance].networkOptions.aliasHost ?: self.defaultEventHost;
NSString *urlString = [NSString stringWithFormat:aliasURLFormat, kMPURLScheme, self.defaultEventHost, kMPIdentityVersion, kMPIdentityKey, stateMachine.apiKey, pathComponent];
NSURL *defaultURL = [NSURL URLWithString:urlString];

BOOL overrides = [MParticle sharedInstance].networkOptions.overridesAliasSubdirectory;
if (![MParticle sharedInstance].networkOptions.eventsOnly && ![MParticle sharedInstance].networkOptions.aliasHost) {
eventHost = [MParticle sharedInstance].networkOptions.eventsHost ?: kMPURLHost;
eventHost = [MParticle sharedInstance].networkOptions.eventsHost ?: self.defaultEventHost;
overrides = [MParticle sharedInstance].networkOptions.overridesEventsSubdirectory;
}

Expand Down
1 change: 1 addition & 0 deletions mParticle-Apple-SDK/Utils/MPStateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
@property (nonatomic) BOOL automaticSessionTracking;
@property (nonatomic) BOOL allowASR;
@property (nonatomic, nullable) MPDataPlanOptions *dataPlanOptions;
@property (nonatomic) BOOL enableDirectRouting;

+ (MPEnvironment)environment;
+ (void)setEnvironment:(MPEnvironment)environment;
Expand Down

0 comments on commit 50685bf

Please sign in to comment.