Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DRY validation in Bottle?

My Bottle apps haven't been very DRY, here's a test-case:

from uuid import uuid4
from bottle import Bottle, response

foo_app = Bottle()

@foo_app.post('/foo')
def create():
    if not request.json:
        response.status = 400
        return {'error': 'ValidationError', 'error_message': 'Body required'}
    body = request.json
    body.update({'id': uuid4().get_hex())
    # persist to db
    # ORM might set 'id' on the Model layer rather than setting it here
    # ORM will validate, as will db, so wrap this in a try/catch
    response.status = 201
    return body

@foo_app.put('/foo/<id>')
def update(id):
    if not request.json:
        response.status = 400
        return {'error': 'ValidationError', 'error_message': 'Body required'}
    elif 'id' not in request.json:
        response.status = 400
        return {'error': 'ValidationError', 'error_message': '`id` required'}
    db = {} # should be actual db cursor or whatever
    if 'id' not in db:
        response.status = 404
        return {'error': 'Not Found',
                'error_message': 'Foo `id` "{id}" not found'.format(id)}
    body = request.json
    # persist to db, return updated object
    # another try/catch here in case of update error (from ORM and/or db)
    return body

One way of solving this issue is to have a global error handler, and raise errors all over the place.

Another is to use decorators, which also have overhead issues.

Is there a better way of doing the validation side of each route? - I'm thinking of something like:

foo_app.post('/foo', middleware=[HAS_BODY_F, ID_IN_DB_F])
like image 857
A T Avatar asked Oct 20 '22 10:10

A T


1 Answers

Ended up finding Bottle's built-in "middleware" called "plugins" (reference):

from bottle import Bottle, request, response

app = Bottle()


def has_body(f):
    def inner(*args, **kwargs):
        if request.json:
            return f(*args, **kwargs)

        response.status = 400
        return {'error': 'ValidationError',
                'error_message': 'Body is required (and must be JSON).'}
    return inner


def body_req(required):
    def body_req_middleware(f):
        def inner(*args, **kwargs):
            intersection = required.intersection(set(request.json.keys()))
            if intersection != required:
                response.status = 400
                return {'error': 'ValidationError',
                        'error_message': 'Key(s): {} are not in JSON payload'
                        ''.format(', '.join('{!r}'.format(k)
                                            for k in required - intersection))}
            return f(*args, **kwargs)
        return inner
    return body_req_middleware


@app.post('/foo', apply=(has_body, body_req({'id', 'f'})))
def get_foo():
    return {'foo': 'bar'}
like image 154
A T Avatar answered Oct 22 '22 02:10

A T