我有一个终结点,它返回一个PYDANIC对象.然而,我希望在某些情况下响应代码不是200(例如,如果我的服务不正常).我如何使用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()