pythonasyncioapiasyncfastapiframeworkjsonjson-schemaopenapiopenapi3pydanticpython-typespython3redocreststarletteswaggerswagger-uiuvicornweb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
686 B
31 lines
686 B
from fastapi import APIRouter, FastAPI
|
|
from fastapi.responses import JSONResponse
|
|
|
|
|
|
class UnicornException(Exception):
|
|
def __init__(self, name: str):
|
|
self.name = name
|
|
|
|
|
|
router = APIRouter(
|
|
prefix="/unicorns",
|
|
exception_handlers={
|
|
UnicornException: lambda request, exc: JSONResponse(
|
|
status_code=418,
|
|
content={
|
|
"message": f"Oops! {exc.name} did something. There goes a rainbow..."
|
|
},
|
|
)
|
|
},
|
|
)
|
|
|
|
|
|
@router.get("/{name}")
|
|
async def read_unicorn(name: str):
|
|
if name == "yolo":
|
|
raise UnicornException(name=name)
|
|
return {"unicorn_name": name}
|
|
|
|
|
|
app = FastAPI()
|
|
app.include_router(router)
|
|
|