| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import json |
| 4 |
from typing_extensions import Generic, TypeVar, Iterator, AsyncIterator |
| 5 |
|
| 6 |
import httpx2 |
| 7 |
|
| 8 |
from .._models import construct_type_unchecked |
| 9 |
|
| 10 |
_T = TypeVar("_T") |
| 11 |
|
| 12 |
|
| 13 |
class JSONLDecoder(Generic[_T]): |
| 14 |
"""A decoder for [JSON Lines](https://jsonlines.org) format. |
| 15 |
|
| 16 |
This class provides an iterator over a byte-iterator that parses each JSON Line |
| 17 |
into a given type. |
| 18 |
""" |
| 19 |
|
| 20 |
http_response: httpx2.Response |
| 21 |
"""The HTTP response this decoder was constructed from""" |
| 22 |
|
| 23 |
def __init__( |
| 24 |
self, |
| 25 |
*, |
| 26 |
raw_iterator: Iterator[bytes], |
| 27 |
line_type: type[_T], |
| 28 |
http_response: httpx2.Response, |
| 29 |
) -> None: |
| 30 |
super().__init__() |
| 31 |
self.http_response = http_response |
| 32 |
self._raw_iterator = raw_iterator |
| 33 |
self._line_type = line_type |
| 34 |
self._iterator = self.__decode__() |
| 35 |
|
| 36 |
def close(self) -> None: |
| 37 |
"""Close the response body stream. |
| 38 |
|
| 39 |
This is called automatically if you consume the entire stream. |
| 40 |
""" |
| 41 |
self.http_response.close() |
| 42 |
|
| 43 |
def __decode__(self) -> Iterator[_T]: |
| 44 |
buf = b"" |
| 45 |
for chunk in self._raw_iterator: |
| 46 |
for line in chunk.splitlines(keepends=True): |
| 47 |
buf += line |
| 48 |
if buf.endswith((b"\r", b"\n", b"\r\n")): |
| 49 |
yield construct_type_unchecked( |
| 50 |
value=json.loads(buf), |
| 51 |
type_=self._line_type, |
| 52 |
) |
| 53 |
buf = b"" |
| 54 |
|
| 55 |
# flush |
| 56 |
if buf: |
| 57 |
yield construct_type_unchecked( |
| 58 |
value=json.loads(buf), |
| 59 |
type_=self._line_type, |
| 60 |
) |
| 61 |
|
| 62 |
def __next__(self) -> _T: |
| 63 |
return self._iterator.__next__() |
| 64 |
|
| 65 |
def __iter__(self) -> Iterator[_T]: |
| 66 |
for item in self._iterator: |
| 67 |
yield item |
| 68 |
|
| 69 |
|
| 70 |
class AsyncJSONLDecoder(Generic[_T]): |
| 71 |
"""A decoder for [JSON Lines](https://jsonlines.org) format. |
| 72 |
|
| 73 |
This class provides an async iterator over a byte-iterator that parses each JSON Line |
| 74 |
into a given type. |
| 75 |
""" |
| 76 |
|
| 77 |
http_response: httpx2.Response |
| 78 |
|
| 79 |
def __init__( |
| 80 |
self, |
| 81 |
*, |
| 82 |
raw_iterator: AsyncIterator[bytes], |
| 83 |
line_type: type[_T], |
| 84 |
http_response: httpx2.Response, |
| 85 |
) -> None: |
| 86 |
super().__init__() |
| 87 |
self.http_response = http_response |
| 88 |
self._raw_iterator = raw_iterator |
| 89 |
self._line_type = line_type |
| 90 |
self._iterator = self.__decode__() |
| 91 |
|
| 92 |
async def close(self) -> None: |
| 93 |
"""Close the response body stream. |
| 94 |
|
| 95 |
This is called automatically if you consume the entire stream. |
| 96 |
""" |
| 97 |
await self.http_response.aclose() |
| 98 |
|
| 99 |
async def __decode__(self) -> AsyncIterator[_T]: |
| 100 |
buf = b"" |
| 101 |
async for chunk in self._raw_iterator: |
| 102 |
for line in chunk.splitlines(keepends=True): |
| 103 |
buf += line |
| 104 |
if buf.endswith((b"\r", b"\n", b"\r\n")): |
| 105 |
yield construct_type_unchecked( |
| 106 |
value=json.loads(buf), |
| 107 |
type_=self._line_type, |
| 108 |
) |
| 109 |
buf = b"" |
| 110 |
|
| 111 |
# flush |
| 112 |
if buf: |
| 113 |
yield construct_type_unchecked( |
| 114 |
value=json.loads(buf), |
| 115 |
type_=self._line_type, |
| 116 |
) |
| 117 |
|
| 118 |
async def __anext__(self) -> _T: |
| 119 |
return await self._iterator.__anext__() |
| 120 |
|
| 121 |
async def __aiter__(self) -> AsyncIterator[_T]: |
| 122 |
async for item in self._iterator: |
| 123 |
yield item |