Skip to content
This repository was archived by the owner on Nov 17, 2022. It is now read-only.
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
4 changes: 2 additions & 2 deletions elemental/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .client import (ElementalException, ElementalLive, InvalidRequest,
InvalidResponse)
InvalidResponse, NotFound)

__all__ = ('ElementalException', 'ElementalLive', 'InvalidResponse', 'InvalidRequest',)
__all__ = ('ElementalException', 'ElementalLive', 'InvalidResponse', 'InvalidRequest', 'NotFound')
9 changes: 9 additions & 0 deletions elemental/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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: "
Expand Down
17 changes: 16 additions & 1 deletion tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import requests

from elemental.client import (ElementalException, ElementalLive, InvalidRequest,
InvalidResponse)
InvalidResponse, NotFound)

USER = "FAKE"
API_KEY = "FAKE"
Expand Down Expand Up @@ -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)

Expand Down