Skip to content

Commit f7137e5

Browse files
m-aciekMaciej Olko
authored andcommitted
Handle 429 status code in response gracefully
1 parent ab447b5 commit f7137e5

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

hyperwallet/tests/test_client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,22 @@ def test_receive_json_error_response_when_content_type_is_not_valid(self, sessio
369369
'Invalid Content-Type specified in Response Header'
370370
)
371371

372+
@mock.patch('requests.Session.request')
373+
def test_receive_too_many_requests_when_429(self, session_mock):
374+
375+
session_mock.return_value = mock.MagicMock(status_code=429)
376+
377+
with self.assertRaises(HyperwalletAPIException) as exc:
378+
self.client._makeRequest()
379+
380+
self.assertEqual(
381+
exc.exception.message.get('errors')[0].get('code'), 'TOO_MANY_REQUESTS'
382+
)
383+
384+
self.assertEqual(
385+
exc.exception.message.get('errors')[0].get('message'), 'Too Many Requests'
386+
)
387+
372388

373389
if __name__ == '__main__':
374390
unittest.main()

hyperwallet/utils/apiclient.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ def _makeRequest(self,
112112

113113
if response.status_code is 204:
114114
return {}
115+
if response.status_code == 429:
116+
raise HyperwalletAPIException({
117+
'errors': [
118+
{'code': 'TOO_MANY_REQUESTS', 'message': 'Too Many Requests'}
119+
]
120+
})
115121

116122
self.__checkResponseHeaderContentType(response)
117123

0 commit comments

Comments
 (0)