From ca0f9727f5122e76460848d1d103ad2922540655 Mon Sep 17 00:00:00 2001 From: davidt99 Date: Wed, 8 Jul 2026 21:39:10 +0300 Subject: [PATCH] fix: defer response parsing in raise_for_status to error paths only Parse the response JSON body and decode the reason only when an error is actually raised, avoiding unnecessary work on successful responses. Co-Authored-By: Claude Opus 4.8 (1M context) --- CHANGES | 4 ++++ intezer_sdk/__init__.py | 2 +- intezer_sdk/api.py | 18 ++++++++---------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/CHANGES b/CHANGES index 6d31182..b700804 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,7 @@ +1.27.6 +------- +- Only parse the response body and decode the reason in raise_for_status when an error is actually raised + 1.27.5 ------- - Add PasswordProtectedArchiveError raised when a submitted archive is password protected diff --git a/intezer_sdk/__init__.py b/intezer_sdk/__init__.py index cbe92da..51de7bd 100644 --- a/intezer_sdk/__init__.py +++ b/intezer_sdk/__init__.py @@ -1 +1 @@ -__version__ = '1.27.5' +__version__ = '1.27.6' diff --git a/intezer_sdk/api.py b/intezer_sdk/api.py index 7caaea8..90b5b11 100644 --- a/intezer_sdk/api.py +++ b/intezer_sdk/api.py @@ -33,23 +33,17 @@ def raise_for_status(response: requests.Response, statuses_to_ignore: list[HTTPStatus | int] = None, allowed_statuses: list[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') - else: - reason = response.reason - if statuses_to_ignore and response.status_code in statuses_to_ignore: return elif allowed_statuses and response.status_code not in allowed_statuses: should_raise = True elif 400 <= response.status_code < 600: + try: + response_json = response.json() + except Exception: + response_json = {} should_raise = True if response.status_code == HTTPStatus.UNAUTHORIZED: raise errors.InvalidApiKeyError(response) @@ -66,6 +60,10 @@ def raise_for_status(response: requests.Response, if should_raise: if not http_error_msg: + if isinstance(response.reason, bytes): + reason = response.reason.decode('utf-8', 'ignore') + else: + reason = response.reason http_error_msg = f'{response.status_code} Client Error: {reason} for url: {response.url}' else: http_error_msg = f'{http_error_msg}, server returns {response_json.get("error")}, details: {response_json.get("details")}'