Skip to content

Infer database URL from Project ID #1898

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 1 commit into from
Aug 20, 2020
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
3 changes: 2 additions & 1 deletion firebase-database/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased

- [changed] The SDK can now infer a default database URL if none is provided in
the config.
- [changed] Added internal HTTP header to the WebChannel connection.
- [feature] Realtime Database now supports connecting to a local emulator via
`FirebaseDatabase#useEmulator()`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ public void getInstanceForAppWithHttpsUrl() {
assertEquals("https://tests.fblocal.com:9000", db.getReference().toString());
}

@Test
public void canInferDatabaseUrlFromProjectId() {
FirebaseApp app =
FirebaseApp.initializeApp(
InstrumentationRegistry.getInstrumentation().getTargetContext(),
new FirebaseOptions.Builder()
.setApplicationId("appid")
.setApiKey("apikey")
.setProjectId("abc123")
.build(),
"canInferDatabaseUrlFromProjectId");
FirebaseDatabase db = FirebaseDatabase.getInstance(app);
assertEquals("https://abc123-default-rtdb.firebaseio.com", db.getReference().toString());
}

@Test
public void getDifferentInstanceForAppWithUrl() {
FirebaseApp app =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,16 @@ public static FirebaseDatabase getInstance(@NonNull String url) {
*/
@NonNull
public static FirebaseDatabase getInstance(@NonNull FirebaseApp app) {
return getInstance(app, app.getOptions().getDatabaseUrl());
String databaseUrl = app.getOptions().getDatabaseUrl();
if (databaseUrl == null) {
if (app.getOptions().getProjectId() == null) {
throw new DatabaseException(
"Failed to get FirebaseDatabase instance: Can't determine Firebase Database URL. "
+ "Be sure to include a Project ID in your configuration.");
}
databaseUrl = "https://" + app.getOptions().getProjectId() + "-default-rtdb.firebaseio.com";
}
return getInstance(app, databaseUrl);
}

/**
Expand Down