Skip to content

Commit 23c3a5b

Browse files
committed
seuqential - add sync tests for sse, jsonl & ndjson
1 parent a40a640 commit 23c3a5b

3 files changed

Lines changed: 55 additions & 4 deletions

File tree

src/aiopenapi3/request.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,26 @@ def iter_json(response: httpx.Response) -> Iterator["JSON"]:
344344
https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
345345
"""
346346

347+
def iter_json(response: httpx.Response) -> Iterator["JSON"]:
348+
for chunk in response.iter_text():
349+
data_ = ""
350+
for line in chunk.splitlines(keepends=True):
351+
data_ += line
352+
if not data_.endswith(("\r\r", "\n\n", "\r\n\r\n")):
353+
continue
354+
355+
v = dict()
356+
for l in data_.splitlines(keepends=False):
357+
if l == "":
358+
continue
359+
cmd, _, value = l.partition(":")
360+
if cmd not in ("event", "data", "id", "retry", ""):
361+
# ignore
362+
continue
363+
v[cmd or "comment"] = value.lstrip()
364+
data_ = ""
365+
yield v
366+
elif False:
347367
import ijson
348368

349369
class ReadEventStream:

tests/sequential_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,3 @@ async def test_sse(server, client):
109109
async with req.sequence() as sequence:
110110
async for obj in sequence:
111111
print(obj)
112-
await asyncio.sleep(1.1)

tests/v32_test.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ async def test_MediaType(httpx_mock, with_schema_itemSchema):
3232
import pydantic
3333

3434
api = OpenAPI("https://example.org/api/", with_schema_itemSchema, session_factory=httpx.AsyncClient)
35-
ServerSentEvent: pydantic.BaseModel = api.components.schemas["ServerSentEvent"].get_type()
3635

3736
records = with_schema_itemSchema["components"]["examples"]["LogJSONPerLine"]["value"].strip("\n").split("\n")
37+
38+
ServerSentEvent: pydantic.BaseModel = api.components.schemas["ServerSentEvent"].get_type()
3839
t = pydantic.TypeAdapter(list[ServerSentEvent])
39-
ct = "\n\n".join(f"data: {i}" for i in records * 16)
40+
ct = "\n\n".join(f"data: {i}\n: {idx}" for idx, i in enumerate(records * 16))
4041

4142
httpx_mock.add_response(
4243
url="https://example.org/api/json_seq",
@@ -95,15 +96,46 @@ def test_MediaType_itemSchema_sync(httpx_mock, with_schema_itemSchema):
9596

9697
records = with_schema_itemSchema["components"]["examples"]["LogJSONPerLine"]["value"].strip("\n").split("\n")
9798

99+
ServerSentEvent: pydantic.BaseModel = api.components.schemas["ServerSentEvent"].get_type()
100+
t = pydantic.TypeAdapter(list[ServerSentEvent])
101+
ct = "\n\n".join(f"data: {i}\n: {idx}" for idx, i in enumerate(records * 16))
102+
98103
httpx_mock.add_response(
99104
url="https://example.org/api/json_seq",
100105
headers={"Content-Type": "application/json-seq"},
101106
stream=IteratorStream([b"\x1e" + i.encode() + b"\n" for i in (records * 16)]),
102107
)
108+
httpx_mock.add_response(
109+
url="https://example.org/api/jsonl",
110+
headers={"Content-Type": "application/jsonl"},
111+
stream=IteratorStream([i.encode() + b"\n" for i in (records * 16)]),
112+
)
113+
httpx_mock.add_response(
114+
url="https://example.org/api/ndjson",
115+
headers={"Content-Type": "application/x-ndjson"},
116+
stream=IteratorStream([i.encode() + b"\n" for i in (records * 16)]),
117+
)
118+
httpx_mock.add_response(
119+
url="https://example.org/api/text_events", headers={"Content-Type": "text/event-stream"}, content=ct
120+
)
103121

104122
req = api.createRequest("json_seq")
105123
with req.sequence() as sequence:
106-
print(sequence.headers)
124+
for obj in sequence:
125+
print(obj)
126+
127+
req = api.createRequest("jsonl")
128+
with req.sequence() as sequence:
129+
for obj in sequence:
130+
print(obj)
131+
132+
req = api.createRequest("ndjson")
133+
with req.sequence() as sequence:
134+
for obj in sequence:
135+
print(obj)
136+
137+
req = api.createRequest("text_events")
138+
with req.sequence() as sequence:
107139
for obj in sequence:
108140
print(obj)
109141

0 commit comments

Comments
 (0)