diff --git a/openmeteo_requests/Client.py b/openmeteo_requests/Client.py index de632c8..3d74dbc 100644 --- a/openmeteo_requests/Client.py +++ b/openmeteo_requests/Client.py @@ -1,4 +1,4 @@ -"""Sync client""" +"""Open-Meteo API client based on the requests library""" from __future__ import annotations @@ -13,39 +13,39 @@ from openmeteo_sdk.WeatherApiResponse import WeatherApiResponse T = TypeVar("T") +TSession = TypeVar("TSession", bound=requests.Session) -class OpenMeteoRequests(Exception): - """Open Meteo Error""" - - -def decode_messages(cls: type[T], data: bytes) -> list[T]: - """Decode byte to an array of FlatBuffers messages""" - messages = [] - total = len(data) - pos = int(0) - while pos < total: - length = int.from_bytes(data[pos : pos + 4], byteorder="little") - message = cls.GetRootAs(data, pos + 4) - messages.append(message) - pos += length + 4 - return messages +class OpenMeteoRequestsError(Exception): + """Open-Meteo Error""" class Client: - """Open Meteo Client SYNC""" + """Open-Meteo API Client""" - def __init__(self, session: requests.Session | None = None): + def __init__(self, session: TSession | None = None): self.session = session or requests.Session() def _get(self, cls: type[T], url: str, params: any) -> list[T]: + params["format"] = "flatbuffers" + response = self.session.get(url, params=params) if response.status_code in [400, 429]: response_body = response.json() - raise OpenMeteoRequests(response_body) + raise OpenMeteoRequestsError(response_body) response.raise_for_status() - return decode_messages(cls, response.content) + + data = response.content + messages = [] + total = len(data) + pos = int(0) + while pos < total: + length = int.from_bytes(data[pos : pos + 4], byteorder="little") + message = cls.GetRootAs(data, pos + 4) + messages.append(message) + pos += length + 4 + return messages def weather_api(self, url: str, params: any) -> list[WeatherApiResponse]: """Get and decode as weather api""" diff --git a/tests/test_methods.py b/tests/test_methods.py index 6f81381..e9c068e 100644 --- a/tests/test_methods.py +++ b/tests/test_methods.py @@ -17,7 +17,6 @@ def test_fetch_all(): # 'timezone': 'auto', # 'current': ['temperature_2m','precipitation'], # 'current_weather': 1, - "format": "flatbuffers", } results = om.weather_api("https://archive-api.open-meteo.com/v1/archive", params=params)