All Projects → ashleysommer → sanic-restplus

ashleysommer / sanic-restplus

Licence: other
Fully featured framework for fast, easy and documented API development with Sanic

Projects that are alternatives of or similar to sanic-restplus

Sanic session
Provides server-backed sessions for Sanic using Redis, Memcache and more.
Stars: ✭ 131 (+23.58%)
Mutual labels:  sanic
json-head
JSON microservice for performing HEAD requests
Stars: ✭ 31 (-70.75%)
Mutual labels:  sanic
sanic-admin
sanic-admin is a command line tool for automatically restarting sanic.
Stars: ✭ 15 (-85.85%)
Mutual labels:  sanic
Owllook
owllook-小说搜索引擎
Stars: ✭ 2,163 (+1940.57%)
Mutual labels:  sanic
pait
Python Modern API Tools, fast to code
Stars: ✭ 24 (-77.36%)
Mutual labels:  sanic
sanic-wtf
Sanic meets WTForms
Stars: ✭ 24 (-77.36%)
Mutual labels:  sanic
Graphql Server
This is the core package for using GraphQL in a custom server easily
Stars: ✭ 65 (-38.68%)
Mutual labels:  sanic
microAuth
A fast, documented, and tested python3 API boilerplate
Stars: ✭ 24 (-77.36%)
Mutual labels:  sanic
pynaivechain
Python implementation of naivechain project
Stars: ✭ 18 (-83.02%)
Mutual labels:  sanic
Python36
These are python sample projects which are written in python
Stars: ✭ 28 (-73.58%)
Mutual labels:  python3-6
Asyncorm
Fully Async ORM inspired in django's
Stars: ✭ 182 (+71.7%)
Mutual labels:  sanic
Sanic
Async Python 3.7+ web server/framework | Build fast. Run fast.
Stars: ✭ 15,660 (+14673.58%)
Mutual labels:  sanic
ethereumd-proxy
Proxy client-server for Ethereum node using bitcoin JSON-RPC interface.
Stars: ✭ 21 (-80.19%)
Mutual labels:  sanic
Sanic Cors
A Sanic extension for handling Cross Origin Resource Sharing (CORS), making cross-origin AJAX possible. Based on flask-cors by Cory Dolphin.
Stars: ✭ 143 (+34.91%)
Mutual labels:  sanic
sanic-plugin-toolkit
Easily create Plugins for Sanic!
Stars: ✭ 49 (-53.77%)
Mutual labels:  sanic
Sanic Nginx Docker Example
Sanic + Nginx + Docker basic example
Stars: ✭ 77 (-27.36%)
Mutual labels:  sanic
Metis
测试题小程序 包含后端api接口 可能会改成gitbook应用了吧
Stars: ✭ 79 (-25.47%)
Mutual labels:  sanic
sanic-graphql-example
Sanic using Graphsql + SQLAlchemy example
Stars: ✭ 21 (-80.19%)
Mutual labels:  sanic
robot-mind-meld
A little game powered by word vectors
Stars: ✭ 31 (-70.75%)
Mutual labels:  sanic
sanic-ext
Extended Sanic functionality
Stars: ✭ 26 (-75.47%)
Mutual labels:  sanic

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.

Compatibility

  • Sanic-RestPlus requires Python 3.7+.
  • Sanic-RestPlus works with Sanic v21.3+

Important Compatibility Notice

Sanic-RestPlus version 0.6.0 was reworked and now requires Sanic v21.3 or later.

Sanic-RestPlus version 0.4.1 (and previous versions) does not work on Sanic 19.12+, see this bug here: ashleysommer/sanic-plugin-toolkit#15

Please use Sanic-Restplus v0.5.x if you need to deploy on Sanic v19.12 or v20.12

If you are using the Sanic v20.12LTS, please use Sanic-RestPlus v0.5.6.

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 sanic_plugin_toolkit import SanicPluginRealm
app = Sanic(__name__)
realm = SanicPluginRealm(app)
rest_assoc = realm.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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].