1 """Helpers for RESTful API."""
6 from jsonpath
import jsonpath
10 _LOGGER = logging.getLogger(__name__)
14 value: str |
None, json_attrs: list[str], json_attrs_path: str |
None
16 """Parse JSON attributes."""
18 _LOGGER.warning(
"Empty reply found when expecting JSON data")
23 if json_attrs_path
is not None:
24 json_dict = jsonpath(json_dict, json_attrs_path)
28 if isinstance(json_dict, list):
29 json_dict = json_dict[0]
30 if isinstance(json_dict, dict):
31 return {k: json_dict[k]
for k
in json_attrs
if k
in json_dict}
34 "JSON result was not a dictionary or list with 0th element a dictionary"
37 _LOGGER.warning(
"REST result could not be parsed as JSON")
38 _LOGGER.debug(
"Erroneous JSON: %s", value)
dict[str, Any] parse_json_attributes(str|None value, list[str] json_attrs, str|None json_attrs_path)