Skip to content

Instantly share code, notes, and snippets.

Last active Apr 26, 2016
What would you like to do?
__author__ = "Iván Corrales Solera <>"
__written_date = "24/04/2016"
import falcon
import json
from import BookDB
class BookResource(object):
def __init__(self):
self.props = ''
self.bookDB = BookDB()
def check_secret_token(req, resp, resource, params):
if req.get_header('X-MyHeader') is None:
raise falcon.HTTPBadRequest('Bad request', 'No valid')
def on_get(self,req,resp):
if req.get_param('id'):
book ='id')))
if book is None:
raise falcon.HTTPError(falcon.HTTP_404,'Invalid ID','There is not a book with this id')
resp.status = falcon.HTTP_200
resp.body = json.dumps(book.__dict__)
books = self.bookDB.all()
resp.status = falcon.HTTP_200
resp.body = json.dumps([book.__dict__ for book in books])
def on_delete(self,req,resp):
resp.status = falcon.HTTP_200
def on_post(self,req,resp):
raw_json =
except Exception as ex:
raise falcon.HTTPError(falcon.HTTP_400, 'Error', ex.message)
book = json.loads(raw_json, encoding='utf-8')
except ValueError:
raise falcon.HTTPError(falcon.HTTP_400, 'Invalid JSON','Could not decode the request body. The ''JSON was incorrect.')
resp.status = falcon.HTTP_201
resp.body = book
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment