Skip to main content

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.

flask-restful

Document

click me

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/<todo_id:string>')


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

Metadata

Release files for sanic-restful-api 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sanic-restful-api 0.2.1
File Size Uploaded
sanic-restful-api-0.2.1.tar.gz 19.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sanic-restful-api 0.2.1
File Interpreter ABI Platform
sanic_restful_api-0.2.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 40.2 kB

Release files / sanic-restful-api-0.2.1.tar.gz

Download URL sanic-restful-api-0.2.1.tar.gz
Size 19.2 kB
Tags Source
SHA-256 checksum
How to use checksums
08d4ccbe1b9eb5159da912c8b7256de0b2a868f039be3856744e2c1a85303c78
BLAKE2b-256 checksum
How to use checksums
27f5b1fc8d4b2173aae8b02cae33b262968595486bf28f864ba6f303314b3bc9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.0

Release files / sanic_restful_api-0.2.1-py2.py3-none-any.whl

Download URL sanic_restful_api-0.2.1-py2.py3-none-any.whl
Size 21.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
7484297d6d23ffff91e92ccdbca87f417ea703e8b14c878eeffc86715f6f6788
BLAKE2b-256 checksum
How to use checksums
f96ff5df005b520445c066174644a5686bbf5fba128d18e58bf468fb06b35ba7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.0

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.3

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release 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