Sanic-Restful-Api
Simple framework for creating REST APIs
Introduce
After writing Sanic for a long time, I found that there was no framework similar to flask-restful, so I wrote one myself.
Document
Installation
pip install sanic-restful-api
Simple uses
from sanic import Sanic
from sanic_restful_api import reqparse, abort, Api, Resource
app = Sanic(__name__)
api = Api(app)
TODOS = {
'todo1': {'task': 'build an API'},
'todo2': {'task': '?????'},
'todo3': {'task': 'profit!'},
}
def abort_if_todo_doesnt_exist(todo_id):
if todo_id not in TODOS:
abort(404, message="Todo {} doesn't exist".format(todo_id))
parser = reqparse.RequestParser()
parser.add_argument('task')
# Todo
# show a single todo item and lets you delete them
class Todo(Resource):
async def get(self, request, todo_id):
abort_if_todo_doesnt_exist(todo_id)
return TODOS[todo_id]
async def delete(self, request, todo_id):
abort_if_todo_doesnt_exist(todo_id)
del TODOS[todo_id]
return '', 204
async def put(self, request, todo_id):
args = parser.parse_args(request)
task = {'task': args['task']}
TODOS[todo_id] = task
return task, 201
# TodoList
# shows a list of all todos, and lets you POST to add new tasks
class TodoList(Resource):
async def get(self, request):
return TODOS
async def post(self, request):
args = parser.parse_args(request)
todo_id = 'todo%d' % (len(TODOS) + 1)
TODOS[todo_id] = {'task': args['task']}
return TODOS[todo_id], 201
##
# Actually setup the Api resource routing here
##
api.add_resource(TodoList, '/todos')
api.add_resource(Todo, '/todos/<string:todo_id>')
if __name__ == '__main__':
app.run(debug=True)
Metadata
Release files for sanic-restful-api 0.0.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sanic-restful-api-0.0.8.tar.gz | 17.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sanic_restful_api-0.0.8-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 37.9 kB
Release files / sanic-restful-api-0.0.8.tar.gz
| Download URL | sanic-restful-api-0.0.8.tar.gz |
|---|---|
| Size | 17.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f090f983c2cb2d1422230b296aab4ab86eced3f4da848e29ba060b75c13904d1
|
|
BLAKE2b-256 checksum How to use checksums |
930b270e262fff7ffd76819917b5dbbfc870c46dd4fb0004306d632cc2f23db2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8
|
Release files / sanic_restful_api-0.0.8-py2.py3-none-any.whl
| Download URL | sanic_restful_api-0.0.8-py2.py3-none-any.whl |
|---|---|
| Size | 20.8 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
d2f0e04472a32cee7ec26998d50bfc4bccc5ac8c39378c5c71ca9f246c90bfee
|
|
BLAKE2b-256 checksum How to use checksums |
fa573fc5cde3d721d3c58ffa9219d1de809db6f691f59e220c74ba4210e4bc28
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8
|