Skip to content

Commit

Permalink
feat!: return empty dict on empty responses in Client.request (#400)
Browse files Browse the repository at this point in the history
This simplifies the API of the request method, and fixes the return
values to always be a dict.
  • Loading branch information
jooola committed Jul 2, 2024
1 parent db37e63 commit 9f46adb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
7 changes: 3 additions & 4 deletions hcloud/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ def request( # type: ignore[no-untyped-def]
**kwargs,
)

content = response.content
content = {}
try:
if len(content) > 0:
if len(response.content) > 0:
content = response.json()
except (TypeError, ValueError):
self._raise_exception_from_response(response)
Expand All @@ -229,5 +229,4 @@ def request( # type: ignore[no-untyped-def]
else:
self._raise_exception_from_response(response)

# TODO: return an empty dict instead of an empty string when content == "".
return content # type: ignore[return-value]
return content
6 changes: 1 addition & 5 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ def test__get_headers(self, client):
"Authorization": "Bearer project_token",
}

def test_request_library_mocked(self, client):
response = client.request("POST", "url", params={"1": 2})
assert response.__class__.__name__ == "MagicMock"

def test_request_ok(self, client, response):
client._requests_session.request.return_value = response
response = client.request(
Expand Down Expand Up @@ -142,7 +138,7 @@ def test_request_empty_content_200(self, client, response):
response = client.request(
"POST", "http://url.com", params={"argument": "value"}, timeout=2
)
assert response == ""
assert response == {}

def test_request_500_empty_content(self, client, fail_response):
fail_response.status_code = 500
Expand Down

0 comments on commit 9f46adb

Please sign in to comment.