Skip to content

Commit e6451ef

Browse files
authored
chore: add a nice repr to CogniteHTTPResponse + tests (#2658)
1 parent 9dc5827 commit e6451ef

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

cognite/client/response.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ def json(self) -> Any:
118118
self._json_cache = self._response.json()
119119
return self._json_cache
120120

121+
def __repr__(self) -> str:
122+
return f"<CogniteHTTPResponse [{self.status_code} {self.reason_phrase}]>"
123+
121124
def raise_for_status(self) -> CogniteHTTPResponse:
122125
"""
123126
Raises a CogniteHTTPStatusError if the response status code is 4xx or 5xx.

tests/tests_unit/test_http_client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
get_global_async_httpx_client,
1515
)
1616
from cognite.client.config import global_config
17+
from cognite.client.response import CogniteHTTPResponse
1718

1819

1920
@pytest.fixture
@@ -171,3 +172,21 @@ async def test_client_settings_match_global_config(self, monkeypatch: pytest.Mon
171172
assert pool._keepalive_expiry == 5
172173
assert pool._ssl_context.verify_mode == ssl.CERT_NONE # disable_ssl should cause this
173174
assert pool._ssl_context.check_hostname is False
175+
176+
177+
def make_response(status_code: int) -> CogniteHTTPResponse:
178+
request = httpx.Request("GET", URL)
179+
return CogniteHTTPResponse(httpx.Response(status_code=status_code, request=request))
180+
181+
182+
class TestCogniteHTTPResponseRepr:
183+
@pytest.mark.parametrize(
184+
"status_code, expected",
185+
[
186+
(200, "<CogniteHTTPResponse [200 OK]>"),
187+
(404, "<CogniteHTTPResponse [404 Not Found]>"),
188+
(500, "<CogniteHTTPResponse [500 Internal Server Error]>"),
189+
],
190+
)
191+
def test_repr_format(self, status_code: int, expected: str) -> None:
192+
assert repr(make_response(status_code)) == expected

0 commit comments

Comments
 (0)