Skip to content

Commit c81d7f4

Browse files
sisppmrowla
authored andcommitted
lfs: authenticate with Git credentials only to Batch API requests
1 parent 87a9e91 commit c81d7f4

File tree

1 file changed

+26
-35
lines changed

1 file changed

+26
-35
lines changed

src/scmrepo/git/lfs/client.py

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import logging
2-
from collections.abc import Awaitable, Iterable
2+
from collections.abc import Iterable
33
from contextlib import AbstractContextManager
4-
from functools import wraps
5-
from typing import TYPE_CHECKING, Any, Callable, Optional
4+
from typing import TYPE_CHECKING, Any, Optional
65

76
import aiohttp
87
from dvc_http import HTTPFileSystem
@@ -32,29 +31,6 @@ def _prepare_credentials(self, **config):
3231
return {}
3332

3433

35-
def _authed(f: Callable[..., Awaitable]):
36-
"""Set credentials and retry the given coroutine if needed."""
37-
38-
# pylint: disable=protected-access
39-
@wraps(f) # type: ignore[arg-type]
40-
async def wrapper(self, *args, **kwargs):
41-
try:
42-
return await f(self, *args, **kwargs)
43-
except aiohttp.ClientResponseError as exc:
44-
if exc.status != 401:
45-
raise
46-
session = await self._set_session()
47-
if session.auth:
48-
raise
49-
auth = self._get_auth()
50-
if auth is None:
51-
raise
52-
self._session._auth = auth
53-
return await f(self, *args, **kwargs)
54-
55-
return wrapper
56-
57-
5834
class LFSClient(AbstractContextManager):
5935
"""Naive read-only LFS HTTP client."""
6036

@@ -112,7 +88,6 @@ def _get_auth(self) -> Optional[aiohttp.BasicAuth]:
11288
async def _set_session(self) -> aiohttp.ClientSession:
11389
return await self.fs.fs.set_session()
11490

115-
@_authed
11691
async def _batch_request(
11792
self,
11893
objects: Iterable[Pointer],
@@ -134,14 +109,30 @@ async def _batch_request(
134109
headers = dict(self.headers)
135110
headers["Accept"] = self.JSON_CONTENT_TYPE
136111
headers["Content-Type"] = self.JSON_CONTENT_TYPE
137-
async with session.post(
138-
url,
139-
headers=headers,
140-
json=body,
141-
) as resp:
142-
return await resp.json()
143-
144-
@_authed
112+
try:
113+
async with session.post(
114+
url,
115+
headers=headers,
116+
json=body,
117+
raise_for_status=True,
118+
) as resp:
119+
data = await resp.json()
120+
except aiohttp.ClientResponseError as exc:
121+
if exc.status != 401:
122+
raise
123+
auth = self._get_auth()
124+
if auth is None:
125+
raise
126+
async with session.post(
127+
url,
128+
auth=auth,
129+
headers=headers,
130+
json=body,
131+
raise_for_status=True,
132+
) as resp:
133+
data = await resp.json()
134+
return data
135+
145136
async def _download(
146137
self,
147138
storage: "LFSStorage",

0 commit comments

Comments
 (0)