Skip to content

Commit

Permalink
Always refresh token on 'unauthorized' (#145)
Browse files Browse the repository at this point in the history
If HTTP status is 'unauthorized' always try to refresh token and try again one more time
  • Loading branch information
itamarga authored Aug 8, 2024
1 parent d14bd3b commit 5dfcc7e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion intezer_sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.21.3'
__version__ = '1.21.4'
2 changes: 1 addition & 1 deletion intezer_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def request_with_refresh_expired_access_token(self,
self._refresh_token_if_needed()
response = self._request(method, path, data, headers, files, stream, base_url, timeout_in_seconds)

if response.status_code == HTTPStatus.UNAUTHORIZED and not self._token_expiration:
if response.status_code == HTTPStatus.UNAUTHORIZED:
self._set_access_token()
response = self._request(method, path, data, headers, files, stream, base_url, timeout_in_seconds)

Expand Down
4 changes: 4 additions & 0 deletions tests/unit/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def test_api_raise_invalid_api_key_error_when_unauthorized_received(self):

with responses.RequestsMock() as mock:
mock.add('GET', f'{self.full_url}/some-route', status=HTTPStatus.UNAUTHORIZED)
mock.add('POST',
url=f'{self.full_url}/get-access-token',
status=HTTPStatus.OK,
json={'result': 'access-token', 'expire_at': 2166920067})
response = api.request_with_refresh_expired_access_token('GET', '/some-route')

with self.assertRaises(errors.InvalidApiKeyError):
Expand Down

0 comments on commit 5dfcc7e

Please sign in to comment.