Skip to content
Draft
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
35 changes: 24 additions & 11 deletions pyiceberg/catalog/rest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ def _parse_endpoints(cls, v: list[str] | None) -> set[Endpoint] | None:

class ListNamespaceResponse(IcebergBaseModel):
namespaces: list[Identifier] = Field()
next_page_token: str | None = Field(default=None, alias="next-page-token")


class NamespaceResponse(IcebergBaseModel):
Expand Down Expand Up @@ -1182,19 +1183,31 @@ def drop_namespace(self, namespace: str | Identifier) -> None:
def list_namespaces(self, namespace: str | Identifier = ()) -> list[Identifier]:
self._check_endpoint(Capability.V1_LIST_NAMESPACES)
namespace_tuple = self.identifier_to_tuple(namespace)
response = self._session.get(
self.url(
f"{Endpoints.list_namespaces}?parent={self._encode_namespace_path(namespace_tuple)}"
if namespace_tuple
else Endpoints.list_namespaces
),
url = (
f"{Endpoints.list_namespaces}?parent={self._encode_namespace_path(namespace_tuple)}"
if namespace_tuple
else Endpoints.list_namespaces
)
try:
response.raise_for_status()
except HTTPError as exc:
_handle_non_200_response(exc, {404: NoSuchNamespaceError})

return ListNamespaceResponse.model_validate_json(response.text).namespaces
namespaces: list[Identifier] = []
page_token: str | None = None

while True:
params = {"pageToken": page_token} if page_token else None
response = self._session.get(self.url(url), params=params)
try:
response.raise_for_status()
except HTTPError as exc:
_handle_non_200_response(exc, {404: NoSuchNamespaceError})

parsed = ListNamespaceResponse.model_validate_json(response.text)
namespaces.extend(parsed.namespaces)

if not parsed.next_page_token:
break
page_token = parsed.next_page_token

return namespaces

@retry(**_RETRY_ARGS)
def load_namespace_properties(self, namespace: str | Identifier) -> Properties:
Expand Down
99 changes: 99 additions & 0 deletions tests/catalog/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,105 @@ def test_list_namespace_with_parent_200(rest_mock: Mocker) -> None:
]


def test_list_namespaces_paginated_200(rest_mock: Mocker) -> None:
# First page with next-page-token
rest_mock.get(
f"{TEST_URI}v1/namespaces",
json={
"namespaces": [["ns1"], ["ns2"]],
"next-page-token": "page2token",
},
status_code=200,
request_headers=TEST_HEADERS,
)
# Second page with next-page-token
rest_mock.get(
f"{TEST_URI}v1/namespaces?pageToken=page2token",
json={
"namespaces": [["ns3"], ["ns4"]],
"next-page-token": "page3token",
},
status_code=200,
request_headers=TEST_HEADERS,
)
# Third page without next-page-token (last page)
rest_mock.get(
f"{TEST_URI}v1/namespaces?pageToken=page3token",
json={
"namespaces": [["ns5"]],
},
status_code=200,
request_headers=TEST_HEADERS,
)

result = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN).list_namespaces()
assert result == [
("ns1",),
("ns2",),
("ns3",),
("ns4",),
("ns5",),
]


def test_list_namespaces_with_parent_paginated_200(rest_mock: Mocker) -> None:
# First page
rest_mock.get(
f"{TEST_URI}v1/namespaces?parent=accounting",
json={
"namespaces": [["accounting", "tax"]],
"next-page-token": "page2",
},
status_code=200,
request_headers=TEST_HEADERS,
)
# Second page (last)
rest_mock.get(
f"{TEST_URI}v1/namespaces?parent=accounting&pageToken=page2",
json={
"namespaces": [["accounting", "payroll"]],
},
status_code=200,
request_headers=TEST_HEADERS,
)

result = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN).list_namespaces(("accounting",))
assert result == [
("accounting", "tax"),
("accounting", "payroll"),
]


def test_list_namespaces_paginated_200_none_next_page_token(rest_mock: Mocker) -> None:
# First page with next-page-token
rest_mock.get(
f"{TEST_URI}v1/namespaces",
json={
"namespaces": [["ns1"], ["ns2"]],
"next-page-token": "page2token",
},
status_code=200,
request_headers=TEST_HEADERS,
)
# The last page with None next-page-token
rest_mock.get(
f"{TEST_URI}v1/namespaces?pageToken=page2token",
json={
"namespaces": [["ns3"]],
"next-page-token": None,
},
status_code=200,
request_headers=TEST_HEADERS,
)

result = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN).list_namespaces()
assert result == [
("ns1",),
("ns2",),
("ns3",),
]


def test_list_namespace_with_parent_404(rest_mock: Mocker) -> None:
rest_mock.get(
f"{TEST_URI}v1/namespaces?parent=some_namespace",
Expand Down
Loading