Handler decorators

Handler decorators in Brink help you take care of common task in a neat and convenient way.

brink.decorators.require_request_model(cls, *args, *, validate=True, **kwargs)[source]

Makes a handler require that a request body that map towards the given model is provided. Unless the validate option is set to False the data will be validated against the model’s fields.

The model will be passed to the handler as the last positional argument.

@require_request_model(Model)
async def handle_model(request, model):
    return 200, model
brink.decorators.use_ws_subhandlers(handler)[source]

Allows the handler to return any number of subhandlers that will be run in parallel. This makes it much cleaner and easier to write a handler that both listens for incoming messages on the socket connection, while also watching a changefeed from RethinkDB.

Example usage

@use_ws_subhandlers
async def handle_feed(request, ws):
    async def handle_incoming(_, ws):
        async for msg in ws:
            await Item(value=msg.data).save()

    async def handle_change(_, ws):
        async for item in await Item.changes():
            ws.send_json(item)

    return [handle_incoming, handle_change]