Skip to main content

Simple framework for aiomysql

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.

flask-restful

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


Download files

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

Source Distribution

aiomysql-core-0.0.1.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

aiomysql_core-0.0.1-py2.py3-none-any.whl (4.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file aiomysql-core-0.0.1.tar.gz.

File metadata

  • Download URL: aiomysql-core-0.0.1.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for aiomysql-core-0.0.1.tar.gz
Algorithm Hash digest
SHA256 0dc3e514a9a445a3bb21cc6cffb08f11b14b176ac11d6cb103f1c6c4b329b476
MD5 638a2f46eebb3fe68977d9dfc22d45d3
BLAKE2b-256 a4e055371f51a2df0d497b8734ba04e2102b4c415dc5214773fb77b690611458

See more details on using hashes here.

File details

Details for the file aiomysql_core-0.0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: aiomysql_core-0.0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for aiomysql_core-0.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 85a94055f7f31a25657d8eabd3fc1af59d5ccded8a38a733d9eb178b268ba8d6
MD5 aafa5e8489524a5dd474f78afd5fabba
BLAKE2b-256 e42a8dc18aca44756cf1d6a37c81bea0487e34c5b04797b2953b2dfb488ff64a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page