Skip to content

Instantly share code, notes, and snippets.

@chbndrhnns
Created August 31, 2021 09:08
Show Gist options
  • Save chbndrhnns/5bed4756099b00227edd962562a77acb to your computer and use it in GitHub Desktop.
Save chbndrhnns/5bed4756099b00227edd962562a77acb to your computer and use it in GitHub Desktop.
FastAPI response wrapper
from typing import Generic, TypeVar
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
from pydantic.generics import GenericModel
T = TypeVar("T")
class Response(GenericModel, Generic[T]):
data: T
class One(BaseModel):
is_enabled: bool = True
class Two(BaseModel):
is_enabled: bool = False
app = FastAPI()
@app.get("/one", response_model=Response[One])
async def get_one():
...
@app.get("/two", response_model=Response[Two])
async def get_two():
...
if __name__ == "__main__":
uvicorn.run(app, log_config=None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment