Skip to content
This repository has been archived by the owner on Oct 3, 2018. It is now read-only.

Bogdanp/falcon_sugar

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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.

About

A little sugar for Falcon applications.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages