|
| 1 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Demonstration of the Firebase REST API in Python""" |
| 16 | + |
| 17 | +try: |
| 18 | + from functools import lru_cache |
| 19 | +except ImportError: |
| 20 | + from functools32 import lru_cache |
| 21 | +# [START rest_writing_data] |
| 22 | +import json |
| 23 | + |
| 24 | +import httplib2 |
| 25 | +from oauth2client.client import GoogleCredentials |
| 26 | + |
| 27 | +_FIREBASE_SCOPES = [ |
| 28 | + 'https://www.googleapis.com/auth/firebase.database', |
| 29 | + 'https://www.googleapis.com/auth/userinfo.email'] |
| 30 | + |
| 31 | + |
| 32 | +# Memoize the authorized http, to avoid fetching new access tokens |
| 33 | +@lru_cache() |
| 34 | +def _get_http(): |
| 35 | + """Provides an authed http object.""" |
| 36 | + http = httplib2.Http() |
| 37 | + # Use application default credentials to make the Firebase calls |
| 38 | + # https://firebase.google.com/docs/reference/rest/database/user-auth |
| 39 | + creds = GoogleCredentials.get_application_default().create_scoped( |
| 40 | + _FIREBASE_SCOPES) |
| 41 | + creds.authorize(http) |
| 42 | + return http |
| 43 | + |
| 44 | + |
| 45 | +def firebase_put(path, value=None): |
| 46 | + """Writes data to Firebase. |
| 47 | +
|
| 48 | + An HTTP PUT writes an entire object at the given database path. Updates to |
| 49 | + fields cannot be performed without overwriting the entire object |
| 50 | +
|
| 51 | + Args: |
| 52 | + path - the url to the Firebase object to write. |
| 53 | + value - a json string. |
| 54 | + """ |
| 55 | + response, content = _get_http().request(path, method='PUT', body=value) |
| 56 | + return json.loads(content) |
| 57 | + |
| 58 | + |
| 59 | +def firebase_patch(path, value=None): |
| 60 | + """Update specific children or fields |
| 61 | +
|
| 62 | + An HTTP PATCH allows specific children or fields to be updated without |
| 63 | + overwriting the entire object. |
| 64 | +
|
| 65 | + Args: |
| 66 | + path - the url to the Firebase object to write. |
| 67 | + value - a json string. |
| 68 | + """ |
| 69 | + response, content = _get_http().request(path, method='PATCH', body=value) |
| 70 | + return json.loads(content) |
| 71 | + |
| 72 | + |
| 73 | +def firebase_post(path, value=None): |
| 74 | + """Add an object to an existing list of data. |
| 75 | +
|
| 76 | + An HTTP POST allows an object to be added to an existing list of data. |
| 77 | + A successful request will be indicated by a 200 OK HTTP status code. The |
| 78 | + response content will contain a new attribute "name" which is the key for |
| 79 | + the child added. |
| 80 | +
|
| 81 | + Args: |
| 82 | + path - the url to the Firebase list to append to. |
| 83 | + value - a json string. |
| 84 | + """ |
| 85 | + response, content = _get_http().request(path, method='POST', body=value) |
| 86 | + return json.loads(content) |
| 87 | +# [END rest_writing_data] |
| 88 | + |
| 89 | + |
| 90 | +def firebase_get(path): |
| 91 | + """Read the data at the given path. |
| 92 | +
|
| 93 | + An HTTP GET request allows reading of data at a particular path. |
| 94 | + A successful request will be indicated by a 200 OK HTTP status code. |
| 95 | + The response will contain the data being retrieved. |
| 96 | +
|
| 97 | + Args: |
| 98 | + path - the url to the Firebase object to read. |
| 99 | + """ |
| 100 | + response, content = _get_http().request(path, method='GET') |
| 101 | + return json.loads(content) |
| 102 | + |
| 103 | + |
| 104 | +def firebase_delete(path): |
| 105 | + """Removes the data at a particular path. |
| 106 | +
|
| 107 | + An HTTP DELETE removes the data at a particular path. A successful request |
| 108 | + will be indicated by a 200 OK HTTP status code with a response containing |
| 109 | + JSON null. |
| 110 | +
|
| 111 | + Args: |
| 112 | + path - the url to the Firebase object to delete. |
| 113 | + """ |
| 114 | + response, content = _get_http().request(path, method='DELETE') |
0 commit comments