from fastapi import FastAPI, Request, Response
from pydantic import BaseModel
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import StreamingResponse, JSONResponse
from typing import Any, Optional,List
import json

class ResponseModel(BaseModel):
    code: int
    message: str
    data: Any = None  # 使用Optional[Any]也是可以的


class StandardResponse(BaseModel):
    success: bool
    error_code: Optional[int] = None
    error_msg: Optional[str] = None
    records: Optional[Any] = None


# class ResponseFormatterMiddleware(BaseHTTPMiddleware):
#     async def dispatch(self, request: Request, call_next):
#         response = await call_next(request)
     
#         if response.status_code >= 200 and response.status_code < 300:
#             try:                
#                 response_body = response.body.decode('utf-8')
#                 response_data = {"code": SUCCESS_CODE, "message": SUCCESS_MESSAGE, "data": response_body}
#                 response.body = str(response_data).encode('utf-8')  # 注意这里需要根据实际情况调整
#                 response.media_type = 'application/json'
#             except Exception as e:
#                 print(f"Error formatting response: {e}")
        
#         return response