Last active
December 16, 2017 20:16
-
-
Save hiranya911/a0384400bab3c025e1f416000b9de40c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# File: superheroes.py | |
import firebase_admin | |
from firebase_admin import db | |
import flask | |
app = flask.Flask(__name__) | |
firebase_admin.initialize_app(options={ | |
'databaseURL': 'https://<DB_NAME>.firebaseio.com' | |
}) | |
SUPERHEROES = db.reference('superheroes') | |
@app.route('/heroes', methods=['POST']) | |
def create_hero(): | |
req = flask.request.json | |
hero = SUPERHEROES.push(req) | |
return flask.jsonify({'id': hero.key}), 201 | |
@app.route('/heroes/<id>') | |
def read_hero(id): | |
return flask.jsonify(_ensure_hero(id)) | |
@app.route('/heroes/<id>', methods=['PUT']) | |
def update_hero(id): | |
_ensure_hero(id) | |
req = flask.request.json | |
SUPERHEROES.child(id).update(req) | |
return flask.jsonify({'success': True}) | |
@app.route('/heroes/<id>', methods=['DELETE']) | |
def delete_hero(id): | |
_ensure_hero(id) | |
SUPERHEROES.child(id).delete() | |
return flask.jsonify({'success': True}) | |
def _ensure_hero(id): | |
hero = SUPERHEROES.child(id).get() | |
if not hero: | |
flask.abort(404) | |
return hero |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment