jsonapi.flask

The py-jsonapi extension for flask. Binding the API to a flask application is dead simple:

import flask
import jsonapi
import jsonapi.flask

app = flask.Flask(__name__)
api = jsonapi.flask.FlaskAPI("/api", db=..., flask_app=app)

# You can also initialize the API with the flask application using the
# *init_app()* method:
api.init_app(app)

You can add the models to the API as usual. They will be available under /api.

current_api

You can access the current APi via the extensions dictionary of the flask application:

app.extensions["jsonapi"]

or you use the global variable current_api:

from jsonapi.flask import current_api

The API instance is also added to the jinja environment:

<p>
    You can download your profile
    <a href="{{ jsonapi.reverse_url('User', 'resource', id=current_user.id) }}">
        here
    </a>
</p>

API

class jsonapi.flask.FlaskAPI(uri, db, settings=None, flask_app=None)[source]

Bases: jsonapi.base.api.API

Implements the API for flask. You can provide the flask application later via init_app().

debug

This is a proxy for the flask_app debug attribute. This means, that you must enable the debug mode on the flask application.

flask_app

The flask application, this API is registered on.

init_app(app)[source]

Registers this API on the flask application app.

Parameters:app (flask.Flask) –
handle_request(path=None)[source]

Handles a request to the API.

jsonapi.flask.current_api = <LocalProxy unbound>

Acts as a proxy for a werkzeug local. Forwards all operations to a proxied object. The only operations not supported for forwarding are right handed operands and any kind of assignment.

Example usage:

from werkzeug.local import Local
l = Local()

# these are proxies
request = l('request')
user = l('user')


from werkzeug.local import LocalStack
_response_local = LocalStack()

# this is a proxy
response = _response_local()

Whenever something is bound to l.user / l.request the proxy objects will forward all operations. If no object is bound a RuntimeError will be raised.

To create proxies to Local or LocalStack objects, call the object as shown above. If you want to have a proxy to an object looked up by a function, you can (as of Werkzeug 0.6.1) pass a function to the LocalProxy constructor:

session = LocalProxy(lambda: get_current_request().session)

Changed in version 0.6.1: The class can be instanciated with a callable as well now.