Single Redfish resource or collection; supports lazy child resolution and member listing.
| 74 | |
| 75 | |
| 76 | class Endpoint: |
| 77 | """Single Redfish resource or collection; supports lazy child resolution and member listing.""" |
| 78 | |
| 79 | NAME: str = "Endpoint" |
| 80 | |
| 81 | def __init__(self, url: str, client: RedFishClient) -> None: |
| 82 | self.log = get_logger(f"{__name__}:{Endpoint.NAME}") |
| 83 | self.url: str = url |
| 84 | self.client: RedFishClient = client |
| 85 | self._children: Dict[str, "Endpoint"] = {} |
| 86 | self.data: Dict[str, Any] = self.get_data() |
| 87 | self.id: str = "" |
| 88 | self.members_names: List[str] = [] |
| 89 | |
| 90 | if self.has_members: |
| 91 | self.members_names = self.get_members_names() |
| 92 | |
| 93 | if self.data: |
| 94 | try: |
| 95 | self.id = self.data["Id"] |
| 96 | except KeyError: |
| 97 | self.id = self.data["@odata.id"].split("/")[-1] |
| 98 | else: |
| 99 | self.log.warning(f"No data could be loaded for {self.url}") |
| 100 | |
| 101 | def __getitem__(self, key: str) -> "Endpoint": |
| 102 | if not isinstance(key, str) or not key or "/" in key: |
| 103 | raise KeyError(key) |
| 104 | |
| 105 | if key not in self._children: |
| 106 | child_url: str = f'{self.url.rstrip("/")}/{key}' |
| 107 | self._children[key] = Endpoint(child_url, self.client) |
| 108 | |
| 109 | return self._children[key] |
| 110 | |
| 111 | def list_children(self) -> List[str]: |
| 112 | return list(self._children.keys()) |
| 113 | |
| 114 | def query(self, url: str) -> Dict[str, Any]: |
| 115 | data: Dict[str, Any] = {} |
| 116 | try: |
| 117 | self.log.debug(f"Querying {url}") |
| 118 | _, _data, _ = self.client.query(endpoint=url) |
| 119 | if not _data: |
| 120 | self.log.warning(f"Empty response from {url}") |
| 121 | else: |
| 122 | data = json.loads(_data) |
| 123 | except KeyError as e: |
| 124 | self.log.error(f"KeyError while querying {url}: {e}") |
| 125 | except HTTPError as e: |
| 126 | self.log.error(f"HTTP error while querying {url} - {e.code} - {e.reason}") |
| 127 | except json.JSONDecodeError as e: |
| 128 | self.log.error(f"JSON decode error while querying {url}: {e}") |
| 129 | except Exception as e: |
| 130 | self.log.error( |
| 131 | f"Unexpected error while querying {url}: {type(e).__name__}: {e}" |
| 132 | ) |
| 133 | return data |
no outgoing calls
no test coverage detected