firebase.database package

A simple python wrapper for Google’s Firebase Database REST API

class firebase.database.Database(credentials, database_url, requests)[source]

Bases: object

Firebase Database Service

Parameters
  • credentials (Credentials) – Service Account Credentials.

  • database_url (str) – databaseURL from Firebase configuration.

  • requests (Session) – Session to make HTTP requests.

build_headers(token=None)[source]

Build Request Header.

Parameters

token (str) – (Optional) Firebase Auth User ID Token, defaults to None.

Returns

Request Header.

Return type

dict

build_request_url(token)[source]

Builds Request URL for query.

Parameters

token (str) – Firebase Auth User ID Token

Returns

Request URL

Return type

str

check_token(database_url, path, token)[source]

Builds Request URL to write/update/remove data.

Parameters
  • database_url (str) – databaseURL from Firebase configuration.

  • path (str) – Path to data.

  • token (str) – Firebase Auth User ID Token

Returns

Request URL

Return type

str

child(*args)[source]

Build paths to your data.

Parameters

args (str) – Positional arguments to build path to database.

Returns

A reference to the instance object.

Return type

Database

conditional_remove(etag, token=None)[source]

Conditionally delete data from database.

Parameters
  • etag (str) – Unique identifier for specific data at a specified location.

  • token (str) – (Optional) Firebase Auth User ID Token, defaults to None.

Returns

Successful attempt returns None, in case of ETag mismatch an updated ETag for the specific data is returned in dict object

Return type

None

conditional_set(data, etag, token=None, json_kwargs={})[source]

Conditionally add data to database.

Parameters
  • data (dict) – Data to be stored in database.

  • etag (str) – Unique identifier for specific data at a specified location.

  • token (str) – (Optional) Firebase Auth User ID Token, defaults to None.

  • json_kwargs (dict) – (Optional) Keyword arguments to send to json.dumps() methods for serialization of data, defaults to {} (empty dict object).

Returns

Successful attempt returns the data specified to store, failed attempt (due to ETag mismatch) returns the current ETag for the specified path.

Return type

dict

end_at(end)[source]

Filter data where child key value ends at specified value.

Parameters

end (int or float or str) – Arbitrary ending points for queries.

Returns

A reference to the instance object.

Return type

Database

equal_to(equal)[source]

Filter data where child key value is equal to specified value.

Parameters

equal (int or float or str) – Arbitrary point for queries.

Returns

A reference to the instance object.

Return type

Database

generate_key()[source]

Generate Firebase’s push IDs.

Returns

Firebase’s push IDs

Return type

str

get(token=None, json_kwargs={})[source]

Read data from database.

Parameters
  • token (str) – (Optional) Firebase Auth User ID Token, defaults to None.

  • json_kwargs (dict) – (Optional) Keyword arguments to send to json.dumps() method for deserialization of data, defaults to {} (empty dict object).

Returns

The data associated with the path.

Return type

dict

get_etag(token=None)[source]

Fetches Firebase ETag at a specified location.

Parameters

token (str) – (Optional) Firebase Auth User ID Token, defaults to None.

Returns

Firebase ETag

Return type

str

limit_to_first(limit_first)[source]

Filter the number of data to receive from top.

Parameters

limit_first (int) – Maximum number of children to select from top.

Returns

A reference to the instance object.

Return type

Database

limit_to_last(limit_last)[source]

Filter the number of data to receive from bottom.

Parameters

limit_last (int) – Maximum number of children to select from bottom.

Returns

A reference to the instance object.

Return type

Database

order_by_child(order)[source]

Filter data by a common child key.

Parameters

order (str) – Child key name.

Returns

A reference to the instance object.

Return type

Database

order_by_key()[source]

Filter data by their keys.

Returns

A reference to the instance object.

Return type

Database

order_by_value()[source]

Filter data by the value of their child keys.

Returns

A reference to the instance object.

Return type

Database

push(data, token=None, json_kwargs={})[source]

Add data to database.

This method adds a Firebase Push ID at the end of the specified path, and then adds/stores the data in database, unlike set() which does not use a Firebase Push ID.

Parameters
  • data (dict) – Data to be stored in database.

  • token (str) – (Optional) Firebase Auth User ID Token, defaults to None.

  • json_kwargs (dict) – (Optional) Keyword arguments to send to json.dumps() method for serialization of data, defaults to {} (empty dict object).

Returns

Child key (Firebase Push ID) name of the data.

Return type

dict

remove(token=None)[source]

Delete data from database.

Parameters

token (str) – (Optional) Firebase Auth User ID Token, defaults to None.

Returns

Successful attempt returns None.

Return type

None

set(data, token=None, json_kwargs={})[source]

Add data to database.

This method writes the data in database in the specified path, unlike push() which creates a Firebase Push ID then writes the data to database.

Parameters
  • data (dict) – Data to be stored in database.

  • token (str) – (Optional) Firebase Auth User ID Token, defaults to None.

  • json_kwargs (dict) – (Optional) Keyword arguments to send to json.dumps() method for serialization of data, defaults to {} (empty dict object).

Returns

Successful attempt returns the data specified to add to database.

Return type

dict

shallow()[source]

Limit the depth of the response.

Returns

A reference to the instance object.

Return type

Database

sort(origin, by_key, reverse=False)[source]

Further sort data based on a child key value.

Parameters
  • origin (dict) – Data to be sorted (generally the output from get() method).

  • by_key (str) – Child key name to sort by.

  • reverse (bool) – (Optional) Whether to return data in descending order, defaults to False (data is returned in ascending order).

Returns

Sorted version of the data.

Return type

dict

start_at(start)[source]

Filter data where child key value starts from specified value.

Parameters

start (int or float or str) – Arbitrary starting points for queries.

Returns

A reference to the instance object.

Return type

Database

stream(stream_handler, token=None, stream_id=None, is_async=True)[source]
update(data, token=None, json_kwargs={})[source]

Update stored data of database.

Parameters
  • data (dict) – Data to be updated.

  • token (str) – (Optional) Firebase Auth User ID Token, defaults to None.

  • json_kwargs (dict) – (Optional) Keyword arguments to send to json.dumps() method for serialization of data, defaults to {} (empty dict object).

Returns

Successful attempt returns the data specified to update.

Return type

dict