From 732e30975828cf525fe27dc406712eb3d9b4d812 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 4 Oct 2022 23:20:23 +0200 Subject: Wrapped and shuffled endpoint config around to Windy class --- windy/__init__.py | 16 +++++++++++++++- windy/point_forecast.py | 13 +++++-------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/windy/__init__.py b/windy/__init__.py index ba397a9..346cece 100644 --- a/windy/__init__.py +++ b/windy/__init__.py @@ -1 +1,15 @@ -from . import point_forecast +from pint import UnitRegistry +from requests import Session + +import point_forecast + + +class Windy: + def __init__(self, registry: UnitRegistry, session: Session = None, api: str = "https://api.windy.com/api"): + self.registry = registry + self.session = session or Session() + 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) diff --git a/windy/point_forecast.py b/windy/point_forecast.py index 83c328d..976f785 100644 --- a/windy/point_forecast.py +++ b/windy/point_forecast.py @@ -1,8 +1,6 @@ from dataclasses import dataclass from enum import Enum -import requests - class _StrEnum(Enum): def __str__(self): @@ -45,8 +43,6 @@ class Request: model: Model parameters: list = None levels: list = None - endpoint: str = "/point-forecast/v2" - api: str = "https://api.windy.com/api" def body(self): body = { @@ -60,9 +56,10 @@ class Request: body['levels'] = [str(x) for x in self.levels] return body - def url(self): - return self.api + self.endpoint +@dataclass +class Endpoint: + path: str -def point_forecast(request: Request): - return requests.post(request.url(), json=request.body()) + def __call__(self, windy, request: Request): + return windy.session.post(windy.api + self.path, json=request.body()) -- cgit v1.1