From 9f46adb946eb2770ee4f3a4e87cfc1c8b9b33c28 Mon Sep 17 00:00:00 2001 From: Jonas L Date: Tue, 2 Jul 2024 12:11:49 +0200 Subject: [PATCH] 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. --- hcloud/_client.py | 7 +++---- tests/unit/test_client.py | 6 +----- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/hcloud/_client.py b/hcloud/_client.py index 37aa1bd7..1d9fcf30 100644 --- a/hcloud/_client.py +++ b/hcloud/_client.py @@ -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) @@ -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 diff --git a/tests/unit/test_client.py b/tests/unit/test_client.py index 36ac9288..4a1da1a9 100644 --- a/tests/unit/test_client.py +++ b/tests/unit/test_client.py @@ -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( @@ -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