Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-2118] Added a method to set API URL #1350

Merged
merged 4 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Branch-TestBed/Branch-SDK-Tests/BNCAPIServerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "BNCSystemObserver.h"
#import "BNCConfig.h"
#import "BranchConstants.h"
#import "Branch.h"

@interface BNCAPIServerTest : XCTestCase

Expand Down Expand Up @@ -370,4 +371,42 @@ - (void)testValidationServiceURL_EUTracking {
XCTAssertTrue([url hasPrefix:expectedUrlPrefix]);
}

- (void)testDefaultAPIURL {
BNCServerAPI *serverAPI = [BNCServerAPI new];
XCTAssertNil(serverAPI.customAPIURL);

NSString *storedUrl = [[BNCServerAPI sharedInstance] installServiceURL];
NSString *expectedUrl = [BNC_API_URL stringByAppendingString: @"/v1/install"];
XCTAssertEqualObjects(storedUrl, expectedUrl);
}

- (void)testSetAPIURL_Example {
NSString *url = @"https://www.example.com";
[Branch setAPIUrl:url];

NSString *storedUrl = [[BNCServerAPI sharedInstance] installServiceURL];
NSString *expectedUrl = [url stringByAppendingString: @"/v1/install"];
XCTAssertEqualObjects(storedUrl, expectedUrl);

[Branch setAPIUrl:BNC_API_URL];
}

- (void)testSetAPIURL_InvalidHttp {
NSString *url = @"Invalid://www.example.com";
[Branch setAPIUrl:url];

NSString *storedUrl = [[BNCServerAPI sharedInstance] installServiceURL];
NSString *expectedUrl = [BNC_API_URL stringByAppendingString: @"/v1/install"];
XCTAssertEqualObjects(storedUrl, expectedUrl);
}

- (void)testSetAPIURL_InvalidEmpty {
NSString *url = @"";
[Branch setAPIUrl:url];

NSString *storedUrl = [[BNCServerAPI sharedInstance] installServiceURL];
NSString *expectedUrl = [BNC_API_URL stringByAppendingString: @"/v1/install"];
XCTAssertEqualObjects(storedUrl, expectedUrl);
}

@end
28 changes: 0 additions & 28 deletions Branch-TestBed/Branch-SDK-Tests/BNCPreferenceHelperTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -199,34 +199,6 @@ - (void)testURLSkipList {
XCTAssert([filterDesc isEqualToString:valueDesc]);
}

- (void)testSetAPIURL_Example {

NSString *url = @"https://www.example.com/";
[self.prefHelper setBranchAPIURL:url] ;

NSString *urlStored = self.prefHelper.branchAPIURL ;
XCTAssert([url isEqualToString:urlStored]);
}

- (void)testSetAPIURL_InvalidHttp {

NSString *url = @"Invalid://www.example.com/";
[self.prefHelper setBranchAPIURL:url] ;

NSString *urlStored = self.prefHelper.branchAPIURL ;
XCTAssert(![url isEqualToString:urlStored]);
XCTAssert([urlStored isEqualToString:BNC_API_URL]);
}

- (void)testSetAPIURL_InvalidEmpty {

[self.prefHelper setBranchAPIURL:@""] ;

NSString *urlStored = self.prefHelper.branchAPIURL ;
XCTAssert(![urlStored isEqualToString:@""]);
XCTAssert([urlStored isEqualToString:BNC_API_URL]);
}

- (void)testSetCDNBaseURL_Example {

NSString *url = @"https://www.example.com/";
Expand Down
5 changes: 4 additions & 1 deletion Branch-TestBed/Branch-TestBed/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@

// Branch.useTestBranchKey = YES; // Make sure to comment this line out for production apps!!!
Branch *branch = [Branch getInstance];


// Change the Branch base API URL
//[Branch setAPIUrl:@"https://api3.branch.io"];

// test pre init support
//[self testDispatchToIsolationQueue:branch]

Expand Down Expand Up @@ -137,7 +140,7 @@
}
}

