diff --git a/eodhd/APIs/BaseAPI.py b/eodhd/APIs/BaseAPI.py index b4e919d..f7c605c 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: @@ -24,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()) @@ -35,7 +36,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..8ae2574 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 @@ -119,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()) @@ -130,7 +131,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: