{"id":20049428,"url":"https://github.com/weaming/json-api","last_synced_at":"2025-05-05T11:30:54.419Z","repository":{"id":62572795,"uuid":"148577210","full_name":"weaming/json-api","owner":"weaming","description":"Make you focus on writting business logic code, just return dict data for API, or other Response directly.","archived":false,"fork":false,"pushed_at":"2020-04-15T01:18:07.000Z","size":30,"stargazers_count":3,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-08T21:41:30.674Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/weaming.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-13T03:32:38.000Z","updated_at":"2021-10-24T22:16:17.000Z","dependencies_parsed_at":"2022-11-03T18:30:24.584Z","dependency_job_id":null,"html_url":"https://github.com/weaming/json-api","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weaming%2Fjson-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weaming%2Fjson-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weaming%2Fjson-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/weaming%2Fjson-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/weaming","download_url":"https://codeload.github.com/weaming/json-api/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252488910,"owners_count":21756234,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T11:50:00.014Z","updated_at":"2025-05-05T11:30:54.165Z","avatar_url":"https://github.com/weaming.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSON API\n\nMake you focus on writting business logic code, just return dict data for API, or other Response directly.\n\nWhat 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.\n\nThe key point is it's api `magic.json_api(view_func)` wraps your logic code, then check the request data and reponse data.\n\n```\npip3 install json-api\n```\n\n## Usage Examples\n### Sanic Framework\n\n```\nimport os\nfrom sanic import Sanic\n\nfrom json_api.magic_sanic import MagicSanic\n\nmagic = MagicSanic()\napp = Sanic()\nmagic.set_app(app)\n\n\nasync def index(request, name=\"world\"):\n    return {\"hello\": name}\n\n\nasync def register(request, name, email, password):\n    password = password or generate_random_password()\n    user = new_user(name, email, password)\n    return user.to_dict()\n\nasync def get_user_information(request, name):\n    user = get_user_by_name(name)\n    if not user:\n        return {'reason': 'not found'}, 404\n    return user.to_dict()\n\n\nmagic.add_route(\"/\", index)\nmagic.add_route(\"/api/user/register\", register, methods=[\"POST\"])\nmagic.add_route(\"/api/user/info\", get_user_information)\n\n\nif __name__ == \"__main__\":\n    debug = bool(os.getenv(\"DEBUG\"))\n    app.run(host=\"0.0.0.0\", port=8000, debug=debug)\n```\n\n### Django Example\n\n```\nfrom json_api.magic_django import MagicDjango\nfrom .utils import JSONEncoder\n\nmagic = MagicDjango()\nmagic.encoder = JSONEncoder\n\nurlpatterns = [\n    path('admin/', admin.site.urls),\n    path('api/', include([\n        path('translate', magic.json_api(trans))\n    ]))\n]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweaming%2Fjson-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fweaming%2Fjson-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweaming%2Fjson-api/lists"}