Skip to content

Commit 1f433a4

Browse files
authored
Fix retrying logic (#480)
1 parent 6a34421 commit 1f433a4

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

pyiceberg/catalog/rest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def _retry_hook(retry_state: RetryCallState) -> None:
128128
_RETRY_ARGS = {
129129
"retry": retry_if_exception_type(AuthorizationExpiredError),
130130
"stop": stop_after_attempt(2),
131-
"before": _retry_hook,
131+
"before_sleep": _retry_hook,
132132
"reraise": True,
133133
}
134134

@@ -446,10 +446,10 @@ def _response_to_table(self, identifier_tuple: Tuple[str, ...], table_response:
446446
catalog=self,
447447
)
448448

449-
def _refresh_token(self, session: Optional[Session] = None, new_token: Optional[str] = None) -> None:
449+
def _refresh_token(self, session: Optional[Session] = None, initial_token: Optional[str] = None) -> None:
450450
session = session or self._session
451-
if new_token is not None:
452-
self.properties[TOKEN] = new_token
451+
if initial_token is not None:
452+
self.properties[TOKEN] = initial_token
453453
elif CREDENTIAL in self.properties:
454454
self.properties[TOKEN] = self._fetch_access_token(session, self.properties[CREDENTIAL])
455455

tests/catalog/test_rest.py

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -306,24 +306,39 @@ def test_list_namespace_with_parent_200(rest_mock: Mocker) -> None:
306306
]
307307

308308

309-
def test_list_namespaces_419(rest_mock: Mocker) -> None:
309+
def test_list_namespaces_token_expired(rest_mock: Mocker) -> None:
310310
new_token = "new_jwt_token"
311311
new_header = dict(TEST_HEADERS)
312312
new_header["Authorization"] = f"Bearer {new_token}"
313313

314-
rest_mock.post(
314+
namespaces = rest_mock.register_uri(
315+
"GET",
315316
f"{TEST_URI}v1/namespaces",
316-
json={
317-
"error": {
318-
"message": "Authorization expired.",
319-
"type": "AuthorizationExpiredError",
320-
"code": 419,
321-
}
322-
},
323-
status_code=419,
324-
request_headers=TEST_HEADERS,
325-
)
326-
rest_mock.post(
317+
[
318+
{
319+
"status_code": 419,
320+
"json": {
321+
"error": {
322+
"message": "Authorization expired.",
323+
"type": "AuthorizationExpiredError",
324+
"code": 419,
325+
}
326+
},
327+
"headers": TEST_HEADERS,
328+
},
329+
{
330+
"status_code": 200,
331+
"json": {"namespaces": [["default"], ["examples"], ["fokko"], ["system"]]},
332+
"headers": new_header,
333+
},
334+
{
335+
"status_code": 200,
336+
"json": {"namespaces": [["default"], ["examples"], ["fokko"], ["system"]]},
337+
"headers": new_header,
338+
},
339+
],
340+
)
341+
tokens = rest_mock.post(
327342
f"{TEST_URI}v1/oauth/tokens",
328343
json={
329344
"access_token": new_token,
@@ -333,19 +348,24 @@ def test_list_namespaces_419(rest_mock: Mocker) -> None:
333348
},
334349
status_code=200,
335350
)
336-
rest_mock.get(
337-
f"{TEST_URI}v1/namespaces",
338-
json={"namespaces": [["default"], ["examples"], ["fokko"], ["system"]]},
339-
status_code=200,
340-
request_headers=new_header,
341-
)
342351
catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN, credential=TEST_CREDENTIALS)
343352
assert catalog.list_namespaces() == [
344353
("default",),
345354
("examples",),
346355
("fokko",),
347356
("system",),
348357
]
358+
assert namespaces.call_count == 2
359+
assert tokens.call_count == 1
360+
361+
assert catalog.list_namespaces() == [
362+
("default",),
363+
("examples",),
364+
("fokko",),
365+
("system",),
366+
]
367+
assert namespaces.call_count == 3
368+
assert tokens.call_count == 1
349369

350370

351371
def test_create_namespace_200(rest_mock: Mocker) -> None:

0 commit comments

Comments
 (0)