Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit ae799f0

Browse files
chore: Refractors with respect to gemini review comments and keeping only AuthorizedSession in the tests
Signed-off-by: Radhika Agrawal <agrawalradhika@google.com>
1 parent 8269318 commit ae799f0

5 files changed

Lines changed: 17 additions & 27 deletions

File tree

google/auth/aio/transport/aiohttp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class Request(transport.Request):
113113
.. automethod:: __call__
114114
"""
115115

116-
def __init__(self, session: aiohttp.ClientSession = None):
116+
def __init__(self, session: Optional[aiohttp.ClientSession] = None):
117117
self._session = session
118118
self._closed = False
119119

google/auth/aio/transport/mtls.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ def _create_temp_file(content: bytes):
4343
str: The path to the temporary file.
4444
"""
4545
# Create a temporary file that is readable only by the owner.
46-
fd, path = tempfile.mkstemp()
46+
fd, file_path = tempfile.mkstemp()
4747
try:
4848
with os.fdopen(fd, "wb") as f:
4949
f.write(content)
50-
yield path
50+
yield file_path
5151
finally:
5252
# Securely delete the file after use.
53-
if os.path.exists(path):
54-
os.remove(path)
53+
if os.path.exists(file_path):
54+
os.remove(file_path)
5555

5656

5757
def make_client_cert_ssl_context(
@@ -140,7 +140,7 @@ def default_client_cert_source():
140140
client certificate bytes and private key bytes, both in PEM format.
141141
142142
Raises:
143-
google.auth.exceptions.DefaultClientCertSourceError: If the default
143+
google.auth.exceptions.MutualTLSChannelError: If the default
144144
client SSL credentials don't exist or are malformed.
145145
"""
146146
if not has_default_client_cert_source():
@@ -215,13 +215,11 @@ async def get_client_cert_and_key(client_cert_callback=None):
215215
the cert and key.
216216
"""
217217
if client_cert_callback:
218-
try:
219-
# If it's awaitable, this works.
220-
cert, key = await client_cert_callback()
221-
except TypeError:
222-
# If it's not awaitable (e.g., a tuple), result is already the data.
223-
cert, key = client_cert_callback()
224-
218+
result = client_cert_callback()
219+
if asyncio.iscoroutine(result):
220+
cert, key = await result
221+
else:
222+
cert, key = result
225223
return True, cert, key
226224

227225
has_cert, cert, key, _ = await get_client_ssl_credentials()

google/auth/aio/transport/sessions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def __init__(
128128
if not _auth_request and AIOHTTP_INSTALLED:
129129
_auth_request = AiohttpRequest()
130130
self._is_mtls = False
131-
self._cached_Cert = None
131+
self._cached_cert = None
132132
if _auth_request is None:
133133
raise exceptions.TransportError(
134134
"`auth_request` must either be configured or the external package `aiohttp` must be installed to use the default value."
@@ -177,7 +177,7 @@ async def configure_mtls_channel(self, client_cert_callback=None):
177177
)
178178

179179
# Re-create the auth request with the new SSL context
180-
if isinstance(self._auth_request, AiohttpRequest):
180+
if AIOHTTP_INSTALLED and isinstance(self._auth_request, AiohttpRequest):
181181
connector = aiohttp.TCPConnector(ssl=ssl_context)
182182
new_session = aiohttp.ClientSession(connector=connector)
183183
await self._auth_request.close()

noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def blacken(session):
9191
@nox.session(python=DEFAULT_PYTHON_VERSION)
9292
def mypy(session):
9393
"""Verify type hints are mypy compatible."""
94-
session.install("-e", ".")
94+
session.install("-e", ".[aiohttp]")
9595
session.install(
9696
"mypy",
9797
"types-certifi",

tests/transport/aio/test_sessions_mtls.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ async def test_configure_mtls_channel(
5656

5757
mock_creds = mock.Mock(spec=credentials.Credentials)
5858
session = sessions.AsyncAuthorizedSession(mock_creds)
59-
6059
await session.configure_mtls_channel()
6160

6261
assert session._is_mtls is True
@@ -71,10 +70,7 @@ async def test_configure_mtls_channel_disabled(self, mock_exists):
7170
mock_exists.return_value = False
7271
mock_creds = mock.Mock(spec=credentials.Credentials)
7372

74-
try:
75-
session = sessions.AsyncAuthorizedSession(mock_creds)
76-
except AttributeError:
77-
session = sessions.Session()
73+
session = sessions.AsyncAuthorizedSession(mock_creds)
7874
await session.configure_mtls_channel()
7975

8076
# If the file doesn't exist, it shouldn't error; it just won't use mTLS
@@ -92,13 +88,9 @@ async def test_configure_mtls_channel_invalid_format(self, mock_file, mock_exist
9288
mock_exists.return_value = True
9389
mock_creds = mock.Mock(spec=credentials.Credentials)
9490

95-
try:
96-
session = sessions.AsyncAuthorizedSession(mock_creds)
97-
except AttributeError:
98-
session = sessions.Session()
91+
session = sessions.AsyncAuthorizedSession(mock_creds)
9992
with pytest.raises(
100-
exceptions.MutualTLSChannelError, match="is in an invalid format"
101-
):
93+
exceptions.MutualTLSChannelError):
10294
await session.configure_mtls_channel()
10395

10496
@pytest.mark.asyncio

0 commit comments

Comments
 (0)