-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(api): Make base API abstract
Prepare for daily schedules
- Loading branch information
1 parent
c67e337
commit 7c91efa
Showing
7 changed files
with
80 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
"""The Yasno Outages API module.""" | ||
|
||
from .base import YasnoOutagesApi | ||
from .weekly import YasnoWeeklyOutagesApi | ||
|
||
__all__ = ["YasnoOutagesApi", "YasnoWeeklyOutagesApi"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Base class for Yasno outages API.""" | ||
|
||
import logging | ||
from abc import ABC, abstractmethod | ||
|
||
import requests | ||
|
||
from .const import API_ENDPOINT | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class YasnoOutagesApi(ABC): | ||
"""Abstract base class to interact with Yasno outages API.""" | ||
|
||
def __init__(self, city: str | None = None, group: str | None = None) -> None: | ||
"""Initialize the YasnoBaseApi with city and group.""" | ||
self.group = group | ||
self.city = city | ||
self.api_url = API_ENDPOINT | ||
self.schedule = None | ||
|
||
def fetch_schedule(self) -> None: | ||
"""Fetch outages from the API and store the raw schedule.""" | ||
try: | ||
response = requests.get(self.api_url, timeout=60) | ||
response.raise_for_status() | ||
self.schedule = response.json() | ||
except requests.RequestException as error: | ||
LOGGER.exception("Error fetching schedule from Yasno API: %s", error) # noqa: TRY401 | ||
self.schedule = {} | ||
|
||
@abstractmethod | ||
def get_cities(self) -> list[str]: | ||
"""Get a list of available cities.""" | ||
|
||
@abstractmethod | ||
def get_group_schedule(self, city: str, group: str) -> list: | ||
"""Get the schedule for a specific group.""" | ||
|
||
@abstractmethod | ||
def get_events(self) -> list[dict]: | ||
"""Get all events.""" | ||
|
||
@abstractmethod | ||
def get_current_event(self) -> dict | None: | ||
"""Get the current event.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"""Constants for the Yasno Outages API.""" | ||
|
||
API_ENDPOINT = ( | ||
"https://api.yasno.com.ua/api/v1/pages/home/schedule-turn-off-electricity" | ||
) | ||
START_OF_DAY = 0 | ||
END_OF_DAY = 24 | ||
|
||
OUTAGES_TEMPLATE_NAME = "electricity-outages-daily-schedule" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters