Skip to content
This repository has been archived by the owner on Aug 26, 2019. It is now read-only.

Added parser to datetime and decimal to json and force cast 'path' to string; #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 18 additions & 6 deletions firebase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@
import urlparse #for urlparse and urljoin
import os #for os.path.dirname
import json #for dumps


import datetime #for parse datetime object to string
import decimal #for parse decimal to string

class JSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime):
return obj.isoformat()
elif isinstance(obj, datetime.timedelta):
return total_seconds(obj)
elif isinstance(obj, decimal.Decimal):
return float(obj)
else:
return json.JSONEncoder.default(self, obj)

class Firebase():
ROOT_URL = '' #no trailing slash
auth_token = None

def __init__(self, root_url, auth_token=None):
self.ROOT_URL = root_url.rstrip('/')
Expand All @@ -16,16 +28,16 @@ def __init__(self, root_url, auth_token=None):

def child(self, path):
root_url = '%s/' % self.ROOT_URL
url = urlparse.urljoin(root_url, path.lstrip('/'))
return Firebase(url)
url = urlparse.urljoin(root_url, str(path).lstrip('/'))
return Firebase(url, auth_token=self.auth_token)

def parent(self):
url = os.path.dirname(self.ROOT_URL)
#If url is the root of your Firebase, return None
up = urlparse.urlparse(url)
if up.path == '':
return None #maybe throw exception here?
return Firebase(url)
return Firebase(url, auth_token=self.auth_token)

def name(self):
return os.path.basename(self.ROOT_URL)
Expand Down Expand Up @@ -75,7 +87,7 @@ def __request(self, method, **kwargs):
#Firebase API does not accept form-encoded PUT/POST data. It needs to
#be JSON encoded.
if 'data' in kwargs:
kwargs['data'] = json.dumps(kwargs['data'])
kwargs['data'] = json.dumps(kwargs['data'], cls=JSONEncoder)

params = {}
if self.auth_token:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
requests >=1.2.0,<1.2.99
requests >=1.2.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
author = 'Michael Huynh',
author_email = '[email protected]',
url = 'http://github.com/mikexstudios/python-firebase',
install_requires = ['requests >=1.2.0,<1.2.99'],
install_requires = ['requests >=1.2.0'],
classifiers = [
'Programming Language :: Python',
'License :: OSI Approved :: BSD License',
Expand Down