Skip to content

Add ability to get/change server url used by the SDK. #729

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

Merged
merged 2 commits into from
Jan 6, 2016
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
1 change: 1 addition & 0 deletions Parse/Internal/PFInternalUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#import "PFMultiProcessFileLockController.h"
#import "PFHash.h"
#import "Parse_Private.h"
#import "ParseClientConfiguration_Private.h"

#if TARGET_OS_IOS
#import "PFProduct.h"
Expand Down
4 changes: 4 additions & 0 deletions Parse/Internal/ParseClientConfiguration_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@

NS_ASSUME_NONNULL_BEGIN

extern NSString *const _ParseDefaultServerURLString;

@interface ParseClientConfiguration ()

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

@property (nonatomic, copy, readwrite) NSString *server;

@property (nonatomic, assign, readwrite, getter=isLocalDatastoreEnabled) BOOL localDatastoreEnabled;

@property (nullable, nonatomic, copy, readwrite) NSString *applicationGroupIdentifier;
Expand Down
2 changes: 0 additions & 2 deletions Parse/Internal/Parse_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

#import "ParseManager.h"

extern NSString *const _ParseDefaultServerURLString;

@class PFEventuallyQueue;

@interface Parse ()
Expand Down
5 changes: 2 additions & 3 deletions Parse/Parse.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@

#import "PFCategoryLoader.h"

NSString *const _ParseDefaultServerURLString = @"https://api.parse.com/1";

@implementation Parse

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

[self initializeWithConfiguration:currentParseConfiguration_];

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

ParseManager *manager = [[ParseManager alloc] initWithConfiguration:configuration
serverURL:[NSURL URLWithString:[PFInternalUtils parseServerURLString]]];
serverURL:[NSURL URLWithString:configuration.server]];
[manager startManaging];

currentParseManager_ = manager;
Expand Down
14 changes: 14 additions & 0 deletions Parse/ParseClientConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nullable, nonatomic, copy) NSString *clientKey;

/**
The URL of the server that is being used by the SDK.
Defaults to `https://api.parse.com/1`.

@note Setting this property to a non-valid URL or `nil` will throw an `NSInvalidArgumentException`.
*/
@property (nonatomic, copy) NSString *server;

///--------------------------------------
#pragma mark - Enabling Local Datastore
///--------------------------------------
Expand Down Expand Up @@ -106,6 +114,12 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nullable, nonatomic, copy, readonly) NSString *clientKey;

/**
The URL of the server that is being used by the SDK.
Defaults to `https://api.parse.com/1`
*/
@property (nonatomic, copy, readonly) NSString *server;

///--------------------------------------
#pragma mark - Enabling Local Datastore
///--------------------------------------
Expand Down
29 changes: 20 additions & 9 deletions Parse/ParseClientConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#import "PFHash.h"
#import "PFObjectUtilities.h"

NSString *const _ParseDefaultServerURLString = @"https://api.parse.com/1";

@implementation ParseClientConfiguration

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

_networkRetryAttempts = PFCommandRunningDefaultMaxAttemptsCount;
_server = [_ParseDefaultServerURLString copy];

