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

smile 2021-09-15 08:14:24

Introduce

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

install

pip install flask-sugar

Example

# 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
@app.post("/item/<a>")
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)

function

flask run --reload
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

file

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

swagger

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

redoc

I hope you guys can give me some star

Please bring the original link to reprint ,thank
Similar articles

2021-09-15