Skip to content

Commit c63a5ac

Browse files
ohmayrclundin25release-please[bot]tommilliganandyrzhao
authored
cleanup: minor code cleanup (#1589)
* chore: Add aiohttp requirements test constraint. (#1566) See #1565 for more information. * chore(main): release 2.33.0 (#1560) * chore(main): release 2.33.0 * fix: retry token request on retryable status code (#1563) * fix: retry token request on retryable status code * feat(auth): Update get_client_ssl_credentials to support X.509 workload certs (#1558) * feat(auth): Update get_client_ssl_credentials to support X.509 workload certs * feat(auth): Update has_default_client_cert_source * feat(auth): Fix formatting * feat(auth): Fix test__mtls_helper.py * feat(auth): Fix function name in tests * chore: Refresh system test creds. * feat(auth): Fix style * feat(auth): Fix casing * feat(auth): Fix linter issue * feat(auth): Fix coverage issue --------- Co-authored-by: Carl Lundin <[email protected]> Co-authored-by: Carl Lundin <[email protected]> * chore: Update ECP deps. (#1583) * chore(main): release 2.34.0 (#1574) * cleanup: minor code cleanup * fix lint issues --------- Co-authored-by: Carl Lundin <[email protected]> Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com> Co-authored-by: Tom Milligan <[email protected]> Co-authored-by: Andy Zhao <[email protected]> Co-authored-by: Carl Lundin <[email protected]>
1 parent 3945c44 commit c63a5ac

File tree

4 files changed

+30
-32
lines changed

4 files changed

+30
-32
lines changed

google/auth/aio/transport/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,9 @@
3636
"""Sequence[int]: HTTP status codes indicating a request can be retried.
3737
"""
3838

39-
DEFAULT_REFRESH_STATUS_CODES = google.auth.transport.DEFAULT_REFRESH_STATUS_CODES
40-
"""Sequence[int]: Which HTTP status code indicate that credentials should be
41-
refreshed.
42-
"""
4339

44-
DEFAULT_MAX_REFRESH_ATTEMPTS = 3
45-
"""int: How many times to refresh the credentials and retry a request."""
40+
DEFAULT_MAX_RETRY_ATTEMPTS = 3
41+
"""int: How many times to retry a request."""
4642

4743

4844
class Response(metaclass=abc.ABCMeta):

google/auth/aio/transport/aiohttp.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""Transport adapter for AIOHTTP Requests.
15+
"""Transport adapter for Asynchronous HTTP Requests based on aiohttp.
1616
"""
1717

1818
import asyncio
@@ -32,7 +32,7 @@
3232

3333
class Response(transport.Response):
3434
"""
35-
Represents an HTTP response and its data. It is returned by ``google.auth.aio.transport.sessions.AuthorizedSession``.
35+
Represents an HTTP response and its data. It is returned by ``google.auth.aio.transport.sessions.AsyncAuthorizedSession``.
3636
3737
Args:
3838
response (aiohttp.ClientResponse): An instance of aiohttp.ClientResponse.
@@ -83,8 +83,8 @@ class Request(transport.Request):
8383
"""Asynchronous Requests request adapter.
8484
8585
This class is used internally for making requests using aiohttp
86-
in a consistent way. If you use :class:`AuthorizedSession` you do not need
87-
to construct or use this class directly.
86+
in a consistent way. If you use :class:`google.auth.aio.transport.sessions.AsyncAuthorizedSession`
87+
you do not need to construct or use this class directly.
8888
8989
This class can be useful if you want to configure a Request callable
9090
with a custom ``aiohttp.ClientSession`` in :class:`AuthorizedSession` or if
@@ -100,7 +100,7 @@ class Request(transport.Request):
100100
# Custom aiohttp Session Example:
101101
session = session=aiohttp.ClientSession(auto_decompress=False)
102102
request = google.auth.aio.transport.aiohttp.Request(session=session)
103-
auth_sesion = google.auth.aio.transport.sessions.AuthorizedSession(auth_request=request)
103+
auth_sesion = google.auth.aio.transport.sessions.AsyncAuthorizedSession(auth_request=request)
104104
105105
Args:
106106
session (aiohttp.ClientSession): An instance :class:`aiohttp.ClientSession` used

google/auth/aio/transport/sessions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ async def with_timeout(coro):
7575
_remaining_time()
7676

7777

78-
class AuthorizedSession:
78+
class AsyncAuthorizedSession:
7979
"""This is an asynchronous implementation of :class:`google.auth.requests.AuthorizedSession` class.
8080
We utilize an instance of a class that implements :class:`google.auth.aio.transport.Request` configured
8181
by the caller or otherwise default to `google.auth.aio.transport.aiohttp.Request` if the external aiohttp
@@ -89,7 +89,7 @@ class AuthorizedSession:
8989
import aiohttp
9090
from google.auth.aio.transport import sessions
9191
92-
async with sessions.AuthorizedSession(credentials) as authed_session:
92+
async with sessions.AsyncAuthorizedSession(credentials) as authed_session:
9393
response = await authed_session.request(
9494
'GET', 'https://www.googleapis.com/storage/v1/b')
9595
@@ -172,7 +172,7 @@ async def request(
172172
"""
173173

174174
retries = _exponential_backoff.AsyncExponentialBackoff(
175-
total_attempts=transport.DEFAULT_MAX_REFRESH_ATTEMPTS
175+
total_attempts=transport.DEFAULT_MAX_RETRY_ATTEMPTS
176176
)
177177
async with timeout_guard(max_allowed_time) as with_timeout:
178178
await with_timeout(

tests/transport/aio/test_sessions.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from google.auth.aio.credentials import AnonymousCredentials
2323
from google.auth.aio.transport import (
2424
_DEFAULT_TIMEOUT_SECONDS,
25-
DEFAULT_MAX_REFRESH_ATTEMPTS,
25+
DEFAULT_MAX_RETRY_ATTEMPTS,
2626
DEFAULT_RETRYABLE_STATUS_CODES,
2727
Request,
2828
Response,
@@ -146,7 +146,7 @@ async def test_timeout_with_async_task_timing_out_before_context(
146146
)
147147

148148

149-
class TestAuthorizedSession(object):
149+
class TestAsyncAuthorizedSession(object):
150150
TEST_URL = "http://example.com/"
151151
credentials = AnonymousCredentials()
152152

@@ -159,14 +159,14 @@ async def mocked_content(self):
159159
@pytest.mark.asyncio
160160
async def test_constructor_with_default_auth_request(self):
161161
with patch("google.auth.aio.transport.sessions.AIOHTTP_INSTALLED", True):
162-
authed_session = sessions.AuthorizedSession(self.credentials)
162+
authed_session = sessions.AsyncAuthorizedSession(self.credentials)
163163
assert authed_session._credentials == self.credentials
164164
await authed_session.close()
165165

166166
@pytest.mark.asyncio
167167
async def test_constructor_with_provided_auth_request(self):
168168
auth_request = MockRequest()
169-
authed_session = sessions.AuthorizedSession(
169+
authed_session = sessions.AsyncAuthorizedSession(
170170
self.credentials, auth_request=auth_request
171171
)
172172

@@ -177,7 +177,7 @@ async def test_constructor_with_provided_auth_request(self):
177177
async def test_constructor_raises_no_auth_request_error(self):
178178
with patch("google.auth.aio.transport.sessions.AIOHTTP_INSTALLED", False):
179179
with pytest.raises(TransportError) as exc:
180-
sessions.AuthorizedSession(self.credentials)
180+
sessions.AsyncAuthorizedSession(self.credentials)
181181

182182
exc.match(
183183
"`auth_request` must either be configured or the external package `aiohttp` must be installed to use the default value."
@@ -187,7 +187,7 @@ async def test_constructor_raises_no_auth_request_error(self):
187187
async def test_constructor_raises_incorrect_credentials_error(self):
188188
credentials = Mock()
189189
with pytest.raises(InvalidType) as exc:
190-
sessions.AuthorizedSession(credentials)
190+
sessions.AsyncAuthorizedSession(credentials)
191191

192192
exc.match(
193193
f"The configured credentials of type {type(credentials)} are invalid and must be of type `google.auth.aio.credentials.Credentials`"
@@ -199,7 +199,7 @@ async def test_request_default_auth_request_success(self):
199199
mocked_chunks = [b"Cavefish ", b"have ", b"no ", b"sight."]
200200
mocked_response = b"".join(mocked_chunks)
201201
m.get(self.TEST_URL, status=200, body=mocked_response)
202-
authed_session = sessions.AuthorizedSession(self.credentials)
202+
authed_session = sessions.AsyncAuthorizedSession(self.credentials)
203203
response = await authed_session.request("GET", self.TEST_URL)
204204
assert response.status_code == 200
205205
assert response.headers == {"Content-Type": "application/json"}
@@ -216,7 +216,7 @@ async def test_request_provided_auth_request_success(self, mocked_content):
216216
content=mocked_content,
217217
)
218218
auth_request = MockRequest(mocked_response)
219-
authed_session = sessions.AuthorizedSession(self.credentials, auth_request)
219+
authed_session = sessions.AsyncAuthorizedSession(self.credentials, auth_request)
220220
response = await authed_session.request("GET", self.TEST_URL)
221221
assert response.status_code == 200
222222
assert response.headers == {"Content-Type": "application/json"}
@@ -229,21 +229,21 @@ async def test_request_provided_auth_request_success(self, mocked_content):
229229
@pytest.mark.asyncio
230230
async def test_request_raises_timeout_error(self):
231231
auth_request = MockRequest(side_effect=asyncio.TimeoutError)
232-
authed_session = sessions.AuthorizedSession(self.credentials, auth_request)
232+
authed_session = sessions.AsyncAuthorizedSession(self.credentials, auth_request)
233233
with pytest.raises(TimeoutError):
234234
await authed_session.request("GET", self.TEST_URL)
235235

236236
@pytest.mark.asyncio
237237
async def test_request_raises_transport_error(self):
238238
auth_request = MockRequest(side_effect=TransportError)
239-
authed_session = sessions.AuthorizedSession(self.credentials, auth_request)
239+
authed_session = sessions.AsyncAuthorizedSession(self.credentials, auth_request)
240240
with pytest.raises(TransportError):
241241
await authed_session.request("GET", self.TEST_URL)
242242

243243
@pytest.mark.asyncio
244244
async def test_request_max_allowed_time_exceeded_error(self):
245245
auth_request = MockRequest(side_effect=TransportError)
246-
authed_session = sessions.AuthorizedSession(self.credentials, auth_request)
246+
authed_session = sessions.AsyncAuthorizedSession(self.credentials, auth_request)
247247
with patch("time.monotonic", side_effect=[0, 1, 1]):
248248
with pytest.raises(TimeoutError):
249249
await authed_session.request("GET", self.TEST_URL, max_allowed_time=1)
@@ -254,14 +254,16 @@ async def test_request_max_retries(self, retry_status):
254254
mocked_response = MockResponse(status_code=retry_status)
255255
auth_request = MockRequest(mocked_response)
256256
with patch("asyncio.sleep", return_value=None):
257-
authed_session = sessions.AuthorizedSession(self.credentials, auth_request)
257+
authed_session = sessions.AsyncAuthorizedSession(
258+
self.credentials, auth_request
259+
)
258260
await authed_session.request("GET", self.TEST_URL)
259-
assert auth_request.call_count == DEFAULT_MAX_REFRESH_ATTEMPTS
261+
assert auth_request.call_count == DEFAULT_MAX_RETRY_ATTEMPTS
260262

261263
@pytest.mark.asyncio
262264
async def test_http_get_method_success(self):
263265
expected_payload = b"content is retrieved."
264-
authed_session = sessions.AuthorizedSession(self.credentials)
266+
authed_session = sessions.AsyncAuthorizedSession(self.credentials)
265267
with aioresponses() as m:
266268
m.get(self.TEST_URL, status=200, body=expected_payload)
267269
response = await authed_session.get(self.TEST_URL)
@@ -271,7 +273,7 @@ async def test_http_get_method_success(self):
271273
@pytest.mark.asyncio
272274
async def test_http_post_method_success(self):
273275
expected_payload = b"content is posted."
274-
authed_session = sessions.AuthorizedSession(self.credentials)
276+
authed_session = sessions.AsyncAuthorizedSession(self.credentials)
275277
with aioresponses() as m:
276278
m.post(self.TEST_URL, status=200, body=expected_payload)
277279
response = await authed_session.post(self.TEST_URL)
@@ -281,7 +283,7 @@ async def test_http_post_method_success(self):
281283
@pytest.mark.asyncio
282284
async def test_http_put_method_success(self):
283285
expected_payload = b"content is retrieved."
284-
authed_session = sessions.AuthorizedSession(self.credentials)
286+
authed_session = sessions.AsyncAuthorizedSession(self.credentials)
285287
with aioresponses() as m:
286288
m.put(self.TEST_URL, status=200, body=expected_payload)
287289
response = await authed_session.put(self.TEST_URL)
@@ -291,7 +293,7 @@ async def test_http_put_method_success(self):
291293
@pytest.mark.asyncio
292294
async def test_http_patch_method_success(self):
293295
expected_payload = b"content is retrieved."
294-
authed_session = sessions.AuthorizedSession(self.credentials)
296+
authed_session = sessions.AsyncAuthorizedSession(self.credentials)
295297
with aioresponses() as m:
296298
m.patch(self.TEST_URL, status=200, body=expected_payload)
297299
response = await authed_session.patch(self.TEST_URL)
@@ -301,7 +303,7 @@ async def test_http_patch_method_success(self):
301303
@pytest.mark.asyncio
302304
async def test_http_delete_method_success(self):
303305
expected_payload = b"content is deleted."
304-
authed_session = sessions.AuthorizedSession(self.credentials)
306+
authed_session = sessions.AsyncAuthorizedSession(self.credentials)
305307
with aioresponses() as m:
306308
m.delete(self.TEST_URL, status=200, body=expected_payload)
307309
response = await authed_session.delete(self.TEST_URL)

0 commit comments

Comments
 (0)