diff options
-rw-r--r-- | windy/__init__.py | 4 | ||||
-rw-r--r-- | windy/point_forecast.py | 24 |
2 files changed, 21 insertions, 7 deletions
diff --git a/windy/__init__.py b/windy/__init__.py index b5e36a9..cd9c67f 100644 --- a/windy/__init__.py +++ b/windy/__init__.py @@ -11,5 +11,5 @@ class Windy: self.api = api self._point_forecast = point_forecast.Endpoint("/point-forecast/v2") - def point_forecast(self, request: point_forecast.Request): - return self._point_forecast(self, request) + def point_forecast(self, *args, **kwargs): + return self._point_forecast(self, *args, **kwargs) diff --git a/windy/point_forecast.py b/windy/point_forecast.py index 976f785..7f1930a 100644 --- a/windy/point_forecast.py +++ b/windy/point_forecast.py @@ -2,10 +2,20 @@ from dataclasses import dataclass from enum import Enum +def _json(value): + try: + return value.json() + except AttributeError: + return value + + class _StrEnum(Enum): def __str__(self): return self.value + def json(self): + return self.value + class Model(_StrEnum): AROME = "arome" @@ -44,16 +54,16 @@ class Request: parameters: list = None levels: list = None - def body(self): + def json(self): body = { 'key': self.key, 'lat': self.lat, 'lon': self.lon, - 'model': str(self.model), + 'model': _json(self.model), 'parameters': self.parameters or [], } if self.levels: - body['levels'] = [str(x) for x in self.levels] + body['levels'] = [_json(x) for x in self.levels] return body @@ -61,5 +71,9 @@ class Request: class Endpoint: path: str - def __call__(self, windy, request: Request): - return windy.session.post(windy.api + self.path, json=request.body()) + def __call__(self, ctx, *args, **kwargs): + try: + body = args[0].json() + except (IndexError, AttributeError): + body = Request(*args, **kwargs).json() + return ctx.session.post(ctx.api + self.path, json=body) |