Skip to main content

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.

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
from sanic_restplus.restplus import restplus
from spf import SanicPluginsFramework
app = Sanic(__name__)
spf = SanicPluginsFramework(app)
rest_assoc = spf.register_plugin(restplus)

api = Api(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)

rest_assoc.api(api)

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

Documentation

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

REMOVED Flask-Restplus changelog. See the flask-restplus changelog in the relevant parent repository.

Download files

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

Source Distribution

sanic-restplus-0.4.1.tar.gz (2.5 MB view details)

Uploaded Source

Built Distribution

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

sanic_restplus-0.4.1-py2.py3-none-any.whl (2.5 MB view details)

Uploaded Python 2Python 3

File details

Details for the file sanic-restplus-0.4.1.tar.gz.

File metadata

  • Download URL: sanic-restplus-0.4.1.tar.gz
  • Upload date:
  • Size: 2.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

File hashes

Hashes for sanic-restplus-0.4.1.tar.gz
Algorithm Hash digest
SHA256 b401c45671b3ea745f4558873e5120d17176a17725fa0492b0092d386f9e6f1b
MD5 402a6596a923a58a57621abbc6d8cac0
BLAKE2b-256 e6c63fe39519bd02b398a1cbe2aa7697fc234b316a60a6340b6bf892ff3a48b6

See more details on using hashes here.

File details

Details for the file sanic_restplus-0.4.1-py2.py3-none-any.whl.

File metadata

  • Download URL: sanic_restplus-0.4.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 2.5 MB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

File hashes

Hashes for sanic_restplus-0.4.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6d12e0eab6fa1698b184305ff5faad70fa672c2a44138f8f31f56e4316200bce
MD5 09bf1cf42e0658f308dff1c3be8af201
BLAKE2b-256 cf917ae29cbfe64ff8fc5b5f74ba851b8c1330d5b03ea207e989f48b94f9cfee

See more details on using hashes here.

Release history Release notifications | RSS feed

0.6.4

2 files

0.6.3

2 files

0.6.2

2 files

0.6.1

2 files

0.6.0

2 files

0.5.6.post1

2 files

0.5.6

2 files

0.5.5

2 files

0.5.4

2 files

0.5.3.post3

2 files

0.5.3.post2

2 files

0.5.3.post1

2 files

0.5.3

2 files

0.5.2

2 files

0.5.1.post2

2 files

0.5.1.post1

2 files

0.5.1

2 files

0.5.0

2 files

0.4.1.post1

2 files

This release

0.4.1 This release

2 files

0.4.0.post1

2 files

0.4.0

2 files

0.3.5.post1

2 files

0.3.5

2 files

0.3.4

2 files

0.3.3

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page