Skip to content

Commit b414a10

Browse files
committed
feat(storage): add async credential wrapper
1 parent 4e91c54 commit b414a10

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Async Wrapper around Google Auth Credentials"""
2+
3+
import asyncio
4+
from google.auth.aio import credentials as async_creds
5+
from google.auth.transport.requests import Request
6+
7+
class AsyncCredsWrapper(async_creds.Credentials):
8+
"""Wraps synchronous Google Auth credentials to provide an asynchronous interface.
9+
10+
This class adapts standard synchronous `google.auth.credentials.Credentials` for use
11+
in asynchronous contexts. It offloads blocking operations, such as token refreshes,
12+
to a separate thread using `asyncio.loop.run_in_executor`.
13+
14+
Args:
15+
sync_creds (google.auth.credentials.Credentials): The synchronous credentials
16+
instance to wrap.
17+
"""
18+
19+
def __init__(self, sync_creds):
20+
super().__init__()
21+
self.creds = sync_creds
22+
self._loop = asyncio.get_event_loop()
23+
24+
async def refresh(self, _request):
25+
await self._loop.run_in_executor(
26+
None, self.creds.refresh, Request()
27+
)
28+
29+
async def before_request(self, _request, method, url, headers):
30+
if self.creds.valid:
31+
self.creds.apply(headers)
32+
return
33+
34+
await self._loop.run_in_executor(
35+
None, self.creds.before_request, Request(), method, url, headers
36+
)

0 commit comments

Comments
 (0)