Skip to content

Commit 2efe670

Browse files
committed
# 0.1.3
* simplify storing cached Response data
1 parent 214b235 commit 2efe670

7 files changed

Lines changed: 9 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 0.1.3
2+
* simplify storing cached Response data
3+
14
# 0.1.2
25
* added `auto_read_body` to request and backends in order to automatically read response body so that all retry logic could happen in case of errors
36

extapi/http/backends/aiohttp.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
class AiohttpResponseWrap(BackendResponseProtocol[aiohttp.ClientResponse]):
16-
__slots__ = ("_original",)
16+
__slots__ = ("_original", "_body")
1717

1818
def __init__(self, response: aiohttp.ClientResponse, *, body: bytes | None = None):
1919
self._original = response
@@ -39,14 +39,8 @@ async def json(
3939
encoding: str | None,
4040
loads: Callable[[str], Any] = DEFAULT_JSON_DECODER,
4141
) -> Any:
42-
if self._body is not None:
43-
if encoding is None:
44-
s = self._body.decode()
45-
else:
46-
s = self._body.decode(encoding=encoding)
47-
return loads(s)
48-
49-
# if body is not supplied - delegate to original
42+
# always delegate to original aiohttp.ClientResponse
43+
# because the data has already been read
5044
return await self._original.json(encoding=encoding, loads=loads)
5145

5246

extapi/http/backends/httpx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414
class HttpxResponseWrap(BackendResponseProtocol[httpx.Response]):
15-
__slots__ = ("_original",)
15+
__slots__ = ("_original", "_body")
1616

1717
def __init__(self, response: httpx.Response, *, body: bytes | None = None):
1818
self._original = response

extapi/http/types.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,12 @@ class Response(Generic[T]):
6666
headers: CIMultiDict = field(default_factory=lambda: CIMultiDict())
6767
backend_response: BackendResponseProtocol[T]
6868

69-
_data: bytes | None = None
70-
7169
@property
7270
def original(self) -> T:
7371
return self.backend_response.original()
7472

7573
async def read(self) -> bytes:
76-
if self._data is not None:
77-
return self._data
78-
79-
self._data = await self.backend_response.read()
80-
return self._data
74+
return await self.backend_response.read()
8175

8276
async def json(
8377
self,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "extapi"
3-
version = "0.1.2"
3+
version = "0.1.3"
44
description = "External API library"
55
authors = [
66
{ name = "KTS", email = "hello@kts.tech" }

tests/conftest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ def response_simple(request_simple: RequestData) -> Response[Any]:
3939
url=request_simple.url,
4040
status=200,
4141
backend_response=DummyBackendResponse(),
42-
_data=b"",
4342
)
4443

4544

tests/exthttp/test_types.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ async def test_has_data_double(self):
5050

5151
assert response.status == 200
5252
assert response.headers == CIMultiDict()
53-
assert response._data is None
5453
assert await response.read() == b"some-data"
55-
assert response._data is not None
5654
assert await response.read() == b"some-data"
5755

5856
async def test_ctx_mgr_not_closable(self):

0 commit comments

Comments
 (0)