This repository has been archived by the owner. It is now read-only.
A little sugar for Falcon applications.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
falcon_sugar
tests
.bumpversion.cfg
.codeclimate.yml
.gitignore
.travis.yml
LICENSE
Pipfile
Pipfile.lock
README.md
setup.cfg
setup.py
test.py

README.md

falcon_sugar

Build Status Maintainability Test Coverage

A little bit of sugar for Falcon apps.

Installation

pipenv install falcon_sugar

or

pipenv install falcon_sugar[marshmallow]

Usage

falcon_sugar.Resource

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

falcon_sugar.marshmallow

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

Limitations

Any decorator that doesn't return the result of the decorated function will make it so that Resources don't return a result. This means that builtin Falcon decorators like before and after are incompatible with this library.

License

falcon_sugar is licensed under Apache 2.0. Please see LICENSE for licensing details.