-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: httpx timeout error handling (#139)
Co-authored-by: leoguillaume <[email protected]>
- Loading branch information
1 parent
ee37cf1
commit 588e385
Showing
14 changed files
with
360 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import json | ||
from typing import AsyncIterator | ||
|
||
from fastapi.responses import StreamingResponse | ||
from starlette.types import Send | ||
|
||
|
||
class StreamingResponseWithStatusCode(StreamingResponse): | ||
""" | ||
Variation of StreamingResponse that can dynamically decide the HTTP status code, | ||
based on the return value of the content iterator (parameter `content`). | ||
Expects the content to yield either just str content as per the original `StreamingResponse` | ||
or else tuples of (`content`: `str`, `status_code`: `int`). | ||
""" | ||
|
||
body_iterator: AsyncIterator[str | bytes] | ||
response_started: bool = False | ||
|
||
async def stream_response(self, send: Send) -> None: | ||
more_body = True | ||
try: | ||
first_chunk = await self.body_iterator.__anext__() | ||
if isinstance(first_chunk, tuple): | ||
first_chunk_content, self.status_code = first_chunk | ||
else: | ||
first_chunk_content, self.status_code = first_chunk, 200 | ||
|
||
if isinstance(first_chunk_content, str): | ||
first_chunk_content = first_chunk_content.encode(self.charset) | ||
|
||
await send({"type": "http.response.start", "status": self.status_code, "headers": self.raw_headers}) | ||
|
||
self.response_started = True | ||
await send({"type": "http.response.body", "body": first_chunk_content, "more_body": more_body}) | ||
|
||
async for chunk in self.body_iterator: | ||
if isinstance(chunk, tuple): | ||
content, status_code = chunk | ||
if status_code // 100 != 2: | ||
# an error occurred mid-stream | ||
if not isinstance(content, bytes): | ||
content = content.encode(self.charset) | ||
more_body = False | ||
await send({"type": "http.response.body", "body": content, "more_body": more_body}) | ||
return | ||
else: | ||
content = chunk | ||
|
||
if isinstance(content, str): | ||
content = content.encode(self.charset) | ||
more_body = True | ||
await send({"type": "http.response.body", "body": content, "more_body": more_body}) | ||
|
||
except Exception: | ||
more_body = False | ||
error_resp = {"error": {"message": "Internal Server Error"}} | ||
error_event = f"event: error\ndata: {json.dumps(error_resp)}\n\n".encode(self.charset) | ||
if not self.response_started: | ||
await send({"type": "http.response.start", "status": 500, "headers": self.raw_headers}) | ||
await send({"type": "http.response.body", "body": error_event, "more_body": more_body}) | ||
if more_body: | ||
await send({"type": "http.response.body", "body": b"", "more_body": False}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.