{ "info": { "author": "Quintus Labs", "author_email": "", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3.5" ], "description": "# PyFireConnect\n\nA python connector library for firebase [Firebase REST API](https://firebase.google.com). You can use this in a plain vanilla Python application or django application.\n\n\n**Key Highlights of Features**\n\n_Compatible with Python 2.7 - 3.7_\n\n_Authentication_\n\n_Database_\n\n_Storage_\n\n_Social Signup_\n\n## Installation\n\n```python\npip install pyfireconnect\n```\n\n## Getting Started\n\n### Python Version\n\npyfireconnect was written for python 3 and tested with 2.7\n\n### Add pyfireconnect to your application\n\nFor use with only user based authentication we can create the following configuration:\n\n```python\nimport pyfireconnect\n\nconfig = {\n \"apiKey\": \"apiKey\",\n \"authDomain\": \"projectId.firebaseapp.com\",\n \"databaseURL\": \"https://databaseName.firebaseio.com\",\n \"storageBucket\": \"projectId.appspot.com\"\n}\n\nfirebase = pyfireconnect.initialize(config)\n```\n\nWe can optionally add a [service account credential](https://firebase.google.com/docs/server/setup#prerequisites) to our\nconfiguration that will allow our server to authenticate with Firebase as an admin and disregard any security rules.\n\n```python\nimport pyfireconnect\n\nconfig = {\n \"apiKey\": \"apiKey\",\n \"authDomain\": \"projectId.firebaseapp.com\",\n \"databaseURL\": \"https://databaseName.firebaseio.com\",\n \"storageBucket\": \"projectId.appspot.com\",\n \"serviceAccount\": \"path/to/serviceAccountCredentials.json\"\n}\n\nfirebase = pyfireconnect.initialize(config)\n```\n\nAdding a service account will authenticate as an admin by default for all database queries, check out the\n[Authentication documentation](#authentication) for how to authenticate users.\n\n### Use Services\n\nA pyfireconnect app can use multiple Firebase services.\n\n```firebase.auth()``` - [Authentication](#authentication)\n\n```firebase.database()``` - [Database](#database)\n\n```firebase.storage()``` - [Storage](#storage)\n\nCheck out the documentation for each service for further details.\n\n## Authentication\n\nThe ```sign_in_with_email_and_password()``` method will return user data including a token you can use to adhere to security rules.\n\nEach of the following methods accepts a user token: ```get()```, ```push()```, ```set()```, ```update()```, ```remove()``` and ```stream()```.\n\n```python\n# Get a reference to the auth service\nauth = firebase.auth()\n\n# Log the user in\nuser = auth.sign_in_with_email_and_password(email, password)\n\n\n#social sign up\nfb_access_token = \"EAANUSasYcQEBAJNudphsPoizjpohueZA3nX7ZCFdpWlDmgJ19zDdu3dJQQ4sZBXDMA6KTqY58MBAK6kBeegZBnoVtRmHmAmkc26pAarUr6ycAYlZArUH2m5RbgEst2ms6mc5JVrSJGJsGyQOgdzRPeJkdIdEoekPG0DnJBdndsMce4ycD6OrEixZCcwPVKOiZBEKy0wTOJQ3wZDZD\"\n\nrequest_uri = 'http://localhost'\n\nsocial_user = auth.social_signup(fb_access_token, \"facebook.com\", request_uri)\n\n\n\n# Get a reference to the database service\ndb = firebase.database()\n\n# data to save\ndata = {\n \"name\": \"Mortimer 'Morty' Smith\"\n}\n\n# Pass the user's idToken to the push method\nresults = db.child(\"users\").push(data, user['idToken'])\n```\n\n### Token expiry\n\nA user's idToken expires after 1 hour, so be sure to use the user's refreshToken to avoid stale tokens.\n```\nuser = auth.sign_in_with_email_and_password(email, password)\n# before the 1 hour expiry:\nuser = auth.refresh(user['refreshToken'])\n# now we have a fresh token\nuser['idToken']\n```\n\n### Custom tokens\n\nYou can also create users using [custom tokens](https://firebase.google.com/docs/auth/server/create-custom-tokens), for example:\n```\ntoken = auth.create_custom_token(\"your_custom_id\")\n```\nYou can also pass in additional claims.\n```\ntoken_with_additional_claims = auth.create_custom_token(\"your_custom_id\", {\"premium_account\": True})\n```\nYou can then send these tokens to the client to sign in, or sign in as the user on the server.\n```\nuser = auth.sign_in_with_custom_token(token)\n```\n\n### Manage Users\n\n#### Creating users\n\n```python\nauth.create_user_with_email_and_password(email, password)\n```\nNote: Make sure you have the Email/password provider enabled in your Firebase dashboard under Auth -> Sign In Method.\n\n#### Verifying emails\n\n```python\nauth.send_email_verification(user['idToken'])\n```\n\n#### Sending password reset emails\n\n```python\nauth.send_password_reset_email(\"email\")\n```\n\n#### Get account information\n```python\nauth.get_account_info(user['idToken'])\n```\n\n#### Refreshing tokens\n```python\nuser = auth.refresh(user['refreshToken'])\n```\n\n## Database\n\nYou can build paths to your data by using the ```child()``` method.\n\n```python\ndb = firebase.database()\ndb.child(\"users\").child(\"Morty\")\n```\n\n### Save Data\n\n#### push\n\nTo save data with a unique, auto-generated, timestamp-based key, use the ```push()``` method.\n\n```python\ndata = {\"name\": \"Mortimer 'Morty' Smith\"}\ndb.child(\"users\").push(data)\n```\n\n#### set\n\nTo create your own keys use the ```set()``` method. The key in the example below is \"Morty\".\n\n```python\ndata = {\"name\": \"Mortimer 'Morty' Smith\"}\ndb.child(\"users\").child(\"Morty\").set(data)\n```\n\n#### update\n\nTo update data for an existing entry use the ```update()``` method.\n\n```python\ndb.child(\"users\").child(\"Morty\").update({\"name\": \"Mortiest Morty\"})\n```\n\n#### remove\n\nTo delete data for an existing entry use the ```remove()``` method.\n\n```python\ndb.child(\"users\").child(\"Morty\").remove()\n```\n\n#### multi-location updates\n\nYou can also perform [multi-location updates](https://www.firebase.com/blog/2015-09-24-atomic-writes-and-more.html) with the ```update()``` method.\n\n```python\ndata = {\n \"users/Morty/\": {\n \"name\": \"Mortimer 'Morty' Smith\"\n },\n \"users/Rick/\": {\n \"name\": \"Rick Sanchez\"\n }\n}\n\ndb.update(data)\n```\n\nTo perform multi-location writes to new locations we can use the ```generate_key()``` method.\n\n```python\ndata = {\n \"users/\"+ref.generate_key(): {\n \"name\": \"Mortimer 'Morty' Smith\"\n },\n \"users/\"+ref.generate_key(): {\n \"name\": \"Rick Sanchez\"\n }\n}\n\ndb.update(data)\n```\n\n### Retrieve Data\n\n#### val\nQueries return a PyreResponse object. Calling ```val()``` on these objects returns the query data.\n\n```\nusers = db.child(\"users\").get()\nprint(users.val()) # {\"Morty\": {\"name\": \"Mortimer 'Morty' Smith\"}, \"Rick\": {\"name\": \"Rick Sanchez\"}}\n```\n\n#### key\nCalling ```key()``` returns the key for the query data.\n\n```\nuser = db.child(\"users\").get()\nprint(user.key()) # users\n```\n\n#### each\nReturns a list of objects on each of which you can call ```val()``` and ```key()```.\n\n```\nall_users = db.child(\"users\").get()\nfor user in all_users.each():\n print(user.key()) # Morty\n print(user.val()) # {name\": \"Mortimer 'Morty' Smith\"}\n```\n\n#### get\n\nTo return data from a path simply call the ```get()``` method.\n\n```python\nall_users = db.child(\"users\").get()\n```\n\n#### shallow\n\nTo return just the keys at a particular path use the ```shallow()``` method.\n\n```python\nall_user_ids = db.child(\"users\").shallow().get()\n```\n\nNote: ```shallow()``` can not be used in conjunction with any complex queries.\n\n#### streaming\n\nYou can listen to live changes to your data with the ```stream()``` method.\n\n```python\ndef stream_handler(message):\n print(message[\"event\"]) # put\n print(message[\"path\"]) # /-K7yGTTEp7O549EzTYtI\n print(message[\"data\"]) # {'title': 'pyfireconnect', \"body\": \"etc...\"}\n\nmy_stream = db.child(\"posts\").stream(stream_handler)\n```\n\nYou should at least handle `put` and `patch` events. Refer to [\"Streaming from the REST API\"][streaming] for details.\n\n[streaming]: https://firebase.google.com/docs/reference/rest/database/#section-streaming\n\nYou can also add a ```stream_id``` to help you identify a stream if you have multiple running:\n\n```\nmy_stream = db.child(\"posts\").stream(stream_handler, stream_id=\"new_posts\")\n```\n\n#### close the stream\n\n```python\nmy_stream.close()\n```\n\n### Complex Queries\n\nQueries can be built by chaining multiple query parameters together.\n\n```python\nusers_by_name = db.child(\"users\").order_by_child(\"name\").limit_to_first(3).get()\n```\nThis query will return the first three users ordered by name.\n\n#### order_by_child\n\nWe begin any complex query with ```order_by_child()```.\n\n```python\nusers_by_name = db.child(\"users\").order_by_child(\"name\").get()\n```\nThis query will return users ordered by name.\n\n#### equal_to\n\nReturn data with a specific value.\n\n```python\nusers_by_score = db.child(\"users\").order_by_child(\"score\").equal_to(10).get()\n```\nThis query will return users with a score of 10.\n\n#### start_at and end_at\n\nSpecify a range in your data.\n\n```python\nusers_by_score = db.child(\"users\").order_by_child(\"score\").start_at(3).end_at(10).get()\n```\nThis query returns users ordered by score and with a score between 3 and 10.\n\n#### limit_to_first and limit_to_last\n\nLimits data returned.\n\n```python\nusers_by_score = db.child(\"users\").order_by_child(\"score\").limit_to_first(5).get()\n```\nThis query returns the first five users ordered by score.\n\n#### order_by_key\n\nWhen using ```order_by_key()``` to sort your data, data is returned in ascending order by key.\n\n```python\nusers_by_key = db.child(\"users\").order_by_key().get()\n```\n\n#### order_by_value\n\nWhen using ```order_by_value()```, children are ordered by their value.\n\n```python\nusers_by_value = db.child(\"users\").order_by_value().get()\n```\n\n\n## Storage\n\nThe storage service allows you to upload images to Firebase.\n\n### child\n\nJust like with the Database service, you can build paths to your data with the Storage service.\n\n```python\nstorage.child(\"images/example.jpg\")\n```\n\n### put\n\nThe put method takes the path to the local file and an optional user token.\n\n```python\nstorage = firebase.storage()\n# as admin\nstorage.child(\"images/example.jpg\").put(\"example2.jpg\")\n# as user\nstorage.child(\"images/example.jpg\").put(\"example2.jpg\", user['idToken'])\n```\n\n### download\n\nThe download method takes the path to the saved database file and the name you want the downloaded file to have.\n\n```\nstorage.child(\"images/example.jpg\").download(\"downloaded.jpg\")\n```\n\n### get_url\n\nThe get_url method takes the path to the saved database file and returns the storage url.\n\n```\nstorage.child(\"images/example.jpg\").get_url()\n# https://firebasestorage.googleapis.com/v0/b/storage-url.appspot.com/o/images%2Fexample.jpg?alt=media\n```\n\n### Helper Methods\n\n#### generate_key\n\n```db.generate_key()``` is an implementation of Firebase's [key generation algorithm](https://www.firebase.com/blog/2015-02-11-firebase-unique-identifiers.html).\n\nSee multi-location updates for a potential use case.\n\n#### sort\n\nSometimes we might want to sort our data multiple times. For example, we might want to retrieve all articles written between a\ncertain date then sort those articles based on the number of likes.\n\nCurrently the REST API only allows us to sort our data once, so the ```sort()``` method bridges this gap.\n\n```python\narticles = db.child(\"articles\").order_by_child(\"date\").start_at(startDate).end_at(endDate).get()\narticles_by_likes = db.sort(articles, \"likes\")\n```\n\n### Common Errors\n\n#### Index not defined\n\nIndexing is [not enabled](https://www.firebase.com/docs/security/guide/indexing-data.html) for the database reference.\n\nThis project is forked from https://github.com/thisbejim/pyrebase", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/quintuslabs/pyfireconnect", "keywords": "Firebase,Python 3", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "pyfireconnect", "package_url": "https://pypi.org/project/pyfireconnect/", "platform": "", "project_url": "https://pypi.org/project/pyfireconnect/", "project_urls": { "Homepage": "https://github.com/quintuslabs/pyfireconnect" }, "release_url": "https://pypi.org/project/pyfireconnect/1.0.3/", "requires_dist": null, "requires_python": "", "summary": "A simple python wrapper for the Firebase API compatible with Python 3.7, supports social signup", "version": "1.0.3" }, "last_serial": 5999616, "releases": { "1.0.0": [], "1.0.1": [ { "comment_text": "", "digests": { "md5": "94cd7d3cfc6e72f425411d02b02de615", "sha256": "11ce06eefdf529e5e04329e15b25e25d0827b76f7f319de96f7ee1ce06fedf59" }, "downloads": -1, "filename": "pyfireconnect-1.0.1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "94cd7d3cfc6e72f425411d02b02de615", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 12193, "upload_time": "2019-09-23T04:12:22", "url": "https://files.pythonhosted.org/packages/0e/d0/90bb3f5e97d1888ead408190d26f0525b619c077cf3af8030394d0159cb7/pyfireconnect-1.0.1-py2.py3-none-any.whl" } ], "1.0.2": [ { "comment_text": "", "digests": { "md5": "c41b52b952aaec701da2b2bb990061d7", "sha256": "fe6a49c030ee6dc8e48dd3ae4f64e50b2b25f42dbbfc3ce4d28909ab55a47eb2" }, "downloads": -1, "filename": "pyfireconnect-1.0.2-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "c41b52b952aaec701da2b2bb990061d7", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 12644, "upload_time": "2019-09-27T15:23:33", "url": "https://files.pythonhosted.org/packages/32/6c/760f5bb3b1eda159553eaba2b163d8ae1f2db22a8f24d4ec5fde118eb127/pyfireconnect-1.0.2-py2.py3-none-any.whl" } ], "1.0.3": [ { "comment_text": "", "digests": { "md5": "bee9f86d2e1823802f0580293ff9bf25", "sha256": "68b2f5c47760ed6d4a75700d021b63ebf44bc33c3c6f3b61b6e505f7e3585904" }, "downloads": -1, "filename": "pyfireconnect-1.0.3.tar.gz", "has_sig": false, "md5_digest": "bee9f86d2e1823802f0580293ff9bf25", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 16795, "upload_time": "2019-10-19T11:24:41", "url": "https://files.pythonhosted.org/packages/35/b3/4532a1ad7de2fed22a18fc19d180e72aedefc000ee54790cbe5f46546401/pyfireconnect-1.0.3.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "bee9f86d2e1823802f0580293ff9bf25", "sha256": "68b2f5c47760ed6d4a75700d021b63ebf44bc33c3c6f3b61b6e505f7e3585904" }, "downloads": -1, "filename": "pyfireconnect-1.0.3.tar.gz", "has_sig": false, "md5_digest": "bee9f86d2e1823802f0580293ff9bf25", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 16795, "upload_time": "2019-10-19T11:24:41", "url": "https://files.pythonhosted.org/packages/35/b3/4532a1ad7de2fed22a18fc19d180e72aedefc000ee54790cbe5f46546401/pyfireconnect-1.0.3.tar.gz" } ] }