Download src/shared/responses.py from anaselamr/yaz: direct link, hf CLI and curl.
- Browser
- Download file 2.5 kB
-
https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/shared/responses.py
- Command line
-
hf download hf://spaces/anaselamr/yaz/src/shared/responses.py
-
curl -L -o responses.py https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/shared/responses.py
2.5 kB
| """ | |
| YAZ Care Platform - Response Helper Functions | |
| Utility functions for creating standardized API responses | |
| """ | |
| from datetime import datetime, timezone | |
| from typing import Any, TypeVar | |
| from uuid import uuid4 | |
| from shared.models.schemas import StandardResponse, ErrorResponse | |
| T = TypeVar("T") | |
| def create_response( | |
| data: Any = None, | |
| message: str = "Operation completed successfully", | |
| success: bool = True, | |
| request_id: str | None = None | |
| ) -> dict: | |
| """ | |
| Create a standardized API response dictionary. | |
| Args: | |
| data: Response payload data | |
| message: Human-readable message | |
| success: Whether the operation succeeded | |
| request_id: Optional request tracking ID | |
| Returns: | |
| Dictionary suitable for JSON response | |
| """ | |
| return { | |
| "success": success, | |
| "message": message, | |
| "data": data, | |
| "timestamp": datetime.now(timezone.utc).isoformat(), | |
| "request_id": request_id or str(uuid4()) | |
| } | |
| def create_error_response( | |
| error: str, | |
| error_code: str | None = None, | |
| details: dict[str, Any] | None = None, | |
| request_id: str | None = None | |
| ) -> dict: | |
| """ | |
| Create a standardized error response dictionary. | |
| Args: | |
| error: Error message | |
| error_code: Application-specific error code | |
| details: Additional error details | |
| request_id: Optional request tracking ID | |
| Returns: | |
| Dictionary suitable for JSON error response | |
| """ | |
| return { | |
| "success": False, | |
| "error": error, | |
| "error_code": error_code, | |
| "details": details, | |
| "timestamp": datetime.now(timezone.utc).isoformat(), | |
| "request_id": request_id or str(uuid4()) | |
| } | |
| def create_paginated_response( | |
| items: list, | |
| total: int, | |
| page: int = 1, | |
| page_size: int = 50, | |
| message: str = "Items retrieved successfully" | |
| ) -> dict: | |
| """ | |
| Create a standardized paginated response. | |
| Args: | |
| items: List of items for current page | |
| total: Total number of items | |
| page: Current page number | |
| page_size: Items per page | |
| message: Response message | |
| Returns: | |
| Dictionary with pagination metadata | |
| """ | |
| return create_response( | |
| data={ | |
| "items": items, | |
| "total": total, | |
| "page": page, | |
| "page_size": page_size, | |
| "total_pages": (total + page_size - 1) // page_size | |
| }, | |
| message=message | |
| ) | |