Skip to content

Commit

Permalink
Test the check to determine if raise_for_status should be called
Browse files Browse the repository at this point in the history
  • Loading branch information
SmittieC committed Oct 24, 2023
1 parent 21108bf commit 6dd1ae7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
5 changes: 4 additions & 1 deletion commcare_export/commcare_hq_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ def session(self, session):
def api_url(self):
return '%s/a/%s/api/v%s' % (self.url, self.project, self.version)

def _should_raise_for_status(self, response):
return "Retry-After" not in response.headers

def get(self, resource, params=None):
"""
Gets the named resource. When the server returns a 429 (too many requests), the process will sleep for
Expand Down Expand Up @@ -146,7 +149,7 @@ def _get(resource, params=None):
response = self.session.get(
resource_url, params=params, auth=self.__auth, timeout=60
)
if "Retry-After" not in response.headers:
if self._should_raise_for_status(response):
response.raise_for_status()
return response

Expand Down
Empty file added tests/__init__.py
Empty file.
22 changes: 21 additions & 1 deletion tests/test_commcare_hq_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
SimplePaginator,
get_paginator,
)

from mock import Mock, patch

class FakeSession(object):

Expand Down Expand Up @@ -291,6 +291,26 @@ def test_message_log(self):
[1, 2, 3]
)

@patch("commcare_export.commcare_hq_client.CommCareHqClient.session")
def test_dont_raise_on_too_many_requests(self, session_mock):
response = requests.Response()
response.headers = {'Retry-After': "0.0"}
client = CommCareHqClient(
'/fake/commcare-hq/url', 'fake-project', None, None
)

self.assertFalse(client._should_raise_for_status(response))

@patch("commcare_export.commcare_hq_client.CommCareHqClient.session")
def test_raise_on_too_many_requests(self, session_mock):
response = requests.Response()
response.headers = {}

client = CommCareHqClient(
'/fake/commcare-hq/url', 'fake-project', None, None
)

self.assertTrue(client._should_raise_for_status(response))

class TestDatePaginator(unittest.TestCase):

Expand Down

0 comments on commit 6dd1ae7

Please sign in to comment.