summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-10-05 15:59:41 +0200
committerAki <please@ignore.pl>2022-10-05 15:59:41 +0200
commit0e2be1cb255dc263e286dd064c30156c95ceaa0c (patch)
tree2be5c10393370326cc4942f7ab6c1f4eb7b9c99f
parent8b940784355d4f0c273ff6a729c35e143c8a4dda (diff)
downloadwindy-0e2be1cb255dc263e286dd064c30156c95ceaa0c.zip
windy-0e2be1cb255dc263e286dd064c30156c95ceaa0c.tar.gz
windy-0e2be1cb255dc263e286dd064c30156c95ceaa0c.tar.bz2
Point forecast now allows Request or arguments for request
-rw-r--r--windy/__init__.py4
-rw-r--r--windy/point_forecast.py24
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)