Simple framework for creating REST APIs
Project description
sanic-restful
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.
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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sanic-restful-api-0.0.4.tar.gz
(33.8 kB
view hashes)
Built Distribution
Close
Hashes for sanic_restful_api-0.0.4-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ee39e403c04e4658c115da39bc54ca0ec2019b23bfa2660c4346d806713ff07 |
|
MD5 | a2977263a10c2e75ed54b6c658eda90a |
|
BLAKE2b-256 | 1410245fcc0c27b286cc085dfa7718ded7408abbf0a3a710ad79374b3531edb7 |