Skip to content

Commit 5d53a19

Browse files
committed
interface: parse API remaining requests counter
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
1 parent 54fdda1 commit 5d53a19

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

aemet_opendata/const.py

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
AOD_WIND_SPEED: Final[str] = "wind-speed"
114114
AOD_WIND_SPEED_MAX: Final[str] = "wind-speed-max"
115115

116+
API_HDR_REQ_COUNT: Final[str] = "Remaining-request-count"
116117
API_ID_PFX: Final[str] = "id"
117118
API_MIN_STATION_DISTANCE_KM: Final[int] = 40
118119
API_MIN_TOWN_DISTANCE_KM: Final[int] = 40
@@ -140,6 +141,7 @@
140141

141142
RAW_FORECAST_DAILY: Final[str] = "forecast-daily"
142143
RAW_FORECAST_HOURLY: Final[str] = "forecast-hourly"
144+
RAW_REQ_COUNT: Final[str] = "requests-counter"
143145
RAW_STATIONS: Final[str] = "stations"
144146
RAW_TOWNS: Final[str] = "towns"
145147

aemet_opendata/interface.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
AOD_WIND_DIRECTION,
4242
AOD_WIND_SPEED,
4343
AOD_WIND_SPEED_MAX,
44+
API_HDR_REQ_COUNT,
4445
API_MIN_STATION_DISTANCE_KM,
4546
API_MIN_TOWN_DISTANCE_KM,
4647
API_URL,
@@ -54,6 +55,7 @@
5455
HTTP_MAX_REQUESTS,
5556
RAW_FORECAST_DAILY,
5657
RAW_FORECAST_HOURLY,
58+
RAW_REQ_COUNT,
5759
RAW_STATIONS,
5860
RAW_TOWNS,
5961
)
@@ -105,6 +107,7 @@ def __init__(
105107
self._api_raw_data = {
106108
RAW_FORECAST_DAILY: {},
107109
RAW_FORECAST_HOURLY: {},
110+
RAW_REQ_COUNT: None,
108111
RAW_STATIONS: {},
109112
RAW_TOWNS: {},
110113
}
@@ -122,9 +125,7 @@ def __init__(
122125
self.station = None
123126
self.town = None
124127

125-
async def set_api_raw_data(
126-
self, key: str, subkey: str | None, data: dict[str, Any] | None
127-
) -> None:
128+
async def set_api_raw_data(self, key: str, subkey: str | None, data: Any) -> None:
128129
"""Save API raw data if not empty."""
129130
if data is not None:
130131
async with self._api_raw_data_lock:
@@ -150,6 +151,10 @@ async def api_call(self, cmd: str, fetch_data: bool = False) -> dict[str, Any]:
150151
except ClientError as err:
151152
raise AemetError(err) from err
152153

154+
req_count = resp.headers.get(API_HDR_REQ_COUNT)
155+
if req_count is not None:
156+
await self.set_api_raw_data(RAW_REQ_COUNT, None, req_count)
157+
153158
_LOGGER.debug(
154159
"api_call: cmd=%s status=%s content_type=%s",
155160
cmd,
@@ -208,6 +213,10 @@ async def api_data(self, url: str) -> dict[str, Any]:
208213
except ClientError as err:
209214
raise AemetError(err) from err
210215

216+
req_count = resp.headers.get(API_HDR_REQ_COUNT)
217+
if req_count is not None:
218+
await self.set_api_raw_data(RAW_REQ_COUNT, None, req_count)
219+
211220
_LOGGER.debug(
212221
"api_data: url=%s status=%s content_type=%s",
213222
url,

0 commit comments

Comments
 (0)