Spaces:
Running
Running
Wimmboo commited on
Commit ·
b1024aa
1
Parent(s): 22bdc0f
fix streaming: use client.send() with stream=True instead of context manager
Browse files- proxy/client.py +14 -11
proxy/client.py
CHANGED
|
@@ -65,17 +65,20 @@ async def forward_request(provider: Provider, real_key: str, request: Request, b
|
|
| 65 |
async with httpx.AsyncClient(timeout=_TIMEOUT, follow_redirects=True) as client:
|
| 66 |
try:
|
| 67 |
if stream:
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
| 79 |
else:
|
| 80 |
upstream = await client.post(target_url, headers=headers, json=body)
|
| 81 |
except httpx.TimeoutException as exc:
|
|
|
|
| 65 |
async with httpx.AsyncClient(timeout=_TIMEOUT, follow_redirects=True) as client:
|
| 66 |
try:
|
| 67 |
if stream:
|
| 68 |
+
req = client.build_request("POST", target_url, headers=headers, json=body)
|
| 69 |
+
upstream = await client.send(req, stream=True)
|
| 70 |
+
|
| 71 |
+
if upstream.status_code >= 400:
|
| 72 |
+
content = await upstream.aread()
|
| 73 |
+
logger.warning("Upstream error from %s: %d %s", provider.name, upstream.status_code, content[:500])
|
| 74 |
+
raise HTTPException(status_code=upstream.status_code, detail=content.decode("utf-8", errors="replace"))
|
| 75 |
+
|
| 76 |
+
return StreamingResponse(
|
| 77 |
+
_stream_chunks(upstream),
|
| 78 |
+
status_code=upstream.status_code,
|
| 79 |
+
media_type=upstream.headers.get("content-type", "text/event-stream"),
|
| 80 |
+
headers={k: v for k, v in upstream.headers.items() if k.lower() in ("cache-control", "x-request-id")},
|
| 81 |
+
)
|
| 82 |
else:
|
| 83 |
upstream = await client.post(target_url, headers=headers, json=body)
|
| 84 |
except httpx.TimeoutException as exc:
|