Fully featured framework for fast, easy and documented API development with Flask
Python Other
Clone or download
Latest commit 9032694 Jun 2, 2018
Permalink
Failed to load latest commit information.
doc Make the order preservation an optionnal parameter May 16, 2018
examples Use the new Flask 0.11/1.0 extension naming convention Jun 3, 2016
flask_restplus Add 'Namespace' to export list May 24, 2018
requirements use environment markers to specify enum34 requirement to fix pip install May 22, 2018
tests Make the order preservation an optionnal parameter May 16, 2018
.editorconfig Added editorconfig Jan 15, 2017
.gitignore Ignore benchmark and profiling results Oct 24, 2017
.pyup.yml Added pyup config Oct 24, 2017
.travis.yml Disable Python 3.3 build on Travis Oct 24, 2017
AUTHORS.rst Added namespace parsing of authorizations Mar 6, 2018
CHANGELOG.rst Update to version 0.11.0.dev for next development cycle May 16, 2018
CONTRIBUTING.rst Add a line about the changelog in contributing guidelines Oct 19, 2016
LICENSE Change license to BSD 3 Clause May 7, 2018
MANIFEST.in Upgrade to Swagger UI 3.4.0 (fix #338) Oct 24, 2017
README.rst Update to version 0.11.0.dev for next development cycle May 16, 2018
bumpr.rc Drop support for Python 3.3 Oct 23, 2017
coverage.rc Initial import Aug 14, 2014
package.json Change license to BSD 3 Clause May 7, 2018
readthedocs.pip Misc fixes Jan 17, 2016
setup.cfg Port benchmarks to pytest-benchmark May 16, 2017
setup.py use environment markers to specify enum34 requirement to fix pip install May 22, 2018
tasks.py Fix assets command May 16, 2018
tox.ini Drop support for Python 3.3 Oct 23, 2017
travis-bench-after-success.sh Fix benchmark May 16, 2017

README.rst

Flask RestPlus

Build status Code coverage Documentation status License Supported Python versions Join the chat at https://gitter.im/noirbizarre/flask-restplus

Flask-RESTPlus is an extension for Flask that adds support for quickly building REST APIs. Flask-RESTPlus encourages best practices with minimal setup. If you are familiar with Flask, Flask-RESTPlus should be easy to pick up. It provides a coherent collection of decorators and tools to describe your API and expose its documentation properly using Swagger.

Compatibility

Flask-RestPlus requires Python 2.7 or 3.4+.

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_restplus import Api, Resource, fields

app = Flask(__name__)
api = Api(app, version='1.0', title='TodoMVC API',
    description='A simple TodoMVC API',
)

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

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


class TodoDAO(object):
    def __init__(self):
        self.counter = 0
        self.todos = []

    def get(self, id):
        for todo in self.todos:
            if todo['id'] == id:
                return todo
        api.abort(404, "Todo {} doesn't exist".format(id))

    def create(self, data):
        todo = data
        todo['id'] = self.counter = self.counter + 1
        self.todos.append(todo)
        return todo

    def update(self, id, data):
        todo = self.get(id)
        todo.update(data)
        return todo

    def delete(self, id):
        todo = self.get(id)
        self.todos.remove(todo)


DAO = TodoDAO()
DAO.create({'task': 'Build an API'})
DAO.create({'task': '?????'})
DAO.create({'task': 'profit!'})


@ns.route('/')
class TodoList(Resource):
    '''Shows a list of all todos, and lets you POST to add new tasks'''
    @ns.doc('list_todos')
    @ns.marshal_list_with(todo)
    def get(self):
        '''List all tasks'''
        return DAO.todos

    @ns.doc('create_todo')
    @ns.expect(todo)
    @ns.marshal_with(todo, code=201)
    def post(self):
        '''Create a new task'''
        return DAO.create(api.payload), 201


@ns.route('/<int:id>')
@ns.response(404, 'Todo not found')
@ns.param('id', 'The task identifier')
class Todo(Resource):
    '''Show a single todo item and lets you delete them'''
    @ns.doc('get_todo')
    @ns.marshal_with(todo)
    def get(self, id):
        '''Fetch a given resource'''
        return DAO.get(id)

    @ns.doc('delete_todo')
    @ns.response(204, 'Todo deleted')
    def delete(self, id):
        '''Delete a task given its identifier'''
        DAO.delete(id)
        return '', 204

    @ns.expect(todo)
    @ns.marshal_with(todo)
    def put(self, id):
        '''Update a task given its identifier'''
        return DAO.update(id, api.payload)


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

Documentation

The documentation is hosted on Read the Docs