Skip to content

Commit

Permalink
actually fix lint and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Bunting committed Jul 9, 2024
1 parent 6b0f48d commit aa703ae
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/supergood/vendors/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ServerSentEvent:
Storage class for streamed server side events
"""

event: str | None
data: Optional[str]
id: str | None
retry: int | None
event: Optional[str]
data: List[str]
id: Optional[str]
retry: Optional[int]
5 changes: 3 additions & 2 deletions src/supergood/vendors/httpx.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from typing import Optional
from uuid import uuid4

import httpx
Expand Down Expand Up @@ -132,14 +133,14 @@ def _parse_sse(chunk: str):
# if we got to the end but didn't get a dispatch instruction, sse was invalid
return None

def _wrap_iter_bytes(response: httpx.Response):
def _wrap_iter_bytes(response: httpx.Response, chunk_size: Optional[int] = None):
request_id = getattr(response, REQUEST_ID_KEY)
status_text = response.extensions.get("reason_phrase", None)
if status_text:
status_text = status_text.decode("utf-8")
response_chunks = []
data = b""
for chunk in _original_response_iter_bytes(response):
for chunk in _original_response_iter_bytes(response, chunk_size):
for line in chunk.splitlines(keepends=True):
data += line
if data.endswith((b"\r\r", b"\n\n", b"\r\n\r\n")):
Expand Down
Empty file added tests/vendors/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions tests/vendors/test_httpx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import httpx
from pytest_httpserver import HTTPServer

from supergood.api import Api


class TestHttpx:
def test_httpx_streaming(self, httpserver: HTTPServer, supergood_client):
raw_response = 'data: {"valid": "json"}\n\ndata: [DONE]\n\n'
httpserver.expect_request("/stream").respond_with_data(
response_data=raw_response
)
resp = b""
with httpx.stream("GET", httpserver.url_for("/stream")) as s:
for data in s.iter_bytes(chunk_size=1):
resp += data
assert resp.decode("utf-8") == raw_response
supergood_client.flush_cache()
args = Api.post_events.call_args[0][0]
# verify 2 entries, one each for each response field
assert len(args[0]["response"]["body"]) == 2
# verifies valid JSON is indexible
assert args[0]["response"]["body"][0]["data"][0]["valid"] == "json"
supergood_client.kill()

0 comments on commit aa703ae

Please sign in to comment.