Commit ab00c60 1 parent 7536a43 commit ab00c60 Copy full SHA for ab00c60
File tree 4 files changed +65
-0
lines changed
4 files changed +65
-0
lines changed Original file line number Diff line number Diff line change
1
+ import os
2
+ from flask import jsonify
3
+ from google .cloud import firestore
4
+
5
+ def read (request ):
6
+ if request .method == 'OPTIONS' :
7
+ headers = {
8
+ 'Access-Control-Allow-Origin' : '*' ,
9
+ 'Access-Control-Allow-Methods' : 'GET' ,
10
+ 'Access-Control-Allow-Headers' : 'Content-Type' ,
11
+ 'Access-Control-Max-Age' : '3600'
12
+ }
13
+ return ('' , 204 , headers )
14
+
15
+ headers = {
16
+ 'Access-Control-Allow-Origin' : '*'
17
+ }
18
+
19
+ try :
20
+ db = firestore .Client (database = os .getenv ('DATABASE_NAME' ))
21
+ collection_name = os .getenv ('COLLECTION_NAME' )
22
+ visitors_ref = db .collection (collection_name )
23
+ visitors = [doc .to_dict () for doc in visitors_ref .stream ()]
24
+ return jsonify (visitors ), 200 , headers
25
+ except Exception as e :
26
+ return f"An error Occured: { e } " , 404 , headers
Original file line number Diff line number Diff line change
1
+ functions-framework == 3.*
2
+ google-cloud-firestore == 2.16.1
3
+ Flask == 3.0.0
Original file line number Diff line number Diff line change
1
+ import os
2
+ from google .cloud import firestore
3
+
4
+ def update_visitor (request ):
5
+ if request .method == 'OPTIONS' :
6
+ headers = {
7
+ 'Access-Control-Allow-Origin' : '*' ,
8
+ 'Access-Control-Allow-Methods' : 'POST' ,
9
+ 'Access-Control-Allow-Headers' : 'Content-Type' ,
10
+ 'Access-Control-Max-Age' : '3600'
11
+ }
12
+ return ('' , 204 , headers )
13
+
14
+ headers = {
15
+ 'Access-Control-Allow-Origin' : '*'
16
+ }
17
+
18
+ db = firestore .Client (database = os .getenv ('DATABASE_NAME' ))
19
+ collection_name = os .getenv ('COLLECTION_NAME' )
20
+ document_id = os .getenv ('DOCUMENT_ID' )
21
+
22
+ doc_ref = db .collection (collection_name ).document (document_id )
23
+ doc = doc_ref .get ()
24
+
25
+ if doc .exists :
26
+ current_count = doc .to_dict ().get ('counter' , 0 )
27
+ new_count = current_count + 1
28
+
29
+ doc_ref .update ({'counter' : new_count })
30
+
31
+ return f"Counter updated to { new_count } " , 200 , headers
32
+ else :
33
+ return "Document not found" , 404 , headers
Original file line number Diff line number Diff line change
1
+ functions-framework == 3.*
2
+ google-cloud-firestore == 2.16.1
3
+ Flask == 3.0.0
You can’t perform that action at this time.
0 commit comments