Skip to content

Commit 7fbcfed

Browse files
committed
http: refactor auth loop
1 parent c0b7726 commit 7fbcfed

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

vdirsyncer/http.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ async def request(
261261
kwargs["ssl"] = ssl_context
262262

263263
headers = kwargs.pop("headers", {})
264-
num_401 = 0
265-
while num_401 < 2:
264+
response: aiohttp.ClientResponse | None = None
265+
for _attempt in range(2):
266266
if auth:
267267
headers["Authorization"] = auth.get_auth_header(method, url)
268268
try:
@@ -281,17 +281,24 @@ async def request(
281281
raise TransientNetworkError(str(e)) from e
282282
raise e from None
283283

284+
if response is None:
285+
raise RuntimeError("No HTTP response obtained")
286+
284287
if response.ok or not auth:
285288
# we don't need to do the 401-loop if we don't do auth in the first place
286289
break
287290

288291
if response.status == 401:
289-
num_401 += 1
290292
auth.handle_401(response)
293+
# retry once more after handling the 401 challenge
294+
continue
291295
else:
292296
# some other error, will be handled later on
293297
break
294298

299+
if response is None:
300+
raise RuntimeError("No HTTP response obtained")
301+
295302
# See https://github.com/kennethreitz/requests/issues/2042
296303
content_type = response.headers.get("Content-Type", "")
297304
if (

0 commit comments

Comments
 (0)