Skip to main content

JSON API

Make you focus on writting business logic code, just return dict data for API, or other Response directly.

What you need to do is inherit the Magic class and overwrite several methods, then use method it provides to add route to your framework app.

The key point is it's api magic.json_api(view_func) wraps your logic code, then check the request data and reponse data.

pip3 install json-api

Usage Examples

Sanic Framework

import os
from sanic import Sanic

from json_api.magic_sanic import MagicSanic

magic = MagicSanic()
app = Sanic()
magic.set_app(app)


async def index(request, name="world"):
    return {"hello": name}


async def register(request, name, email, password):
    password = password or generate_random_password()
    user = new_user(name, email, password)
    return user.to_dict()

async def get_user_information(request, name):
    user = get_user_by_name(name)
    if not user:
        return {'reason': 'not found'}, 404
    return user.to_dict()


magic.add_route("/", index)
magic.add_route("/api/user/register", register, methods=["POST"])
magic.add_route("/api/user/info", get_user_information)


if __name__ == "__main__":
    debug = bool(os.getenv("DEBUG"))
    app.run(host="0.0.0.0", port=8000, debug=debug)

Django Example

from json_api.magic_django import MagicDjango
from .utils import JSONEncoder

magic = MagicDjango()
magic.encoder = JSONEncoder

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include([
        path('translate', magic.json_api(trans))
    ]))
]

Metadata

Release files for json-api 0.1.22

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

Release files / json_api-0.1.22-py3-none-any.whl

Download URL json_api-0.1.22-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fd772f022fe037e69749f8d1f1cc867d5a86c0eebc1c36ff201abac0b416e215
BLAKE2b-256 checksum
How to use checksums
7264eb0538c9aa414f034460cc9905e9055f20975070fa6829d9107e41ae3178
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.4.2 requests/2.20.1 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/2.7.16

Security for json-api 0.1.22

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