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..fbbdc80 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}) @@ -86,6 +91,10 @@ 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): raise InvalidResponse( f"{http_method}: {url} failed\nResponse: " 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)