File size: 5,516 Bytes
63f0b06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from typing import Any, Dict, List, Optional, Union
from datetime import datetime
import traceback

from .logger import get_logger
from .exceptions import FragmentaError

logger = get_logger(__name__)

class APIResponse:
    
    @staticmethod
    def success(data: Any = None, message: str = None, meta: Dict[str, Any] = None) -> Dict[str, Any]:
        response = {
            "success": True,
            "timestamp": datetime.utcnow().isoformat(),
            "data": data
        }
        
        if message:
            response["message"] = message
        
        if meta:
            response["meta"] = meta
        
        logger.debug(f"API Success Response: {message or 'Operation completed'}")
        return response
    
    @staticmethod
    def error(
        error: Union[str, Exception, FragmentaError],
        status_code: int = 500,
        details: Dict[str, Any] = None,
        include_traceback: bool = False
    ) -> Dict[str, Any]:
        if isinstance(error, FragmentaError):
            message = error.message
            error_details = error.details.copy() if error.details else {}
            if details:
                error_details.update(details)
        elif isinstance(error, Exception):
            message = str(error)
            error_details = {
                "type": type(error).__name__,
                **(details or {})
            }
        else:
            message = str(error)
            error_details = details or {}
        
        response = {
            "success": False,
            "timestamp": datetime.utcnow().isoformat(),
            "error": {
                "message": message,
                "code": status_code,
                "details": error_details
            }
        }
        
        if include_traceback and isinstance(error, Exception):
            response["error"]["traceback"] = traceback.format_exc()
        
        logger.error(f"API Error Response ({status_code}): {message}")
        return response
    
    @staticmethod
    def validation_error(field_errors: Dict[str, List[str]]) -> Dict[str, Any]:
        return APIResponse.error(
            "Validation failed",
            status_code=400,
            details={
                "validation_errors": field_errors,
                "total_errors": sum(len(errors) for errors in field_errors.values())
            }
        )
    
    @staticmethod
    def not_found(resource: str, identifier: str = None) -> Dict[str, Any]:
        message = f"{resource.title()} not found"
        if identifier:
            message += f": {identifier}"
        
        return APIResponse.error(
            message,
            status_code=404,
            details={
                "resource_type": resource,
                "identifier": identifier
            }
        )
    
    @staticmethod
    def unauthorized(message: str = "Authentication required") -> Dict[str, Any]:
        return APIResponse.error(
            message,
            status_code=401,
            details={"auth_required": True}
        )
    
    @staticmethod
    def forbidden(message: str = "Access denied") -> Dict[str, Any]:
        return APIResponse.error(
            message,
            status_code=403,
            details={"access_denied": True}
        )
    
    @staticmethod
    def progress(
        current: int,
        total: int,
        message: str = None,
        data: Any = None
    ) -> Dict[str, Any]:
        percentage = (current / total * 100) if total > 0 else 0
        
        response = {
            "success": True,
            "timestamp": datetime.utcnow().isoformat(),
            "progress": {
                "current": current,
                "total": total,
                "percentage": round(percentage, 2),
                "completed": current >= total
            }
        }
        
        if message:
            response["progress"]["message"] = message
        
        if data:
            response["data"] = data
        
        return response

def handle_api_error(func):
    def wrapper(*args, **kwargs):
        try:
            result = func(*args, **kwargs)
            if isinstance(result, dict) and "success" in result:
                return result
            return APIResponse.success(result)
        
        except FragmentaError as e:
            return APIResponse.error(e)
        
        except Exception as e:
            logger.exception(f"Unexpected error in {func.__name__}")
            return APIResponse.error(
                "Internal server error",
                status_code=500,
                details={"function": func.__name__}
            )
    
    return wrapper

def paginate_response(
    data: List[Any],
    page: int = 1,
    per_page: int = 10,
    total: int = None
) -> Dict[str, Any]:
    if total is None:
        total = len(data)
    
    start_idx = (page - 1) * per_page
    end_idx = start_idx + per_page
    page_data = data[start_idx:end_idx]
    
    total_pages = (total + per_page - 1) // per_page
    
    meta = {
        "pagination": {
            "page": page,
            "per_page": per_page,
            "total_items": total,
            "total_pages": total_pages,
            "has_next": page < total_pages,
            "has_prev": page > 1,
            "next_page": page + 1 if page < total_pages else None,
            "prev_page": page - 1 if page > 1 else None
        }
    }
    
    return APIResponse.success(
        data=page_data,
        meta=meta
    )