Enhance OxiAPI and Node classes with type hints and property updates
- Updated the `OxiAPI` class to check for `None` explicitly when setting authentication credentials. - Added type hints to the `Node` class and introduced a TODO for future enhancements. - Refactored properties in the `NodeView` class to include type hints and improved handling of optional data retrieval.
This commit is contained in:
@@ -3,6 +3,7 @@ from requests import Session
|
|||||||
from .node import Node
|
from .node import Node
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: Add custom adapter for Oxi
|
||||||
class OxiAPI:
|
class OxiAPI:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@@ -14,7 +15,7 @@ class OxiAPI:
|
|||||||
self.base_url = url.rstrip("/")
|
self.base_url = url.rstrip("/")
|
||||||
self._session = Session()
|
self._session = Session()
|
||||||
self._session.verify = verify
|
self._session.verify = verify
|
||||||
if username and password:
|
if username is not None and password is not None:
|
||||||
self._session.auth = (username, password)
|
self._session.auth = (username, password)
|
||||||
self.node = Node(self._session, self.base_url)
|
self.node = Node(self._session, self.base_url)
|
||||||
|
|
||||||
|
|||||||
@@ -7,11 +7,11 @@ if TYPE_CHECKING:
|
|||||||
from requests import Session
|
from requests import Session
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: Add type hints
|
||||||
class Node:
|
class Node:
|
||||||
def __init__(self, session: "Session", base_url: str):
|
def __init__(self, session: "Session", base_url: str):
|
||||||
self._session = session
|
self._session = session
|
||||||
self._base_url = base_url
|
self._base_url = base_url
|
||||||
self._data = None
|
|
||||||
|
|
||||||
def __call__(self, name: str) -> NodeView:
|
def __call__(self, name: str) -> NodeView:
|
||||||
url = f"{self._base_url}/node/show/{name}"
|
url = f"{self._base_url}/node/show/{name}"
|
||||||
|
|||||||
27
oxi/view.py
27
oxi/view.py
@@ -20,36 +20,41 @@ class NodeView:
|
|||||||
return response.status_code
|
return response.status_code
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def ip(self):
|
def name(self) -> str:
|
||||||
|
return self._data.get("name")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def ip(self) -> str:
|
||||||
return self._data.get("ip")
|
return self._data.get("ip")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def full_name(self):
|
def full_name(self) -> str:
|
||||||
return self._data.get("full_name")
|
return self._data.get("full_name")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def group(self):
|
def group(self) -> str:
|
||||||
return self._data.get("group")
|
return self._data.get("group")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def model(self):
|
def model(self) -> str:
|
||||||
return self._data.get("model")
|
return self._data.get("model")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def last_status(self):
|
def last_status(self) -> str:
|
||||||
return self._data.get("last").get("status")
|
last = self._data.get("last") or {}
|
||||||
|
return last.get("status")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def last_check(self):
|
def last_check(self) -> str:
|
||||||
return self._data.get("last").get("start")
|
last = self._data.get("last") or {}
|
||||||
|
return last.get("start")
|
||||||
|
|
||||||
@property
|
def refresh(self) -> str:
|
||||||
def refresh(self):
|
|
||||||
result = self._updater()
|
result = self._updater()
|
||||||
if result != 200:
|
if result != 200:
|
||||||
raise ValueError(f"Failed to refresh node {self.full_name}")
|
raise ValueError(f"Failed to refresh node {self.full_name}")
|
||||||
return "OK"
|
return "OK"
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def config(self):
|
def config(self) -> NodeConfig:
|
||||||
return NodeConfig(self._session, self.full_name, self.model, self._base_url)
|
return NodeConfig(self._session, self.full_name, self.model, self._base_url)
|
||||||
|
|||||||
Reference in New Issue
Block a user