|
| 1 | +# Copyright 2022 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import asyncio |
| 16 | + |
| 17 | +from firebase_admin import firestore_async |
| 18 | + |
| 19 | +# pylint: disable=invalid-name |
| 20 | +def init_firestore_async_client(): |
| 21 | + # [START init_firestore_async_client] |
| 22 | + import firebase_admin |
| 23 | + from firebase_admin import firestore_async |
| 24 | + |
| 25 | + # Application Default credentials are automatically created. |
| 26 | + app = firebase_admin.initialize_app() |
| 27 | + db = firestore_async.client() |
| 28 | + # [END init_firestore_async_client] |
| 29 | + |
| 30 | +def init_firestore_async_client_application_default(): |
| 31 | + # [START init_firestore_async_client_application_default] |
| 32 | + import firebase_admin |
| 33 | + from firebase_admin import credentials |
| 34 | + from firebase_admin import firestore_async |
| 35 | + |
| 36 | + # Use the application default credentials. |
| 37 | + cred = credentials.ApplicationDefault() |
| 38 | + |
| 39 | + firebase_admin.initialize_app(cred) |
| 40 | + db = firestore_async.client() |
| 41 | + # [END init_firestore_async_client_application_default] |
| 42 | + |
| 43 | +def init_firestore_async_client_service_account(): |
| 44 | + # [START init_firestore_async_client_service_account] |
| 45 | + import firebase_admin |
| 46 | + from firebase_admin import credentials |
| 47 | + from firebase_admin import firestore_async |
| 48 | + |
| 49 | + # Use a service account. |
| 50 | + cred = credentials.Certificate('path/to/serviceAccount.json') |
| 51 | + |
| 52 | + app = firebase_admin.initialize_app(cred) |
| 53 | + |
| 54 | + db = firestore_async.client() |
| 55 | + # [END init_firestore_async_client_service_account] |
| 56 | + |
| 57 | +def close_async_sessions(): |
| 58 | + import firebase_admin |
| 59 | + from firebase_admin import firestore_async |
| 60 | + |
| 61 | + # [START close_async_sessions] |
| 62 | + app = firebase_admin.initialize_app() |
| 63 | + db = firestore_async.client() |
| 64 | + |
| 65 | + # Perform firestore tasks... |
| 66 | + |
| 67 | + # Delete app to ensure that all the async sessions are closed gracefully. |
| 68 | + firebase_admin.delete_app(app) |
| 69 | + # [END close_async_sessions] |
| 70 | + |
| 71 | +async def read_data(): |
| 72 | + import firebase_admin |
| 73 | + from firebase_admin import firestore_async |
| 74 | + |
| 75 | + app = firebase_admin.initialize_app() |
| 76 | + db = firestore_async.client() |
| 77 | + |
| 78 | + # [START read_data] |
| 79 | + doc_ref = db.collection('users').document('alovelace') |
| 80 | + doc = await doc_ref.get() |
| 81 | + if doc.exists: |
| 82 | + return f'data: {doc.to_dict()}' |
| 83 | + # [END read_data] |
| 84 | + |
| 85 | +async def add_data(): |
| 86 | + import firebase_admin |
| 87 | + from firebase_admin import firestore_async |
| 88 | + |
| 89 | + app = firebase_admin.initialize_app() |
| 90 | + db = firestore_async.client() |
| 91 | + |
| 92 | + # [START add_data] |
| 93 | + doc_ref = db.collection("users").document("alovelace") |
| 94 | + await doc_ref.set({ |
| 95 | + "first": "Ada", |
| 96 | + "last": "Lovelace", |
| 97 | + "born": 1815 |
| 98 | + }) |
| 99 | + # [END add_data] |
| 100 | + |
| 101 | +def firestore_async_client_with_asyncio_eventloop(): |
| 102 | + # [START firestore_async_client_with_asyncio_eventloop] |
| 103 | + import asyncio |
| 104 | + import firebase_admin |
| 105 | + from firebase_admin import firestore_async |
| 106 | + |
| 107 | + app = firebase_admin.initialize_app() |
| 108 | + db = firestore_async.client() |
| 109 | + |
| 110 | + # Create coroutine to add user data. |
| 111 | + async def add_data(): |
| 112 | + doc_ref = db.collection("users").document("alovelace") |
| 113 | + print("Start adding user...") |
| 114 | + await doc_ref.set({ |
| 115 | + "first": "Ada", |
| 116 | + "last": "Lovelace", |
| 117 | + "born": 1815 |
| 118 | + }) |
| 119 | + print("Done adding user!") |
| 120 | + |
| 121 | + # Another corutine with secondary tasks we want to complete. |
| 122 | + async def while_waiting(): |
| 123 | + print("Start other tasks...") |
| 124 | + await asyncio.sleep(2) |
| 125 | + print("Finished with other tasks!") |
| 126 | + |
| 127 | + # Initialize an eventloop to execute tasks until completion. |
| 128 | + loop = asyncio.get_event_loop() |
| 129 | + tasks = [add_data(), while_waiting()] |
| 130 | + loop.run_until_complete(asyncio.gather(*tasks)) |
| 131 | + firebase_admin.delete_app(app) |
| 132 | + # [END firestore_async_client_with_asyncio_eventloop] |
0 commit comments