From b99f52b5d7bd44289b4e54ea0dbdf672298d5858 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Tue, 16 Jul 2024 18:16:35 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20post-p?= =?UTF-8?q?rocessor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- google/auth/_credentials_base.py | 1 + google/auth/aio/credentials.py | 12 +++++------- google/auth/credentials.py | 7 ++----- google/auth/transport/_requests_base.py | 5 +---- google/oauth2/aio/credentials.py | 10 ++++++---- tests/oauth2/test_credentials_async.py | 16 +++++++--------- tests/test_credentials_async.py | 6 +++--- 7 files changed, 25 insertions(+), 32 deletions(-) diff --git a/google/auth/_credentials_base.py b/google/auth/_credentials_base.py index 78ee983ce..29462dc0c 100644 --- a/google/auth/_credentials_base.py +++ b/google/auth/_credentials_base.py @@ -19,6 +19,7 @@ from google.auth import _helpers + class _BaseCredentials(metaclass=abc.ABCMeta): """Base class for all credentials. diff --git a/google/auth/aio/credentials.py b/google/auth/aio/credentials.py index f0a2d50ba..f4a9df285 100644 --- a/google/auth/aio/credentials.py +++ b/google/auth/aio/credentials.py @@ -17,9 +17,7 @@ from google.auth import _helpers -from google.auth._credentials_base import ( - _BaseCredentials -) +from google.auth._credentials_base import _BaseCredentials class Credentials(_BaseCredentials): @@ -66,7 +64,7 @@ async def refresh(self, request): not be refreshed. """ raise NotImplementedError("Refresh must be implemented") - + async def before_request(self, request, method, url, headers): """Performs credential-specific before request logic. @@ -96,15 +94,15 @@ class StaticCredentials(Credentials): token is valid and not expired. StaticCredentials will never attempt to refresh the token. """ - + def __init__(self, token): super(StaticCredentials, self).__init__() self.token = token - + @_helpers.copy_docstring(Credentials) async def refresh(self, request): raise NotImplementedError("Refresh is not supported in StaticCredentials.") - + @_helpers.copy_docstring(Credentials) async def before_request(self, request, method, url, headers): await self.apply(headers) diff --git a/google/auth/credentials.py b/google/auth/credentials.py index fafe22684..eb55202b1 100644 --- a/google/auth/credentials.py +++ b/google/auth/credentials.py @@ -23,10 +23,7 @@ from google.auth import exceptions from google.auth import metrics from google.auth._refresh_worker import RefreshThreadManager -from google.auth._credentials_base import ( - - _BaseCredentials -) +from google.auth._credentials_base import _BaseCredentials DEFAULT_UNIVERSE_DOMAIN = "googleapis.com" @@ -52,7 +49,7 @@ class Credentials(_BaseCredentials): def __init__(self): super(Credentials, self).__init__() - + self.expiry = None """Optional[datetime]: When the token expires and is no longer valid. If this is None, the token is assumed to never expire.""" diff --git a/google/auth/transport/_requests_base.py b/google/auth/transport/_requests_base.py index 3a9f851d0..ec718d909 100644 --- a/google/auth/transport/_requests_base.py +++ b/google/auth/transport/_requests_base.py @@ -31,10 +31,7 @@ class _BaseAuthorizedSession(metaclass=abc.ABCMeta): add to the request. """ - def __init__( - self, - credentials, - ): + def __init__(self, credentials): self.credentials = credentials @abc.abstractmethod diff --git a/google/oauth2/aio/credentials.py b/google/oauth2/aio/credentials.py index 68fd2c5e7..7080ece05 100644 --- a/google/oauth2/aio/credentials.py +++ b/google/oauth2/aio/credentials.py @@ -43,17 +43,19 @@ def __init__(self): super(Credentials, self).__init__() async def refresh(self, request): - raise NotImplementedError("refresh is currently not supported for OAuth 2.0 access tokens.") + raise NotImplementedError( + "refresh is currently not supported for OAuth 2.0 access tokens." + ) -class CredentialsBuilder(): +class CredentialsBuilder: """Builder class for constructing Asynchronous Credentials using OAuth 2.0 access and refresh tokens. """ def __init__(self): self.credentials = Credentials() - + def setToken(self, token): """Sets the OAuth 2.0 access token. @@ -67,7 +69,7 @@ def setToken(self, token): # def setExpiry(self, expiry=None): # self.credentials.expiry = expiry # return self - + def build(self): """Constructs and returns google.oauth2.aio.credentials.Credentials object. diff --git a/tests/oauth2/test_credentials_async.py b/tests/oauth2/test_credentials_async.py index ac1efd84c..bbf0ae845 100644 --- a/tests/oauth2/test_credentials_async.py +++ b/tests/oauth2/test_credentials_async.py @@ -30,24 +30,22 @@ class TestCredentials(object): - - @pytest.mark.asyncio def test_default_state(self): - credentials = (credentials_async.CredentialsBuilder() - .setToken(token=None) - .build()) + credentials = ( + credentials_async.CredentialsBuilder().setToken(token=None).build() + ) assert credentials.token is None @pytest.mark.asyncio async def test_token_usage_metrics(self): - credentials = (credentials_async.CredentialsBuilder() - .setToken(token="token") - .build()) + credentials = ( + credentials_async.CredentialsBuilder().setToken(token="token").build() + ) headers = {} await credentials.before_request(mock.Mock(), None, None, headers) assert headers["authorization"] == "Bearer token" - + # TODO(ohmayr): The below header can be tested once usage metrics # are implemented in google.oauth2.aio.credentials. # assert headers["x-goog-api-client"] == "cred-type/u" diff --git a/tests/test_credentials_async.py b/tests/test_credentials_async.py index b370cbe15..023037d0d 100644 --- a/tests/test_credentials_async.py +++ b/tests/test_credentials_async.py @@ -41,7 +41,7 @@ async def test_before_request(): request = "token2" headers = {} - + # Second call shouldn't affect token or headers. await credentials.before_request(request, "http://example.com", "GET", headers) assert credentials.token == "orchid" @@ -63,7 +63,7 @@ async def test_static_credentials_before_request(): request = "token2" headers = {} - + # Second call shouldn't affect token or headers. await static_creds.before_request(request, "http://example.com", "GET", headers) assert static_creds.token == "orchid" @@ -78,4 +78,4 @@ async def test_static_credentials_refresh(): with pytest.raises(NotImplementedError) as exc: await static_creds.refresh(request) - assert exc.value == "Refresh is not supported in StaticCredentials." \ No newline at end of file + assert exc.value == "Refresh is not supported in StaticCredentials."