From eacb39c9b77237294af52e66d9950b8baebc2a92 Mon Sep 17 00:00:00 2001 From: Paul Sutherland Date: Tue, 4 Feb 2025 14:46:53 -0500 Subject: [PATCH 1/2] Use requests.exceptions.JSONDecodeError instead of json.JSONDecodeError Requests has attempted to consolidate json decoding errors in https://github.com/psf/requests/pull/5856 which was released in requests==2.27.0. Since this project is requests>=2.31.0 we are safe to incorporate the change. --- eodhd/APIs/BaseAPI.py | 5 ++--- eodhd/apiclient.py | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/eodhd/APIs/BaseAPI.py b/eodhd/APIs/BaseAPI.py index b4e919d..1475b04 100644 --- a/eodhd/APIs/BaseAPI.py +++ b/eodhd/APIs/BaseAPI.py @@ -1,9 +1,8 @@ -from json.decoder import JSONDecodeError import sys from requests import get as requests_get from requests import ConnectionError as requests_ConnectionError from requests import Timeout as requests_Timeout -from requests.exceptions import HTTPError as requests_HTTPError +from requests.exceptions import HTTPError as requests_HTTPError, JSONDecodeError as requests_JSONDecodeError from rich.console import Console class BaseAPI: @@ -35,7 +34,7 @@ def _rest_get_method(self, api_key: str, endpoint: str = "", uri: str = "", quer message = f"({resp.status_code}) {self._api_url} - {resp_message}" self.console.log(message) - except JSONDecodeError as err: + except requests_JSONDecodeError as err: self.console.log(err) try: diff --git a/eodhd/apiclient.py b/eodhd/apiclient.py index 7f1205b..1b14cbc 100644 --- a/eodhd/apiclient.py +++ b/eodhd/apiclient.py @@ -1,6 +1,5 @@ """apiclient.py""" -from json.decoder import JSONDecodeError import sys from enum import Enum from datetime import datetime @@ -11,7 +10,7 @@ from requests import get as requests_get from requests import ConnectionError as requests_ConnectionError from requests import Timeout as requests_Timeout -from requests.exceptions import HTTPError as requests_HTTPError +from requests.exceptions import HTTPError as requests_HTTPError, JSONDecodeError as requests_JSONDecodeError from rich.console import Console from rich.progress import track @@ -130,7 +129,7 @@ def _rest_get(self, endpoint: str = "", uri: str = "", querystring: str = "") -> message = f"({resp.status_code}) {self._api_url} - {resp_message}" self.console.log(message) - except JSONDecodeError as err: + except requests_JSONDecodeError as err: self.console.log(err) try: From 87a1a2b1af10f35c18403b420e261a083980dfe9 Mon Sep 17 00:00:00 2001 From: Paul Sutherland Date: Tue, 4 Feb 2025 14:49:35 -0500 Subject: [PATCH 2/2] Handle non-json error messages by checking mimetype header. --- eodhd/APIs/BaseAPI.py | 4 +++- eodhd/apiclient.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/eodhd/APIs/BaseAPI.py b/eodhd/APIs/BaseAPI.py index 1475b04..f7c605c 100644 --- a/eodhd/APIs/BaseAPI.py +++ b/eodhd/APIs/BaseAPI.py @@ -23,7 +23,9 @@ def _rest_get_method(self, api_key: str, endpoint: str = "", uri: str = "", quer if resp.status_code != 200: try: - if "message" in resp.json(): + if resp.headers.get("Content-Type") != 'application/json': + resp_message = resp.text + elif "message" in resp.json(): resp_message = resp.json()["message"] elif "errors" in resp.json(): self.console.log(resp.json()) diff --git a/eodhd/apiclient.py b/eodhd/apiclient.py index 1b14cbc..8ae2574 100644 --- a/eodhd/apiclient.py +++ b/eodhd/apiclient.py @@ -118,7 +118,9 @@ def _rest_get(self, endpoint: str = "", uri: str = "", querystring: str = "") -> if resp.status_code != 200: try: - if "message" in resp.json(): + if resp.headers.get("Content-Type") != 'application/json': + resp_message = resp.text + elif "message" in resp.json(): resp_message = resp.json()["message"] elif "errors" in resp.json(): self.console.log(resp.json())