Skip to content

Commit a0b3d66

Browse files
feat(jsonl): add .close() method (#11)
1 parent fffe7e5 commit a0b3d66

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

Diff for: src/gitpod/_decoders/jsonl.py

+26-4
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,29 @@ class JSONLDecoder(Generic[_T]):
1717
into a given type.
1818
"""
1919

20-
http_response: httpx.Response | None
20+
http_response: httpx.Response
2121
"""The HTTP response this decoder was constructed from"""
2222

2323
def __init__(
24-
self, *, raw_iterator: Iterator[bytes], line_type: type[_T], http_response: httpx.Response | None
24+
self,
25+
*,
26+
raw_iterator: Iterator[bytes],
27+
line_type: type[_T],
28+
http_response: httpx.Response,
2529
) -> None:
2630
super().__init__()
2731
self.http_response = http_response
2832
self._raw_iterator = raw_iterator
2933
self._line_type = line_type
3034
self._iterator = self.__decode__()
3135

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+
3243
def __decode__(self) -> Iterator[_T]:
3344
buf = b""
3445
for chunk in self._raw_iterator:
@@ -63,17 +74,28 @@ class AsyncJSONLDecoder(Generic[_T]):
6374
into a given type.
6475
"""
6576

66-
http_response: httpx.Response | None
77+
http_response: httpx.Response
6778

6879
def __init__(
69-
self, *, raw_iterator: AsyncIterator[bytes], line_type: type[_T], http_response: httpx.Response | None
80+
self,
81+
*,
82+
raw_iterator: AsyncIterator[bytes],
83+
line_type: type[_T],
84+
http_response: httpx.Response,
7085
) -> None:
7186
super().__init__()
7287
self.http_response = http_response
7388
self._raw_iterator = raw_iterator
7489
self._line_type = line_type
7590
self._iterator = self.__decode__()
7691

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+
7799
async def __decode__(self) -> AsyncIterator[_T]:
78100
buf = b""
79101
async for chunk in self._raw_iterator:

0 commit comments

Comments
 (0)