A suite of sanic sanic_utils
Project description
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.3.0
dataclasses 0.6
dataclasses-json 0.4.3
CONTRIBUTING
LICENSE
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 Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for sanic_utils-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a8c638161004b0f103bfffcaabb0737a5373b136cccd95d270753c93bd42b21 |
|
MD5 | 63d44cc9060bafbb2e923a66cb952846 |
|
BLAKE2b-256 | 13ec1f060f1abce22fb28a115c9249c83654630895f8f7d412670536f506b29f |