Skip to content

Commit 083b458

Browse files
committed
feat: support optional params in rest catalog
1 parent 38ebb19 commit 083b458

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

pyiceberg/catalog/rest/__init__.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
import re
1718
from enum import Enum
1819
from typing import (
1920
TYPE_CHECKING,
@@ -82,6 +83,9 @@
8283
ICEBERG_REST_SPEC_VERSION = "0.14.1"
8384

8485

86+
CAMEL_TO_SNAKE_CASE_PATTERN = re.compile(r"(?<!^)(?=[A-Z])")
87+
88+
8589
class Endpoints:
8690
get_config: str = "config"
8791
list_namespaces: str = "namespaces"
@@ -104,6 +108,28 @@ class Endpoints:
104108
view_exists: str = "namespaces/{namespace}/views/{view}"
105109

106110

111+
ENDPOINT_PARAMS_MAP: dict[str, tuple[str]] = {
112+
Endpoints.drop_table: ("purgeRequested",),
113+
}
114+
115+
116+
def _get_endpoint_params(endpoint: str, **kwargs: Any) -> dict[str, Any] | None:
117+
"""Get the query parameters for the endpoint."""
118+
if not kwargs or endpoint not in ENDPOINT_PARAMS_MAP:
119+
return None
120+
121+
snake_case_query_params = {
122+
param: CAMEL_TO_SNAKE_CASE_PATTERN.sub("_", param).lower() for param in ENDPOINT_PARAMS_MAP[endpoint]
123+
}
124+
125+
_params = {}
126+
for camel, snake in snake_case_query_params.items():
127+
if snake in kwargs:
128+
_params[camel] = kwargs[snake]
129+
130+
return _params
131+
132+
107133
class IdentifierKind(Enum):
108134
TABLE = "table"
109135
VIEW = "view"
@@ -584,7 +610,9 @@ def register_table(self, identifier: Union[str, Identifier], metadata_location:
584610
return self._response_to_table(self.identifier_to_tuple(identifier), table_response)
585611

586612
@retry(**_RETRY_ARGS)
587-
def list_tables(self, namespace: Union[str, Identifier]) -> List[Identifier]:
613+
def list_tables(
614+
self, namespace: Union[str, Identifier], page_token: str | None = None, page_size: int | None = None
615+
) -> List[Identifier]:
588616
namespace_tuple = self._check_valid_namespace_identifier(namespace)
589617
namespace_concat = NAMESPACE_SEPARATOR.join(namespace_tuple)
590618
response = self._session.get(self.url(Endpoints.list_tables, namespace=namespace_concat))
@@ -616,8 +644,9 @@ def load_table(self, identifier: Union[str, Identifier]) -> Table:
616644

617645
@retry(**_RETRY_ARGS)
618646
def drop_table(self, identifier: Union[str, Identifier], purge_requested: bool = False) -> None:
647+
params = _get_endpoint_params(Endpoints.drop_table, purge_requested=purge_requested)
619648
response = self._session.delete(
620-
self.url(Endpoints.drop_table, prefixed=True, purge=purge_requested, **self._split_identifier_for_path(identifier)),
649+
self.url(Endpoints.drop_table, prefixed=True, **self._split_identifier_for_path(identifier)), params=params
621650
)
622651
try:
623652
response.raise_for_status()

tests/catalog/test_rest.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import pyiceberg
2626
from pyiceberg.catalog import PropertiesUpdateSummary, load_catalog
27-
from pyiceberg.catalog.rest import OAUTH2_SERVER_URI, SNAPSHOT_LOADING_MODE, RestCatalog
27+
from pyiceberg.catalog.rest import OAUTH2_SERVER_URI, SNAPSHOT_LOADING_MODE, Endpoints, RestCatalog, _get_endpoint_params
2828
from pyiceberg.exceptions import (
2929
AuthorizationExpiredError,
3030
NamespaceAlreadyExistsError,
@@ -1621,3 +1621,10 @@ def test_drop_view_204(rest_mock: Mocker) -> None:
16211621
request_headers=TEST_HEADERS,
16221622
)
16231623
RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN).drop_view(("some_namespace", "some_view"))
1624+
1625+
1626+
def test_get_endpoint_params() -> None:
1627+
params = _get_endpoint_params(Endpoints.drop_table, purge_requested=True)
1628+
assert params == {
1629+
"purgeRequested": True,
1630+
}

0 commit comments

Comments
 (0)