flask-toolkits


Nameflask-toolkits JSON
Version 0.4.8 PyPI version JSON
download
home_page
SummaryFlask toolkits to boost your development and simplify flask, its featured with AutoSwagger
upload_time2022-06-13 04:33:34
maintainer
docs_urlNone
authordanangjoyoo (Agus Danangjoyo)
requires_python
license
keywords flask middleware http request response swagger openapi toolkit
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Flask Toolkits
[![Downloads](https://static.pepy.tech/personalized-badge/flask-toolkits?period=total&units=international_system&left_color=black&right_color=blue&left_text=Downloads)](https://pepy.tech/project/flask-toolkits)

## Repository
- [GITHUB](https://github.com/Danangjoyoo/flask-toolkits)

## Installation
```
pip install flask-toolkits
```

## Description
Flask toolkits implements and provides several features from `FastAPI` like:
- Automatic API documentation (define the function and we'll generate the `swagger`/`openapi` spec for you)
- Passing parameters through `view`/`router` function which is unable in `Flask` before
- Easy Middleware setup
- Parameters and schema validation using `Pydantic`
- Response classes that could return any type of data without worried to get error
- much more..


## Changelogs
- v0.0
    - First Upload
- v0.1
    - Integration with [flask-http-middleware](https://pypi.org/project/flask-http-middleware)
    - [pydantic](https://pypi.org/project/pydantic) support for JSON arguments and validation
    - Multiple response type generator
    - Added `JSONResponse` class to replace `jsonify` roles in send dictionary data with encoding improvements.
- v0.2
    - Supported enumeration API documentation
    - Added support for type hint from `typing`'s generic (ex: `Optional`, `Union`, `List`)
    - Fixed input parameter validations
- v0.3
    - Support `File` and `Form` input parameters validation and automatic swagger.
    - Added constraint feature for parameters (ex: limit, max/min length, greater/less than, equals than etc)
- v0.4
    - Support `Authorization` header in openapi spec.
    - Added `Authorization` processing function for security and can be used as `login` or `auth`.

## Key Tools inside this `toolkit`
- Automatic API documentation (`swagger`/`openapi`)
- Request-Response direct HTTP middleware (`flask-http-middleware`)
- Automatic parameters validation (`pydantic`)
- Response generator (JSON, Plain Text, HTML)

## Automatic Parameters Validation
The original `Blueprints` class from `flask` can't insert most of arguments inside endpoint.
Here our `APIRouter` allows you to have arguments inside your endpoint
```
from typing import Optional
from flask_toolkits import APIRouter, Body, Header, Query
from flask_toolkits.responses import JSONResponse


router = APIRouter("email", import_name=__name__, static_folder="/routers/email", url_prefix="/email")


@router.post("/read", tags=["Email Router])
def get_email(
    id: int,
    name: Optional[str],
):
    return JSONResponse({"id": id, "name": name})

```

## Automatic API Documentation
Here our `APIRouter` allows you to auto-documenting your endpoint through `AutoSwagger`.
Define the new router using `APIRouter` class, lets put it in another pyfile

`email_view.py`
```
from typing import Optional
from flask_toolkits import APIRouter, Body, Header, Query
from flask_toolkits.responses import JSONResponse


router = APIRouter("email", import_name=__name__, static_folder="/routers/email", url_prefix="/email")


@router.post("/read", tags=["Email Router])
def get_email(
    id: int = Body(...),
    name: Optional[str] = Body(...),
    token: int = Header(...),
    race: Optional[str] = Query(None)
):
    return JSONResponse({"id":id, "name": name})
```

`main.py`
```
from flask import Flask
from flask_toolkits import AutoSwagger

from email_view import router as email_router


app = Flask(__name__)

auto_swagger = AutoSwagger()

app.register_blueprint(email_router)
app.register_blueprint(auto_swagger)


if __name__ == "__main__":
    app.run()
```

then you can go to `http://localhost:5000/docs` and you will found you router is already documented

![alt text](https://github.com/Danangjoyoo/flask-toolkits/blob/main/docs/auto1.png?raw=true)

---

## Supported Field Parameters
`flask-toolkits` provide multiple field parameters such as `Header`, `Query`, `Body`, `Path`, `File`, `Form`

---

## Easy Security Scheme Setup and Documentation
`flask-toolkits` helps you to define your security scheme for authorization easier than before. In advance this also give you automated documentation.

### Basic Usage
lets assume you have your own bearer security schema. You just have to create a new instance of `HTTPBearerSecurity()` to enable automatic documentation on it.
```
from flask import request
from flask_toolkits import APIRouter
from flask_toolkits.security import HTTPBearerSecurity

router = APIRouter("api", __name__)

@router.get("/home", security=HTTPBearerSecurity())
def home(message: str):
    if my_security_scheme(request):
        return JSONResponse({"message": message})
    return JSONResponse({"message": "invalid authorization"})
```

this is how it looks like
![alt text](https://github.com/Danangjoyoo/flask-toolkits/blob/main/docs/auth0.png?raw=true)

on you clicked it
![alt text](https://github.com/Danangjoyoo/flask-toolkits/blob/main/docs/auth1.png?raw=true)

### Define your own security scheme
If you want to define your own security scheme you can follow below guidance
```
from flask import request
from flask_toolkits import APIRouter
from flask_toolkits.security import HTTPBearerSecurity

class JWTBearer(HTTPBearerSecurity):
    def __init__(self):
        super().__init__()

    def __call__(self, req):
        data = self.get_authorization_data(req)
        if data != "abcdefghij":
            raise Exception("This is not good")
        return req

router = APIRouter("api", __name__)

@router.get("/home", security=JWTBearer())
def home(message: str):
    if my_security_scheme(request):
        return JSONResponse({"message": message})
    return JSONResponse({"message": "invalid authorization"})

```
Overriding `__call__` method inside the subclass would define your security schema for the routers that are using your security scheme

### Define to all endpoints in a router
Just pass it to `APIRouter` and all its endpoint will use that security scheme!
```
router_with_bearer = APIRouter("api", __name__, security=JWTBearer())
```
but don't worries! You can also override it by just defining in the router decorator!
```
@router_with_bearer.get("/home", security=AnotherBearerSecurity())
def home():
    return {"message": "hello"}
```


---

## Request-Response direct HTTP middleware
```
import time
from flask import Flask
from flask_toolkits.middleware import MiddlewareManager, BaseHTTPMiddleware

app = Flask(__name__)

class MetricsMiddleware(BaseHTTPMiddleware):
    def __init__(self):
        super().__init__()

    def dispatch(self, request, call_next):
        t0 = time.time()
        response = call_next(request)
        response_time = time.time()-t0
        response.headers.add("response_time", response_time)
        return response

app.wsgi_app = MiddlewareManager(app)
app.wsgi_app.add_middleware(MetricsMiddleware)

@app.get("/health")
def health():
    return {"message":"I'm healthy"}
```


            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "flask-toolkits",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "flask,middleware,http,request,response,swagger,openapi,toolkit",
    "author": "danangjoyoo (Agus Danangjoyo)",
    "author_email": "<agus.danangjoyo.blog@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/37/35/e20c151052f790a6d2e819596720f5c521702288d9c1c11eb2dbe64be185/flask-toolkits-0.4.8.tar.gz",
    "platform": null,
    "description": "# Flask Toolkits\n[![Downloads](https://static.pepy.tech/personalized-badge/flask-toolkits?period=total&units=international_system&left_color=black&right_color=blue&left_text=Downloads)](https://pepy.tech/project/flask-toolkits)\n\n## Repository\n- [GITHUB](https://github.com/Danangjoyoo/flask-toolkits)\n\n## Installation\n```\npip install flask-toolkits\n```\n\n## Description\nFlask toolkits implements and provides several features from `FastAPI` like:\n- Automatic API documentation (define the function and we'll generate the `swagger`/`openapi` spec for you)\n- Passing parameters through `view`/`router` function which is unable in `Flask` before\n- Easy Middleware setup\n- Parameters and schema validation using `Pydantic`\n- Response classes that could return any type of data without worried to get error\n- much more..\n\n\n## Changelogs\n- v0.0\n    - First Upload\n- v0.1\n    - Integration with [flask-http-middleware](https://pypi.org/project/flask-http-middleware)\n    - [pydantic](https://pypi.org/project/pydantic) support for JSON arguments and validation\n    - Multiple response type generator\n    - Added `JSONResponse` class to replace `jsonify` roles in send dictionary data with encoding improvements.\n- v0.2\n    - Supported enumeration API documentation\n    - Added support for type hint from `typing`'s generic (ex: `Optional`, `Union`, `List`)\n    - Fixed input parameter validations\n- v0.3\n    - Support `File` and `Form` input parameters validation and automatic swagger.\n    - Added constraint feature for parameters (ex: limit, max/min length, greater/less than, equals than etc)\n- v0.4\n    - Support `Authorization` header in openapi spec.\n    - Added `Authorization` processing function for security and can be used as `login` or `auth`.\n\n## Key Tools inside this `toolkit`\n- Automatic API documentation (`swagger`/`openapi`)\n- Request-Response direct HTTP middleware (`flask-http-middleware`)\n- Automatic parameters validation (`pydantic`)\n- Response generator (JSON, Plain Text, HTML)\n\n## Automatic Parameters Validation\nThe original `Blueprints` class from `flask` can't insert most of arguments inside endpoint.\nHere our `APIRouter` allows you to have arguments inside your endpoint\n```\nfrom typing import Optional\nfrom flask_toolkits import APIRouter, Body, Header, Query\nfrom flask_toolkits.responses import JSONResponse\n\n\nrouter = APIRouter(\"email\", import_name=__name__, static_folder=\"/routers/email\", url_prefix=\"/email\")\n\n\n@router.post(\"/read\", tags=[\"Email Router])\ndef get_email(\n    id: int,\n    name: Optional[str],\n):\n    return JSONResponse({\"id\": id, \"name\": name})\n\n```\n\n## Automatic API Documentation\nHere our `APIRouter` allows you to auto-documenting your endpoint through `AutoSwagger`.\nDefine the new router using `APIRouter` class, lets put it in another pyfile\n\n`email_view.py`\n```\nfrom typing import Optional\nfrom flask_toolkits import APIRouter, Body, Header, Query\nfrom flask_toolkits.responses import JSONResponse\n\n\nrouter = APIRouter(\"email\", import_name=__name__, static_folder=\"/routers/email\", url_prefix=\"/email\")\n\n\n@router.post(\"/read\", tags=[\"Email Router])\ndef get_email(\n    id: int = Body(...),\n    name: Optional[str] = Body(...),\n    token: int = Header(...),\n    race: Optional[str] = Query(None)\n):\n    return JSONResponse({\"id\":id, \"name\": name})\n```\n\n`main.py`\n```\nfrom flask import Flask\nfrom flask_toolkits import AutoSwagger\n\nfrom email_view import router as email_router\n\n\napp = Flask(__name__)\n\nauto_swagger = AutoSwagger()\n\napp.register_blueprint(email_router)\napp.register_blueprint(auto_swagger)\n\n\nif __name__ == \"__main__\":\n    app.run()\n```\n\nthen you can go to `http://localhost:5000/docs` and you will found you router is already documented\n\n![alt text](https://github.com/Danangjoyoo/flask-toolkits/blob/main/docs/auto1.png?raw=true)\n\n---\n\n## Supported Field Parameters\n`flask-toolkits` provide multiple field parameters such as `Header`, `Query`, `Body`, `Path`, `File`, `Form`\n\n---\n\n## Easy Security Scheme Setup and Documentation\n`flask-toolkits` helps you to define your security scheme for authorization easier than before. In advance this also give you automated documentation.\n\n### Basic Usage\nlets assume you have your own bearer security schema. You just have to create a new instance of `HTTPBearerSecurity()` to enable automatic documentation on it.\n```\nfrom flask import request\nfrom flask_toolkits import APIRouter\nfrom flask_toolkits.security import HTTPBearerSecurity\n\nrouter = APIRouter(\"api\", __name__)\n\n@router.get(\"/home\", security=HTTPBearerSecurity())\ndef home(message: str):\n    if my_security_scheme(request):\n        return JSONResponse({\"message\": message})\n    return JSONResponse({\"message\": \"invalid authorization\"})\n```\n\nthis is how it looks like\n![alt text](https://github.com/Danangjoyoo/flask-toolkits/blob/main/docs/auth0.png?raw=true)\n\non you clicked it\n![alt text](https://github.com/Danangjoyoo/flask-toolkits/blob/main/docs/auth1.png?raw=true)\n\n### Define your own security scheme\nIf you want to define your own security scheme you can follow below guidance\n```\nfrom flask import request\nfrom flask_toolkits import APIRouter\nfrom flask_toolkits.security import HTTPBearerSecurity\n\nclass JWTBearer(HTTPBearerSecurity):\n    def __init__(self):\n        super().__init__()\n\n    def __call__(self, req):\n        data = self.get_authorization_data(req)\n        if data != \"abcdefghij\":\n            raise Exception(\"This is not good\")\n        return req\n\nrouter = APIRouter(\"api\", __name__)\n\n@router.get(\"/home\", security=JWTBearer())\ndef home(message: str):\n    if my_security_scheme(request):\n        return JSONResponse({\"message\": message})\n    return JSONResponse({\"message\": \"invalid authorization\"})\n\n```\nOverriding `__call__` method inside the subclass would define your security schema for the routers that are using your security scheme\n\n### Define to all endpoints in a router\nJust pass it to `APIRouter` and all its endpoint will use that security scheme!\n```\nrouter_with_bearer = APIRouter(\"api\", __name__, security=JWTBearer())\n```\nbut don't worries! You can also override it by just defining in the router decorator!\n```\n@router_with_bearer.get(\"/home\", security=AnotherBearerSecurity())\ndef home():\n    return {\"message\": \"hello\"}\n```\n\n\n---\n\n## Request-Response direct HTTP middleware\n```\nimport time\nfrom flask import Flask\nfrom flask_toolkits.middleware import MiddlewareManager, BaseHTTPMiddleware\n\napp = Flask(__name__)\n\nclass MetricsMiddleware(BaseHTTPMiddleware):\n    def __init__(self):\n        super().__init__()\n\n    def dispatch(self, request, call_next):\n        t0 = time.time()\n        response = call_next(request)\n        response_time = time.time()-t0\n        response.headers.add(\"response_time\", response_time)\n        return response\n\napp.wsgi_app = MiddlewareManager(app)\napp.wsgi_app.add_middleware(MetricsMiddleware)\n\n@app.get(\"/health\")\ndef health():\n    return {\"message\":\"I'm healthy\"}\n```\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Flask toolkits to boost your development and simplify flask, its featured with AutoSwagger",
    "version": "0.4.8",
    "split_keywords": [
        "flask",
        "middleware",
        "http",
        "request",
        "response",
        "swagger",
        "openapi",
        "toolkit"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "502c94435c98161f98de9d4d0b1f4925",
                "sha256": "95ebed930ee20335c567de22a45a1cdede2f1a2675c60e461b6ec7fe18bebb9f"
            },
            "downloads": -1,
            "filename": "flask_toolkits-0.4.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "502c94435c98161f98de9d4d0b1f4925",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 1018072,
            "upload_time": "2022-06-13T04:33:31",
            "upload_time_iso_8601": "2022-06-13T04:33:31.979499Z",
            "url": "https://files.pythonhosted.org/packages/3c/f0/af2f40765dbbfbb5a3908c983bbbd2248eac50dd5052a8c95bebbfa097ac/flask_toolkits-0.4.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "bd938dc6fb497edb3c14ac35896041f6",
                "sha256": "165acd5cad1025336c6ebc39b56189049d9734b3148459a8d87cbb87576a5a21"
            },
            "downloads": -1,
            "filename": "flask-toolkits-0.4.8.tar.gz",
            "has_sig": false,
            "md5_digest": "bd938dc6fb497edb3c14ac35896041f6",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 1010897,
            "upload_time": "2022-06-13T04:33:34",
            "upload_time_iso_8601": "2022-06-13T04:33:34.584661Z",
            "url": "https://files.pythonhosted.org/packages/37/35/e20c151052f790a6d2e819596720f5c521702288d9c1c11eb2dbe64be185/flask-toolkits-0.4.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-06-13 04:33:34",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "flask-toolkits"
}
        
Elapsed time: 0.40548s