Skip to main content

JSON API tools for Flask

Project description

Build Status

Flask JsonTools

JSON API tools for Flask

Table of Contents

  • View Utilities

    • @jsonapi

      • JsonResponse

      • make_json_response()

  • JsonClient

View Utilities

@jsonapi

Decorate a view function that talks JSON.

Such function can return:

  • tuples of `(response, status[, headers]): to set custom status code and optionally - headers

  • Instances of `JsonResponse <#jsonresponse>`__

  • The result of helper function `make_json_response <#make_json_response>`__

Example:

from flask.ext.jsontools import jsonapi

@app.route('/users')
@jsonapi
def list_users():
    return [
        {'id': 1, 'login': 'kolypto'},
        #...
    ]

@app.route('/user/<int:id>', methods=['DELETE'])
def delete_user(id):
    return {'error': 'Access denied'}, 403

JsonResponse

Extends `flask.Request <http://flask.pocoo.org/docs/api/#incoming-request-data>`__ and encodes the response with JSON. Views decorated with `@jsonapi <#jsonapi>`__ return these objects.

Arguments:

Methods:

  • preprocess_response_data(response): Override to get custom response behavior.

  • get_json(): Get the original response data.

  • __getitem__(key): Get an item from the response data

The extra methods allows to reuse views:

from flask.ext.jsontools import jsonapi

@app.route('/user', methods=['GET'])
@jsonapi
def list_users():
    return [ { 1: 'first', 2: 'second' } ]

@app.route('/user/<int:id>', methods=['GET'])
@jsonapi
def get_user(id):
    return list_users().get_json()[id]  # Long form
    return list_users()[id]  # Shortcut

make_json_response()

Helper function that actually preprocesses view return value into `JsonResponse <#jsonresponse>`__.

Accepts rv as any of:

  • tuple of (response, status[, headers])

  • Object to encode as JSON

JsonClient

JsonClient is a JSON-aware test client: it can post JSON and parse JSON responses into `JsonResponse <#jsonresponse>`__.

from myapplication import Application
from flask.ext.jsontools import JsonClient

def JsonTest(unittest.TestCase):
    def setUp(self):
        self.app = Application(__name__)
        self.app.test_client_class = JsonClient

    def testCreateUser(self):
        with self.app.test_client() as c:
            rv = c.post('/user/', json={'name': 'kolypto'})
            # rv is JsonResponse
            rv.status_code
            rv.get_json()['user']  # Long form for the previous
            rv['user']  # Shortcut for the previous

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_jsontools-0.0.3-0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_jsontools-0.0.3-0.linux-x86_64.tar.gz (5.8 kB view details)

Uploaded Source

File details

Details for the file flask_jsontools-0.0.3-0.tar.gz.

File metadata

File hashes

Hashes for flask_jsontools-0.0.3-0.tar.gz
Algorithm Hash digest
SHA256 7a9ac3cac84561e0fee7f910a3575453dcf67d3a450cc0b36a00c3c12fec4f28
MD5 edf6b3bfb123ad6e55825f4ad8a8370b
BLAKE2b-256 699ae3e998da4b0a1300454341260d56f7eada9535ae9a45b1890187d9b0fe82

See more details on using hashes here.

File details

Details for the file flask_jsontools-0.0.3-0.linux-x86_64.tar.gz.

File metadata

File hashes

Hashes for flask_jsontools-0.0.3-0.linux-x86_64.tar.gz
Algorithm Hash digest
SHA256 567a63414b89666371540f6a05381e5532a2ae98dffac4929bb267d09bdec8ee
MD5 8b6c4eb2c4f09a18b979c5e7a874e2be
BLAKE2b-256 47b24061c4f0fb7a40b52f874ed1159f7cd901cfd2954fdf8fc40950b6d56892

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page