Giter Site home page Giter Site logo

falcon_sugar's Introduction

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.

falcon_sugar's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

falcon_sugar's Issues

Resource don't work with @falcon.before decorator

Hi

this is the code

class TestAuthResource(Resource):
    
    @falcon.before(authenticate(True))
    def on_get(self, req, resp):
        """Handles GET requests"""
        print("pass")
        return {'message': 'authentication work'}

and this is the authenticate function

def authenticate(val=False):
    def hook(req, resp, resource, params):
        if val is True:
            token = req.get_header('Authorization')
            try:
                payload = jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM])
            except (jwt.DecodeError, jwt.ExpiredSignatureError):
                description = ('Please provide a valid auth token '
                               'as part of the request.')
                raise falcon.HTTPUnauthorized('Auth token not valid', description)

            if not _token_is_valid(payload['email']):
                description = ('The provided auth token is not valid. '
                               'Please request a new token and try again.')
                raise falcon.HTTPUnauthorized('User not valid', description)

    return hook

authenticate is quite simple, take the authorization header and check if is correct or not
if not raise an error if yes, at the moment do nothing

when i test the get method
the print return "pass", so is confirmed that is routed to the get method but the header is a 204, no content, so the Resource method don't take the return

thanks in advance

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.