Created
August 26, 2018 19:30
-
-
Save hiranya911/83ead69a111051776fada07c5249c8ef 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: main.py | |
import firebase_admin | |
from firebase_admin import db | |
import flask | |
firebase_admin.initialize_app(options={ | |
'databaseURL': 'https://<DB_NAME>.firebaseio.com', | |
}) | |
SUPERHEROES = db.reference('superheroes') | |
def create_hero(request): | |
req = request.json | |
hero = SUPERHEROES.push(req) | |
return flask.jsonify({'id': hero.key}), 201 | |
def read_hero(id): | |
hero = SUPERHEROES.child(id).get() | |
if not hero: | |
return 'Resource not found', 404 | |
return flask.jsonify(hero) | |
def update_hero(id, request): | |
hero = SUPERHEROES.child(id).get() | |
if not hero: | |
return 'Resource not found', 404 | |
req = request.json | |
SUPERHEROES.child(id).update(req) | |
return flask.jsonify({'success': True}) | |
def delete_hero(id): | |
hero = SUPERHEROES.child(id).get() | |
if not hero: | |
return 'Resource not found', 404 | |
SUPERHEROES.child(id).delete() | |
return flask.jsonify({'success': True}) | |
def heroes(request): | |
if request.path == '/' or request.path == '': | |
if request.method == 'POST': | |
return create_hero(request) | |
else: | |
return 'Method not supported', 405 | |
if request.path.startswith('/'): | |
id = request.path.lstrip('/') | |
if request.method == 'GET': | |
return read_hero(id) | |
elif request.method == 'DELETE': | |
return delete_hero(id) | |
elif request.method == 'PUT': | |
return update_hero(id, request) | |
else: | |
return 'Method not supported', 405 | |
return 'URL not found', 404 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment