Skip to content

Instantly share code, notes, and snippets.

@hiranya911
Created August 26, 2018 19:30
Show Gist options
  • Save hiranya911/83ead69a111051776fada07c5249c8ef to your computer and use it in GitHub Desktop.
Save hiranya911/83ead69a111051776fada07c5249c8ef to your computer and use it in GitHub Desktop.
# 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