Skip to content

billyshambrook/aiofirebase

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

aiofirebase

PyPI version

asyncio (PEP 3156) Firebase client library.

Install

aiofirebase requires Python 3.5 or above.

$ pip install aiofirebase

Usage

Setup

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/")

Retrieving data

We can now read data from our database:

>>> await firebase.get(path='dinosaurs')
"dinosaurs": {
  "lambeosaurus": {
    "height": 2.1,
    "length": 12.5,
    "weight": 5000
  }
}

Saving data

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
  }
}

Update data

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})

Saving lists of data

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'})

Removing data

Data can also be removed by making a DELETE request:

>>> await firebase.delete('dinosaurs/stegosaurus')

Watch for updates

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')

About

Asyncio Firebase client library.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages