Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(BaseClient): prevent raising an error if None is passed to the log_dir #148

Merged
merged 10 commits into from
Feb 21, 2024
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixed

- prevent an exception if the `log_dir` for the `OhsomeClient` was set to `None`

## 0.3.0

### Added
Expand Down
27 changes: 9 additions & 18 deletions ohsome/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(
RetryError by the underlying library.
"""
self.log = log
self.log_dir = Path(log_dir)
self.log_dir = Path(log_dir or DEFAULT_LOG_DIR)
if self.log:
self.log_dir.mkdir(parents=True, exist_ok=True)
if base_api_url is not None:
Expand Down Expand Up @@ -130,7 +130,7 @@ def __init__(
)
self._parameters = None
self._metadata = None
self._url = None
self._metadata_url = self.base_api_url + "metadata"

@property
def base_api_url(self):
Expand All @@ -142,12 +142,8 @@ def start_timestamp(self):
Returns the temporal extent of the current ohsome API
:return:
"""
if self._metadata is None:
self._query_metadata()
return dt.datetime.fromisoformat(
self._metadata["extractRegion"]["temporalExtent"]["fromTimestamp"].strip(
"Z"
)
self.metadata["extractRegion"]["temporalExtent"]["fromTimestamp"].strip("Z")
)

@property
Expand All @@ -156,10 +152,8 @@ def end_timestamp(self):
Returns the temporal extent of the current ohsome API
:return:
"""
if self._metadata is None:
self._query_metadata()
return dt.datetime.fromisoformat(
self._metadata["extractRegion"]["temporalExtent"]["toTimestamp"].strip("Z")
self.metadata["extractRegion"]["temporalExtent"]["toTimestamp"].strip("Z")
)

@property
Expand All @@ -168,9 +162,7 @@ def api_version(self):
Returns the version of the ohsome API
:return:
"""
if self._metadata is None:
self._query_metadata()
return self._metadata["apiVersion"]
return self.metadata["apiVersion"]

@property
def metadata(self):
Expand All @@ -183,21 +175,20 @@ def _query_metadata(self):
Send ohsome GET request
:return:
"""
self._url = self._base_api_url + "metadata"
try:
response = self._session().get(self._url)
response = self._session().get(self._metadata_url)
response.raise_for_status()
except requests.exceptions.ConnectionError:
raise OhsomeException(
message="Connection Error: Query could not be sent. Make sure there are no network "
f"problems and that the ohsome API URL {self._url} is valid.",
url=self._url,
f"problems and that the ohsome API URL {self._metadata_url} is valid.",
url=self._metadata_url,
params=self._parameters,
)
except requests.exceptions.HTTPError as e:
raise OhsomeException(
message=e.response.json()["message"],
url=self._url,
url=self._metadata_url,
params=self._parameters,
error_code=e.response.status_code,
)
Expand Down
Loading
Loading