Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion firebase_admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def credential(self):
def options(self):
return self._options

def get_token(self):
def _get_token(self):
"""Returns an OAuth2 bearer token.

This method may return a cached token. But it handles cache invalidation, and therefore
Expand Down
77 changes: 22 additions & 55 deletions firebase_admin/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
_RESERVED_FILTERS = ('$key', '$value', '$priority')


def get_reference(path='/', app=None):
def reference(path='/', app=None):
"""Returns a database Reference representing the node at the specified path.

If no path is specified, this function returns a Reference that represents the database root.
Expand Down Expand Up @@ -69,7 +69,7 @@ class Reference(object):
def __init__(self, **kwargs):
"""Creates a new Reference using the provided parameters.

This method is for internal use only. Use db.get_reference() to obtain an instance of
This method is for internal use only. Use db.reference() to obtain an instance of
Reference.
"""
self._client = kwargs.get('client')
Expand Down Expand Up @@ -119,7 +119,7 @@ def child(self, path):
full_path = self._pathurl + '/' + path
return Reference(client=self._client, path=full_path)

def get_value(self):
def get(self):
"""Returns the value at the current location of the database.

Returns:
Expand All @@ -130,41 +130,21 @@ def get_value(self):
"""
return self._client.request('get', self._add_suffix())

def get_priority(self):
"""Returns the priority of this node, if specified.

Returns:
object: A priority value or None.

Raises:
ApiCallError: If an error occurs while communicating with the remote database server.
"""
return self._client.request('get', self._add_suffix('/.priority.json'))

def set_value(self, value, priority=None):
def set(self, value):
"""Sets the data at this location to the given value.

The value must be JSON-serializable and not None. If a priority is specified, the node will
be assigned that priority along with the value.
The value must be JSON-serializable and not None.

Args:
value: JSON-serialable value to be set at this location.
priority: A numeric or alphanumeric priority value (optional).

Raises:
ValueError: If the value is None or priority is invalid.
ValueError: If the value is None.
TypeError: If the value is not JSON-serializable.
ApiCallError: If an error occurs while communicating with the remote database server.
"""
if value is None:
raise ValueError('Value must not be None.')
if priority is not None:
Reference._check_priority(priority)
if isinstance(value, dict):
value = dict(value)
value['.priority'] = priority
else:
value = {'.value' : value, '.priority' : priority}
params = {'print' : 'silent'}
self._client.request_oneway('put', self._add_suffix(), json=value, params=params)

Expand All @@ -191,7 +171,7 @@ def push(self, value=''):
push_id = output.get('name')
return self.child(push_id)

def update_children(self, value):
def update(self, value):
"""Updates the specified child keys of this Reference to the provided values.

Args:
Expand Down Expand Up @@ -257,20 +237,6 @@ def order_by_value(self):
"""
return Query(order_by='$value', client=self._client, pathurl=self._add_suffix())

def order_by_priority(self):
"""Creates a Query that orderes data by priority.

Returned Query can be used to set additional parameters, and execute complex database
queries (e.g. limit queries, range queries). Due to a limitation of the
underlying REST API, the order-by-priority constraint can only be enforced during
the execution time of the Query. When the Query returns results, the actual results
will be returned as an unordered collection.

Returns:
Query: A database Query instance.
"""
return Query(order_by='$priority', client=self._client, pathurl=self._add_suffix())

def _add_suffix(self, suffix='.json'):
return self._pathurl + suffix

Expand All @@ -294,8 +260,7 @@ class Query(object):
the final result is returned by the server as an unordered collection. Therefore the Query
interface performs another round of sorting at the client-side before returning the results
to the caller. This client-side sorted results are returned to the user as a Python
OrderedDict. However, client-side sorting is not feasible for order-by-priority queries.
Therefore for such queries results are returned as a regular unordered dict.
OrderedDict.
"""

def __init__(self, **kwargs):
Expand All @@ -315,7 +280,7 @@ def __init__(self, **kwargs):
if kwargs:
raise ValueError('Unexpected keyword arguments: {0}'.format(kwargs))

def set_limit_first(self, limit):
def limit_to_first(self, limit):
"""Creates a query with limit, and anchors it to the start of the window.

