Skip to content

Commit

Permalink
interface: add raw API data
Browse files Browse the repository at this point in the history
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
  • Loading branch information
Noltari committed Aug 23, 2023
1 parent 8608615 commit 212c919
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
5 changes: 5 additions & 0 deletions aemet_opendata/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
30 changes: 26 additions & 4 deletions aemet_opendata/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] = {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)."""
Expand All @@ -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
Expand Down

0 comments on commit 212c919

Please sign in to comment.