41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
"""
|
|
Response formatter for API responses.
|
|
"""
|
|
|
|
from typing import Any, Dict, Optional
|
|
from datetime import datetime
|
|
|
|
|
|
class ResponseFormatter:
|
|
"""Format API responses consistently"""
|
|
|
|
def success(self, data: Any, message: str = "Success") -> Dict[str, Any]:
|
|
"""Format success response"""
|
|
return {
|
|
"status": "success",
|
|
"message": message,
|
|
"data": data,
|
|
"timestamp": datetime.utcnow().isoformat()
|
|
}
|
|
|
|
def error(self, message: str, code: str = "ERROR", details: Optional[Dict] = None) -> Dict[str, Any]:
|
|
"""Format error response"""
|
|
response = {
|
|
"status": "error",
|
|
"message": message,
|
|
"code": code,
|
|
"timestamp": datetime.utcnow().isoformat()
|
|
}
|
|
|
|
if details:
|
|
response["details"] = details
|
|
|
|
return response
|
|
|
|
def health(self, healthy: bool = True, components: Optional[Dict] = None) -> Dict[str, Any]:
|
|
"""Format health check response"""
|
|
return {
|
|
"status": "healthy" if healthy else "unhealthy",
|
|
"timestamp": datetime.utcnow().isoformat(),
|
|
"components": components or {}
|
|
} |