Framework-specific instructions

Django Rest Framework

Django Rest Framework works straight out of the box. Simply add @qval() to your views or use validate() inside.

Django

For Django without DRF you may need to add the exception handler to settings.MIDDLEWARE. Qval attempts to do it automatically if DJANO_SETTINGS_MODULE is set. Otherwise, you’ll see the following message:

WARNING:root:Unable to add the APIException middleware to the MIDDLEWARE list. Django does not
support handling APIException without DRF integration. Define DJANGO_SETTINGS_MODULE or
add 'qval.framework_integration.HandleAPIExceptionDjango' to the MIDDLEWARE list.

Take a look at the plain Django example here.

Flask

If you are using Flask, you will need to setup the exception handlers:

from flask import Flask
from qval.framework_integration import setup_flask_error_handlers
...
app = Flask(__name__)
setup_flask_error_handlers(app)

Since request in Flask is a global object, you may want to curry @qval() before usage:

from flask import request
from qval import qval_curry

# Firstly, curry `qval()`
qval = qval_curry(request)
...

# Then use it as a decorator.
# Note: you view now must accept `request` as its first argument
@app.route(...)
@qval(...)
def view(request, params):
...

Check out the full Flask example. You can run the example using the command below:

$ PYTHONPATH=. FLASK_APP=examples/flask-example.py flask run

Falcon

Similarly to Flask, with Falcon you will need to setup the error handlers:

import falcon
from qval.framework_integration import setup_falcon_error_handlers
...
app = falcon.API()
setup_falcon_error_handlers(app)

Full Falcon example can be found in the github repository.

Use the following command to run the app:

$ PYTHONPATH=. python examples/falcon-example.py