Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/pyopenweathermap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
UnauthorizedError,
TooManyRequestsError
)
from .client.owm_abstract_client import OWMClient
from .client.weather.abstract_weather_client import OWMWeatherClient
from .client.owm_client_factory import create_owm_client
from .weather import (
CurrentWeather, MinutelyWeatherForecast, HourlyWeatherForecast, DailyWeatherForecast, WeatherReport, DailyTemperature, WeatherCondition
)
from .client.air_pollution_client import AirPollutionReport
from .air_pollution import CurrentAirPollution, AirPollutionReport
7 changes: 1 addition & 6 deletions src/pyopenweathermap/client/air_pollution_client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from .owm_abstract_client import OWMClient
from ..data_converter import DataConverter
from ..exception import UnauthorizedError
from ..weather import WeatherReport
from ..air_pollution import AirPollutionReport

CURRENT_AIR_POLLUTION_API_URL = 'https://api.openweathermap.org/data/2.5/air_pollution'
FORECAST_API_URL = 'https://api.openweathermap.org/data/2.5/air_pollution/forecast'

class OWMAirPollutionClient(OWMClient):
class OWMAirPollutionClient:
def __init__(self, api_key):
super().__init__()
self.api_key = api_key
Expand All @@ -23,9 +21,6 @@ async def get_air_pollution(self, lat, lon) -> AirPollutionReport:

return AirPollutionReport(current, hourly)

async def get_weather(self, lat, lon) -> WeatherReport:
raise NotImplementedError('Air Pollution client does not have get_weather()')

async def validate_key(self) -> bool:
url = self._get_url(50.06, 14.44, 'api_type')
try:
Expand Down
8 changes: 4 additions & 4 deletions src/pyopenweathermap/client/owm_client_factory.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import logging

from .free_client import OWMFreeClient
from .onecall_client import OWMOneCallClient
from .weather.free_client import OWMFreeClient
from .weather.onecall_client import OWMOneCallClient
from .air_pollution_client import OWMAirPollutionClient


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))
logger.info('Initializing OWM client with for type: ' + str(api_type))
if api_type == 'v3.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)
if api_type == 'air_pollution':
return OWMAirPollutionClient(api_key)
else:
raise Exception('Unsupported API type ' + str(api_type))
raise Exception('Unsupported type ' + str(api_type))
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from abc import ABC, abstractmethod
from ..weather import WeatherReport
from ..air_pollution import AirPollutionReport
from ..http_client import HttpClient
from ...http_client import HttpClient


class OWMClient(ABC):
class OWMWeatherClient(ABC):
http_client: HttpClient

def __init__(self) -> None:
Expand All @@ -14,10 +13,6 @@ def __init__(self) -> None:
async def get_weather(self, lat, lon) -> WeatherReport:
pass

@abstractmethod
async def get_air_pollution(self, lat, lon) -> AirPollutionReport:
pass

@abstractmethod
async def validate_key(self) -> bool:
pass
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .owm_abstract_client import OWMClient
from .abstract_weather_client import OWMWeatherClient
from ..data_converter import DataConverter
from ..exception import UnauthorizedError
from ..weather import WeatherReport
Expand All @@ -8,7 +8,7 @@
FORECAST_API_URL = 'https://api.openweathermap.org/data/2.5/forecast'


class OWMFreeClient(OWMClient):
class OWMFreeClient(OWMWeatherClient):
def __init__(self, api_key, api_type, units="metric", lang='en'):
super().__init__()
self.api_key = api_key
Expand All @@ -27,9 +27,6 @@ async def get_weather(self, lat, lon) -> WeatherReport:
hourly = [DataConverter.free_to_hourly_weather_forecast(item) for item in json_response['list']]
return WeatherReport(current,[], hourly, [])

async def get_air_pollution(self, lat, lon) -> AirPollutionReport:
raise NotImplementedError('free client does not have get_air_pollution()')

async def validate_key(self) -> bool:
url = self._get_url(50.06, 14.44)
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from .owm_abstract_client import OWMClient
from .abstract_weather_client import OWMWeatherClient
from ..data_converter import DataConverter
from ..exception import UnauthorizedError
from ..weather import WeatherReport
from ..air_pollution import AirPollutionReport

API_URL = 'https://api.openweathermap.org/data/3.0/onecall'


class OWMOneCallClient(OWMClient):
class OWMOneCallClient(OWMWeatherClient):
def __init__(self, api_key, api_version, units="metric", lang='en'):
super().__init__()
self.api_key = api_key
Expand All @@ -31,9 +30,6 @@ async def get_weather(self, lat, lon) -> WeatherReport:

return WeatherReport(current, minutely, hourly, daily)

async def get_air_pollution(self, lat, lon) -> AirPollutionReport:
raise NotImplementedError('free client does not have get_air_pollution()')

async def validate_key(self) -> bool:
url = (f"{self._get_url(50.06, 14.44)}"
f"&exclude=current,minutely,hourly,daily,alerts)")
Expand Down
Loading