Skip to content
This repository was archived by the owner on Nov 17, 2022. It is now read-only.

Commit 7b9f6e4

Browse files
authored
client: raise NotFound exception on 404 (#83)
* raise NotFound exception on 404 Raise a NotFound exception for status code 404. * updated based on feedback
1 parent 60930e8 commit 7b9f6e4

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

elemental/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .client import (ElementalException, ElementalLive, InvalidRequest,
2-
InvalidResponse)
2+
InvalidResponse, NotFound)
33

4-
__all__ = ('ElementalException', 'ElementalLive', 'InvalidResponse', 'InvalidRequest',)
4+
__all__ = ('ElementalException', 'ElementalLive', 'InvalidResponse', 'InvalidRequest', 'NotFound')

elemental/client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ class InvalidResponse(ElementalException):
2424
pass
2525

2626

27+
class NotFound(InvalidResponse):
28+
"""Exception raised by 'request' with NotFound"""
29+
pass
30+
31+
2732
EventIdDict = TypedDict('EventIdDict', {'id': str})
2833

2934
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],
8691

8792
except requests.exceptions.RequestException as e:
8893
raise InvalidRequest(f"{http_method}: {url} failed\n{e}")
94+
if response.status_code == 404:
95+
raise NotFound(
96+
f"{http_method}: {url} failed\nResponse: "
97+
f"{response.status_code}\n{response.text}")
8998
if response.status_code not in (200, 201):
9099
raise InvalidResponse(
91100
f"{http_method}: {url} failed\nResponse: "

tests/client_test.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import requests
77

88
from elemental.client import (ElementalException, ElementalLive, InvalidRequest,
9-
InvalidResponse)
9+
InvalidResponse, NotFound)
1010

1111
USER = "FAKE"
1212
API_KEY = "FAKE"
@@ -114,6 +114,21 @@ def test_send_request_should_raise_InvalidResponse_on_invalid_status_code():
114114
f"Response: 404\n{response_from_elemental_api}")
115115

116116

117+
def test_send_request_should_raise_NotFound_on_404():
118+
response_from_elemental_api = file_fixture('fail_to_create_response.xml')
119+
120+
client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)
121+
client.session.request = mock.MagicMock(return_value=mock_response(
122+
status=404, text=response_from_elemental_api))
123+
124+
with pytest.raises(NotFound) as exc_info:
125+
client.send_request(
126+
'POST', f'{ELEMENTAL_ADDRESS}/live_events', HEADERS, REQUEST_BODY)
127+
128+
assert str(exc_info.value).endswith(
129+
f"Response: 404\n{response_from_elemental_api}")
130+
131+
117132
def test_create_event():
118133
client = ElementalLive(ELEMENTAL_ADDRESS, USER, API_KEY)
119134

0 commit comments

Comments
 (0)