asyncio (PEP 3156) Firebase client library.
aiofirebase requires Python 3.5 or above.
$ pip install aiofirebase
Begin by importing aiofirebase:
>>> import aiofirebase
And create a client that we will use to interact with firebase:
>>> firebase = aiofirebase.FirebaseHTTP("https://<YOUR-FIREBASE-APP>.firebaseio.com/")
We can now read data from our database:
>>> await firebase.get(path='dinosaurs')
"dinosaurs": {
"lambeosaurus": {
"height": 2.1,
"length": 12.5,
"weight": 5000
}
}
We can also create new data in the database:
>>> await firebase.put(path='dinosaurs', value={'stegosaurus': {'height': 4, 'length': 9, 'weight': 2500}})
{
'stegosaurus': {
"height": 4,
"length": 9,
"weight: 2500
}
}
We can also update specific children at a location without overwriting existing data:
>>> await firebase.patch(path='dinosaurs/stegosaurus', value={'width': 2})
{
"width": 2
}
Note: If we had done a create
instead of a update
, the data we added in the create
would of been overwritten.
We can also update values at multiple locations in your Firebase database at the same time:
>>> await firebase.patch(path='dinosaurs', value={'lambeosaurus/width': 1, 'stegosaurus/width': 2})
If you need to save lists of data, each item should be saved against a unique key. Firebase will generate this unique key and save the child by making a POST request.
>>> await firebase.post(path='posts', value={'author': 'alanisawesome', 'title': 'The Turing Machine'})
Data can also be removed by making a DELETE request:
>>> await firebase.delete('dinosaurs/stegosaurus')
Firebase provides a way to receive events when data changes in your database via the EventSource protocol.
aiofirebase can listen for these events, and send each event received to a callback of your choice.
>>> async def mycallback(event, data):
... print('{} event received.'.format(event))
...
>>> await firebase.stream(callback=mycallback, path='dinosaurs')