Fully featured framework for fast, easy and documented API development with Sanic
Clone or download
Pull request Compare This branch is 38 commits ahead, 99 commits behind noirbizarre:master.
ashleysommer Fixed an issue that caused sanic-restplus to not be able to pull app-…
…wide config variables from app when the UI template was rendered.

Added a make_static.bat for windows machines, to match make_static.sh for linux.
Bumped version.
Latest commit 0359073 Apr 11, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
doc Rebased the whole sanic-restplus project onto the latest Flask-restpl… Nov 1, 2017
examples allow the API constructor to take a url to additional CSS to apply to… Nov 10, 2017
requirements Fixed an issue that caused sanic-restplus to not be able to pull app-… Apr 11, 2018
sanic_restplus Fixed an issue that caused sanic-restplus to not be able to pull app-… Apr 11, 2018
tests Rebased the whole sanic-restplus project onto the latest Flask-restpl… Nov 1, 2017
.bowerrc First cut at sanic port. Nov 1, 2017
.editorconfig Added editorconfig Jan 15, 2017
.gitignore Bumped min requirements to new sanic version 0.7.0 and new SPF. Dec 25, 2017
.pyup.yml Added pyup config Oct 24, 2017
.travis.yml Disable Python 3.3 build on Travis Oct 24, 2017
AUTHORS.rst Rebased the whole sanic-restplus project onto the latest Flask-restpl… Nov 1, 2017
CHANGELOG.rst Bumped min requirements to new sanic version 0.7.0 and new SPF. Dec 25, 2017
CONTRIBUTING.rst Add a line about the changelog in contributing guidelines Oct 19, 2016
LICENSE Give credit to the initial flask-restful contributors (fix noirbizarr… Apr 21, 2016
MANIFEST.in Rebased the whole sanic-restplus project onto the latest Flask-restpl… Nov 1, 2017
README.rst claified project status in README Nov 1, 2017
bower.json Fix an unneeded werkzeug import (these will soon be all gone) Nov 1, 2017
bumpr.rc Rebased the whole sanic-restplus project onto the latest Flask-restpl… Nov 1, 2017
coverage.rc Rebased the whole sanic-restplus project onto the latest Flask-restpl… Nov 1, 2017
make_static.bat Fixed an issue that caused sanic-restplus to not be able to pull app-… Apr 11, 2018
make_static.sh add a make_static.sh helper file, for populating the swaggerui static… Nov 1, 2017
package.json Rebased the whole sanic-restplus project onto the latest Flask-restpl… Nov 1, 2017
readthedocs.pip Misc fixes Jan 17, 2016
setup.cfg Port benchmarks to pytest-benchmark May 16, 2017
setup.py Rebased the whole sanic-restplus project onto the latest Flask-restpl… Nov 1, 2017
tasks.py Rebased the whole sanic-restplus project onto the latest Flask-restpl… Nov 1, 2017
test1.py Bumped min requirements to new sanic version 0.7.0 and new SPF. Dec 25, 2017
tox.ini Drop support for Python 3.3 Oct 23, 2017
travis-bench-after-success.sh Fix benchmark May 16, 2017

README.rst

Sanic RestPlus

Sanic-RESTPlus is an extension for Sanic that adds support for quickly building REST APIs. Sanic-RESTPlus encourages best practices with minimal setup. If you are familiar with Sanic, Sanic-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.

NOTE:

I intend to release Sanic-restplus on pypi when it is finished being ported from Flask to Sanic.

However in its current state I believe it is not yet ready to be submitted to pypi.

There are still references to modules from Flask and Werkzeug in the code, which I need to remove and provide workaround functionality. Also in its current state it will crash, a lot.

Compatibility

Sanic-RestPlus requires Python 3.5+.

Installation

In the near future, you will be able to install Sanic-Restplus with pip:

$ pip install sanic-restplus

or with easy_install:

$ easy_install sanic-restplus

Quick start

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

from sanic import Sanic
from sanic_restplus import Api, Resource, fields

app = Sanic(__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)
    async def get(self, request):
        '''List all tasks'''
        return DAO.todos

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


@ns.route('/<id:int>')
@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)
    async def get(self, request, id):
        '''Fetch a given resource'''
        return DAO.get(id)

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

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


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

Documentation

The documentation is hosted on Read the Docs That is the Flask RestPlus documentation, the Sanic-Restplus docs are not converted yet.