diff options
Diffstat (limited to 'windy.py')
-rw-r--r-- | windy.py | 53 |
1 files changed, 29 insertions, 24 deletions
@@ -1,9 +1,8 @@ +from dataclasses import dataclass from enum import Enum import requests -API = "https://api.windy.com/api" - class _StrEnum(Enum): def __str__(self): @@ -38,26 +37,32 @@ class Level(_StrEnum): H150 = "150h" -class PointForecast: - ENDPOINT = "/point-forecast/v2" - - def __init__(self, lat: float, lon: float, model: Model, parameters=None, levels=None, key=None): - self.lat = lat - self.lon = lon - self.model = model - self.parameters = parameters or [] - self.levels = levels or ["surface"] - self.key = key - - def request(self, /, key=None): - return requests.post(API + self.ENDPOINT, json=self.body(key=key)) - - def body(self, /, lat=None, lon=None, model=None, parameters=None, levels=None, key=None): - return { - 'lat': lat or self.lat, - 'lon': lon or self.lon, - 'model': str(model or self.model), - 'parameters': parameters or self.parameters, - 'levels': levels or self.levels, - 'key': key or self.key, +@dataclass +class Config: + key: str + api: str = "https://api.windy.com/api" + + +@dataclass +class Request: + lat: float + lon: float + model: Model + parameters: list = None + levels: list = None + endpoint: str = "/point-forecast/v2" + + def json(self): + body = { + 'lat': self.lat, + 'lon': self.lon, + 'model': str(self.model), + 'parameters': self.parameters or [], } + if self.levels: + body['levels'] = [str(x) for x in self.levels] + return body + + +def point_forecast(request: Request, config: Config): + return requests.post(config.api + request.endpoint, json=request.json()) |