Skip to content

Commit 2591963

Browse files
committed
Fix: Simplify retry handling
1 parent 7a41532 commit 2591963

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Version 2.0.9
2+
3+
:Date: November 25, 2025
4+
5+
* Fix: Simplify retry handling in token refresh to avoid nested retry
6+
17
Version 2.0.8
28
-------------
39

src/obelisk/asynchronous/base.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,56 +61,55 @@ async def _get_token(self) -> None:
6161

6262
async with httpx.AsyncClient() as client:
6363
request: httpx.Response | None = None
64-
response = None
64+
response: dict[Any, str] | None = None
6565
last_error = None
6666
retry = self.retry_strategy.make()
67-
while not response or await retry.should_retry():
67+
while not response:
6868
try:
6969
request = await client.post(
7070
self.kind.token_url,
7171
json=payload if self.kind.use_json_auth else None,
7272
data=payload if not self.kind.use_json_auth else None,
7373
headers=headers,
7474
)
75-
76-
response = request.json()
75+
if request:
76+
response = request.json()
77+
break
7778
except Exception as e: # noqa: PERF203 # retry strategy should add delay
7879
last_error = e
7980
self.log.error(e)
80-
continue
81+
82+
if not await retry.should_retry():
83+
break
8184

8285
if response is None and last_error is not None:
8386
raise last_error
8487

8588
if request is None:
8689
raise last_error or ObeliskError("Could not create HTTP request")
8790

88-
if request.status_code != 200:
89-
if "error" in response:
91+
if request.status_code != 200 or not response:
92+
if response and "error" in response:
9093
self.log.warning(f"Could not authenticate, {response['error']}")
9194
raise AuthenticationError
9295

9396
self._token = response["access_token"]
9497
self._token_expires = datetime.now() + timedelta(
95-
seconds=response["expires_in"]
98+
seconds=float(response["expires_in"])
9699
)
97100

98101
async def _verify_token(self) -> None:
99102
if (
100103
self._token is None
101104
or self._token_expires is None
102-
or self._token_expires >= (datetime.now() - self.grace_period)
105+
or self._token_expires <= (datetime.now() - self.grace_period)
103106
):
104-
retry = self.retry_strategy.make()
105-
first = True
106-
while first or await retry.should_retry():
107-
first = False
108-
try:
109-
await self._get_token()
110-
return
111-
except: # noqa: E722
112-
self.log.info("excepted, Retrying token fetch")
113-
continue
107+
try:
108+
await self._get_token()
109+
return
110+
except: # noqa: E722
111+
# Retry is handled insdie get_token()
112+
self.log.info("excepted, Cannot get token!")
114113

115114
async def http_post(
116115
self, url: str, data: Any = None, params: dict[str, str] | None = None
@@ -187,7 +186,7 @@ async def http_get(
187186
response = None
188187
retry = self.retry_strategy.make()
189188
last_error = None
190-
while not response or await retry.should_retry():
189+
while not response:
191190
if response is not None:
192191
self.log.debug(f"Retrying, last response: {response.status_code}")
193192

@@ -203,8 +202,12 @@ async def http_get(
203202
except Exception as e:
204203
self.log.error(e)
205204
last_error = e
206-
continue
205+
206+
if not await retry.should_retry():
207+
break
207208

208209
if not response and last_error:
209210
raise last_error
210-
return response
211+
if response:
212+
return response
213+
raise ObeliskError("Could not complete HTTP operation")

0 commit comments

Comments
 (0)