From 045af9cc8cf3db183f418f1f002064e178a98772 Mon Sep 17 00:00:00 2001 From: Oscar Nord Date: Tue, 12 Jan 2021 17:02:16 +0100 Subject: [PATCH 1/2] raise NotFound exception on 404 Raise a NotFound exception for status code 404. --- elemental/__init__.py | 4 ++-- elemental/client.py | 16 +++++++++++++--- tests/client_test.py | 17 ++++++++++++++++- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/elemental/__init__.py b/elemental/__init__.py index b6a8118..3523691 100644 --- a/elemental/__init__.py +++ b/elemental/__init__.py @@ -1,4 +1,4 @@ from .client import (ElementalException, ElementalLive, InvalidRequest, - InvalidResponse) + InvalidResponse, NotFound) -__all__ = ('ElementalException', 'ElementalLive', 'InvalidResponse', 'InvalidRequest',) +__all__ = ('ElementalException', 'ElementalLive', 'InvalidResponse', 'InvalidRequest', 'NotFound') diff --git a/elemental/client.py b/elemental/client.py index f8e2666..5f3b3e4 100644 --- a/elemental/client.py +++ b/elemental/client.py @@ -24,6 +24,11 @@ class InvalidResponse(ElementalException): pass +class NotFound(InvalidResponse): + """Exception raised by 'request' with NotFound""" + pass + + EventIdDict = TypedDict('EventIdDict', {'id': str}) EventStatusDict = TypedDict('EventStatusDict', {'origin_url': str, 'backup_url': Optional[str], 'status': str}) @@ -87,9 +92,14 @@ def send_request(self, http_method: str, url: str, headers: Dict[str, str], except requests.exceptions.RequestException as e: raise InvalidRequest(f"{http_method}: {url} failed\n{e}") if response.status_code not in (200, 201): - raise InvalidResponse( - f"{http_method}: {url} failed\nResponse: " - f"{response.status_code}\n{response.text}") + if response.status_code == 404: + raise NotFound( + f"{http_method}: {url} failed\nResponse: " + f"{response.status_code}\n{response.text}") + else: + raise InvalidResponse( + f"{http_method}: {url} failed\nResponse: " + f"{response.status_code}\n{response.text}") return response def create_event(self, event_xml: str, timeout: Optional[int] = None) -> EventIdDict: diff --git a/tests/client_test.py b/tests/client_test.py index 31a20cd..0553a90 100644 --- a/tests/client_test.py +++ b/tests/client_test.py @@ -6,7 +6,7 @@ import requests from elemental.client import (ElementalException, ElementalLive, InvalidRequest, - InvalidResponse) + InvalidResponse, NotFound) USER = "FAKE" API_KEY = "FAKE" @@ -114,6 +114,21 @@ def test_send_request_should_raise_InvalidResponse_on_invalid_status_code(): f"Response: 404\n{response_from_elemental_api}") +def test_send_request_should_raise_NotFound_on_404(): + response_from_elemental_api = file_fixture('fail_to_create_response.xml') + + client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY) + client.session.request = mock.MagicMock(return_value=mock_response( + status=404, text=response_from_elemental_api)) + + with pytest.raises(NotFound) as exc_info: + client.send_request( + 'POST', f'{ELEMENTAL_ADDRESS}/live_events', HEADERS, REQUEST_BODY) + + assert str(exc_info.value).endswith( + f"Response: 404\n{response_from_elemental_api}") + + def test_create_event(): client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY) From 413916f8fecf61c73fde999f79a982a09b50f0be Mon Sep 17 00:00:00 2001 From: Oscar Nord Date: Thu, 14 Jan 2021 08:58:45 +0100 Subject: [PATCH 2/2] updated based on feedback --- elemental/client.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/elemental/client.py b/elemental/client.py index 5f3b3e4..fbbdc80 100644 --- a/elemental/client.py +++ b/elemental/client.py @@ -91,15 +91,14 @@ def send_request(self, http_method: str, url: str, headers: Dict[str, str], except requests.exceptions.RequestException as e: raise InvalidRequest(f"{http_method}: {url} failed\n{e}") + if response.status_code == 404: + raise NotFound( + f"{http_method}: {url} failed\nResponse: " + f"{response.status_code}\n{response.text}") if response.status_code not in (200, 201): - if response.status_code == 404: - raise NotFound( - f"{http_method}: {url} failed\nResponse: " - f"{response.status_code}\n{response.text}") - else: - raise InvalidResponse( - f"{http_method}: {url} failed\nResponse: " - f"{response.status_code}\n{response.text}") + raise InvalidResponse( + f"{http_method}: {url} failed\nResponse: " + f"{response.status_code}\n{response.text}") return response def create_event(self, event_xml: str, timeout: Optional[int] = None) -> EventIdDict: