Skip to main content

restful_model

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": []
}

Metadata

Release files for restful_model 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for restful_model 0.1.0
File Size Uploaded Provenance Actions
restful_model-0.1.0.tar.gz 11.3 kB Without provenance Details for restful_model-0.1.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for restful_model 0.1.0
File Interpreter ABI Platform Actions
restful_model-0.1.0-py3.7.egg Legacy Egg format - - Details for restful_model-0.1.0-py3.7.egg
restful_model-0.1.0-py3-none-any.whl Python 3 none any Details for restful_model-0.1.0-py3-none-any.whl

Total release size: 33.5 kB

Release files / restful_model-0.1.0.tar.gz

Download URL restful_model-0.1.0.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
77502512e8988b6e904e5ade84d7e9ff0308b47175b15da566d1cefe5ecd2593
BLAKE2b-256 checksum
How to use checksums
859855e5895322d8c84226f6b17c445eaaba4871c06017a571f7d21496141438
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.7

Release files / restful_model-0.1.0-py3.7.egg

Download URL restful_model-0.1.0-py3.7.egg
Size 10.9 kB
Tags Egg
SHA-256 checksum
How to use checksums
58b90621622091c01790bbc3641bcac9a815b412a102e5e07ab30112c4c61277
BLAKE2b-256 checksum
How to use checksums
edc9b40bd020429960954eb1d73dff042966122516d94d4c2ce469b0ba195c54
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.7

Release files / restful_model-0.1.0-py3-none-any.whl

Download URL restful_model-0.1.0-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5f9632e8be07688a9a414d6095cc8ece74ca8a06df489ba40ab853a86d7e86c9
BLAKE2b-256 checksum
How to use checksums
a589e6fd1455671c85d785ef31577c8df48074513e8229e1d0284c303bc94325
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.7

Security for restful_model 0.1.0

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

3 release files No provenance

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