From 212c919a6e735b98b8829c1b9a9996d48a55a636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Fern=C3=A1ndez=20Rojas?= Date: Wed, 23 Aug 2023 12:25:04 +0200 Subject: [PATCH] interface: add raw API data MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Álvaro Fernández Rojas --- aemet_opendata/const.py | 5 +++++ aemet_opendata/interface.py | 30 ++++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/aemet_opendata/const.py b/aemet_opendata/const.py index 62456be..dde5eac 100644 --- a/aemet_opendata/const.py +++ b/aemet_opendata/const.py @@ -62,3 +62,8 @@ ATTR_DATA: Final[str] = "data" ATTR_RESPONSE: Final[str] = "response" + +RAW_FORECAST_DAILY: Final[str] = "forecast-daily" +RAW_FORECAST_HOURLY: Final[str] = "forecast-hourly" +RAW_STATIONS: Final[str] = "stations" +RAW_TOWNS: Final[str] = "towns" diff --git a/aemet_opendata/interface.py b/aemet_opendata/interface.py index 874c873..6b79baa 100644 --- a/aemet_opendata/interface.py +++ b/aemet_opendata/interface.py @@ -23,6 +23,10 @@ API_URL, ATTR_DATA, ATTR_RESPONSE, + RAW_FORECAST_DAILY, + RAW_FORECAST_HOURLY, + RAW_STATIONS, + RAW_TOWNS, ) from .exceptions import AemetError, AuthError, TooManyRequests from .helpers import parse_station_coordinates, parse_town_code @@ -46,6 +50,12 @@ def __init__( options: ConnectionOptions, ) -> None: """Init AEMET OpenData API.""" + self._api_raw_data: dict[str, Any] = { + RAW_FORECAST_DAILY: {}, + RAW_FORECAST_HOURLY: {}, + RAW_STATIONS: {}, + RAW_TOWNS: {}, + } self.aiohttp_session = aiohttp_session self.dist_hp: bool = False self.headers: dict[str, Any] = { @@ -114,6 +124,10 @@ async def api_data(self, url: str) -> dict[str, Any]: return cast(dict[str, Any], resp_json) + def raw_data(self) -> dict[str, Any]: + """Return raw AEMET OpenData API data.""" + return self._api_raw_data + def calc_distance( self, start: tuple[float, float], end: tuple[float, float] ) -> Distance: @@ -196,9 +210,11 @@ async def get_conventional_observation_station_data( self, station: str, fetch_data: bool = True ) -> dict[str, Any]: """Get data from conventional observation station.""" - return await self.api_call( + res = await self.api_call( f"observacion/convencional/datos/estacion/{station}", fetch_data ) + self._api_raw_data[RAW_STATIONS][station] = res + return res async def get_lightnings_map(self) -> dict[str, Any]: """Get map with lightning falls (last 6h).""" @@ -208,23 +224,29 @@ async def get_specific_forecast_town_daily( self, town: str, fetch_data: bool = True ) -> dict[str, Any]: """Get daily forecast for specific town (daily).""" - return await self.api_call( + res = await self.api_call( f"prediccion/especifica/municipio/diaria/{parse_town_code(town)}", fetch_data, ) + self._api_raw_data[RAW_FORECAST_DAILY][town] = res + return res async def get_specific_forecast_town_hourly( self, town: str, fetch_data: bool = True ) -> dict[str, Any]: """Get hourly forecast for specific town (hourly).""" - return await self.api_call( + res = await self.api_call( f"prediccion/especifica/municipio/horaria/{parse_town_code(town)}", fetch_data, ) + self._api_raw_data[RAW_FORECAST_HOURLY][town] = res + return res async def get_town(self, town: str) -> dict[str, Any]: """Get information about specific town.""" - return await self.api_call(f"maestro/municipio/{town}") + res = await self.api_call(f"maestro/municipio/{town}") + self._api_raw_data[RAW_TOWNS][town] = res + return res async def get_town_by_coordinates( self, latitude: float, longitude: float