Last active
January 17, 2022 19:45
-
-
Save therefromhere/5acc827ec7a27db9edcb24fe2ece7d37 to your computer and use it in GitHub Desktop.
OBSOLETE, see comment below ||| Connection to the Firestore emulator in python, since it wasn't at that time supported by the official SDK, see https://github.com/googleapis/google-cloud-python/issues/7500
This file contains 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
import os | |
from unittest import mock | |
import grpc | |
from google.auth.credentials import Credentials | |
from google.cloud import firestore | |
from google.cloud.firestore_v1.gapic import firestore_client | |
from google.cloud.firestore_v1.gapic.transports import firestore_grpc_transport | |
def get_firestore_client() -> firestore.Client: | |
""" | |
Return a firestore Client, connects to an emulator if FIREBASE_FIRESTORE_EMULATOR_ADDRESS is set | |
eg, using https://firebase.google.com/docs/functions/local-emulator | |
firebase emulators:start --only firestore | |
:return: | |
""" | |
emulator_address = os.environ.get("FIREBASE_FIRESTORE_EMULATOR_ADDRESS") | |
if emulator_address: | |
dummy_project = "foo" | |
dummy_credentials = mock.MagicMock(spec=Credentials) | |
db = firestore.Client(project=dummy_project, credentials=dummy_credentials) | |
# hack - set the internal firestore api client to point at the emulator, | |
# used by ref: google.cloud.firestore_v1.client.Client._firestore_api | |
print(f"connecting to firestore emulator at {emulator_address}") | |
channel = grpc.insecure_channel(emulator_address) | |
transport = firestore_grpc_transport.FirestoreGrpcTransport(channel=channel) | |
db._firestore_api_internal = firestore_client.FirestoreClient(transport=transport) | |
else: | |
db = firestore.Client() | |
return db |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you @therefromhere! This is very useful for me 🙂
I'm waiting for the Firebase Authentication <-> python admin sdk integration. For those who are interested:
firebase/firebase-admin-python#531