wag-managment-api-service-v.../api_library/response_handlers.py

39 lines
1.3 KiB
Python

from typing import Any, Optional
from fastapi import status
from fastapi.responses import JSONResponse
class ResponseHandler:
@staticmethod
def success(message: str, data: Optional[Any] = None, status_code: int = status.HTTP_200_OK) -> JSONResponse:
"""Create a success response"""
return JSONResponse(
content={
"completed": True,
"message": message,
"data": data or {},
},
status_code=status_code,
)
@staticmethod
def error(message: str, data: Optional[Any] = None, status_code: int = status.HTTP_400_BAD_REQUEST) -> JSONResponse:
"""Create an error response"""
return JSONResponse(
content={
"completed": False,
"message": message,
"data": data or {},
},
status_code=status_code,
)
@staticmethod
def unauthorized(message: str = "Unauthorized access") -> JSONResponse:
"""Create an unauthorized response"""
return ResponseHandler.error(message, status_code=status.HTTP_401_UNAUTHORIZED)
@staticmethod
def not_found(message: str = "Resource not found") -> JSONResponse:
"""Create a not found response"""
return ResponseHandler.error(message, status_code=status.HTTP_404_NOT_FOUND)