Flash automatically injects and validates parameters, and automatically generates documents. Flash sugar is an API extension

smile 2021-09-15 08:14:24


I've been following suit recently fastapi Wrote a flask Of api Expand flask-sugar,flask-sugar The request header can be automatically injected and verified ,cookie, Query parameters , Path parameter , Request body json,form Forms , file , The response data , And generate the corresponding swagger file .

file : https://shangsky.github.io/fl...

Source code : https://github.com/shangsky/f...


pip install flask-sugar


# Save as app.py
from flask_sugar import Sugar, Header
from pydantic import BaseModel
app = Sugar(__name__)
class Item(BaseModel):
name: str
size: int
class Resp(BaseModel):
a: int
b: str
c: str
item: Item
def demo(
a: int, # Path parameter
item: Item, # Request body json Parameters
b: str = "default_query_param_b", # Query parameters
c: str = Header("default_header_param_b"), # Request header parameters
) -> Resp:
"""demo page"""
return Resp(a=a, b=b, c=c, item=item)


flask run --reload
* Running on (Press CTRL+C to quit)


Now access swagger Document address http://localhost:5000/doc:


visit Redoc Document address http://localhost:5000/redoc:


I hope you guys can give me some star

Please bring the original link to reprint ,thank
Similar articles