Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from fastapi import FastAPI, Body
from pydantic import BaseModel
app = FastAPI()
class Number(BaseModel):
value: int
class Response(BaseModel):
code: int
message: str
result: bool
odd_examples = {
"odd": {
"summary": "Odd number",
"value": {
"value": 1111
}
},
"even": {
"summary": "Even number",
"value": {
"value": 322
}
},
"alphabet": {
"summary": "Example using alphabet",
"description": "Using invalid, non-number input. Will raise `Error: Unprocessable Entity` message.",
"value": {
"value": "abc"
}
},
}
odd_responses = {
200: {
"description": "Success",
"content": {
"application/json": {
"examples": {
"odd": {
"summary": "Odd Number",
"value": {"code": 0, "message": "Success", "result": True}
},
"even": {
"summary": "Even Number",
"value": {"code": 0, "message": "Success", "result": False}
},
}
}
}
},
}
@app.post("/odd", response_model=Response, responses=odd_responses)
def odd(number: Number = Body(..., examples=odd_examples)):
return Response(code=200, message="Success", result=number.value % 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment