Skip to main content

Helpers, syntaxic sugar and Swagger documentation for Flask-Restful

Project description

Flask-RestPlus provide syntaxic suger, helpers and automatically generated Swagger documentation on top of Flask-Restful.

Compatibility

Flask-RestPlus requires Python 2.7+.

Installation

You can install Flask-Restplus with pip:

$ pip install flask-restplus

or with easy_install:

$ easy_install flask-restplus

Quick start

With Flask-Restplus, you only import the api instance to route and document your endpoints.

from flask import Flask
from flask.ext.restplus import Api, Resource, fields

app = Flask(__name__)
api = Api(app, version='1.0', title='Todo API',
    description='A simple TODO API extracted from the original flask-restful example',
)

ns = api.namespace('todos', description='TODO operations')

TODOS = {
    'todo1': {'task': 'build an API'},
    'todo2': {'task': '?????'},
    'todo3': {'task': 'profit!'},
}

todo = api.model('Todo', {
    'task': fields.String(required=True, description='The task details')
})

listed_todo = api.model('ListedTodo', {
    'id': fields.String(required=True, description='The todo ID'),
    'todo': fields.Nested(todo, description='The Todo')
})


def abort_if_todo_doesnt_exist(todo_id):
    if todo_id not in TODOS:
        api.abort(404, "Todo {} doesn't exist".format(todo_id))

parser = api.parser()
parser.add_argument('task', type=str, required=True, help='The task details', location='form')


@ns.route('/<string:todo_id>')
@api.doc(responses={404: 'Todo not found'}, params={'todo_id': 'The Todo ID'})
class Todo(Resource):
    '''Show a single todo item and lets you delete them'''
    @api.doc(description='todo_id should be in {0}'.format(', '.join(TODOS.keys())))
    @api.marshal_with(todo)
    def get(self, todo_id):
        '''Fetch a given resource'''
        abort_if_todo_doesnt_exist(todo_id)
        return TODOS[todo_id]

    @api.doc(responses={204: 'Todo deleted'})
    def delete(self, todo_id):
        '''Delete a given resource'''
        abort_if_todo_doesnt_exist(todo_id)
        del TODOS[todo_id]
        return '', 204

    @api.doc(parser=parser)
    @api.marshal_with(todo)
    def put(self, todo_id):
        '''Update a given resource'''
        args = parser.parse_args()
        task = {'task': args['task']}
        TODOS[todo_id] = task
        return task


@ns.route('/')
class TodoList(Resource):
    '''Shows a list of all todos, and lets you POST to add new tasks'''
    @api.marshal_list_with(listed_todo)
    def get(self):
        '''List all todos'''
        return [{'id': id, 'todo': todo} for id, todo in TODOS.items()]

    @api.doc(parser=parser)
    @api.marshal_with(todo, code=201)
    def post(self):
        '''Create a todo'''
        args = parser.parse_args()
        todo_id = 'todo%d' % (len(TODOS) + 1)
        TODOS[todo_id] = {'task': args['task']}
        return TODOS[todo_id], 201


if __name__ == '__main__':
    app.run(debug=True)

Documentation

The documentation is hosted on Read the Docs

Changelog

0.6.0

  • Python 2.6 support

  • Experimental polymorphism support (single inheritance only)
    • Added Polymorph field

    • Added discriminator attribute support on String fields

    • Added api.inherit() method

  • Added ClassName field

0.5.1

  • Fix for parameter with schema (do not set type=string)

0.5.0

  • Allow shorter syntax to set operation id: @api.doc('my-operation')

  • Added a shortcut to specify the expected input model: @api.expect(my_fields)

  • Added title attribute to fields

  • Added @api.extend() to extend models

  • Ensure coherence between required and allow_null for NestedField

  • Support list of primitive types and list of models as body

  • Upgraded to latest version of Swagger UI

  • Fixes

0.4.2

  • Rename apidoc blueprint into restplus_doc to avoid collisions

0.4.1

  • Added SWAGGER_VALIDATOR_URL config parameter

  • Added readonly field parameter

  • Upgraded to latest version of Swagger UI

0.4.0

  • Port to Flask-Restful 0.3+

  • Use the default Blueprint/App mecanism

  • Allow to hide some ressources or methods using @api.doc(False) or @api.hide

  • Allow to globally customize the default operationId with the default_id callable parameter

0.3.0

  • Switch to Swagger 2.0 (Major breakage)
    • notes documentation is now description

    • nickname documentation is now id

    • new responses declaration format

  • Added missing body parameter to document body input

  • Last release before Flask-Restful 0.3+ compatibility switch

0.2.4

  • Handle description and required attributes on fields.List

0.2.3

  • Fix custom fields registeration

0.2.2

  • Fix model list in declaration

0.2.1

  • Allow to type custom fields with Api.model

  • Handle custom fields into fieds.List

0.2

  • Upgraded to SwaggerUI 0.2.22

  • Support additional field documentation attributes: required, description, enum, min, max and default

  • Initial support for model in RequestParser

0.1.3

  • Fix Api.marshal() shortcut

0.1.2

  • Added Api.marshal_with() and Api.marshal_list_with() decorators

  • Added Api.marshal() shortcut

0.1.1

  • Use zip_safe=False for proper packaging.

0.1

  • Initial release

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-restplus-0.6.0.tar.gz (435.8 kB view details)

Uploaded Source

Built Distribution

flask_restplus-0.6.0-py2.py3-none-any.whl (443.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file flask-restplus-0.6.0.tar.gz.

File metadata

File hashes

Hashes for flask-restplus-0.6.0.tar.gz
Algorithm Hash digest
SHA256 907461f10ef4349ce0ce294fbef8d7399e3371cbef8469083b61e7c516b6a459
MD5 006bfa4463fb2228daa0ec7ffaf262a1
BLAKE2b-256 d2eb7154f42b5b2c9b909556ca6e965926002d57b1c87c0fce92adfd414135bb

See more details on using hashes here.

File details

Details for the file flask_restplus-0.6.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for flask_restplus-0.6.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9ea90fd69c0ea7afb91f01b4bd08661c31850bf8171da5844f99771f8b80556a
MD5 6f4a7e58f5169079d179bb1a5394ecd3
BLAKE2b-256 752c9a41c39f386f06b5df353dba1771e0e6b17c47a182b245c42f09cef0ea13

See more details on using hashes here.

Supported by

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