I have an endpoint which returns a Pydantic object. However, I would like a response code other than 200 in some cases (for example if my service in not healthy). How can I achieve that with FastAPI?
class ServiceHealth(BaseModel):
http_ok: bool = True
database_ok: bool = False
def is_everything_ok(self) -> bool:
return self.http_ok and self.database_ok
@router.get("/health")
def health() -> ServiceHealth:
return ServiceHealth()