A Flask extension that forces extremely skinny controllers.
@app.route("/dogs/<int:dog_id>", methods=["POST"])
@skinny.responses({200: responses.greet_dog, 400: responses.bad_request})
@skinny.intent(intents.greet_dog)
def greet_dog(dog_id):
pass
This extension contains two decorators, @skinny.intent
and @skinny.responses
.
@skinny.intent(callable)
callable
: Receivesflask.request
, returnsstatus_code
andoutcome
.
@skinny.responses({status_code: callable, ...})
callable
: Receivesflask.request
andoutcome
, returnsheaders
andbody
.
@skinny.responses
will choose an appropriate response by status_code
.
from flask import Flask
from flask_skinny import skinny
from random import randint
import json
def intent(request):
if randint(0, 1) == 0:
status_code = 200
outcome = "OK"
else:
status_code = 403
outcome = "Forbidden"
return status_code, outcome
def response(request, outcome):
headers = {"content-type": "application/json"}
body = json.dumps({"message": outcome}) + "\n"
return headers, body
app = Flask(__name__)
@app.route("/", methods=["GET"])
@skinny.responses({200: response, 403: response})
@skinny.intent(intent)
def index():
pass
See app.py.
$ pip install flask-skinny
Create new Pull Request.