A little bit of sugar for Falcon apps.
pipenv install falcon_sugar
or
pipenv install falcon_sugar[marshmallow]
This class lets you write request handlers that return
their results
rather than writing to resp.media
:
import falcon
from falcon_sugar import Resource
# This:
class People(Resource):
def on_get(self, req, resp):
return {"people": []}
def on_post(self, req, resp):
return falcon.HTTP_201, {}
class Person(Resource):
def on_delete(self, req, resp, pk):
pass
# Instead of this:
class People:
def on_get(self, req, resp):
resp.media = {"people": []}
def on_post(self, req, resp):
resp.status = falcon.HTTP_201
resp.media = {}
class Person(Resource):
def on_delete(self, req, resp, pk):
resp.status = falcon.HTTP_201
A Marshmallow-based validator.
from falcon_sugar import Resource, marshmallow
from marshmallow import Schema, fields, post_load, validate
class PersonSchema(Schema):
id = fields.Integer(dump_only=True)
name = fields.String(required=True)
age = fields.Integer(required=True, validate=[validate.Range(0, 130)])
@post_load
def make_person(self, data):
return PersonModel(**data)
class People(Resource):
person_schema = PersonSchema()
@marshmallow.dump(person_schema, many=True)
def on_get(self, req, resp):
return [PersonModel(id=1, name="Jim Gordon", age=36)]
@marshmallow.validate(person_schema)
@marshmallow.dump(person_schema)
def on_post(self, req, resp):
person = req.context["marshmallow"]
person.save()
return falcon.HTTP_201, person
Any decorator that doesn't return the result of the decorated function
will make it so that Resource
s don't return a result. This means
that builtin Falcon decorators like before
and after
are
incompatible with this library.
falcon_sugar is licensed under Apache 2.0. Please see LICENSE for licensing details.