Skip to content

Commit

Permalink
Merge pull request #148 from intezer/feat/rate-limit
Browse files Browse the repository at this point in the history
feature(api) - update raise from status to support too many request e… TKT-6264
  • Loading branch information
almogch authored Dec 17, 2024
2 parents 710defa + 99f7f2e commit 19dbda9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.21.7
_______
- Raise AnalysisRateLimitError when analysis rejected due to rate limit exceeded error returning from server

1.21.6
_______
- Fix sending data in the body of the request
Expand Down
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.6'
__version__ = '1.21.7'
3 changes: 2 additions & 1 deletion intezer_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ def raise_for_status(response: requests.Response,
elif response.status_code == HTTPStatus.CONFLICT:
if response_json.get('result', {}).get('is_skipped_by_rule'):
raise errors.AnalysisSkippedByRuleError(response)
elif response.status_code == HTTPStatus.TOO_MANY_REQUESTS:
raise errors.AnalysisRateLimitError(response)
elif response.status_code == HTTPStatus.FORBIDDEN:
if response_json.get('error') == 'Insufficient Permissions':
raise errors.InsufficientPermissionsError(response)
elif response.status_code == HTTPStatus.BAD_REQUEST:
http_error_msg = '\n'.join([f'{key}:{value}.' for key, value in response_json.get('message', {}).items()])


if should_raise:
if not http_error_msg:
http_error_msg = f'{response.status_code} Client Error: {reason} for url: {response.url}'
Expand Down
8 changes: 8 additions & 0 deletions intezer_sdk/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,11 @@ def __init__(self, response: requests.Response):
class AnalysisSkippedByRuleError(ServerError):
def __init__(self, response: requests.Response):
super().__init__('Analysis skipped by rule', response)

class AnalysisRateLimitError(ServerError):
def __init__(self, response: requests.Response):
super().__init__('Analysis rate limit reached', response)
self.limit = response.headers.get('X-RateLimit-Limit')
self.remaining = response.headers.get('X-RateLimit-Remaining')
self.reset_time_in_sec = response.headers.get('X-RateLimit-Reset')
self.retry_after = response.headers.get('Retry-After')

0 comments on commit 19dbda9

Please sign in to comment.