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.py:filled
  • Loading branch information
OmarThinks committed Jan 30, 2021
commit 9c9c61a6a3a4daa3567faa59e2a3542412f03e21
52 changes: 52 additions & 0 deletions example_app/readme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from typing import Optional
from flask import Flask, request
from pydantic import BaseModel

from flask_pydantic import validate

app = Flask("flask_pydantic_app")

class RequestBodyModel(BaseModel):
name: str
nickname: Optional[str]


class QueryModel(BaseModel):
age: int

@app.route("/", methods=["GET"])
@validate()
def get(query:QueryModel):
age = query.age
return ResponseModel(
age=age,
id=0, name="abc", nickname="123"
)

class ResponseModel(BaseModel):
id: int
age: int
name: str
nickname: Optional[str]

@app.route("/", methods=["POST"])
@validate()
def post(body:RequestBodyModel):
name = body.name
nickname = body.nickname
return ResponseModel(
name=name, nickname=nickname,id=0, age=1000
)

@app.route("/both", methods=["POST"])
@validate()
def get_and_post(body:RequestBodyModel,query:QueryModel):
name = body.name # From request body
nickname = body.nickname # From request body
age = query.age # from query parameters
return ResponseModel(
age=age, name=name, nickname=nickname,
id=0
)

app.run(debug=True)