Skip to main content

Gotta simplify your REST APIs creation!

Sanic's built-in HTTPMethodView is a useful thing, but sometimes one wants more sugar.

Whay not?

Usage example

import logging

from sanic import Sanic

from sanic_restful import Api, ApiEndpoint

logger = logging.getLogger(__name__)

app = Sanic()


@app.middleware('response')
async def cors_headers(request, response):
    """
    Add CORS headers to all responses, including errors.
    """
    response.headers['Access-Control-Allow-Origin'] = request.headers.get('Origin', '*')
    response.headers['Access-Control-Allow-Credentials'] = 'true'
    response.headers['Access-Control-Allow-Methods'] = 'POST, PUT, PATCH, DELETE, OPTIONS, GET'
    response.headers['Access-Control-Allow-Headers'] = "X-Requested-With,X-Prototype-Version," \
                                                       "Content-Type,Cache-Control,Pragma,Origin,Cookie"
    response.headers['Access-Control-Max-Age'] = '3600'


class TodoResource(ApiEndpoint):
    # method_decorators = [auth.auth_required]
    endpoint = 'todos/<todo_id>'

    async def get(self, request, todo_id):
        return {'id': todo_id}

    async def post(self, request, todo_id):
        return {'id': todo_id}, 201


api = Api(app, [TodoResource], '/v0.9/')

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000, debug=True)

And let's try it:

$ http POST http://localhost:8000/v0.9/todos/1
HTTP/1.1 201 Created
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: X-Requested-With,X-Prototype-Version,Content-Type,Cache-Control,Pragma,Origin,Cookie
Access-Control-Allow-Methods: POST, PUT, PATCH, DELETE, OPTIONS, GET
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3600
Connection: keep-alive
Content-Length: 10
Content-Type: application/json
Keep-Alive: 5

{
    "id": "1"
}

Release files for sanic-simple-restful 0.1.0

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-simple-restful 0.1.0
File Size Uploaded
sanic_simple_restful-0.1.0.tar.gz 4.5 kB Details

Built distribution (wheel)

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

Total release size: 9.2 kB

Release files / sanic_simple_restful-0.1.0.tar.gz

Download URL sanic_simple_restful-0.1.0.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6c04f21e5ce87684b31d1e14dbc767e3ed369d64c95e709f548cafb9ab09ae1e
BLAKE2b-256 checksum
How to use checksums
cf56b73897cd2a18d7a8580d44a2522faa44b39ef29ba5af9ba034de56e11869
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release files / sanic_simple_restful-0.1.0-py2.py3-none-any.whl

Download URL sanic_simple_restful-0.1.0-py2.py3-none-any.whl
Size 4.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
51f756371a2dcdec6e94949c5de77dd178c38eaa4aad7608382242fcc352e491
BLAKE2b-256 checksum
How to use checksums
201de906790fc1fa445186a87e1792398e355a13c9658a7a7dc7ff490e5a5cfd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release history Release notifications | RSS feed

This release

0.1.0 This release

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