XHS / xhs_utils /response_guard.py
Trae Bot
Upload Spider_XHS project
c481f8a
def require_keys(obj, keys: list[str], context: str = ""):
if obj is None or not isinstance(obj, dict):
raise KeyError(f"{context}: invalid_object")
for k in keys:
if k not in obj:
raise KeyError(f"{context}: missing_key={k}")
def get_dict(obj, key: str, context: str = "") -> dict:
if obj is None or not isinstance(obj, dict):
raise KeyError(f"{context}: invalid_object")
if key not in obj or obj[key] is None:
raise KeyError(f"{context}: missing_key={key}")
val = obj[key]
if not isinstance(val, dict):
raise KeyError(f"{context}: invalid_dict_key={key}")
return val
def get_list(obj, key: str, context: str = "") -> list:
if obj is None or not isinstance(obj, dict):
raise KeyError(f"{context}: invalid_object")
if key not in obj or obj[key] is None:
raise KeyError(f"{context}: missing_key={key}")
val = obj[key]
if not isinstance(val, list):
raise KeyError(f"{context}: invalid_list_key={key}")
return val