Created
May 24, 2021 06:30
-
-
Save wfng92/1c47646c9561b9fc75c5f0f983820213 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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