Metadata-Version: 2.1
Name: flask-sqlalchemy-api
Version: 0.9.1
Summary: flask-sqlalchemy-api extension for flask application
Home-page: https://github.com/fraoustin/flask-sqlalchemy-api.git
Author: Frédéric Aoustin
Author-email: fraoustin@gmail.com
License: UNKNOWN
Platform: any
Classifier: Programming Language :: Python
Classifier: License :: OSI Approved :: GNU General Public License v2 (GPLv2)
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Environment :: Web Environment
License-File: AUTHORS.txt

# 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


## TODO

- essayer avec une pk sur plusieurs champs
- voir pour plusieurs mimetype
- voir pour test avec html (swagger.json) https://github.com/getsling/flask-swagger
- faire test avec des fk non respecté (voir comment en sqlachemy on les décrit)
- faire un outil pour ouvrir les dbml et faire des query
- voir pour faire des apis lié au fk ex /api/person/(int: id)/computers (https://flask-restless.readthedocs.io/en/stable/requestformat.html)

# Feature

- generate documentation
- TODO

# V. 0.9.0

- init repos
- add api 'ALL', 'POST', 'GET', 'DELETE', 'PUT', 'PATCH'
- manage decorator, serialize, error