Args:
Expand All @@ -334,7 +299,7 @@ def set_limit_first(self, limit):
self._params['limitToFirst'] = limit
return self

def set_limit_last(self, limit):
def limit_to_last(self, limit):
"""Creates a query with limit, and anchors it to the end of the window.

Args:
Expand All @@ -353,7 +318,7 @@ def set_limit_last(self, limit):
self._params['limitToLast'] = limit
return self

def set_start_at(self, start):
def start_at(self, start):
"""Sets the lower bound for a range query.

The Query will only return child nodes with a value greater than or equal to the specified
Expand All @@ -373,7 +338,7 @@ def set_start_at(self, start):
self._params['startAt'] = json.dumps(start)
return self

def set_end_at(self, end):
def end_at(self, end):
"""Sets the upper bound for a range query.

The Query will only return child nodes with a value less than or equal to the specified
Expand All @@ -393,7 +358,7 @@ def set_end_at(self, end):
self._params['endAt'] = json.dumps(end)
return self

def set_equal_to(self, value):
def equal_to(self, value):
"""Sets an equals constraint on the Query.

The Query will only return child nodes whose value is equal to the specified value.
Expand All @@ -419,7 +384,7 @@ def querystr(self):
params.append('{0}={1}'.format(key, self._params[key]))
return '&'.join(params)

def run(self):
def get(self):
"""Executes this Query and returns the results.

The results will be returned as a sorted list or an OrderedDict, except in the case of
Expand Down Expand Up @@ -587,18 +552,19 @@ def __init__(self, url=None, auth=None, session=None):
@classmethod
def from_app(cls, app):
"""Created a new _Client for a given App"""
url = app.options.get('dbURL')
url = app.options.get('databaseURL')
if not url or not isinstance(url, six.string_types):
raise ValueError(
'Invalid dbURL option: "{0}". dbURL must be a non-empty URL string.'.format(url))
'Invalid databaseURL option: "{0}". databaseURL must be a non-empty URL '
'string.'.format(url))
parsed = urllib.parse.urlparse(url)
if parsed.scheme != 'https':
raise ValueError(
'Invalid dbURL option: "{0}". dbURL must be an HTTPS URL.'.format(url))
'Invalid databaseURL option: "{0}". databaseURL must be an HTTPS URL.'.format(url))
elif not parsed.netloc.endswith('.firebaseio.com'):
raise ValueError(
'Invalid dbURL option: "{0}". dbURL must be a valid URL to a Firebase Realtime '
'Database instance.'.format(url))
'Invalid databaseURL option: "{0}". databaseURL must be a valid URL to a '
'Firebase Realtime Database instance.'.format(url))
return _Client('https://{0}'.format(parsed.netloc), _OAuth(app), requests.Session())

def request(self, method, urlpath, **kwargs):
Expand Down Expand Up @@ -668,5 +634,6 @@ def __init__(self, app):
self._app = app

def __call__(self, req):
req.headers['Authorization'] = 'Bearer {0}'.format(self._app.get_token())
# pylint: disable=protected-access
req.headers['Authorization'] = 'Bearer {0}'.format(self._app._get_token())
return req
2 changes: 1 addition & 1 deletion integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def default_app(request):
if not project_id:
raise ValueError('Failed to determine project ID from service account certificate.')
cred = credentials.Certificate(cert_path)
ops = {'dbURL' : 'https://{0}.firebaseio.com'.format(project_id)}
ops = {'databaseURL' : 'https://{0}.firebaseio.com'.format(project_id)}
return firebase_admin.initialize_app(cred, ops)

@pytest.fixture(scope='session')
Expand Down
Loading