Skip to content

Commit

Permalink
Add errorOnTimeout option
Browse files Browse the repository at this point in the history
  • Loading branch information
jm-mailosaur committed Oct 20, 2020
1 parent 6df1947 commit fd9abbd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 9 additions & 3 deletions mailosaur/operations/messages_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def get(self, server, criteria, timeout=10000, received_after=(datetime.today()
if len(server) != 8:
raise MailosaurException("Must provide a valid Server ID.", "invalid_request")

result = self.search(server, criteria, 0, 1, timeout, received_after)
result = self.search(server, criteria, 0, 1, timeout, received_after, True)
return self.get_by_id(result.items[0].id)

def get_by_id(self, id):
Expand Down Expand Up @@ -147,7 +147,7 @@ def delete_all(self, server):
self.handle_http_error(response)
return

def search(self, server, criteria, page=None, items_per_page=None, timeout=None, received_after=None):
def search(self, server, criteria, page=None, items_per_page=None, timeout=None, received_after=None, error_on_timeout=True):
"""Search for messages.
Returns a list of messages matching the specified search criteria, in
Expand All @@ -168,6 +168,9 @@ def search(self, server, criteria, page=None, items_per_page=None, timeout=None,
:type timeout: int
:param received_after: Limits results to only messages received after this date/time.
:type received_after: datetime
:param error_on_timeout: When set to false, an error will not be throw if timeout
is reached (default: true).
:type error_on_timeout: bool
:return: MessageListResult
:rtype: ~mailosaur.models.MessageListResult
:raises:
Expand Down Expand Up @@ -209,6 +212,9 @@ def search(self, server, criteria, page=None, items_per_page=None, timeout=None,

## Stop if timeout will be exceeded
if ((1000 * (datetime.today() - start_time).total_seconds()) + delay) > timeout:
raise MailosaurException("No matching messages found in time. By default, only messages received in the last hour are checked (use receivedAfter to override this).", "search_timeout")
if not error_on_timeout:
return result
else:
raise MailosaurException("No matching messages found in time. By default, only messages received in the last hour are checked (use receivedAfter to override this).", "search_timeout")

time.sleep(delay / 1000)
6 changes: 6 additions & 0 deletions tests/emails_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ def test_get_not_found(self):
with self.assertRaises(MailosaurException):
self.client.messages.get_by_id("efe907e9-74ed-4113-a3e0-a3d41d914765")

def test_search_timeout_errors_suppressed(self):
criteria = SearchCriteria()
criteria.sent_from = "[email protected]"
results = self.client.messages.search(self.server, criteria, timeout=1, error_on_timeout=False).items
self.assertEqual(0, len(results))

def test_search_no_criteria_error(self):
with self.assertRaises(MailosaurException):
self.client.messages.search(self.server, SearchCriteria())
Expand Down

0 comments on commit fd9abbd

Please sign in to comment.