Skip to content

Commit

Permalink
cleanup: minor code cleanup (#1589)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
6 people authored Aug 24, 2024
1 parent 3945c44 commit c63a5ac
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
8 changes: 2 additions & 6 deletions google/auth/aio/transport/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,9 @@
"""Sequence[int]: HTTP status codes indicating a request can be retried.
"""

DEFAULT_REFRESH_STATUS_CODES = google.auth.transport.DEFAULT_REFRESH_STATUS_CODES
"""Sequence[int]: Which HTTP status code indicate that credentials should be
refreshed.
"""

DEFAULT_MAX_REFRESH_ATTEMPTS = 3
"""int: How many times to refresh the credentials and retry a request."""
DEFAULT_MAX_RETRY_ATTEMPTS = 3
"""int: How many times to retry a request."""


class Response(metaclass=abc.ABCMeta):
Expand Down
10 changes: 5 additions & 5 deletions google/auth/aio/transport/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Transport adapter for AIOHTTP Requests.
"""Transport adapter for Asynchronous HTTP Requests based on aiohttp.
"""

import asyncio
Expand All @@ -32,7 +32,7 @@

class Response(transport.Response):
"""
Represents an HTTP response and its data. It is returned by ``google.auth.aio.transport.sessions.AuthorizedSession``.
Represents an HTTP response and its data. It is returned by ``google.auth.aio.transport.sessions.AsyncAuthorizedSession``.
Args:
response (aiohttp.ClientResponse): An instance of aiohttp.ClientResponse.
Expand Down Expand Up @@ -83,8 +83,8 @@ class Request(transport.Request):
"""Asynchronous Requests request adapter.
This class is used internally for making requests using aiohttp
in a consistent way. If you use :class:`AuthorizedSession` you do not need
to construct or use this class directly.
in a consistent way. If you use :class:`google.auth.aio.transport.sessions.AsyncAuthorizedSession`
you do not need to construct or use this class directly.
This class can be useful if you want to configure a Request callable
with a custom ``aiohttp.ClientSession`` in :class:`AuthorizedSession` or if
Expand All @@ -100,7 +100,7 @@ class Request(transport.Request):
# Custom aiohttp Session Example:
session = session=aiohttp.ClientSession(auto_decompress=False)
request = google.auth.aio.transport.aiohttp.Request(session=session)
auth_sesion = google.auth.aio.transport.sessions.AuthorizedSession(auth_request=request)
auth_sesion = google.auth.aio.transport.sessions.AsyncAuthorizedSession(auth_request=request)
Args:
session (aiohttp.ClientSession): An instance :class:`aiohttp.ClientSession` used
Expand Down
6 changes: 3 additions & 3 deletions google/auth/aio/transport/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async def with_timeout(coro):
_remaining_time()


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

retries = _exponential_backoff.AsyncExponentialBackoff(
total_attempts=transport.DEFAULT_MAX_REFRESH_ATTEMPTS
total_attempts=transport.DEFAULT_MAX_RETRY_ATTEMPTS
)
async with timeout_guard(max_allowed_time) as with_timeout:
await with_timeout(
Expand Down
38 changes: 20 additions & 18 deletions tests/transport/aio/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from google.auth.aio.credentials import AnonymousCredentials
from google.auth.aio.transport import (
_DEFAULT_TIMEOUT_SECONDS,
DEFAULT_MAX_REFRESH_ATTEMPTS,
DEFAULT_MAX_RETRY_ATTEMPTS,
DEFAULT_RETRYABLE_STATUS_CODES,
Request,
Response,
Expand Down Expand Up @@ -146,7 +146,7 @@ async def test_timeout_with_async_task_timing_out_before_context(
)


class TestAuthorizedSession(object):
class TestAsyncAuthorizedSession(object):
TEST_URL = "http://example.com/"
credentials = AnonymousCredentials()

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

@pytest.mark.asyncio
async def test_constructor_with_provided_auth_request(self):
auth_request = MockRequest()
authed_session = sessions.AuthorizedSession(
authed_session = sessions.AsyncAuthorizedSession(
self.credentials, auth_request=auth_request
)

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

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

exc.match(
f"The configured credentials of type {type(credentials)} are invalid and must be of type `google.auth.aio.credentials.Credentials`"
Expand All @@ -199,7 +199,7 @@ async def test_request_default_auth_request_success(self):
mocked_chunks = [b"Cavefish ", b"have ", b"no ", b"sight."]
mocked_response = b"".join(mocked_chunks)
m.get(self.TEST_URL, status=200, body=mocked_response)
authed_session = sessions.AuthorizedSession(self.credentials)
authed_session = sessions.AsyncAuthorizedSession(self.credentials)
response = await authed_session.request("GET", self.TEST_URL)
assert response.status_code == 200
assert response.headers == {"Content-Type": "application/json"}
Expand All @@ -216,7 +216,7 @@ async def test_request_provided_auth_request_success(self, mocked_content):
content=mocked_content,
)
auth_request = MockRequest(mocked_response)
authed_session = sessions.AuthorizedSession(self.credentials, auth_request)
authed_session = sessions.AsyncAuthorizedSession(self.credentials, auth_request)
response = await authed_session.request("GET", self.TEST_URL)
assert response.status_code == 200
assert response.headers == {"Content-Type": "application/json"}
Expand All @@ -229,21 +229,21 @@ async def test_request_provided_auth_request_success(self, mocked_content):
@pytest.mark.asyncio
async def test_request_raises_timeout_error(self):
auth_request = MockRequest(side_effect=asyncio.TimeoutError)
authed_session = sessions.AuthorizedSession(self.credentials, auth_request)
authed_session = sessions.AsyncAuthorizedSession(self.credentials, auth_request)
with pytest.raises(TimeoutError):
await authed_session.request("GET", self.TEST_URL)

@pytest.mark.asyncio
async def test_request_raises_transport_error(self):
auth_request = MockRequest(side_effect=TransportError)
authed_session = sessions.AuthorizedSession(self.credentials, auth_request)
authed_session = sessions.AsyncAuthorizedSession(self.credentials, auth_request)
with pytest.raises(TransportError):
await authed_session.request("GET", self.TEST_URL)

@pytest.mark.asyncio
async def test_request_max_allowed_time_exceeded_error(self):
auth_request = MockRequest(side_effect=TransportError)
authed_session = sessions.AuthorizedSession(self.credentials, auth_request)
authed_session = sessions.AsyncAuthorizedSession(self.credentials, auth_request)
with patch("time.monotonic", side_effect=[0, 1, 1]):
with pytest.raises(TimeoutError):
await authed_session.request("GET", self.TEST_URL, max_allowed_time=1)
Expand All @@ -254,14 +254,16 @@ async def test_request_max_retries(self, retry_status):
mocked_response = MockResponse(status_code=retry_status)
auth_request = MockRequest(mocked_response)
with patch("asyncio.sleep", return_value=None):
authed_session = sessions.AuthorizedSession(self.credentials, auth_request)
authed_session = sessions.AsyncAuthorizedSession(
self.credentials, auth_request
)
await authed_session.request("GET", self.TEST_URL)
assert auth_request.call_count == DEFAULT_MAX_REFRESH_ATTEMPTS
assert auth_request.call_count == DEFAULT_MAX_RETRY_ATTEMPTS

@pytest.mark.asyncio
async def test_http_get_method_success(self):
expected_payload = b"content is retrieved."
authed_session = sessions.AuthorizedSession(self.credentials)
authed_session = sessions.AsyncAuthorizedSession(self.credentials)
with aioresponses() as m:
m.get(self.TEST_URL, status=200, body=expected_payload)
response = await authed_session.get(self.TEST_URL)
Expand All @@ -271,7 +273,7 @@ async def test_http_get_method_success(self):
@pytest.mark.asyncio
async def test_http_post_method_success(self):
expected_payload = b"content is posted."
authed_session = sessions.AuthorizedSession(self.credentials)
authed_session = sessions.AsyncAuthorizedSession(self.credentials)
with aioresponses() as m:
m.post(self.TEST_URL, status=200, body=expected_payload)
response = await authed_session.post(self.TEST_URL)
Expand All @@ -281,7 +283,7 @@ async def test_http_post_method_success(self):
@pytest.mark.asyncio
async def test_http_put_method_success(self):
expected_payload = b"content is retrieved."
authed_session = sessions.AuthorizedSession(self.credentials)
authed_session = sessions.AsyncAuthorizedSession(self.credentials)
with aioresponses() as m:
m.put(self.TEST_URL, status=200, body=expected_payload)
response = await authed_session.put(self.TEST_URL)
Expand All @@ -291,7 +293,7 @@ async def test_http_put_method_success(self):
@pytest.mark.asyncio
async def test_http_patch_method_success(self):
expected_payload = b"content is retrieved."
authed_session = sessions.AuthorizedSession(self.credentials)
authed_session = sessions.AsyncAuthorizedSession(self.credentials)
with aioresponses() as m:
m.patch(self.TEST_URL, status=200, body=expected_payload)
response = await authed_session.patch(self.TEST_URL)
Expand All @@ -301,7 +303,7 @@ async def test_http_patch_method_success(self):
@pytest.mark.asyncio
async def test_http_delete_method_success(self):
expected_payload = b"content is deleted."
authed_session = sessions.AuthorizedSession(self.credentials)
authed_session = sessions.AsyncAuthorizedSession(self.credentials)
with aioresponses() as m:
m.delete(self.TEST_URL, status=200, body=expected_payload)
response = await authed_session.delete(self.TEST_URL)
Expand Down

0 comments on commit c63a5ac

Please sign in to comment.