diff --git a/mailosaur/operations/messages_operations.py b/mailosaur/operations/messages_operations.py index 66772f3..143c1d4 100644 --- a/mailosaur/operations/messages_operations.py +++ b/mailosaur/operations/messages_operations.py @@ -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): @@ -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 @@ -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: @@ -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) \ No newline at end of file diff --git a/tests/emails_test.py b/tests/emails_test.py index d611172..ef8575c 100644 --- a/tests/emails_test.py +++ b/tests/emails_test.py @@ -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 = "neverfound@example.com" + 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())