Skip to content

Commit 9f46adb

Browse files
authored
feat!: return empty dict on empty responses in Client.request (#400)
This simplifies the API of the request method, and fixes the return values to always be a dict.
1 parent db37e63 commit 9f46adb

File tree

2 files changed

+4
-9
lines changed

2 files changed

+4
-9
lines changed

hcloud/_client.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ def request( # type: ignore[no-untyped-def]
210210
**kwargs,
211211
)
212212

213-
content = response.content
213+
content = {}
214214
try:
215-
if len(content) > 0:
215+
if len(response.content) > 0:
216216
content = response.json()
217217
except (TypeError, ValueError):
218218
self._raise_exception_from_response(response)
@@ -229,5 +229,4 @@ def request( # type: ignore[no-untyped-def]
229229
else:
230230
self._raise_exception_from_response(response)
231231

232-
# TODO: return an empty dict instead of an empty string when content == "".
233-
return content # type: ignore[return-value]
232+
return content

tests/unit/test_client.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,6 @@ def test__get_headers(self, client):
7474
"Authorization": "Bearer project_token",
7575
}
7676

77-
def test_request_library_mocked(self, client):
78-
response = client.request("POST", "url", params={"1": 2})
79-
assert response.__class__.__name__ == "MagicMock"
80-
8177
def test_request_ok(self, client, response):
8278
client._requests_session.request.return_value = response
8379
response = client.request(
@@ -142,7 +138,7 @@ def test_request_empty_content_200(self, client, response):
142138
response = client.request(
143139
"POST", "http://url.com", params={"argument": "value"}, timeout=2
144140
)
145-
assert response == ""
141+
assert response == {}
146142

147143
def test_request_500_empty_content(self, client, fail_response):
148144
fail_response.status_code = 500

0 commit comments

Comments
 (0)