Files
gogo2/COBY/api/response_formatter.py
Dobromir Popov 622d059aae 18: tests, fixes
2025-08-05 14:11:49 +03:00

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 {}
}