Skip to content

Commit a268f83

Browse files
authored
Fix: REST Catalog should retry on 401 status code (#1741)
This is a follow up item from a recent discussion on the mailing list[1], where the community decided that 401 response should be preferred over 419 response on token expiry. [1] mailing list discussion: https://lists.apache.org/thread/443skhqr59j3fj0ovg4tyxh9d4f4gysc [2] REST Catalog Spec update PR: apache/iceberg#12376
1 parent f942551 commit a268f83

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

pyiceberg/catalog/rest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def _retry_hook(retry_state: RetryCallState) -> None:
148148

149149

150150
_RETRY_ARGS = {
151-
"retry": retry_if_exception_type(AuthorizationExpiredError),
151+
"retry": retry_if_exception_type((AuthorizationExpiredError, UnauthorizedError)),
152152
"stop": stop_after_attempt(2),
153153
"before_sleep": _retry_hook,
154154
"reraise": True,

tests/catalog/test_rest.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,8 @@ def test_list_namespace_with_parent_200(rest_mock: Mocker) -> None:
558558
@pytest.mark.filterwarnings(
559559
"ignore:Deprecated in 0.8.0, will be removed in 1.0.0. Iceberg REST client is missing the OAuth2 server URI:DeprecationWarning"
560560
)
561-
def test_list_namespaces_token_expired(rest_mock: Mocker) -> None:
561+
@pytest.mark.parametrize("status_code", [401, 419])
562+
def test_list_namespaces_token_expired_success_on_retries(rest_mock: Mocker, status_code: int) -> None:
562563
new_token = "new_jwt_token"
563564
new_header = dict(TEST_HEADERS)
564565
new_header["Authorization"] = f"Bearer {new_token}"
@@ -568,12 +569,12 @@ def test_list_namespaces_token_expired(rest_mock: Mocker) -> None:
568569
f"{TEST_URI}v1/namespaces",
569570
[
570571
{
571-
"status_code": 419,
572+
"status_code": status_code,
572573
"json": {
573574
"error": {
574575
"message": "Authorization expired.",
575576
"type": "AuthorizationExpiredError",
576-
"code": 419,
577+
"code": status_code,
577578
}
578579
},
579580
"headers": TEST_HEADERS,

0 commit comments

Comments
 (0)