Skip to content

Commit b12efd1

Browse files
committed
Merge pull request #729 from ParsePlatform/nlutsenko.server
Add ability to get/change server url used by the SDK.
2 parents 40a9a2c + 8b1d1ba commit b12efd1

9 files changed

+68
-19
lines changed

Parse/Internal/PFInternalUtils.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#import "PFMultiProcessFileLockController.h"
3535
#import "PFHash.h"
3636
#import "Parse_Private.h"
37+
#import "ParseClientConfiguration_Private.h"
3738

3839
#if TARGET_OS_IOS
3940
#import "PFProduct.h"

Parse/Internal/ParseClientConfiguration_Private.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111

1212
NS_ASSUME_NONNULL_BEGIN
1313

14+
extern NSString *const _ParseDefaultServerURLString;
15+
1416
@interface ParseClientConfiguration ()
1517

1618
@property (nullable, nonatomic, copy, readwrite) NSString *applicationId;
1719
@property (nullable, nonatomic, copy, readwrite) NSString *clientKey;
1820

21+
@property (nonatomic, copy, readwrite) NSString *server;
22+
1923
@property (nonatomic, assign, readwrite, getter=isLocalDatastoreEnabled) BOOL localDatastoreEnabled;
2024

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

Parse/Internal/Parse_Private.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
#import "ParseManager.h"
1515

16-
extern NSString *const _ParseDefaultServerURLString;
17-
1816
@class PFEventuallyQueue;
1917

2018
@interface Parse ()

Parse/Parse.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333

3434
#import "PFCategoryLoader.h"
3535

36-
NSString *const _ParseDefaultServerURLString = @"https://api.parse.com/1";
37-
3836
@implementation Parse
3937

