REFACTOR ALL APIs
This commit is contained in:
@@ -391,25 +391,65 @@ async def _proxy_request(request: Request, target_path: str, service_url: str, t
|
||||
|
||||
# Get request body if present
|
||||
body = None
|
||||
files = None
|
||||
data = None
|
||||
|
||||
if request.method in ["POST", "PUT", "PATCH"]:
|
||||
body = await request.body()
|
||||
|
||||
content_type = request.headers.get("content-type", "")
|
||||
logger.info(f"Processing {request.method} request with content-type: {content_type}")
|
||||
|
||||
# Handle multipart/form-data (file uploads)
|
||||
if "multipart/form-data" in content_type:
|
||||
logger.info("Detected multipart/form-data, parsing form...")
|
||||
# For multipart/form-data, we need to re-parse and forward as files
|
||||
form = await request.form()
|
||||
logger.info(f"Form parsed, found {len(form)} fields: {list(form.keys())}")
|
||||
|
||||
# Extract files and form fields separately
|
||||
files_dict = {}
|
||||
data_dict = {}
|
||||
|
||||
for key, value in form.items():
|
||||
if hasattr(value, 'file'): # It's a file
|
||||
# Read file content
|
||||
file_content = await value.read()
|
||||
files_dict[key] = (value.filename, file_content, value.content_type)
|
||||
logger.info(f"Found file field '{key}': filename={value.filename}, size={len(file_content)}, type={value.content_type}")
|
||||
else: # It's a regular form field
|
||||
data_dict[key] = value
|
||||
logger.info(f"Found form field '{key}': value={value}")
|
||||
|
||||
files = files_dict if files_dict else None
|
||||
data = data_dict if data_dict else None
|
||||
|
||||
logger.info(f"Forwarding multipart request with files={list(files.keys()) if files else None}, data={list(data.keys()) if data else None}")
|
||||
|
||||
# Remove content-type from headers - httpx will set it with new boundary
|
||||
headers.pop("content-type", None)
|
||||
headers.pop("content-length", None)
|
||||
else:
|
||||
# For other content types, use body as before
|
||||
body = await request.body()
|
||||
logger.info(f"Using raw body, size: {len(body)} bytes")
|
||||
|
||||
# Add query parameters
|
||||
params = dict(request.query_params)
|
||||
|
||||
|
||||
timeout_config = httpx.Timeout(
|
||||
connect=30.0, # Connection timeout
|
||||
read=600.0, # Read timeout: 10 minutes (was 30s)
|
||||
write=30.0, # Write timeout
|
||||
pool=30.0 # Pool timeout
|
||||
)
|
||||
|
||||
|
||||
async with httpx.AsyncClient(timeout=timeout_config) as client:
|
||||
response = await client.request(
|
||||
method=request.method,
|
||||
url=url,
|
||||
headers=headers,
|
||||
content=body,
|
||||
files=files,
|
||||
data=data,
|
||||
params=params
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user