Skip to content

Commit 29c3d79

Browse files
schmidt-sebastianBrian Chen
authored and
Brian Chen
committed
Infer Database URL from project ID (#6332)
1 parent 9da2b24 commit 29c3d79

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

FirebaseDatabase/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Unreleased
2+
- [feature] The SDK can now infer a default database URL if none is provided in
3+
the config.
4+
15
# v6.4.0
26
- [changed] Functionally neutral source reorganization. (#5861)
37

FirebaseDatabase/Sources/Api/FIRDatabase.m

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,22 @@ + (FIRDatabase *)databaseForApp:(FIRApp *)app {
6767
[NSException raise:@"InvalidFIRApp"
6868
format:@"nil FIRApp instance passed to databaseForApp."];
6969
}
70-
return [FIRDatabase databaseForApp:app URL:app.options.databaseURL];
70+
NSString *url = app.options.databaseURL;
71+
if (!url) {
72+
if (!app.options.projectID) {
73+
[NSException
74+
raise:@"MissingProjectId"
75+
format:@"Can't determine Firebase Database URL. Be sure to "
76+
@"include a Project ID when calling "
77+
@"`FirebaseApp.configure()`."];
78+
}
79+
FFLog(@"I-RDB024002", @"Using default host for project %@",
80+
app.options.projectID);
81+
url = [NSString
82+
stringWithFormat:@"https://%@-default-rtdb.firebaseio.com",
83+
app.options.projectID];
84+
}
85+
return [FIRDatabase databaseForApp:app URL:url];
7186
}
7287

7388
+ (FIRDatabase *)databaseForApp:(FIRApp *)app URL:(NSString *)url {

FirebaseDatabase/Tests/Helpers/FIRFakeApp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
@interface FIRFakeApp : NSObject
2323

24-
- (instancetype)initWithName:(NSString *)name URL:(NSString *)url;
24+
- (instancetype)initWithName:(NSString *)name URL:(NSString *_Nullable)url;
2525

2626
@property(nonatomic, readonly) FIRFakeOptions *options;
2727
@property(nonatomic, copy, readonly) NSString *name;

FirebaseDatabase/Tests/Helpers/FIRFakeApp.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@
2121
#import "SharedTestUtilities/FIRComponentTestUtilities.h"
2222

2323
@interface FIRFakeOptions : NSObject
24-
@property(nonatomic, readonly, copy) NSString *databaseURL;
24+
@property(nonatomic, readonly, copy) NSString *_Nullable databaseURL;
25+
@property(nonatomic, readonly, copy) NSString *projectID;
2526
@property(nonatomic, readonly, copy) NSString *googleAppID;
26-
- (instancetype)initWithURL:(NSString *)url;
27+
- (instancetype)initWithURL:(NSString *_Nullable)url;
2728
@end
2829

2930
@implementation FIRFakeOptions
30-
- (instancetype)initWithURL:(NSString *)url {
31+
- (instancetype)initWithURL:(NSString *_Nullable)url {
3132
self = [super init];
3233
if (self) {
3334
_databaseURL = url;
3435
_googleAppID = @"fake-app-id";
36+
_projectID = @"fake-project-id";
3537
}
3638
return self;
3739
}
@@ -64,7 +66,7 @@ - (instancetype)initWithApp:(FIRApp *)app
6466

6567
@implementation FIRFakeApp
6668

67-
- (instancetype)initWithName:(NSString *)name URL:(NSString *)url {
69+
- (instancetype)initWithName:(NSString *)name URL:(NSString *_Nullable)url {
6870
self = [super init];
6971
if (self) {
7072
_name = name;

FirebaseDatabase/Tests/Integration/FIRDatabaseTests.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ - (void)testDatabaseForApp {
4949
}
5050

5151
- (void)testDatabaseForAppWithInvalidURLs {
52-
XCTAssertThrows([self databaseForURL:nil]);
5352
XCTAssertThrows([self databaseForURL:@"not-a-url"]);
5453
XCTAssertThrows([self databaseForURL:@"http://x.example.com/paths/are/bad"]);
5554
}
@@ -75,6 +74,13 @@ - (void)testDatabaseForAppWithHttpsURL {
7574
XCTAssertEqualObjects(@"https://foo.bar.com", [database reference].URL);
7675
}
7776

77+
- (void)testDatabaseForAppWithProjectId {
78+
id app = [[FIRFakeApp alloc] initWithName:@"testDatabaseForAppWithURL" URL:nil];
79+
FIRDatabase *database = [FIRDatabase databaseForApp:app];
80+
XCTAssertEqualObjects(@"https://fake-project-id-default-rtdb.firebaseio.com",
81+
[database reference].URL);
82+
}
83+
7884
- (void)testDifferentInstanceForAppWithURL {
7985
id app = [[FIRFakeApp alloc] initWithName:@"testDifferentInstanceForAppWithURL"
8086
URL:kFirebaseTestAltNamespace];

0 commit comments

Comments
 (0)