Skip to content

Commit

Permalink
Fix DataInvalidError with null unsafe return
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-zenk committed Feb 27, 2024
1 parent 7e3706c commit 65663ba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions onboardapis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

__project_name__ = 'onboardapis'
__description__ = 'A pure Python wrapper for the on-board APIs of many different transportation providers'
__version__ = '1.3.1'
__version__ = '1.3.2'
__author__ = 'Felix Zenk'
__email__ = 'felix.zenk@web.de'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 Felix Zenk'
__copyright__ = 'Copyright (c) 2024 Felix Zenk'
__url__ = 'https://github.com/felix-zenk/onboardapis'

from abc import ABCMeta
Expand Down
15 changes: 9 additions & 6 deletions onboardapis/utils/data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This module contains everything that has to do with data and data management
"""
from __future__ import annotations

import abc
import json
Expand All @@ -14,9 +15,11 @@
from requests import Response, RequestException

from .conversions import coordinates_decimal_to_dms
from ..exceptions import DataInvalidError, APIConnectionError, InitialConnectionError
from ..exceptions import APIConnectionError, InitialConnectionError
from .. import __version__

logger = logging.getLogger(__name__)


def some_or_default(data: Optional[Any], default: Optional[Any] = None) -> Optional[Any]:
"""
Expand Down Expand Up @@ -274,7 +277,7 @@ def _get(self, endpoint: str, *args: Any, base_url: str = None, **kwargs: Any) -
response = self._session.get(f"https://{base_url}{endpoint}", *args, **kwargs)
# Report possible errors / changes in the API
if response.status_code != 200:
logging.warning(f"Request to https://{base_url}{endpoint} returned status code {response.status_code}")
logger.warning(f"Request to https://{base_url}{endpoint} returned status code {response.status_code}")
self._retries = 0
return response
except RequestException as e:
Expand All @@ -283,7 +286,7 @@ def _get(self, endpoint: str, *args: Any, base_url: str = None, **kwargs: Any) -
raise APIConnectionError() from e
# Retry the request if it failed
self._retries += 1
logging.debug(f"Request to https://{base_url}{endpoint} failed! Retry: ({self._retries}/2)")
logger.debug(f"Request to https://{base_url}{endpoint} failed! Retry: ({self._retries}/2)")
return self._get(endpoint, *args, base_url=base_url, **kwargs)

def load(self, key: str, __default: Any = None) -> Any:
Expand Down Expand Up @@ -449,7 +452,7 @@ class JSONDataConnector(DataConnector, metaclass=abc.ABCMeta):

__slots__ = []

def _get(self, endpoint: str, *args: Any, base_url: str = None, **kwargs: Any) -> dict:
def _get(self, endpoint: str, *args: Any, base_url: str = None, **kwargs: Any) -> dict | None:
headers = kwargs.get("headers", {})
headers.update({
"accept": "application/json",
Expand All @@ -458,8 +461,8 @@ def _get(self, endpoint: str, *args: Any, base_url: str = None, **kwargs: Any) -
try:
return super(JSONDataConnector, self)._get(endpoint, *args, base_url=base_url, **kwargs).json()
except json.JSONDecodeError as e:
logging.debug(f"Failed to parse json ({e.__class__.__name__}): {'; '.join(e.args)}")
raise DataInvalidError() from e
logger.warning(f"Failed to parse json ({e.__class__.__name__}): {'; '.join(e.args)}")
return None

def export(self, path: Union[str, PathLike], keys: Sequence = None) -> None:
"""
Expand Down

0 comments on commit 65663ba

Please sign in to comment.