Skip to main content

Flask toolkits to boost your development and simplify flask, its featured with AutoSwagger

Project description

Flask Toolkits

Downloads

Repository

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
    • 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

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"}

Project details


Download files

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

Source Distribution

flask-toolkits-0.4.1.tar.gz (1.0 MB view details)

Uploaded Source

Built Distribution

flask_toolkits-0.4.1-py3-none-any.whl (1.0 MB view details)

Uploaded Python 3

File details

Details for the file flask-toolkits-0.4.1.tar.gz.

File metadata

  • Download URL: flask-toolkits-0.4.1.tar.gz
  • Upload date:
  • Size: 1.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for flask-toolkits-0.4.1.tar.gz
Algorithm Hash digest
SHA256 24a46e43f0d7150de82523ab3ea4e405da55c882730694cfd365ca35019bc10d
MD5 67bdfd8f6d855ce305a0e7b8b13daf59
BLAKE2b-256 2102e86fa389b6d516874600526015a2f04750f356ead5c8001120254082aa92

See more details on using hashes here.

File details

Details for the file flask_toolkits-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: flask_toolkits-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 1.0 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for flask_toolkits-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e4d9334922d7bd7c1c9d15dd4ffca0d6415f4bca3957ba4e7197b7a874a4f8c3
MD5 e789cc0cf418d042fe387f03ada86d59
BLAKE2b-256 a9ba92ff4d04e8a42a77bdc252aa4e450bbc2a9a1a152b0c37d35a425c71c104

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page