Added examples of reading and writing to the database

This commit is contained in:
Benjamin Ramhorst 2022-02-02 23:19:39 +00:00
parent e4bfb40c1a
commit 4585af0387

28
app.py
View file

@ -5,11 +5,37 @@ from firebase_admin import credentials
from firebase_admin import firestore
app = Flask(__name__)
firebase = firebase_admin.initialize_app(credentials.Certificate('firebase-key.json'))
@app.route('/')
def hello_world(): # put application's code here
return 'Hello World! I am the dog you tracked with the amazing sensor from LEG industries...kidding this is just the home page :)'
# An example of reading data from Firebase
# Taken from Firebase documentation
@app.route('/example/read', methods=['GET'])
def exampleRead():
doc_ref = firestore.client().collection(u'cities').document(u'LA')
doc = doc_ref.get()
if doc.exists:
return f'Document data: {doc.to_dict()}'
else:
return u'No such document!'
# An example of saving data to Firebase
# Taken from Firebase documentation
# This should be a POST method...will change later
@app.route('/example/write', methods=['GET'])
def examnpleWrite():
data = {
u'name': u'Los Angeles',
u'state': u'CA',
u'country': u'USA'
}
# Add a new doc in collection 'cities' with ID 'LA'
firestore.client().collection(u'cities').document(u'LA').set(data)
return 'Saved to Firebase'
if __name__ == '__main__':
firebase_admin.initialize_app(credentials.Certificate('firebase-key.json'))
app.run()