summaryrefslogtreecommitdiff
path: root/windy.py
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-09-24 00:09:49 +0200
committerAki <please@ignore.pl>2022-09-24 00:09:49 +0200
commita9e99607ec03e13bb2e819557174e0120b001255 (patch)
treea2b8191308e03bc8d898d50e0cdbb372b5ce7776 /windy.py
parent790b76fa00d1cdc9ad17d8c0986d8ac82e623a77 (diff)
downloadwindy-a9e99607ec03e13bb2e819557174e0120b001255.zip
windy-a9e99607ec03e13bb2e819557174e0120b001255.tar.gz
windy-a9e99607ec03e13bb2e819557174e0120b001255.tar.bz2
Separated config, request, and function that makes real request
Experimenting a little bit with different forms to see what seems alright as public API. Feels like an overkill, since the whole thing is dead simple, but let's do it regardless.
Diffstat (limited to 'windy.py')
-rw-r--r--windy.py53
1 files changed, 29 insertions, 24 deletions
diff --git a/windy.py b/windy.py
index d7567ff..2d5cab6 100644
--- a/windy.py
+++ b/windy.py
@@ -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())