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

dependency(urllib3): relax dependency requirement #147

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

### Changed

- relaxed dependency requirement for `urllib3` to >=2.0.2

## 0.3.0

### Added
Expand Down
25 changes: 8 additions & 17 deletions ohsome/clients.py
Original file line number Diff line number Diff line change
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

Large diffs are not rendered by default.

72 changes: 59 additions & 13 deletions ohsome/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,82 @@
# -*- coding: utf-8 -*-
"""Conftest for shared pytest fixtures"""
import logging
from unittest.mock import patch, PropertyMock

import pytest
from urllib3 import Retry

import ohsome

logger = logging.getLogger(__name__)


@pytest.fixture(scope="session")
def base_client(tmpdir_factory):
@pytest.fixture
def mocked_metadata():
"""A default metadata dictionary.

@return:
"""
return {
"attribution": {
"url": "https://ohsome.org/copyrights",
"text": "© OpenStreetMap contributors",
},
"apiVersion": "1.10.1",
"timeout": 600.0,
"extractRegion": {
"spatialExtent": {
"type": "Polygon",
"coordinates": [
[
[-180.0, -90.0],
[180.0, -90.0],
[180.0, 90.0],
[-180.0, 90.0],
[-180.0, -90.0],
]
],
},
"temporalExtent": {
"fromTimestamp": "2007-10-08T00:00:00Z",
"toTimestamp": "2023-11-25T13:00:00Z",
},
"replicationSequenceNumber": 99919,
},
}


@pytest.fixture
def base_client(mocked_metadata, tmpdir_factory):
"""Session-wide test client."""
temp_directory = tmpdir_factory.mktemp("base_client").mkdir("logs").strpath
client = ohsome.OhsomeClient(log_dir=temp_directory)
assert client.metadata # call metadata once to ensure it is cached
yield client
with patch(
"ohsome.clients._OhsomeInfoClient.metadata",
new_callable=PropertyMock,
return_value=mocked_metadata,
):
client = ohsome.OhsomeClient(log_dir=temp_directory)
yield client


@pytest.fixture(scope="session")
def base_client_without_log():
@pytest.fixture
def base_client_without_log(mocked_metadata):
"""Session-wide test client."""
client = ohsome.OhsomeClient(log=False)
assert client.metadata # call metadata once to ensure it is cached
yield client
with patch(
"ohsome.clients._OhsomeInfoClient.metadata",
new_callable=PropertyMock,
return_value=mocked_metadata,
):
client = ohsome.OhsomeClient(log=False)
yield client


@pytest.fixture(scope="session")
@pytest.fixture
def custom_client_with_wrong_url(tmpdir_factory):
"""Session-wide test client."""
temp_directory = tmpdir_factory.mktemp("base_client").mkdir("logs").strpath
client = ohsome.OhsomeClient(
base_api_url="https://imwrong",
log_dir=temp_directory,
base_api_url="https://imwrong", log_dir=temp_directory, retry=Retry(total=0)
)
yield client

Expand Down
2 changes: 1 addition & 1 deletion ohsome/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_user_agent(base_client):
Checks user agent set by ohsome-py
:return:
"""
resp = base_client._session().get(base_client._url)
resp = base_client._session().get(base_client._metadata_url)
used_user_agent = resp.request.headers["user-agent"]
assert used_user_agent == f"ohsome-py/{OHSOME_VERSION}"

Expand Down
2 changes: 1 addition & 1 deletion ohsome/test/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import geopandas as gpd
import pandas as pd
import pytest
import datetime as dt


@pytest.mark.vcr
Expand Down Expand Up @@ -406,6 +405,7 @@ def test_empty_geodataframe(base_client):
assert result.empty


@pytest.mark.vcr
def test_all_columns_with_timestamps_to_be_without_timezone(base_client):
"""Test whether all the columns with timestamp like 'timestamp', '@timestamp','@validFrom', '@validTo',
'fromTimestamp', 'toTimestamp' and '@snapshotTimestamp' are without timezone
Expand Down
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ requests = "^2.25.1"
pandas = "^2.1.3"
numpy = "^1.20.0"
geopandas = "^0.14.1"
urllib3 = "^2.1.0"
urllib3 = "^2.0.2"
curlify2 = "^2.0.0"

[tool.poetry.group.test.dependencies]
Expand Down
Loading