Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: return empty dict on empty responses in Client.request #400

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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