4038
static ParseManager *currentParseManager_;
@@ -58,6 +56,7 @@ + (void)initialize {
5856
+ (void)setApplicationId:(NSString *)applicationId clientKey:(NSString *)clientKey {
5957
currentParseConfiguration_.applicationId = applicationId;
6058
currentParseConfiguration_.clientKey = clientKey;
59+
currentParseConfiguration_.server = [PFInternalUtils parseServerURLString]; // TODO: (nlutsenko) Clean this up after tests are updated.
6160

6261
[self initializeWithConfiguration:currentParseConfiguration_];
6362

@@ -77,7 +76,7 @@ + (void)initializeWithConfiguration:(ParseClientConfiguration *)configuration {
7776
@"'containingApplicationBundleIdentifier' must be non-nil in extension environment");
7877

7978
ParseManager *manager = [[ParseManager alloc] initWithConfiguration:configuration
80-
serverURL:[NSURL URLWithString:[PFInternalUtils parseServerURLString]]];
79+
serverURL:[NSURL URLWithString:configuration.server]];
8180
[manager startManaging];
8281

8382
currentParseManager_ = manager;

Parse/ParseClientConfiguration.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ NS_ASSUME_NONNULL_BEGIN
4141
*/
4242
@property (nullable, nonatomic, copy) NSString *clientKey;
4343

44+
/**
45+
The URL of the server that is being used by the SDK.
46+
Defaults to `https://api.parse.com/1`.
47+
48+
@note Setting this property to a non-valid URL or `nil` will throw an `NSInvalidArgumentException`.
49+
*/
50+
@property (nonatomic, copy) NSString *server;
51+
4452
///--------------------------------------
4553
#pragma mark - Enabling Local Datastore
4654
///--------------------------------------
@@ -106,6 +114,12 @@ NS_ASSUME_NONNULL_BEGIN
106114
*/
107115
@property (nullable, nonatomic, copy, readonly) NSString *clientKey;
108116

117+
/**
118+
The URL of the server that is being used by the SDK.
119+
Defaults to `https://api.parse.com/1`
120+
*/
121+
@property (nonatomic, copy, readonly) NSString *server;
122+
109123
///--------------------------------------
110124
#pragma mark - Enabling Local Datastore
111125
///--------------------------------------

Parse/ParseClientConfiguration.m

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#import "PFHash.h"
1818
#import "PFObjectUtilities.h"
1919

20+
NSString *const _ParseDefaultServerURLString = @"https://api.parse.com/1";
21+
2022
@implementation ParseClientConfiguration
2123

2224
///--------------------------------------
@@ -32,6 +34,7 @@ - (instancetype)initEmpty {
3234
if (!self) return nil;
3335

3436
_networkRetryAttempts = PFCommandRunningDefaultMaxAttemptsCount;
37+
_server = [_ParseDefaultServerURLString copy];
3538

3639
return self;
3740
}
@@ -57,28 +60,34 @@ + (instancetype)configurationWithBlock:(void (^)(id<ParseMutableClientConfigurat
5760
///--------------------------------------
5861

5962
- (void)setApplicationId:(NSString *)applicationId {
60-
PFConsistencyAssert(applicationId.length, @"'applicationId' should not be nil.");
63+
PFParameterAssert(applicationId.length, @"'applicationId' should not be nil.");
6164
_applicationId = [applicationId copy];
6265
}
6366

6467
- (void)setClientKey:(NSString *)clientKey {
65-
PFConsistencyAssert(clientKey.length, @"'clientKey' should not be nil.");
68+
PFParameterAssert(clientKey.length, @"'clientKey' should not be nil.");
6669
_clientKey = [clientKey copy];
6770
}
6871

72+
- (void)setServer:(NSString *)server {
73+
PFParameterAssert(server.length, @"Server should not be `nil`.");
74+
PFParameterAssert([NSURL URLWithString:server], @"Server should be a valid URL.");
75+
_server = [server copy];
76+
}
77+
6978
- (void)setApplicationGroupIdentifier:(NSString *)applicationGroupIdentifier {
70-
PFConsistencyAssert(applicationGroupIdentifier == nil ||
71-
[PFFileManager isApplicationGroupContainerReachableForGroupIdentifier:applicationGroupIdentifier],
72-
@"ApplicationGroupContainer is unreachable. Please double check your Xcode project settings.");
79+
PFParameterAssert(applicationGroupIdentifier == nil ||
80+
[PFFileManager isApplicationGroupContainerReachableForGroupIdentifier:applicationGroupIdentifier],
81+
@"ApplicationGroupContainer is unreachable. Please double check your Xcode project settings.");
7382

7483
_applicationGroupIdentifier = [applicationGroupIdentifier copy];
7584
}
7685

7786
- (void)setContainingApplicationBundleIdentifier:(NSString *)containingApplicationBundleIdentifier {
78-
PFConsistencyAssert([PFApplication currentApplication].extensionEnvironment,
79-
@"'containingApplicationBundleIdentifier' cannot be set in non-extension environment");
80-
PFConsistencyAssert(containingApplicationBundleIdentifier.length,
81-
@"'containingApplicationBundleIdentifier' should not be nil.");
87+
PFParameterAssert([PFApplication currentApplication].extensionEnvironment,
88+
@"'containingApplicationBundleIdentifier' cannot be set in non-extension environment");
89+
PFParameterAssert(containingApplicationBundleIdentifier.length,
90+
@"'containingApplicationBundleIdentifier' should not be nil.");
8291

8392
_containingApplicationBundleIdentifier = containingApplicationBundleIdentifier;
8493
}
@@ -104,6 +113,7 @@ - (BOOL)isEqual:(id)object {
104113
ParseClientConfiguration *other = object;
105114
return ([PFObjectUtilities isObject:self.applicationId equalToObject:other.applicationId] &&
106115
[PFObjectUtilities isObject:self.clientKey equalToObject:other.clientKey] &&
116+
[self.server isEqualToString:other.server] &&
107117
self.localDatastoreEnabled == other.localDatastoreEnabled &&
108118
[PFObjectUtilities isObject:self.applicationGroupIdentifier equalToObject:other.applicationGroupIdentifier] &&
109119
[PFObjectUtilities isObject:self.containingApplicationBundleIdentifier equalToObject:other.containingApplicationBundleIdentifier] &&
@@ -119,6 +129,7 @@ - (instancetype)copyWithZone:(NSZone *)zone {
119129
// Use direct assignment to skip over all of the assertions that may fail if we're not fully initialized yet.
120130
configuration->_applicationId = [self->_applicationId copy];
121131
configuration->_clientKey = [self->_clientKey copy];
132+
configuration->_server = [self.server copy];
122133
configuration->_localDatastoreEnabled = self->_localDatastoreEnabled;
123134
configuration->_applicationGroupIdentifier = [self->_applicationGroupIdentifier copy];
124135
configuration->_containingApplicationBundleIdentifier = [self->_containingApplicationBundleIdentifier copy];

Tests/Unit/CommandUnitTests.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#import "PFURLSessionCommandRunner.h"
1616
#import "PFUnitTestCase.h"
1717
#import "Parse_Private.h"
18+
#import "ParseClientConfiguration_Private.h"
1819

1920
@interface CommandUnitTests : PFUnitTestCase
2021

Tests/Unit/ParseClientConfigurationTests.m

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,21 @@ - (void)testConfigurationWithBlock {
3737
ParseClientConfiguration *configuration = [ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) {
3838
configuration.applicationId = @"foo";
3939
configuration.clientKey = @"bar";
40+
configuration.server = @"http://localhost";
4041
configuration.localDatastoreEnabled = YES;
4142
configuration.networkRetryAttempts = 1337;
4243
}];
4344

4445
XCTAssertEqualObjects(configuration.applicationId, @"foo");
4546
XCTAssertEqualObjects(configuration.clientKey, @"bar");
47+
XCTAssertEqualObjects(configuration.server, @"http://localhost");
4648
XCTAssertTrue(configuration.localDatastoreEnabled);
4749
XCTAssertEqual(configuration.networkRetryAttempts, 1337);
4850
}
4951

5052
- (void)testEqual {
51-
ParseClientConfiguration *configurationA = [(id)[ParseClientConfiguration alloc] init];
52-
ParseClientConfiguration *configurationB = [(id)[ParseClientConfiguration alloc] init];
53+
ParseClientConfiguration *configurationA = [ParseClientConfiguration emptyConfiguration];
54+
ParseClientConfiguration *configurationB = [ParseClientConfiguration emptyConfiguration];
5355
XCTAssertEqualObjects(configurationA, configurationB);
5456
XCTAssertEqual(configurationA.hash, configurationB.hash);
5557

@@ -67,6 +69,13 @@ - (void)testEqual {
6769
XCTAssertNotEqualObjects(configurationA, configurationB);
6870
configurationB.clientKey = configurationA.clientKey;
6971

72+
configurationA.server = configurationB.server = @"http://localhost";
73+
XCTAssertEqualObjects(configurationA, configurationB);
74+
XCTAssertEqual(configurationA.hash, configurationB.hash);
75+
configurationB.server = @"http://api.parse.com";
76+
XCTAssertNotEqualObjects(configurationA, configurationB);
77+
configurationB.server = configurationA.server;
78+
7079
configurationA.localDatastoreEnabled = configurationB.localDatastoreEnabled = YES;
7180
XCTAssertEqualObjects(configurationA, configurationB);
7281
XCTAssertEqual(configurationA.hash, configurationB.hash);
@@ -85,6 +94,7 @@ - (void)testCopy {
8594
ParseClientConfiguration *configurationA = [ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) {
8695
configuration.applicationId = @"foo";
8796
configuration.clientKey = @"bar";
97+
configuration.server = @"http://localhost";
8898
configuration.localDatastoreEnabled = YES;
8999
configuration.networkRetryAttempts = 1337;
90100
}];
@@ -100,6 +110,7 @@ - (void)testCopy {
100110

101111
XCTAssertEqualObjects(configurationB.applicationId, @"foo");
102112
XCTAssertEqualObjects(configurationB.clientKey, @"bar");
113+
XCTAssertEqualObjects(configurationB.server, @"http://localhost");
103114
XCTAssertTrue(configurationB.localDatastoreEnabled);
104115
XCTAssertEqual(configurationB.networkRetryAttempts, 1337);
105116
}
@@ -125,4 +136,14 @@ - (void)testExtensionDataSharing {
125136
XCTAssertNoThrow(configuration.containingApplicationBundleIdentifier = @"someContainer");
126137
}
127138

139+
- (void)testServerValidation {
140+
[ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> _Nonnull configuration) {
141+
configuration.applicationId = @"a";
142+
configuration.clientKey = @"b";
143+
144+
PFAssertThrowsInvalidArgumentException(configuration.server = @"");
145+
PFAssertThrowsInvalidArgumentException(configuration.server = @"Yolo Yarr");
146+
}];
147+
}
148+
128149
@end

Tests/Unit/ParseSetupUnitTests.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ - (void)testInitializeWithLDSAfterInitializeShouldThrowException {
4848

4949
- (void)testInitializeWithNilApplicationIdNilClientKeyShouldThrowException {
5050
NSString *yolo = nil;
51-
PFAssertThrowsInconsistencyException([Parse setApplicationId:yolo clientKey:yolo]);
52-
PFAssertThrowsInconsistencyException([Parse setApplicationId:yolo clientKey:@"a"]);
53-
PFAssertThrowsInconsistencyException([Parse setApplicationId:@"a" clientKey:yolo]);
51+
PFAssertThrowsInvalidArgumentException([Parse setApplicationId:yolo clientKey:yolo]);
52+
PFAssertThrowsInvalidArgumentException([Parse setApplicationId:yolo clientKey:@"a"]);
53+
PFAssertThrowsInvalidArgumentException([Parse setApplicationId:@"a" clientKey:yolo]);
5454
}
5555

5656
@end

0 commit comments

Comments
 (0)