Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

No project description provided

Project description

travis codecov

restful_model is an sqlalchemy auto generate REATful API

Example

Sanic

app.py

import sqlalchemy as sa
from sanic import Sanic
from sanic.constants import HTTP_METHODS
from restful_model import DataBase
from restful_model.extend.sanic import ApiView

metadata = sa.MetaData()
User = sa.Table(
    'user',
    metadata,
    sa.Column(
        'id',
        sa.Integer,
        autoincrement=True,
        primary_key=True,
        nullable=False,
    ),
    sa.Column(
        'account',
        sa.String(16),
        nullable=False,
    ),
    sqlite_autoincrement=True,
)
class UserView(ApiView):
    __model__ = User

app = Sanic(__name__)
db = DataBase("sqlite:///db.db")
app.db = db

@app.listener('before_server_start')
async def setup_db(app, loop):
    if app.db.loop is None:
        app.db.loop = loop
        app.db.engine = await app.db.create_engine(echo=True)
        if not await app.db.exists_table(User.name):
            await app.db.create_table(User)

userView = UserView.as_view(app.db)
app.add_route(userView, "/user", HTTP_METHODS)
app.add_route(userView, "/user/<id:int>", HTTP_METHODS)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000)

curl test

$ # create
$ curl -X POST http://127.0.0.1:8000/user \
-H 'content-type: application/json' \
-d '{ "account": "test1" }'
> {
    "status": 201,
    "message": "Insert ok!",
    "meta": {"count":1}
}
$ # select
$ curl -X GET http://127.0.0.1:8000/user
> {
    "status": 200,
    "message": "Query ok!",
    "data": [{
        "id": 1,
        "account": "test1"
    }]
}
$ # update
$ curl -X PUT http://127.0.0.1:8000/user \
-H 'content-type: application/json' \
-d '{"where": {"id": 1}, "values": {"account": "test2"}}'
> {
    "status": 201,
    "message": "Update ok!",
    "meta":{
        "count": 1
    }
}
$ curl -X GET http://127.0.0.1:8000/user
> {
    "status": 200,
    "message": "Query ok!",
    "data": [
        {"id": 1,"account": "test2"}
    ]
}
$ # delete
$ curl -X DELETE http://127.0.0.1:8000/user \
-H 'content-type: application/json' \
-d '{"id": 1}'
> {
    "status": 200,
    "message": "Delete ok!",
    "meta": {"count":1}
}
$ curl -X GET http://127.0.0.1:8000/user
> {
    "status": 200,
    "message": "Query ok!",
    "data": []
}

Release history Release notifications

This version

0.1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for restful_model, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size restful_model-0.1.0-py3.7.egg (10.9 kB) File type Egg Python version 3.7 Upload date Hashes View hashes
Filename, size restful_model-0.1.0-py3-none-any.whl (11.2 kB) File type Wheel Python version 3.7 Upload date Hashes View hashes
Filename, size restful_model-0.1.0.tar.gz (11.3 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page