rairo commited on
Commit
03b2938
·
1 Parent(s): b1f72b5

Update utility.py

Browse files
Files changed (1) hide show
  1. utility.py +45 -7
utility.py CHANGED
@@ -23,14 +23,52 @@ import firebase_admin
23
  from firebase_admin import credentials
24
  from firebase_admin import firestore
25
 
26
- # Initialize the SDK
27
- credentials_json_string = os.environ.get("FIREBASE")
28
- credentials_json = json.loads(credentials_json_string)
29
- cred = credentials.Certificate(credentials_json)
30
- firebase_admin.initialize_app(cred)
31
- # Get a Firestore client
32
- db = firestore.client()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
 
34
 
35
  guid = uuid.uuid4()
36
  new_filename = f"{guid}"
 
23
  from firebase_admin import credentials
24
  from firebase_admin import firestore
25
 
26
+ import os
27
+ import json
28
+ import logging
29
+ import firebase_admin
30
+ from firebase_admin import credentials, firestore
31
+
32
+ def init_firestore_from_env(env_var: str = "FIREBASE"):
33
+ """
34
+ Initialise firebase-admin with the service-account JSON that is stored
35
+ in the given environment variable and return a Firestore client.
36
+
37
+ Raises:
38
+ KeyError – env var not set
39
+ json.JSONDecodeError – env var value is not valid JSON
40
+ ValueError – JSON missing required fields or other SDK errors
41
+ Exception – any other unexpected error
42
+ """
43
+ try:
44
+ # 1️⃣ Pull and parse the service-account JSON from the environment
45
+ sa_json = os.environ[env_var] # may raise KeyError
46
+ sa_info = json.loads(sa_json) # may raise JSONDecodeError
47
+
48
+ # 2️⃣ Build a Credential object from the dict
49
+ cred = credentials.Certificate(sa_info) # accepts dict ✔︎
50
+
51
+ # 3️⃣ Initialise the Admin SDK once; reuse if already initialised
52
+ try:
53
+ firebase_admin.get_app() # already initialised?
54
+ except ValueError:
55
+ firebase_admin.initialize_app(cred) # first time only [oai_citation:2‡firebase.google.com](https://firebase.google.com/docs/admin/setup?utm_source=chatgpt.com)
56
+
57
+ # 4️⃣ Return a ready-to-use Firestore client
58
+ return firestore.client() # thin wrapper over gcloud client [oai_citation:3‡firebase.google.com](https://firebase.google.com/docs/reference/admin/python/firebase_admin.firestore?utm_source=chatgpt.com) [oai_citation:4‡stackoverflow.com](https://stackoverflow.com/questions/72105838/how-do-i-query-a-firestore-document-using-firebase-admin-with-get?utm_source=chatgpt.com)
59
+
60
+ except KeyError as e:
61
+ logging.error("%s environment variable is not set", env_var)
62
+ raise
63
+ except (json.JSONDecodeError, ValueError) as e:
64
+ logging.error("Invalid service-account JSON in %s: %s", env_var, e)
65
+ raise
66
+ except Exception as e:
67
+ # Catch-all for anything unexpected: network issues, IAM perms, etc.
68
+ logging.exception("Failed to initialise Firestore: %s", e)
69
+ raise
70
 
71
+ db = init_firestore_from_env() # returns firebase_admin.firestore.Client
72
 
73
  guid = uuid.uuid4()
74
  new_filename = f"{guid}"