-
Notifications
You must be signed in to change notification settings - Fork 289
Firestore API #83
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
Firestore API #83
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
705abcd
Prototyping Firestore integration for Java
hiranya911 084453e
Some code cleanup
hiranya911 39dec4c
Updating comments
hiranya911 cd8030c
Code cleanup
hiranya911 9be05c9
Merge branch 'master' into hkj-firestore-java
hiranya911 dfc6bec
Removed getInstance() methods from FirestoreClient and added getFires…
hiranya911 ef3881f
Merge branch 'hkj-firestore-java' of github.com:FirebasePrivate/fireb…
hiranya911 0cf020b
Merged with master
hiranya911 ab3469a
Minor cleanup
hiranya911 d64b4f6
Adding Firestore OAuth scopes
hiranya911 d87f364
Merge branch 'master' into hkj-firestore-java
hiranya911 8ed4e5f
Some documentation updates
hiranya911 48104da
Reverted a doc change
hiranya911 1771a01
Updating GCS dependency version (#89)
hiranya911 193fcde
Merge branch 'master' into hkj-firestore-java
hiranya911 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/com/google/firebase/cloud/FirestoreClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.google.firebase.cloud; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import com.google.cloud.firestore.Firestore; | ||
import com.google.cloud.firestore.FirestoreOptions; | ||
import com.google.common.base.Strings; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.ImplFirebaseTrampolines; | ||
import com.google.firebase.internal.FirebaseService; | ||
import com.google.firebase.internal.NonNull; | ||
|
||
/** | ||
* FirestoreClient provides access to Google Cloud Firestore. Use this API to obtain a | ||
* <code>com.google.cloud.firestore.Firestore</code> instance, which provides methods for | ||
* updating and querying data in Firestore. | ||
* | ||
* <p>A Google Cloud project ID is required to access Firestore. FirestoreClient determines the | ||
* project ID from the {@link com.google.firebase.FirebaseOptions} used to initialize the underlying | ||
* {@link FirebaseApp}. If that is not available, it examines the credentials used to initialize | ||
* the app. Finally it attempts to get the project ID by looking up the GCLOUD_PROJECT environment | ||
* variable. If a project ID cannot be determined by any of these methods, this API will throw | ||
* a runtime exception. | ||
*/ | ||
public class FirestoreClient { | ||
|
||
private final Firestore firestore; | ||
|
||
private FirestoreClient(FirebaseApp app) { | ||
checkNotNull(app, "FirebaseApp must not be null"); | ||
String projectId = ImplFirebaseTrampolines.getProjectId(app); | ||
checkArgument(!Strings.isNullOrEmpty(projectId), | ||
"Project ID is required for accessing Firestore. Use a service account credential or " | ||
+ "set the project ID explicitly via FirebaseOptions. Alternatively you can also " | ||
+ "set the project ID via the GCLOUD_PROJECT environment variable."); | ||
this.firestore = FirestoreOptions.newBuilder() | ||
.setCredentials(ImplFirebaseTrampolines.getCredentials(app)) | ||
.setProjectId(projectId) | ||
.build() | ||
.getService(); | ||
} | ||
|
||
/** | ||
* Returns the Firestore instance associated with the default Firebase app. | ||
* | ||
* @return A non-null <code>com.google.cloud.firestore.Firestore</code> instance. | ||
*/ | ||
@NonNull | ||
public static Firestore getFirestore() { | ||
return getFirestore(FirebaseApp.getInstance()); | ||
} | ||
|
||
/** | ||
* Returns the Firestore instance associated with the specified Firebase app. | ||
* | ||
* @param app A non-null {@link FirebaseApp}. | ||
* @return A non-null <code>com.google.cloud.firestore.Firestore</code> instance. | ||
*/ | ||
@NonNull | ||
public static Firestore getFirestore(FirebaseApp app) { | ||
return getInstance(app).firestore; | ||
} | ||
|
||
private static synchronized FirestoreClient getInstance(FirebaseApp app) { | ||
FirestoreClientService service = ImplFirebaseTrampolines.getService(app, | ||
SERVICE_ID, FirestoreClientService.class); | ||
if (service == null) { | ||
service = ImplFirebaseTrampolines.addService(app, new FirestoreClientService(app)); | ||
} | ||
return service.getInstance(); | ||
} | ||
|
||
private static final String SERVICE_ID = FirestoreClient.class.getName(); | ||
|
||
private static class FirestoreClientService extends FirebaseService<FirestoreClient> { | ||
|
||
FirestoreClientService(FirebaseApp app) { | ||
super(SERVICE_ID, new FirestoreClient(app)); | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
// NOTE: We don't explicitly tear down anything here (for now). User won't be able to call | ||
// FirestoreClient.getFirestore() any more, but already created Firestore instances will | ||
// continue to work. Request Firestore team to provide a cleanup/teardown method on the | ||
// Firestore object. | ||
} | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/test/java/com/google/firebase/cloud/FirestoreClientIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.google.firebase.cloud; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import com.google.cloud.firestore.DocumentReference; | ||
import com.google.cloud.firestore.Firestore; | ||
import com.google.cloud.firestore.WriteResult; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.firebase.testing.IntegrationTestUtils; | ||
import java.util.Map; | ||
import org.junit.Test; | ||
|
||
public class FirestoreClientIT { | ||
|
||
@Test | ||
public void testFirestoreAccess() throws Exception { | ||
Firestore firestore = FirestoreClient.getFirestore(IntegrationTestUtils.ensureDefaultApp()); | ||
DocumentReference reference = firestore.collection("cities").document("Mountain View"); | ||
ImmutableMap<String, Object> expected = ImmutableMap.<String, Object>of( | ||
"name", "Mountain View", | ||
"country", "USA", | ||
"population", 77846L, | ||
"capital", false | ||
); | ||
WriteResult result = reference.set(expected).get(); | ||
assertNotNull(result); | ||
|
||
Map<String, Object> data = reference.get().get().getData(); | ||
assertEquals(expected.size(), data.size()); | ||
for (Map.Entry<String, Object> entry : expected.entrySet()) { | ||
assertEquals(entry.getValue(), data.get(entry.getKey())); | ||
} | ||
|
||
reference.delete().get(); | ||
assertFalse(reference.get().get().exists()); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/test/java/com/google/firebase/cloud/FirestoreClientTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.google.firebase.cloud; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.fail; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.cloud.firestore.Firestore; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.FirebaseOptions; | ||
import com.google.firebase.TestOnlyImplFirebaseTrampolines; | ||
import com.google.firebase.testing.ServiceAccount; | ||
import java.io.IOException; | ||
import org.junit.After; | ||
import org.junit.Test; | ||
|
||
public class FirestoreClientTest { | ||
|
||
@After | ||
public void tearDown() { | ||
TestOnlyImplFirebaseTrampolines.clearInstancesForTest(); | ||
} | ||
|
||
@Test | ||
public void testExplicitProjectId() throws IOException { | ||
FirebaseApp app = FirebaseApp.initializeApp(new FirebaseOptions.Builder() | ||
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream())) | ||
.setProjectId("explicit-project-id") | ||
.build()); | ||
Firestore firestore = FirestoreClient.getFirestore(app); | ||
assertEquals("explicit-project-id", firestore.getOptions().getProjectId()); | ||
|
||
firestore = FirestoreClient.getFirestore(); | ||
assertEquals("explicit-project-id", firestore.getOptions().getProjectId()); | ||
} | ||
|
||
@Test | ||
public void testServiceAccountProjectId() throws IOException { | ||
FirebaseApp app = FirebaseApp.initializeApp(new FirebaseOptions.Builder() | ||
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream())) | ||
.build()); | ||
Firestore firestore = FirestoreClient.getFirestore(app); | ||
assertEquals("mock-project-id", firestore.getOptions().getProjectId()); | ||
|
||
firestore = FirestoreClient.getFirestore(); | ||
assertEquals("mock-project-id", firestore.getOptions().getProjectId()); | ||
} | ||
|
||
@Test | ||
public void testAppDelete() throws IOException { | ||
FirebaseApp app = FirebaseApp.initializeApp(new FirebaseOptions.Builder() | ||
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream())) | ||
.setProjectId("mock-project-id") | ||
.build()); | ||
|
||
assertNotNull(FirestoreClient.getFirestore(app)); | ||
app.delete(); | ||
try { | ||
FirestoreClient.getFirestore(app); | ||
fail("No error thrown for deleted app"); | ||
} catch (IllegalStateException expected) { | ||
// ignore | ||
} | ||
|
||
try { | ||
FirestoreClient.getFirestore(); | ||
fail("No error thrown for deleted app"); | ||
} catch (IllegalStateException expected) { | ||
// ignore | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to add instructions to CONTRIBUTING.md (or wherever) to open up your Firestore security rules?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done