diff --git a/src/pyopenweathermap/client/onecall_client.py b/src/pyopenweathermap/client/onecall_client.py index 078a8ec..cff548e 100644 --- a/src/pyopenweathermap/client/onecall_client.py +++ b/src/pyopenweathermap/client/onecall_client.py @@ -44,7 +44,10 @@ async def validate_key(self) -> bool: return False def _get_url(self, lat, lon): - return (f"{API_URL}?" + api_url = 'https://api.openweathermap.org/data/3.0/onecall' + if getattr(self, 'api_version', None) == 'v4.0': + api_url = 'https://api.openweathermap.org/data/4.0/onecall' + return (f"{api_url}?" f"lat={lat}&" f"lon={lon}&" f"appid={self.api_key}&" diff --git a/src/pyopenweathermap/client/owm_client_factory.py b/src/pyopenweathermap/client/owm_client_factory.py index e4523fb..34a11d4 100644 --- a/src/pyopenweathermap/client/owm_client_factory.py +++ b/src/pyopenweathermap/client/owm_client_factory.py @@ -9,7 +9,7 @@ def create_owm_client(api_key, api_type, units="metric", lang='en'): logger = logging.getLogger(__name__) logger.info('Initializing OWMClient with api type: ' + str(api_type)) - if api_type == 'v3.0': + if api_type == 'v3.0' or api_type == 'v4.0': return OWMOneCallClient(api_key, api_type, units, lang) if api_type == 'current' or api_type == 'forecast': return OWMFreeClient(api_key, api_type, units, lang) diff --git a/tests/test_all.py b/tests/test_all.py index a5d9f5f..f2c59b3 100644 --- a/tests/test_all.py +++ b/tests/test_all.py @@ -20,6 +20,16 @@ async def test_api_30(): assert report.daily_forecast[0].condition.id is not None +@pytest.mark.network +@pytest.mark.asyncio +async def test_api_40(): + api_key = os.getenv('OWM_API_KEY') + client = create_owm_client(api_key, 'v4.0') + report = await client.get_weather(LATITUDE, LONGITUDE) + assert report.current.date_time is not None + assert report.hourly_forecast[0].condition.id is not None + assert report.daily_forecast[0].condition.id is not None + @pytest.mark.network @pytest.mark.asyncio async def test_free_current_weather(): @@ -115,3 +125,9 @@ def test_forecast_deserialization(): weather = DataConverter.free_to_hourly_weather_forecast(data) assert weather.date_time is not None assert weather.condition.id is not None + +def test_v40_url(): + client = create_owm_client('fake_key', 'v4.0') + url = client._get_url(10.0, 20.0) + assert 'https://api.openweathermap.org/data/4.0/onecall' in url + assert 'fake_key' in url