- Implemented a private `_updater` method to fetch the next node's status. - Added `last_status` and `last_check` properties to retrieve the latest node status and check time. - Introduced a `refresh` property to update the node status and handle errors appropriately.
56 lines
1.3 KiB
Python
56 lines
1.3 KiB
Python
from functools import cached_property
|
|
from typing import TYPE_CHECKING
|
|
|
|
from .conf import NodeConfig
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
from requests import Session
|
|
|
|
|
|
class NodeView:
|
|
def __init__(self, session: "Session", base_url: str, data: dict):
|
|
self._session = session
|
|
self._base_url = base_url
|
|
self._data = data
|
|
|
|
def _updater(self) -> None:
|
|
response = self._session.get(f"{self._base_url}/node/next/{self.full_name}")
|
|
response.raise_for_status()
|
|
return response.status_code
|
|
|
|
@property
|
|
def ip(self):
|
|
return self._data.get("ip")
|
|
|
|
@property
|
|
def full_name(self):
|
|
return self._data.get("full_name")
|
|
|
|
@property
|
|
def group(self):
|
|
return self._data.get("group")
|
|
|
|
@property
|
|
def model(self):
|
|
return self._data.get("model")
|
|
|
|
@property
|
|
def last_status(self):
|
|
return self._data.get("last").get("status")
|
|
|
|
@property
|
|
def last_check(self):
|
|
return self._data.get("last").get("start")
|
|
|
|
@property
|
|
def refresh(self):
|
|
result = self._updater()
|
|
if result != 200:
|
|
raise ValueError(f"Failed to refresh node {self.full_name}")
|
|
return "OK"
|
|
|
|
@cached_property
|
|
def config(self):
|
|
return NodeConfig(self._session, self.full_name, self.model, self._base_url)
|