ELEC60013-ES-CW1/app.py

42 lines
1.3 KiB
Python
Raw Normal View History

2022-02-02 22:52:07 +00:00
from flask import Flask
2022-02-02 23:06:27 +00:00
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
2022-02-02 22:52:07 +00:00
app = Flask(__name__)
firebase = firebase_admin.initialize_app(credentials.Certificate('firebase-key.json'))
2022-02-02 22:52:07 +00:00
@app.route('/')
def hello_world(): # put application's code here
2022-02-02 23:06:27 +00:00
return 'Hello World! I am the dog you tracked with the amazing sensor from LEG industries...kidding this is just the home page :)'
2022-02-02 22:52:07 +00:00
# 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'
2022-02-02 22:52:07 +00:00
if __name__ == '__main__':
app.run()