- (BOOL)application:(UIApplication *)application

Check warning on line 143 in Branch-TestBed/Branch-TestBed/AppDelegate.m

View workflow job for this annotation

GitHub Actions / verify

implementing deprecated method [-Wdeprecated-implementations]

Check warning on line 143 in Branch-TestBed/Branch-TestBed/AppDelegate.m

View workflow job for this annotation

GitHub Actions / verify

implementing deprecated method [-Wdeprecated-implementations]
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
Expand Down
2 changes: 0 additions & 2 deletions BranchSDK/BNCPreferenceHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ NSURL* /* _Nonnull */ BNCURLForBranchDirectory(void);
@property (copy, nonatomic) NSString *lastSystemBuildVersion;
@property (copy, nonatomic) NSString *browserUserAgentString;
@property (copy, nonatomic) NSString *referringURL;
@property (copy, nonatomic) NSString *branchAPIURL;
@property (assign, nonatomic) BOOL limitFacebookTracking;
@property (strong, nonatomic) NSDate *previousAppBuildDate;
@property (assign, nonatomic, readwrite) BOOL disableAdNetworkCallouts;
Expand All @@ -77,7 +76,6 @@ NSURL* /* _Nonnull */ BNCURLForBranchDirectory(void);

+ (BNCPreferenceHelper *)sharedInstance;

- (void)setBranchAPIURL:(NSString *)url;
- (void)setPatternListURL:(NSString *)url;

- (void)setRequestMetadataKey:(NSString *)key value:(NSObject *)value;
Expand Down
30 changes: 0 additions & 30 deletions BranchSDK/BNCPreferenceHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
static NSString * const BRANCH_PREFS_KEY_LAST_RUN_BRANCH_KEY = @"bnc_last_run_branch_key";
static NSString * const BRANCH_PREFS_KEY_LAST_STRONG_MATCH_DATE = @"bnc_strong_match_created_date";

static NSString * const BRANCH_PREFS_KEY_API_URL = @"bnc_api_url";
static NSString * const BRANCH_PREFS_KEY_PATTERN_LIST_URL = @"bnc_pattern_list_url";

static NSString * const BRANCH_PREFS_KEY_RANDOMIZED_DEVICE_TOKEN = @"bnc_randomized_device_token";
Expand Down Expand Up @@ -68,7 +67,6 @@ @interface BNCPreferenceHelper () {
NSOperationQueue *_persistPrefsQueue;
NSString *_lastSystemBuildVersion;
NSString *_browserUserAgentString;
NSString *_branchAPIURL;
NSString *_referringURL;
}

