From 0b81823af046358d923228f4abcffa5f10cf8ad9 Mon Sep 17 00:00:00 2001 From: Ofir Date: Tue, 16 Jul 2024 13:06:08 +0300 Subject: [PATCH 1/2] fix for handling conflict error --- intezer_sdk/__init__.py | 2 +- intezer_sdk/api.py | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/intezer_sdk/__init__.py b/intezer_sdk/__init__.py index 6cef8d1..fe30879 100644 --- a/intezer_sdk/__init__.py +++ b/intezer_sdk/__init__.py @@ -1 +1 @@ -__version__ = '1.21.2' +__version__ = '1.21.3' diff --git a/intezer_sdk/api.py b/intezer_sdk/api.py index 7cf19d1..5578d74 100644 --- a/intezer_sdk/api.py +++ b/intezer_sdk/api.py @@ -45,9 +45,13 @@ def raise_for_status(response: requests.Response, if response.status_code == HTTPStatus.UNAUTHORIZED: raise errors.InvalidApiKeyError(response) elif response.status_code == HTTPStatus.CONFLICT: - is_skipped_by_rule = response.json().get('result', {}).get('is_skipped_by_rule') - if is_skipped_by_rule: - raise errors.AnalysisSkippedByRuleError(response) + try: + is_skipped_by_rule = response.json().get('result', {}).get('is_skipped_by_rule') + if is_skipped_by_rule: + raise errors.AnalysisSkippedByRuleError(response) + except Exception: + pass + http_error_msg = f'{response.status_code} Client Error: {reason} for url: {response.url}' elif response.status_code == HTTPStatus.FORBIDDEN: try: error_message = response.json()['error'] From fdfcfc72956641324f8849fecf24d7c63e0ab088 Mon Sep 17 00:00:00 2001 From: Ofir Date: Tue, 16 Jul 2024 13:19:47 +0300 Subject: [PATCH 2/2] fix for handling conflict error --- CHANGES | 4 ++++ intezer_sdk/api.py | 55 ++++++++++++++++------------------------------ 2 files changed, 23 insertions(+), 36 deletions(-) diff --git a/CHANGES b/CHANGES index 9f8d9da..d114bf0 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,7 @@ +1.21.3 +_______ +- Refactor: simplified raise for status + 1.21.2 _______ - Fix handling of large offline endpoint scan info files diff --git a/intezer_sdk/api.py b/intezer_sdk/api.py index 5578d74..b41087f 100644 --- a/intezer_sdk/api.py +++ b/intezer_sdk/api.py @@ -1,7 +1,6 @@ import datetime import logging import os -import typing from http import HTTPStatus from typing import Any from typing import BinaryIO @@ -25,12 +24,16 @@ logger = logging.getLogger(__name__) - def raise_for_status(response: requests.Response, statuses_to_ignore: List[Union[HTTPStatus, int]] = None, allowed_statuses: List[Union[HTTPStatus, int]] = None): """Raises stored :class:`HTTPError`, if one occurred.""" + try: + response_json = response.json() + except Exception: + response_json = {} + should_raise = False http_error_msg = '' if isinstance(response.reason, bytes): reason = response.reason.decode('utf-8', 'ignore') @@ -40,46 +43,26 @@ def raise_for_status(response: requests.Response, if statuses_to_ignore and response.status_code in statuses_to_ignore: return elif allowed_statuses and response.status_code not in allowed_statuses: - http_error_msg = f'{response.status_code} Custom Error: {reason} for url: {response.url}' - elif 400 <= response.status_code < 500: + should_raise = True + elif 400 <= response.status_code < 600: + should_raise = True if response.status_code == HTTPStatus.UNAUTHORIZED: raise errors.InvalidApiKeyError(response) elif response.status_code == HTTPStatus.CONFLICT: - try: - is_skipped_by_rule = response.json().get('result', {}).get('is_skipped_by_rule') - if is_skipped_by_rule: - raise errors.AnalysisSkippedByRuleError(response) - except Exception: - pass - http_error_msg = f'{response.status_code} Client Error: {reason} for url: {response.url}' + if response_json.get('result', {}).get('is_skipped_by_rule'): + raise errors.AnalysisSkippedByRuleError(response) elif response.status_code == HTTPStatus.FORBIDDEN: - try: - error_message = response.json()['error'] - except Exception: - http_error_msg = f'{response.status_code} Client Error: {reason} for url: {response.url}' - else: - if error_message == 'Insufficient Permissions': - raise errors.InsufficientPermissionsError(response) - elif response.status_code != HTTPStatus.BAD_REQUEST: + 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}' else: - # noinspection PyBroadException - try: - error = response.json() - http_error_msg = '\n'.join([f'{key}:{value}.' for key, value in error['message'].items()]) - except Exception: - http_error_msg = f'{response.status_code} Client Error: {reason} for url: {response.url}' - elif 500 <= response.status_code < 600: - http_error_msg = f'{response.status_code} Server Error: {reason} for url: {response.url}' - - if http_error_msg: - # noinspection PyBroadException - try: - data = response.json() - http_error_msg = f'{http_error_msg}, server returns {data["error"]}, details: {data.get("details")}' - except Exception: - pass - + http_error_msg = f'{http_error_msg}, server returns {response_json.get("error")}, details: {response_json.get("details")}' raise requests.HTTPError(http_error_msg, response=response)