Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle 429 status code in response gracefully. #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions hyperwallet/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,22 @@ def test_receive_json_error_response_when_content_type_is_not_valid(self, sessio
'Invalid Content-Type specified in Response Header'
)

@mock.patch('requests.Session.request')
def test_receive_too_many_requests_when_429(self, session_mock):

session_mock.return_value = mock.MagicMock(status_code=429)

with self.assertRaises(HyperwalletAPIException) as exc:
self.client._makeRequest()

self.assertEqual(
exc.exception.message.get('errors')[0].get('code'), 'TOO_MANY_REQUESTS'
)

self.assertEqual(
exc.exception.message.get('errors')[0].get('message'), 'Too Many Requests'
)


if __name__ == '__main__':
unittest.main()
6 changes: 6 additions & 0 deletions hyperwallet/utils/apiclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ def _makeRequest(self,

if response.status_code is 204:
return {}
if response.status_code == 429:
raise HyperwalletAPIException({
'errors': [
{'code': 'TOO_MANY_REQUESTS', 'message': 'Too Many Requests'}
]
})

self.__checkResponseHeaderContentType(response)

Expand Down