Skip to main content

sanic-utils

A suite of useful Sanic utilities

from_json

A decorator for Sanic endpoints to grab the json from the request and pass it in as a parameter. Default Sanic approach:

from sanic import Sanic, response
from sanic.request import Request

app = Sanic("App Name")

class ExampleInput:
    name: str
    identifier: str

@app.post("/test")
async def test(request: Request):
    json = request.json
    message = None # convert json to object in whatever prefered way
    return response.text("done")

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8112)

Usage of the decorator:

from dataclasses import dataclass

from dataclasses_json import dataclass_json
from sanic import Sanic, response
from sanic.request import Request

from sanic_utils.from_json import from_json

app = Sanic("App Name")

@dataclass_json
@dataclass
class ExampleInput:
    name: str
    identifier: str

@app.post("/test")
@from_json(message_type=ExampleInput)
async def test(request: Request, message: ExampleInput):
    print(message)
    return response.text("done")

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8112)

Usage

Mark endpoint method with @from_json decorator passing in a single param, the resulting message type. The message type should be marked with @dataclass_json and @dataclass.

Dependencies

sanic 20.6.3

dataclasses 0.6

dataclasses-json 0.5.1

CONTRIBUTING

LICENSE

Release files for sanic-utils 0.1.1

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

Built distribution (wheel)

Table of built distributions (wheels) for sanic-utils 0.1.1
File Interpreter ABI Platform
sanic_utils-0.1.1-py3-none-any.whl Python 3 none any Details

Release files / sanic_utils-0.1.1-py3-none-any.whl

Download URL sanic_utils-0.1.1-py3-none-any.whl
Size 2.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
831623066d99c1ac10256c4ef257fca7c1547cfe709d378b85cf91b78349ab52
BLAKE2b-256 checksum
How to use checksums
3aa58f08d614932b0650928b53d6d323ad3233fbff5f6a82f6c14f5208f790b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.8

Release history Release notifications | RSS feed

This release

0.1.1 This release

1 release file

0.1.0

1 release file

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