├── .gitignore ├── README.md ├── firebase └── __init__.py ├── requirements.txt └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.swp 3 | *.pyc 4 | env/ 5 | pip-log.txt 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## ⛔️ DEPRECATED 2 | 3 | [![No Maintenance Intended](http://unmaintained.tech/badge.svg)](http://unmaintained.tech/) 4 | 5 | python-firebase has been unmaintained for years and is no longer supported. Please 6 | consider [using another library][firebase-rest-libraries] instead. 7 | 8 | [firebase-rest-libraries]: https://firebase.google.com/docs/database/rest/start#section-library 9 | 10 | python-firebase 11 | =============== 12 | http://github.com/mikexstudios/python-firebase 13 | by Michael Huynh (mike@mikexstudios.com) 14 | 15 | Purpose: 16 | ------- 17 | 18 | A very simple wrapper for Firebase's REST API. 19 | 20 | How to use 21 | ---------- 22 | 23 | 1. Install python-firebase using pip: 24 | 25 | pip install -e git://github.com/mikexstudios/python-firebase.git#egg=python-firebase 26 | 27 | or with easy_install (not recommended): 28 | 29 | easy_install http://github.com/mikexstudios/python-firebase/tarball/master 30 | 31 | Note that python-firebase depends on requests (http://python-requests.org), 32 | a REST/http client for python. If you used pip or easy_install, the 33 | dependency should automatically be installed. 34 | 35 | 2. Then simply import firebase at the top of your python script: 36 | 37 | from firebase import Firebase 38 | 39 | and then instantiate Firebase, passing in your root url: 40 | 41 | f = Firebase('https://SampleChat.firebaseIO-demo.com/') 42 | 43 | You may optionaly pass a [Firebase authentication token](https://www.firebase.com/docs/security/custom-login.html) to secure your calls: 44 | 45 | f = Firebase('http://SampleChat.firebaseIO-demo.com/', auth_token="") 46 | 47 | Now call the different methods of the Firebase class (see the Firebase 48 | REST API page: http://www.firebase.com/docs/rest-api.html and the source of 49 | `firebase/__init__.py` for what methods are available and how to call 50 | them). For example, to push a list of data: 51 | 52 | f = Firebase('https://SampleChat.firebaseIO-demo.com/message_list') 53 | r = f.push({'user_id': 'wilma', 'text': 'Hello'}) 54 | 55 | The response `r` is a dictionary containing Firebase's REST response: 56 | 57 | {"name":"-INOQPH-aV_psbk3ZXEX"} 58 | 59 | 60 | License 61 | ------- 62 | 63 | django-firebase is BSD licensed. 64 | 65 | -------------------------------------------------------------------------------- /firebase/__init__.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import urlparse #for urlparse and urljoin 3 | import os #for os.path.dirname 4 | import json #for dumps 5 | 6 | 7 | 8 | class Firebase(): 9 | ROOT_URL = '' #no trailing slash 10 | 11 | def __init__(self, root_url, auth_token=None): 12 | self.ROOT_URL = root_url.rstrip('/') 13 | self.auth_token = auth_token 14 | 15 | #These methods are intended to mimic Firebase API calls. 16 | 17 | def child(self, path): 18 | root_url = '%s/' % self.ROOT_URL 19 | url = urlparse.urljoin(root_url, path.lstrip('/')) 20 | return Firebase(url) 21 | 22 | def parent(self): 23 | url = os.path.dirname(self.ROOT_URL) 24 | #If url is the root of your Firebase, return None 25 | up = urlparse.urlparse(url) 26 | if up.path == '': 27 | return None #maybe throw exception here? 28 | return Firebase(url) 29 | 30 | def name(self): 31 | return os.path.basename(self.ROOT_URL) 32 | 33 | def toString(self): 34 | return self.__str__() 35 | def __str__(self): 36 | return self.ROOT_URL 37 | 38 | def set(self, value): 39 | return self.put(value) 40 | 41 | def push(self, data): 42 | return self.post(data) 43 | 44 | def update(self, data): 45 | return self.patch(data) 46 | 47 | def remove(self): 48 | return self.delete() 49 | 50 | 51 | #These mirror REST API functionality 52 | 53 | def put(self, data): 54 | return self.__request('put', data = data) 55 | 56 | def patch(self, data): 57 | return self.__request('patch', data = data) 58 | 59 | def get(self): 60 | return self.__request('get') 61 | 62 | #POST differs from PUT in that it is equivalent to doing a 'push()' in 63 | #Firebase where a new child location with unique name is generated and 64 | #returned 65 | def post(self, data): 66 | return self.__request('post', data = data) 67 | 68 | def delete(self): 69 | return self.__request('delete') 70 | 71 | 72 | #Private 73 | 74 | def __request(self, method, **kwargs): 75 | #Firebase API does not accept form-encoded PUT/POST data. It needs to 76 | #be JSON encoded. 77 | if 'data' in kwargs: 78 | kwargs['data'] = json.dumps(kwargs['data']) 79 | 80 | params = {} 81 | if self.auth_token: 82 | if 'params' in kwargs: 83 | params = kwargs['params'] 84 | del kwargs['params'] 85 | params.update({'auth': self.auth_token}) 86 | 87 | r = requests.request(method, self.__url(), params=params, **kwargs) 88 | r.raise_for_status() #throw exception if error 89 | return r.json() 90 | 91 | 92 | def __url(self): 93 | #We append .json to end of ROOT_URL for REST API. 94 | return '%s.json' % self.ROOT_URL 95 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | requests >=1.2.0,<1.2.99 2 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | 3 | setup( 4 | name = 'python-firebase', 5 | packages = find_packages(), 6 | version = '0.1.0', 7 | description = "Simple wrapper around Firebase's REST API", 8 | author = 'Michael Huynh', 9 | author_email = 'mike@mikexstudios.com', 10 | url = 'http://github.com/mikexstudios/python-firebase', 11 | install_requires = ['requests >=1.2.0,<1.2.99'], 12 | classifiers = [ 13 | 'Programming Language :: Python', 14 | 'License :: OSI Approved :: BSD License', 15 | ] 16 | ) 17 | 18 | --------------------------------------------------------------------------------