imetting_backend/app/core/response.py

14 lines
490 B
Python

from typing import Union
from fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder
def create_api_response(code: str, message: str, data: Union[dict, list, None] = None) -> JSONResponse:
"""Creates a standardized API JSON response."""
return JSONResponse(
status_code=200,
content={
"code": str(code),
"message": message,
"data": jsonable_encoder(data) if data is not None else {},
},
)