Created
June 8, 2023 05:34
-
-
Save daaniam/00eafdba7203c5cda5e2bc29b2970176 to your computer and use it in GitHub Desktop.
FastAPI custom response with data & errors
This file contains hidden or 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
class MyCUstomResponse(JSONResponse): | |
def __init__( | |
self, | |
data: list[t.Any], | |
errors: list[t.Any] | None = None, | |
status_code: int = 200, | |
headers: dict[str, str] | None = None, | |
media_type: str | None = None, | |
background: BackgroundTask | None = None, | |
) -> None: | |
self.data = data | |
if errors is None: | |
errors = [] | |
self.errors = errors | |
self.content = {"data": self.data, "errors": self.errors} | |
super().__init__( | |
content=self.content, | |
status_code=status_code, | |
headers=headers, | |
media_type=media_type, | |
background=background, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment