Skip to content

Instantly share code, notes, and snippets.

@edemnati
Last active August 19, 2023 18:37
Show Gist options
  • Save edemnati/0418aed66673c158206c38c01baf6956 to your computer and use it in GitHub Desktop.
Save edemnati/0418aed66673c158206c38c01baf6956 to your computer and use it in GitHub Desktop.
import os
from datetime import datetime, timezone
import time
import hashlib
import json
import pandas as pd
from flask import escape
from flask import jsonify
#Private dependency package
from mylib.gcp_cloud_util.gcp_cloud_util import gcp_firestore
# Global (instance-wide) scope
# Ref: https://cloud.google.com/functions/docs/bestpractices/tips
#Connect to cloud firestore
try:
my_firestore=gcp_firestore()
res_connect,my_firestore_client=my_firestore.connect()
print('res_connect: ',res_connect)
except:
print("Error: connecting to Cloud firestore.")
raise
# Cloud function update_entities_firestore
def read_articles_firestore(request):
"""
args:
"entity":"Apple"
"""
content_type = request.headers['content-type']
if content_type == 'application/json':
request_json = request.get_json(silent=True)
if request_json:
#Try to read source attribute
try:
data_entity = request_json['entity']
except:
print("Error: Missing attribute 'entities' ")
raise
else:
raise ValueError("Missing one or more parameter")
return jsonify("Missing one or more parameter")
else:
return jsonify("JSON is invalid.")
try:
doc_ref = my_firestore_client.collection(u'articles').where('news_entities', 'array_contains',data_entity).stream()
docs=[]
for doc in doc_ref:
docs.append(doc.to_dict())
except:
print("Error: Reading data. request_json: {}".format(request_json))
raise
return jsonify("Error: Reading data from Firestore")
return jsonify(data=docs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment