Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier to learn README.md #23

Merged
merged 17 commits into from
Jan 31, 2021
Prev Previous commit
Next Next commit
README.me:mod:example1:clear
  • Loading branch information
OmarThinks committed Jan 30, 2021
commit d2c3b3eb82577f354586c6092465cf680f9be8cf
26 changes: 12 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ from flask_pydantic import validate

app = Flask("flask_pydantic_app")

# Request body expectation as a pydantic model
class BodyModel(BaseModel):
# Request body expectations as a pydantic model
class RequestBodyModel(BaseModel):
name: str
nickname: Optional[str]

# API response expectation as a pydantic model
# API response expectations as a pydantic model
class ResponseModel(BaseModel):
id: int
age: int
Expand All @@ -68,29 +68,27 @@ Example 1:
receive inputs from request body
"""
@app.route("/", methods=["POST"])
@validate() # Welcome to flask_pydantic
def post(body:BodyModel): # expected request body
name = body.name
nickname = body.nickname
@validate() # To apply flask_pydantic to this endpoint
def post(body:RequestBodyModel): # expected request body
name_ = body.name
nickname_ = body.nickname
# Now 'name' and 'nickname' are
# received and, validated and sanitized from the request body
# received, validated and sanitized from the request body

# save model to DB
id = 1 # we should have got it from db
age = 1 # we should have got it from db
id_ = 1; age_ = 1 # we should have got them from db

return ResponseModel(
id=id, age=age, name=name,
nickname=nickname,
)
id=id_, age=age_, name=name_, nickname=nickname_
)

class QueryModel(BaseModel):
age: int

# First example Endpoint, Receive request body
@app.route("/", methods=["GET"])
@validate()
def get(body:BodyModel):
def get(body:RequestBodyModel):
name = body.name
nickname = body.nickname
# Now 'name' and 'nickname' are
Expand Down