return self;
}
Expand All @@ -57,28 +60,34 @@ + (instancetype)configurationWithBlock:(void (^)(id<ParseMutableClientConfigurat
///--------------------------------------

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

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

- (void)setServer:(NSString *)server {
PFParameterAssert(server.length, @"Server should not be `nil`.");
PFParameterAssert([NSURL URLWithString:server], @"Server should be a valid URL.");
_server = [server copy];
}

- (void)setApplicationGroupIdentifier:(NSString *)applicationGroupIdentifier {
PFConsistencyAssert(applicationGroupIdentifier == nil ||
[PFFileManager isApplicationGroupContainerReachableForGroupIdentifier:applicationGroupIdentifier],
@"ApplicationGroupContainer is unreachable. Please double check your Xcode project settings.");
PFParameterAssert(applicationGroupIdentifier == nil ||
[PFFileManager isApplicationGroupContainerReachableForGroupIdentifier:applicationGroupIdentifier],
@"ApplicationGroupContainer is unreachable. Please double check your Xcode project settings.");

_applicationGroupIdentifier = [applicationGroupIdentifier copy];
}

- (void)setContainingApplicationBundleIdentifier:(NSString *)containingApplicationBundleIdentifier {
PFConsistencyAssert([PFApplication currentApplication].extensionEnvironment,
@"'containingApplicationBundleIdentifier' cannot be set in non-extension environment");
PFConsistencyAssert(containingApplicationBundleIdentifier.length,
@"'containingApplicationBundleIdentifier' should not be nil.");
PFParameterAssert([PFApplication currentApplication].extensionEnvironment,
@"'containingApplicationBundleIdentifier' cannot be set in non-extension environment");
PFParameterAssert(containingApplicationBundleIdentifier.length,
@"'containingApplicationBundleIdentifier' should not be nil.");

_containingApplicationBundleIdentifier = containingApplicationBundleIdentifier;
}
Expand All @@ -104,6 +113,7 @@ - (BOOL)isEqual:(id)object {
ParseClientConfiguration *other = object;
return ([PFObjectUtilities isObject:self.applicationId equalToObject:other.applicationId] &&
[PFObjectUtilities isObject:self.clientKey equalToObject:other.clientKey] &&
[self.server isEqualToString:other.server] &&
self.localDatastoreEnabled == other.localDatastoreEnabled &&
[PFObjectUtilities isObject:self.applicationGroupIdentifier equalToObject:other.applicationGroupIdentifier] &&
[PFObjectUtilities isObject:self.containingApplicationBundleIdentifier equalToObject:other.containingApplicationBundleIdentifier] &&
Expand All @@ -119,6 +129,7 @@ - (instancetype)copyWithZone:(NSZone *)zone {
// Use direct assignment to skip over all of the assertions that may fail if we're not fully initialized yet.
configuration->_applicationId = [self->_applicationId copy];
configuration->_clientKey = [self->_clientKey copy];
configuration->_server = [self.server copy];
configuration->_localDatastoreEnabled = self->_localDatastoreEnabled;
configuration->_applicationGroupIdentifier = [self->_applicationGroupIdentifier copy];
configuration->_containingApplicationBundleIdentifier = [self->_containingApplicationBundleIdentifier copy];
Expand Down
1 change: 1 addition & 0 deletions Tests/Unit/CommandUnitTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#import "PFURLSessionCommandRunner.h"
#import "PFUnitTestCase.h"
#import "Parse_Private.h"
#import "ParseClientConfiguration_Private.h"

@interface CommandUnitTests : PFUnitTestCase

Expand Down
25 changes: 23 additions & 2 deletions Tests/Unit/ParseClientConfigurationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,21 @@ - (void)testConfigurationWithBlock {
ParseClientConfiguration *configuration = [ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) {
configuration.applicationId = @"foo";
configuration.clientKey = @"bar";
configuration.server = @"http://localhost";
configuration.localDatastoreEnabled = YES;
configuration.networkRetryAttempts = 1337;
}];

XCTAssertEqualObjects(configuration.applicationId, @"foo");
XCTAssertEqualObjects(configuration.clientKey, @"bar");
XCTAssertEqualObjects(configuration.server, @"http://localhost");
XCTAssertTrue(configuration.localDatastoreEnabled);
XCTAssertEqual(configuration.networkRetryAttempts, 1337);
}

- (void)testEqual {
ParseClientConfiguration *configurationA = [(id)[ParseClientConfiguration alloc] init];
ParseClientConfiguration *configurationB = [(id)[ParseClientConfiguration alloc] init];
ParseClientConfiguration *configurationA = [ParseClientConfiguration emptyConfiguration];
ParseClientConfiguration *configurationB = [ParseClientConfiguration emptyConfiguration];
XCTAssertEqualObjects(configurationA, configurationB);
XCTAssertEqual(configurationA.hash, configurationB.hash);

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

configurationA.server = configurationB.server = @"http://localhost";
XCTAssertEqualObjects(configurationA, configurationB);
XCTAssertEqual(configurationA.hash, configurationB.hash);
configurationB.server = @"http://api.parse.com";
XCTAssertNotEqualObjects(configurationA, configurationB);
configurationB.server = configurationA.server;

configurationA.localDatastoreEnabled = configurationB.localDatastoreEnabled = YES;
XCTAssertEqualObjects(configurationA, configurationB);
XCTAssertEqual(configurationA.hash, configurationB.hash);
Expand All @@ -85,6 +94,7 @@ - (void)testCopy {
ParseClientConfiguration *configurationA = [ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) {
configuration.applicationId = @"foo";
configuration.clientKey = @"bar";
configuration.server = @"http://localhost";
configuration.localDatastoreEnabled = YES;
configuration.networkRetryAttempts = 1337;
}];
Expand All @@ -100,6 +110,7 @@ - (void)testCopy {

XCTAssertEqualObjects(configurationB.applicationId, @"foo");
XCTAssertEqualObjects(configurationB.clientKey, @"bar");
XCTAssertEqualObjects(configurationB.server, @"http://localhost");
XCTAssertTrue(configurationB.localDatastoreEnabled);
XCTAssertEqual(configurationB.networkRetryAttempts, 1337);
}
Expand All @@ -125,4 +136,14 @@ - (void)testExtensionDataSharing {
XCTAssertNoThrow(configuration.containingApplicationBundleIdentifier = @"someContainer");
}

- (void)testServerValidation {
[ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> _Nonnull configuration) {
configuration.applicationId = @"a";
configuration.clientKey = @"b";

PFAssertThrowsInvalidArgumentException(configuration.server = @"");
PFAssertThrowsInvalidArgumentException(configuration.server = @"Yolo Yarr");
}];
}

@end
6 changes: 3 additions & 3 deletions Tests/Unit/ParseSetupUnitTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ - (void)testInitializeWithLDSAfterInitializeShouldThrowException {

- (void)testInitializeWithNilApplicationIdNilClientKeyShouldThrowException {
NSString *yolo = nil;
PFAssertThrowsInconsistencyException([Parse setApplicationId:yolo clientKey:yolo]);
PFAssertThrowsInconsistencyException([Parse setApplicationId:yolo clientKey:@"a"]);
PFAssertThrowsInconsistencyException([Parse setApplicationId:@"a" clientKey:yolo]);
PFAssertThrowsInvalidArgumentException([Parse setApplicationId:yolo clientKey:yolo]);
PFAssertThrowsInvalidArgumentException([Parse setApplicationId:yolo clientKey:@"a"]);
PFAssertThrowsInvalidArgumentException([Parse setApplicationId:@"a" clientKey:yolo]);
}

@end