Skip to main content

Sanic framework thin wapper to write JSON API

Project description

sanic json

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

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 = True if os.getenv("DEBUG") else False
    # hot reload in next release: https://github.com/channelcat/sanic/issues/168
    app.run(host="0.0.0.0", port=8000, debug=debug)

Project details


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

If you're not sure about the file name format, learn more about wheel file names.

sanic_json-0.1.11-py2.py3-none-any.whl (3.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file sanic_json-0.1.11-py2.py3-none-any.whl.

File metadata

  • Download URL: sanic_json-0.1.11-py2.py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.5

File hashes

Hashes for sanic_json-0.1.11-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 cb23316b90347c60501db1a51708dcc09094b065836d17b062e1e1423646a1ef
MD5 e1b8a00f57aec7b1b206f70d6cacf76f
BLAKE2b-256 988bc7253a83833a4d9dc46bf54998fe0cf996b42e7af23cc395e7403bb47c23

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page