forked from googleapis/python-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_creds.py
More file actions
57 lines (46 loc) · 1.74 KB
/
Copy pathasync_creds.py
File metadata and controls
57 lines (46 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Async Wrapper around Google Auth Credentials"""
import asyncio
from google.auth.transport.requests import Request
try:
from google.auth.aio import credentials as aio_creds_module
BaseCredentials = aio_creds_module.Credentials
_AIO_AVAILABLE = True
except ImportError:
BaseCredentials = object
_AIO_AVAILABLE = False
class AsyncCredsWrapper(BaseCredentials):
"""Wraps synchronous Google Auth credentials to provide an asynchronous interface.
Args:
sync_creds (google.auth.credentials.Credentials): The synchronous credentials
instance to wrap.
Raises:
ImportError: If instantiated in an environment where 'google.auth.aio'
is not available.
"""
def __init__(self, sync_creds):
if not _AIO_AVAILABLE:
raise ImportError(
"Failed to import 'google.auth.aio'. This module requires a newer version "
"of 'google-auth' which supports asyncio."
)
super().__init__()
self.creds = sync_creds
async def refresh(self, request):
"""Refreshes the access token."""
loop = asyncio.get_running_loop()
await loop.run_in_executor(
None, self.creds.refresh, Request()
)
@property
def valid(self):
"""Checks the validity of the credentials."""
return self.creds.valid
async def before_request(self, request, method, url, headers):
"""Performs credential-specific before request logic."""
if self.valid:
self.creds.apply(headers)
return
loop = asyncio.get_running_loop()
await loop.run_in_executor(
None, self.creds.before_request, Request(), method, url, headers
)