1- import json
21from copy import deepcopy
32import requests
3+ import firebase_admin
4+ from firebase_admin import credentials , storage , firestore
5+ import json
6+ from datetime import datetime
7+
8+ # -----------------------------------
9+ # INIT FIREBASE
10+ # -----------------------------------
11+
12+ cred = credentials .ApplicationDefault ()
13+
14+ firebase_admin .initialize_app (cred , {
15+ "storageBucket" : "benefit-decision-toolkit-play.firebasestorage.app"
16+ })
17+
18+ db = firestore .client ()
19+ bucket = storage .bucket ()
420
521
622# --------------------------------------------
@@ -234,12 +250,49 @@ def transform_situation_format(data):
234250 return data
235251
236252
253+ def save_json_to_storage_and_update_firestore (json_string , firestore_doc_path ):
254+ """
255+ Upload JSON string to Firebase Storage and update Firestore
256+ with the storage path or download URL of the uploaded file.
257+ """
258+
259+ # ---------------------
260+ # Create filename
261+ # Example: exported_2025-02-12_14-30-59.json
262+ # ---------------------
263+ timestamp = datetime .utcnow ().strftime ("%Y-%m-%d_%H-%M-%S" )
264+ filename = f"LibraryApiSchemaExports/export_{ timestamp } .json"
265+
266+ # ---------------------
267+ # Upload to storage
268+ # ---------------------
269+ blob = bucket .blob (filename )
270+ blob .upload_from_string (json_string , content_type = "application/json" )
271+
272+ # Get the storage path
273+ storage_path = blob .name
274+
275+ # ---------------------
276+ # Update Firestore
277+ # ---------------------
278+ doc_ref = db .document (firestore_doc_path )
279+ doc_ref .set ({
280+ "latestJsonStoragePath" : storage_path ,
281+ "updatedAt" : firestore .SERVER_TIMESTAMP
282+ }, merge = True )
283+
284+ print ("Uploaded:" , storage_path )
285+ print ("Firestore updated!" )
286+
287+ return storage_path
288+
289+
237290# --------------------------------------------
238291# Load your OpenAPI JSON here
239292# --------------------------------------------
240293if __name__ == "__main__" :
241294
242- url = "https://library-api-cnsoqyluna-uc.a .run.app/q/openapi.json"
295+ url = "https://library-api-1034049717668.us-central1 .run.app/q/openapi.json"
243296
244297 # Send a GET request
245298 response = requests .get (url )
@@ -260,7 +313,12 @@ def transform_situation_format(data):
260313 check .pop ("inputs" )
261314
262315 # Write JSON file using UTF-8 to avoid errors
263- with open ("endpoint_inputs.json" , "w" , encoding = "utf-8" ) as out :
264- json .dump (check_records , out , indent = 2 , ensure_ascii = False )
316+ json_string = json .dumps (check_records , indent = 2 , ensure_ascii = False )
317+
318+ print ("Parsed json" )
319+ print (json_string )
265320
266- print ("Output written to endpoint_inputs.json" )
321+ save_json_to_storage_and_update_firestore (
322+ json_string ,
323+ firestore_doc_path = "system/config"
324+ )
0 commit comments