GithubHelp home page GithubHelp logo

weaming / json-api Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 30 KB

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

Makefile 2.61% Python 97.39%

json-api's Introduction

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))
    ]))
]

json-api's People

Contributors

weaming avatar shurph avatar

Stargazers

Victor-ray, S avatar  avatar ik5 avatar

Watchers

 avatar  avatar

json-api's Issues

Impossible to use without Django installed

I do not use\have Django. Only Sanic. Here is error:

$ app.py
Traceback (most recent call last):
  File "D:\code\test1\app.py", line 4, in <module>
    from json_api.magic_sanic import MagicSanic
  File "C:\Users\bubnenkov\AppData\Local\Programs\Python\Python37\lib\site-packages\json_api\__init__.py", line 2, in <module>
    from .magic_django import MagicDjango
  File "C:\Users\bubnenkov\AppData\Local\Programs\Python\Python37\lib\site-packages\json_api\magic_django.py", line 6, in <module>

    class MagicDjango(DefaultMagic):
  File "C:\Users\bubnenkov\AppData\Local\Programs\Python\Python37\lib\site-packages\json_api\magic_django.py", line 7, in MagicDjango
    from django.core.serializers.json import DjangoJSONEncoder
ModuleNotFoundError: No module named 'django'

License

I think it might be important to have some license for this public library in terms of future contributions from the community.
Maybe BSD or MIT?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.