Updated claude big changes
This commit is contained in:
0
app/common/__init__.py
Normal file
0
app/common/__init__.py
Normal file
53
app/common/responses.py
Normal file
53
app/common/responses.py
Normal file
@@ -0,0 +1,53 @@
|
||||
"""
|
||||
Standardized API response utilities for FastAPI services.
|
||||
|
||||
Usage:
|
||||
from app.common.responses import error_response, success_response
|
||||
|
||||
# Error responses
|
||||
return error_response("User not found", 404)
|
||||
return error_response("Invalid credentials", 401)
|
||||
|
||||
# Success responses
|
||||
return success_response({"user": user_data})
|
||||
return success_response({"message": "Created"}, 201)
|
||||
"""
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
|
||||
def error_response(message: str, status_code: int = 400, details: str = None) -> JSONResponse:
|
||||
"""
|
||||
Create a standardized error response.
|
||||
|
||||
Args:
|
||||
message: Human-readable error message
|
||||
status_code: HTTP status code (default 400)
|
||||
details: Optional additional details (e.g., exception info)
|
||||
|
||||
Returns:
|
||||
JSONResponse with error data
|
||||
"""
|
||||
content = {
|
||||
"ok": False,
|
||||
"error": message
|
||||
}
|
||||
if details:
|
||||
content["details"] = details
|
||||
return JSONResponse(content=content, status_code=status_code)
|
||||
|
||||
|
||||
def success_response(data: dict = None, status_code: int = 200) -> JSONResponse:
|
||||
"""
|
||||
Create a standardized success response.
|
||||
|
||||
Args:
|
||||
data: Response data dictionary
|
||||
status_code: HTTP status code (default 200)
|
||||
|
||||
Returns:
|
||||
JSONResponse with success data
|
||||
"""
|
||||
content = {"ok": True}
|
||||
if data:
|
||||
content.update(data)
|
||||
return JSONResponse(content=content, status_code=status_code)
|
||||
Reference in New Issue
Block a user