# flask-sqlachemy-api
generate API Rest from Model Class
## Installation
pip install flask-sqlachemy-api
Or
git clone https://github.com/fraoustin/flask-sqlachemy-api.git
cd flask-sqlachemy-api
python setup.py install
You can load test by
flake8 --ignore E501,E226,E128,F401
python -m unittest discover -s tests
## Usage
import os, logging
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_sqlalchemy_api import ApiRest, error_api
app = Flask(__name__)
# db SQLAlchemy
database_file = "sqlite:///{}".format(os.path.join('.', "test.db"))
app.config["SQLALCHEMY_DATABASE_URI"] = database_file
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy()
class Todo(db.Model):
__tablename__ = 'todo'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String, nullable=False)
description = db.Column(db.String, nullable=True)
status = db.Column(db.String, nullable=True)
apiManager = ApiRest(db)
for method in ['ALL', 'POST', 'GET', 'DELETE', 'PUT', 'PATCH']:
apiManager.add_api(Todo, method)
app.register_blueprint(apiManager)
if __name__ == "__main__":
db.init_app(app)
with app.app_context():
db.create_all()
app.logger.setLevel(logging.DEBUG)
app.run(host='0.0.0.0', port=5000, debug=True)
This code generate api url:
- GET http://127.0.0.1:5000/api/v1/todos
- POST http://127.0.0.1:5000/api/v1/todo
- GET http://127.0.0.1:5000/api/v1/todo/<id>
- DELETE http://127.0.0.1:5000/api/v1/todo/<id>
- PUT http://127.0.0.1:5000/api/v1/todo/<id>
- PATCH http://127.0.0.1:5000/api/v1/todo/<id>
flask-sqlachemy-api generate 6 Apis from Model Class
- ALL: it's a get request for list of item with url http://domain/api_path/items. You can add parameter filter, orderby, offset, limit.
http://127.0.0.1:5000/api/v1/todos?orderby=title%20desc method GET
http://127.0.0.1:5000/api/v1/todos?orderby=title%20desc&filter=id%3D method GET
http://127.0.0.1:5000/api/v1/todos?offset=50&limit=50 method GET
- GET: it's get request for a specific item with url http://domain/api_path/item/<id>
http://127.0.0.1:5000/api/v1/todo/1 method GET
- DELETE: it's delete request for a specific item url http://domain/api_path/item/<id>
http://127.0.0.1:5000/api/v1/todo/1 method DELETE
- POST: it's post request for add a item with url http://domain/api_path/item. You add data on your request
http://127.0.0.1:5000/api/v1/todo method POST
- PUT: it's put request for modify a specific item with url http://domain/api_path/item/<id>. You add data on your request
http://127.0.0.1:5000/api/v1/todo/1 method PUT
- PATCH: it's patch request for modify a specific item on specific column with url http://domain/api_path/item/<id>. You add data on your request
http://127.0.0.1:5000/api/v1/todo/1 method PATCH
You can
- specific url (default /api/v1) on ApiRest
- add decorator (login, ...) in sample/sample1.py
- specific endpoint
- specific serialize: transform item to json
- specific api action on column using the comment of column (add "not create by api", "not visible by api", "not update by api")
## Sample
You can launch sample 0 and 1
python sample/sample0.py &
python sample/sample0_test.py
You can launch sample3
python sample/sample3.py
And you test API by swagger UI on http://127.0.0.1:5000/swagger
## TODO
- voir pour faire des apis lié au fk ex /api/person/(int: id)/computers (https://flask-restless.readthedocs.io/en/stable/requestformat.html)
- faire exemple flask-sqlalchemy-api + dbml-to-sqlalchemy
- faire full application todo avec gestion user
# Feature
- generate documentation
- TODO
# V. 0.9.2
- change README
# V. 0.9.1
- correction setup
# V. 0.9.0
- init repos
- add api 'ALL', 'POST', 'GET', 'DELETE', 'PUT', 'PATCH'
- manage decorator, serialize, error
Raw data
{
"_id": null,
"home_page": "https://github.com/fraoustin/flask-sqlalchemy-api.git",
"name": "flask-sqlalchemy-api",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Fr\u00e9d\u00e9ric Aoustin",
"author_email": "fraoustin@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/63/9d/39206c4cba0b03c00ba365dcedc78a7c355638e5a0df1e90aee4a87772bb/flask-sqlalchemy-api-0.9.3.tar.gz",
"platform": "any",
"description": "# flask-sqlachemy-api\n\ngenerate API Rest from Model Class\n\n\n## Installation\n\n\n pip install flask-sqlachemy-api\n \nOr\n\n git clone https://github.com/fraoustin/flask-sqlachemy-api.git\n cd flask-sqlachemy-api\n python setup.py install\n\nYou can load test by\n\n flake8 --ignore E501,E226,E128,F401\n python -m unittest discover -s tests\n\n\n## Usage\n\n import os, logging\n from flask import Flask\n from flask_sqlalchemy import SQLAlchemy\n from flask_sqlalchemy_api import ApiRest, error_api\n\n app = Flask(__name__)\n\n # db SQLAlchemy\n database_file = \"sqlite:///{}\".format(os.path.join('.', \"test.db\"))\n app.config[\"SQLALCHEMY_DATABASE_URI\"] = database_file\n app.config[\"SQLALCHEMY_TRACK_MODIFICATIONS\"] = False\n db = SQLAlchemy() \n\n class Todo(db.Model):\n __tablename__ = 'todo'\n\n id = db.Column(db.Integer, primary_key=True, autoincrement=True)\n title = db.Column(db.String, nullable=False)\n description = db.Column(db.String, nullable=True)\n status = db.Column(db.String, nullable=True)\n\n\n apiManager = ApiRest(db)\n for method in ['ALL', 'POST', 'GET', 'DELETE', 'PUT', 'PATCH']:\n apiManager.add_api(Todo, method)\n app.register_blueprint(apiManager)\n\n\n if __name__ == \"__main__\":\n db.init_app(app)\n with app.app_context():\n db.create_all()\n app.logger.setLevel(logging.DEBUG)\n app.run(host='0.0.0.0', port=5000, debug=True)\n\nThis code generate api url:\n\n- GET http://127.0.0.1:5000/api/v1/todos\n- POST http://127.0.0.1:5000/api/v1/todo\n- GET http://127.0.0.1:5000/api/v1/todo/<id>\n- DELETE http://127.0.0.1:5000/api/v1/todo/<id>\n- PUT http://127.0.0.1:5000/api/v1/todo/<id>\n- PATCH http://127.0.0.1:5000/api/v1/todo/<id>\n\n\nflask-sqlachemy-api generate 6 Apis from Model Class\n\n- ALL: it's a get request for list of item with url http://domain/api_path/items. You can add parameter filter, orderby, offset, limit.\n\n http://127.0.0.1:5000/api/v1/todos?orderby=title%20desc method GET\n\n http://127.0.0.1:5000/api/v1/todos?orderby=title%20desc&filter=id%3D method GET\n\n http://127.0.0.1:5000/api/v1/todos?offset=50&limit=50 method GET\n\n- GET: it's get request for a specific item with url http://domain/api_path/item/<id>\n\n http://127.0.0.1:5000/api/v1/todo/1 method GET\n\n- DELETE: it's delete request for a specific item url http://domain/api_path/item/<id>\n\n http://127.0.0.1:5000/api/v1/todo/1 method DELETE\n\n- POST: it's post request for add a item with url http://domain/api_path/item. You add data on your request\n\n http://127.0.0.1:5000/api/v1/todo method POST\n\n- PUT: it's put request for modify a specific item with url http://domain/api_path/item/<id>. You add data on your request\n\n http://127.0.0.1:5000/api/v1/todo/1 method PUT\n\n- PATCH: it's patch request for modify a specific item on specific column with url http://domain/api_path/item/<id>. You add data on your request\n\n http://127.0.0.1:5000/api/v1/todo/1 method PATCH\n\nYou can \n\n- specific url (default /api/v1) on ApiRest\n- add decorator (login, ...) in sample/sample1.py\n- specific endpoint\n- specific serialize: transform item to json\n- specific api action on column using the comment of column (add \"not create by api\", \"not visible by api\", \"not update by api\")\n\n## Sample\n\nYou can launch sample 0 and 1\n\n python sample/sample0.py &\n python sample/sample0_test.py\n\nYou can launch sample3\n\n python sample/sample3.py\n\nAnd you test API by swagger UI on http://127.0.0.1:5000/swagger\n\n## TODO\n\n- voir pour faire des apis li\u00e9 au fk ex /api/person/(int: id)/computers (https://flask-restless.readthedocs.io/en/stable/requestformat.html)\n- faire exemple flask-sqlalchemy-api + dbml-to-sqlalchemy\n- faire full application todo avec gestion user\n\n# Feature\n\n- generate documentation\n- TODO\n\n# V. 0.9.2\n\n- change README\n\n\n# V. 0.9.1\n\n- correction setup\n\n# V. 0.9.0\n\n- init repos\n- add api 'ALL', 'POST', 'GET', 'DELETE', 'PUT', 'PATCH'\n- manage decorator, serialize, error\n\n",
"bugtrack_url": null,
"license": "",
"summary": "flask-sqlalchemy-api extension for flask application",
"version": "0.9.3",
"project_urls": {
"Homepage": "https://github.com/fraoustin/flask-sqlalchemy-api.git"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "639d39206c4cba0b03c00ba365dcedc78a7c355638e5a0df1e90aee4a87772bb",
"md5": "9bfb8592bec8ae0563701b46d7223c83",
"sha256": "d0dbaa93ae3311f481b0c0a3999c1ae3c4829536a9abf52544fd5bc65997f83e"
},
"downloads": -1,
"filename": "flask-sqlalchemy-api-0.9.3.tar.gz",
"has_sig": false,
"md5_digest": "9bfb8592bec8ae0563701b46d7223c83",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 6918,
"upload_time": "2023-07-29T16:54:07",
"upload_time_iso_8601": "2023-07-29T16:54:07.000773Z",
"url": "https://files.pythonhosted.org/packages/63/9d/39206c4cba0b03c00ba365dcedc78a7c355638e5a0df1e90aee4a87772bb/flask-sqlalchemy-api-0.9.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-07-29 16:54:07",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "fraoustin",
"github_project": "flask-sqlalchemy-api",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "flask-sqlalchemy-api"
}