Expand Down Expand Up @@ -156,34 +154,6 @@ - (void) dealloc {

#pragma mark - API methods

- (void)setBranchAPIURL:(NSString *)url {
if ([url hasPrefix:@"http://"] || [url hasPrefix:@"https://"] ){
@synchronized (self) {
_branchAPIURL = [url copy];
[self writeObjectToDefaults:BRANCH_PREFS_KEY_API_URL value:_branchAPIURL];
}
} else {
BNCLogWarning(@"Ignoring invalid custom API URL");
}
}

// TODO: This method is not used with the Tracking domain change. See SDK-2118
- (NSString *)branchAPIURL {
@synchronized (self) {
if (!_branchAPIURL) {
_branchAPIURL = [self readStringFromDefaults:BRANCH_PREFS_KEY_API_URL];
}

// return the default URL in the event there's nothing in storage
if (_branchAPIURL == nil || [_branchAPIURL isEqualToString:@""]) {
_branchAPIURL = [BNC_API_URL copy];
[self writeObjectToDefaults:BRANCH_PREFS_KEY_API_URL value:_branchAPIURL];
}

return _branchAPIURL;
}
}

- (void)setPatternListURL:(NSString *)url {
if ([url hasPrefix:@"http://"] || [url hasPrefix:@"https://"] ){
@synchronized (self) {
Expand Down
2 changes: 2 additions & 0 deletions BranchSDK/BNCServerAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ NS_ASSUME_NONNULL_BEGIN
// Used to enable unit tests without regard for ATT authorization status
@property (nonatomic, assign, readwrite) BOOL automaticallyEnableTrackingDomain;

@property (nonatomic, copy, readwrite, nullable) NSString *customAPIURL;

@end

NS_ASSUME_NONNULL_END
Expand Down
11 changes: 11 additions & 0 deletions BranchSDK/BNCServerAPI.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "BNCSystemObserver.h"
#import "BNCConfig.h"
#import "BranchConstants.h"
#import "BNCPreferenceHelper.h"

@implementation BNCServerAPI

Expand All @@ -29,6 +30,7 @@ - (instancetype)init {
self.useTrackingDomain = NO;
self.useEUServers = NO;
self.automaticallyEnableTrackingDomain = YES;
self.customAPIURL = nil;
}
return self;
}
Expand Down Expand Up @@ -77,6 +79,10 @@ - (BOOL)optedIntoIDFA {

// Linking endpoints are not used for Ads tracking
- (NSString *)getBaseURLForLinkingEndpoints {
if (self.customAPIURL) {
return self.customAPIURL;
}

NSString * urlString;
if (self.useEUServers){
urlString = BNC_EU_API_URL;
Expand All @@ -88,6 +94,11 @@ - (NSString *)getBaseURLForLinkingEndpoints {
}

- (NSString *)getBaseURL {
//Check if user has set a custom API base URL
if (self.customAPIURL) {
return self.customAPIURL;
}

if (self.automaticallyEnableTrackingDomain) {
self.useTrackingDomain = [self optedIntoIDFA];
}
Expand Down
6 changes: 6 additions & 0 deletions BranchSDK/Branch.h
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,12 @@ extern NSString * __nonnull const BNCSpotlightFeature;
*/
- (void)useEUEndpoints;

/**
Sets a custom base URL for all calls to the Branch API.
@param url Base URL that the Branch API will use.
*/
+ (void)setAPIUrl:(NSString *)url;

/**
setDebug is deprecated and all functionality has been disabled.

Expand Down
8 changes: 8 additions & 0 deletions BranchSDK/Branch.m
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,14 @@ - (void)useEUEndpoints {
[BNCServerAPI sharedInstance].useEUServers = YES;
}

+ (void)setAPIUrl:(NSString *)url {
if ([url hasPrefix:@"http://"] || [url hasPrefix:@"https://"] ){
[BNCServerAPI sharedInstance].customAPIURL = url;
} else {
BNCLogWarning(@"Ignoring invalid custom API URL");
}
}

- (void)setDebug {
NSLog(@"Branch setDebug is deprecated and all functionality has been disabled. "
"If you wish to enable logging, please invoke enableLogging. "
Expand Down
6 changes: 0 additions & 6 deletions BranchSDK/BranchPluginSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ NS_ASSUME_NONNULL_BEGIN

+ (BranchPluginSupport *)instance;

/**
Sets a custom base URL for all calls to the Branch API.
@param url Base URL that the Branch API will use.
*/
+ (void)setAPIUrl:(NSString *)url;

/**
Sets a custom CDN base URL.
@param url Base URL for CDN endpoints.
Expand Down
6 changes: 0 additions & 6 deletions BranchSDK/BranchPluginSupport.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ + (BranchPluginSupport *)instance {

#pragma mark - Server URL methods

// With the change to support Apple's tracking domain feature, this API no longer works. See SDK-2118
// Overrides base API URL
+ (void)setAPIUrl:(NSString *)url {
[[BNCPreferenceHelper sharedInstance] setBranchAPIURL:url];
}

// Overrides base CDN URL
+ (void)setCDNBaseUrl:(NSString *)url {
[[BNCPreferenceHelper sharedInstance] setPatternListURL:url];
Expand Down
Loading