From 2c2632969b21b0bdef64bb230fe05a3175c76277 Mon Sep 17 00:00:00 2001 From: almogch Date: Tue, 17 Dec 2024 22:28:01 +0200 Subject: [PATCH 1/3] feature(raise-for-status) - handle too many requests --- intezer_sdk/_api.py | 2 ++ intezer_sdk/errors.py | 63 ++++++++++++++++++++++--------------------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/intezer_sdk/_api.py b/intezer_sdk/_api.py index c44d5c8..00b0f5a 100644 --- a/intezer_sdk/_api.py +++ b/intezer_sdk/_api.py @@ -801,6 +801,8 @@ def _assert_analysis_response_status_code(response: Response): raise errors.AnalysisIsAlreadyRunningError(response, running_analysis_id) elif response.status_code == HTTPStatus.FORBIDDEN: raise errors.InsufficientQuotaError(response) + elif response.status_code == HTTPStatus.TOO_MANY_REQUESTS: + raise errors.AnalysisRateLimitError(response) elif response.status_code == HTTPStatus.BAD_REQUEST: data = response.json() error = data.get('error', '') diff --git a/intezer_sdk/errors.py b/intezer_sdk/errors.py index 00d83ac..ab016e5 100644 --- a/intezer_sdk/errors.py +++ b/intezer_sdk/errors.py @@ -6,9 +6,9 @@ def _parse_erroneous_response(response: requests.Response): try: data = response.json() - return data.get('error', '') + return data.get("error", "") except ValueError: - return '' + return "" class IntezerError(Exception): @@ -27,13 +27,13 @@ def __init__(self, message: str, response: requests.Response): self.response = response detailed_error = _parse_erroneous_response(response) if detailed_error: - message = f'{message}. Error:{detailed_error}' + message = f"{message}. Error:{detailed_error}" super().__init__(message) class AnalysisHasAlreadyBeenSentError(IntezerError): def __init__(self): - super().__init__('Analysis already been sent') + super().__init__("Analysis already been sent") AnalysisHasAlreadyBeenSent = AnalysisHasAlreadyBeenSentError @@ -41,7 +41,7 @@ def __init__(self): class IndexHasAlreadyBeenSentError(IntezerError): def __init__(self): - super().__init__('Index already been sent') + super().__init__("Index already been sent") IndexHasAlreadyBeenSent = IndexHasAlreadyBeenSentError @@ -49,27 +49,27 @@ def __init__(self): class FamilyNotFoundError(IntezerError): def __init__(self, family_id: str): - super().__init__(f'Family not found: {family_id}') + super().__init__(f"Family not found: {family_id}") class HashDoesNotExistError(ServerError): def __init__(self, response: requests.Response): - super().__init__('Hash was not found', response) + super().__init__("Hash was not found", response) class FileTooLargeError(ServerError): def __init__(self, response: requests.Response): - super().__init__('File is too large', response) + super().__init__("File is too large", response) class ReportDoesNotExistError(IntezerError): def __init__(self): - super().__init__('Report was not found') + super().__init__("Report was not found") class AnalysisIsAlreadyRunningError(ServerError): def __init__(self, response: requests.Response, running_analysis_id: Optional[str]): - super().__init__('Analysis already running', response) + super().__init__("Analysis already running", response) self.analysis_id = running_analysis_id @@ -78,7 +78,7 @@ def __init__(self, response: requests.Response, running_analysis_id: Optional[st class InsufficientQuotaError(ServerError): def __init__(self, response: requests.Response): - super().__init__('Insufficient quota', response) + super().__init__("Insufficient quota", response) InsufficientQuota = InsufficientQuotaError @@ -86,7 +86,7 @@ def __init__(self, response: requests.Response): class GlobalApiIsNotInitializedError(IntezerError): def __init__(self): - super().__init__('Global API is not initialized') + super().__init__("Global API is not initialized") GlobalApiIsNotInitialized = GlobalApiIsNotInitializedError @@ -94,7 +94,7 @@ def __init__(self): class AnalysisIsStillRunningError(IntezerError): def __init__(self): - super().__init__('Analysis is still running') + super().__init__("Analysis is still running") AnalysisIsStillRunning = AnalysisIsStillRunningError @@ -102,12 +102,12 @@ def __init__(self): class AnalysisFailedError(IntezerError): def __init__(self): - super().__init__('Analysis failed') + super().__init__("Analysis failed") class InvalidApiKeyError(ServerError): def __init__(self, response: requests.Response): - super().__init__('Invalid api key', response) + super().__init__("Invalid api key", response) InvalidApiKey = InvalidApiKeyError @@ -115,7 +115,7 @@ def __init__(self, response: requests.Response): class IndexFailedError(ServerError): def __init__(self, response: requests.Response): - super().__init__('Index operation failed', response) + super().__init__("Index operation failed", response) IndexFailed = IndexFailedError @@ -123,7 +123,7 @@ def __init__(self, response: requests.Response): class OperationStillRunningError(IntezerError): def __init__(self, operation): - super().__init__(f'{operation} is still running') + super().__init__(f"{operation} is still running") SubAnalysisOperationStillRunning = OperationStillRunningError @@ -132,12 +132,12 @@ def __init__(self, operation): class SubAnalysisNotFoundError(IntezerError): def __init__(self, analysis_id: str): - super().__init__(f'analysis {analysis_id} is not found') + super().__init__(f"analysis {analysis_id} is not found") class InsufficientPermissionsError(ServerError): def __init__(self, response: requests.Response): - super().__init__('Account does not have permission to this route', response) + super().__init__("Account does not have permission to this route", response) class AlertError(IntezerError): @@ -146,17 +146,19 @@ class AlertError(IntezerError): class InvalidAlertMappingError(AlertError): def __init__(self, response: requests.Response): - super().__init__('Bad request - the mapping is probably malformed', response) + super().__init__("Bad request - the mapping is probably malformed", response) class AlertInProgressError(AlertError): def __init__(self, alert_id: str): - super().__init__(f'The alert {alert_id} is being processed at the moment, please try again later') + super().__init__( + f"The alert {alert_id} is being processed at the moment, please try again later" + ) class AlertNotFoundError(AlertError): def __init__(self, alert_id: str): - super().__init__(f'The given alert does not exist - {alert_id}') + super().__init__(f"The given alert does not exist - {alert_id}") class InvalidAlertArgumentError(AlertError): @@ -166,22 +168,23 @@ def __init__(self, message: str): class UrlOfflineError(ServerError): def __init__(self, response: requests.Response): - super().__init__('Url is offline', response) + super().__init__("Url is offline", response) class InvalidUrlError(ServerError): def __init__(self, response: requests.Response): - super().__init__('Invalid url', response) + super().__init__("Invalid url", response) class AnalysisSkippedByRuleError(ServerError): def __init__(self, response: requests.Response): - super().__init__('Analysis skipped by rule', 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') + 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") From 846ee4116ead84ddd3823e58f3ec039840c78211 Mon Sep 17 00:00:00 2001 From: almogch Date: Tue, 17 Dec 2024 22:29:27 +0200 Subject: [PATCH 2/3] feature(raise-for-status) - handle too many requests --- CHANGES | 4 ++++ intezer_sdk/__init__.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 3862bea..e917880 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,7 @@ +1.21.8 +_______ +- Raise AnalysisRateLimitError for all endpoints when rate limit exceeded error returning from server + 1.21.7 _______ - Raise AnalysisRateLimitError when analysis rejected due to rate limit exceeded error returning from server diff --git a/intezer_sdk/__init__.py b/intezer_sdk/__init__.py index 3bdbfad..50cf421 100644 --- a/intezer_sdk/__init__.py +++ b/intezer_sdk/__init__.py @@ -1 +1 @@ -__version__ = '1.21.7' +__version__ = '1.21.8' From 82469fa17bca6ccbddffeb2c09ad2e72f817d3f6 Mon Sep 17 00:00:00 2001 From: almogch Date: Tue, 17 Dec 2024 22:32:55 +0200 Subject: [PATCH 3/3] feature(raise-for-status) - handle too many requests --- intezer_sdk/errors.py | 60 +++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/intezer_sdk/errors.py b/intezer_sdk/errors.py index ab016e5..2daec82 100644 --- a/intezer_sdk/errors.py +++ b/intezer_sdk/errors.py @@ -6,9 +6,9 @@ def _parse_erroneous_response(response: requests.Response): try: data = response.json() - return data.get("error", "") + return data.get('error', '') except ValueError: - return "" + return '' class IntezerError(Exception): @@ -27,13 +27,13 @@ def __init__(self, message: str, response: requests.Response): self.response = response detailed_error = _parse_erroneous_response(response) if detailed_error: - message = f"{message}. Error:{detailed_error}" + message = f'{message}. Error:{detailed_error}' super().__init__(message) class AnalysisHasAlreadyBeenSentError(IntezerError): def __init__(self): - super().__init__("Analysis already been sent") + super().__init__('Analysis already been sent') AnalysisHasAlreadyBeenSent = AnalysisHasAlreadyBeenSentError @@ -41,7 +41,7 @@ def __init__(self): class IndexHasAlreadyBeenSentError(IntezerError): def __init__(self): - super().__init__("Index already been sent") + super().__init__('Index already been sent') IndexHasAlreadyBeenSent = IndexHasAlreadyBeenSentError @@ -49,27 +49,27 @@ def __init__(self): class FamilyNotFoundError(IntezerError): def __init__(self, family_id: str): - super().__init__(f"Family not found: {family_id}") + super().__init__(f'Family not found: {family_id}') class HashDoesNotExistError(ServerError): def __init__(self, response: requests.Response): - super().__init__("Hash was not found", response) + super().__init__('Hash was not found', response) class FileTooLargeError(ServerError): def __init__(self, response: requests.Response): - super().__init__("File is too large", response) + super().__init__('File is too large', response) class ReportDoesNotExistError(IntezerError): def __init__(self): - super().__init__("Report was not found") + super().__init__('Report was not found') class AnalysisIsAlreadyRunningError(ServerError): def __init__(self, response: requests.Response, running_analysis_id: Optional[str]): - super().__init__("Analysis already running", response) + super().__init__('Analysis already running', response) self.analysis_id = running_analysis_id @@ -78,7 +78,7 @@ def __init__(self, response: requests.Response, running_analysis_id: Optional[st class InsufficientQuotaError(ServerError): def __init__(self, response: requests.Response): - super().__init__("Insufficient quota", response) + super().__init__('Insufficient quota', response) InsufficientQuota = InsufficientQuotaError @@ -86,7 +86,7 @@ def __init__(self, response: requests.Response): class GlobalApiIsNotInitializedError(IntezerError): def __init__(self): - super().__init__("Global API is not initialized") + super().__init__('Global API is not initialized') GlobalApiIsNotInitialized = GlobalApiIsNotInitializedError @@ -94,7 +94,7 @@ def __init__(self): class AnalysisIsStillRunningError(IntezerError): def __init__(self): - super().__init__("Analysis is still running") + super().__init__('Analysis is still running') AnalysisIsStillRunning = AnalysisIsStillRunningError @@ -102,12 +102,12 @@ def __init__(self): class AnalysisFailedError(IntezerError): def __init__(self): - super().__init__("Analysis failed") + super().__init__('Analysis failed') class InvalidApiKeyError(ServerError): def __init__(self, response: requests.Response): - super().__init__("Invalid api key", response) + super().__init__('Invalid api key', response) InvalidApiKey = InvalidApiKeyError @@ -115,7 +115,7 @@ def __init__(self, response: requests.Response): class IndexFailedError(ServerError): def __init__(self, response: requests.Response): - super().__init__("Index operation failed", response) + super().__init__('Index operation failed', response) IndexFailed = IndexFailedError @@ -123,7 +123,7 @@ def __init__(self, response: requests.Response): class OperationStillRunningError(IntezerError): def __init__(self, operation): - super().__init__(f"{operation} is still running") + super().__init__(f'{operation} is still running') SubAnalysisOperationStillRunning = OperationStillRunningError @@ -132,12 +132,12 @@ def __init__(self, operation): class SubAnalysisNotFoundError(IntezerError): def __init__(self, analysis_id: str): - super().__init__(f"analysis {analysis_id} is not found") + super().__init__(f'analysis {analysis_id} is not found') class InsufficientPermissionsError(ServerError): def __init__(self, response: requests.Response): - super().__init__("Account does not have permission to this route", response) + super().__init__('Account does not have permission to this route', response) class AlertError(IntezerError): @@ -146,19 +146,19 @@ class AlertError(IntezerError): class InvalidAlertMappingError(AlertError): def __init__(self, response: requests.Response): - super().__init__("Bad request - the mapping is probably malformed", response) + super().__init__('Bad request - the mapping is probably malformed', response) class AlertInProgressError(AlertError): def __init__(self, alert_id: str): super().__init__( - f"The alert {alert_id} is being processed at the moment, please try again later" + f'The alert {alert_id} is being processed at the moment, please try again later' ) class AlertNotFoundError(AlertError): def __init__(self, alert_id: str): - super().__init__(f"The given alert does not exist - {alert_id}") + super().__init__(f'The given alert does not exist - {alert_id}') class InvalidAlertArgumentError(AlertError): @@ -168,23 +168,23 @@ def __init__(self, message: str): class UrlOfflineError(ServerError): def __init__(self, response: requests.Response): - super().__init__("Url is offline", response) + super().__init__('Url is offline', response) class InvalidUrlError(ServerError): def __init__(self, response: requests.Response): - super().__init__("Invalid url", response) + super().__init__('Invalid url', response) class AnalysisSkippedByRuleError(ServerError): def __init__(self, response: requests.Response): - super().__init__("Analysis skipped by rule", 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") + 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')