Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/namecheap/_api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ def normalize_xml_response(data: dict[str, Any]) -> dict[str, Any]:
return normalized


def navigate_path(result: Any, path: str) -> Any:
"""Walk a dot-separated path into a parsed XML response.

A self-closing element (e.g. ``<DomainGetListResult />`` returned for an
account with no domains) is parsed as None, so navigating through it must
not raise; treat a None segment as an empty result.
"""
for key in path.split("."):
result = (result or {}).get(key, {})
return result


class BaseAPI:
"""Base class for API endpoints."""

Expand Down Expand Up @@ -175,8 +187,7 @@ def _request(

# Navigate to specific path if provided
if path:
for key in path.split("."):
result = result.get(key, {})
result = navigate_path(result, path)

# Return empty list if no results
if not result:
Expand Down
27 changes: 27 additions & 0 deletions src/namecheap/_api/base_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Boundary checks for namecheap._api.base. Run: uv run python -m namecheap._api.base_check"""

from namecheap._api.base import navigate_path


def check_navigate_path_reaches_value() -> None:
data = {"DomainGetListResult": {"Domain": [{"@Name": "example.com"}]}}
assert navigate_path(data, "DomainGetListResult.Domain") == [
{"@Name": "example.com"}
]


def check_navigate_path_missing_key_returns_empty() -> None:
assert navigate_path({}, "DomainGetListResult.Domain") == {}


def check_navigate_path_none_segment_returns_empty() -> None:
# Empty account: <DomainGetListResult /> is parsed as None by xmltodict.
data = {"DomainGetListResult": None, "Paging": {"TotalItems": "0"}}
assert navigate_path(data, "DomainGetListResult.Domain") == {}


if __name__ == "__main__":
check_navigate_path_reaches_value()
check_navigate_path_missing_key_returns_empty()
check_navigate_path_none_segment_returns_empty()
print("ok: namecheap._api.base")