-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use server sent events to keep connection alive
- Loading branch information
1 parent
a63780e
commit 3cc9296
Showing
4 changed files
with
99 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
httpx==0.24.0 | ||
markdown==3.4.1 |
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
26 changes: 26 additions & 0 deletions
26
racetrack_commons/racetrack_commons/api/server_sent_events.py
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,26 @@ | ||
import json | ||
from typing import Dict | ||
|
||
from httpx import Response | ||
|
||
RESULT_EVENT = 'event: result\n' | ||
DATA_MESSAGE = 'data: ' | ||
|
||
|
||
def validate_streaming_response(response: Response): | ||
if response.is_success: | ||
return | ||
message = f'HTTP error "{response.status_code} {response.reason_phrase}" ' \ | ||
f'for url {response.request.method} {response.url}' | ||
raise RuntimeError(message) | ||
|
||
|
||
def extract_response_dict(response_text: str) -> Dict: | ||
prefix = RESULT_EVENT + DATA_MESSAGE | ||
last_occurrence = response_text.find(prefix) | ||
if last_occurrence == -1: | ||
raise ValueError('could not find result event in the SSE response') | ||
|
||
remainder = response_text[last_occurrence + len(prefix):] | ||
json_dict = json.loads(remainder) | ||
return json_dict |