Skip to main content

sanic json

A thin wrapper on sanic web framework to help writting JSON API.

Recommond use json-api instead. It's a abstraction library of this library.

pip3 install sanic-json

Exmaple

file api/random.py

def obj_to_dict(obj):
    if not obj or isinstance(obj, (str, int, float)):
        return obj
    elif isinstance(obj, list):
        return [obj_to_dict(x) for x in obj]
    elif isinstance(obj, dict):
        return {k: obj_to_dict(v) for k, v in obj.items()}
    else:
        return {k: obj_to_dict(v) for k, v in obj.__dict__.items()}


# the wrapper will get the args and kwargs value from the `request`
async def random_pohoto(req, count=10):
    res = api.photo.random(count=count)
    return {"data": obj_to_dict(res)}

file app.py

from sanic import Sanic
from sanic_json import get_json_route
from api.random import random_pohoto

app = Sanic()
json_route = get_json_route(app)

json_route("/api/random", random_pohoto)


if __name__ == "__main__":
    import os

    debug = bool(os.getenv("DEBUG"))
    # hot reload in next release: https://github.com/channelcat/sanic/issues/168
    app.run(host="0.0.0.0", port=8000, debug=debug)

Metadata

Release files for sanic-json 0.1.13

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-json 0.1.13
File Interpreter ABI Platform
sanic_json-0.1.13-py3-none-any.whl Python 3 none any Details

Release files / sanic_json-0.1.13-py3-none-any.whl

Download URL sanic_json-0.1.13-py3-none-any.whl
Size 3.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e19b90e958fd58bcb2378cf18884ad3bd9562da7e6585c3986371c916c929a8f
BLAKE2b-256 checksum
How to use checksums
851639125028df04ccc9e2831024c2679fd1885072c1635efddc3b9004a28073
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.1

Security for sanic-json 0.1.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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