Skip to content

Commit

Permalink
🦉 Updates from OwlBot post-processor
Browse files Browse the repository at this point in the history
  • Loading branch information
gcf-owl-bot[bot] committed Jul 16, 2024
1 parent a2f827a commit b99f52b
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 32 deletions.
1 change: 1 addition & 0 deletions google/auth/_credentials_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from google.auth import _helpers


class _BaseCredentials(metaclass=abc.ABCMeta):
"""Base class for all credentials.
Expand Down
12 changes: 5 additions & 7 deletions google/auth/aio/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
7 changes: 2 additions & 5 deletions google/auth/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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."""
Expand Down
5 changes: 1 addition & 4 deletions google/auth/transport/_requests_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions google/oauth2/aio/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
16 changes: 7 additions & 9 deletions tests/oauth2/test_credentials_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 3 additions & 3 deletions tests/test_credentials_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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."
assert exc.value == "Refresh is not supported in StaticCredentials."

0 comments on commit b99f52b

Please sign in to comment.