Skip to content

Commit

Permalink
refactor: tweak code to not fail on older google-auth versions
Browse files Browse the repository at this point in the history
  • Loading branch information
busunkim96 committed Feb 3, 2021
1 parent ba36db6 commit 46d6483
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
58 changes: 44 additions & 14 deletions google/api_core/grpc_helpers.py
Expand Up @@ -216,29 +216,59 @@ def _create_composite_credentials(
)

if credentials_file:
credentials, _ = google.auth.load_credentials_from_file(
credentials_file,
scopes=scopes,
default_scopes=default_scopes
)
try:
credentials, _ = google.auth.load_credentials_from_file(
credentials_file,
scopes=scopes,
default_scopes=default_scopes
)
# google-auth < x.x.x does not have `default_scopes`
# TODO: remove this try/except once google-auth >= x.x.x is required
except TypeError:
credentials, _ = google.auth.load_credentials_from_file(
credentials_file,
scopes=scopes or default_scopes,
)
elif credentials:
credentials = google.auth.credentials.with_scopes_if_required(
credentials,
scopes=scopes,
default_scopes=default_scopes
)
try:
credentials = google.auth.credentials.with_scopes_if_required(
credentials,
scopes=scopes,
default_scopes=default_scopes
)
# google-auth < x.x.x does not have `default_scopes`
# TODO: remove this try/except once google-auth >= x.x.x is required
except TypeError:
credentials = google.auth.credentials.with_scopes_if_required(
credentials,
scopes=scopes or default_scopes,
)

else:
credentials, _ = google.auth.default(scopes=scopes, default_scopes=default_scopes)
try:
credentials, _ = google.auth.default(scopes=scopes, default_scopes=default_scopes)
# google-auth < x.x.x does not have `default_scopes`
# TODO: remove this try/except once google-auth >= x.x.x is required
except TypeError:
credentials, _ = google.auth.default(scopes=scopes or default_scopes)

if quota_project_id and isinstance(credentials, google.auth.credentials.CredentialsWithQuotaProject):
credentials = credentials.with_quota_project(quota_project_id)

request = google.auth.transport.requests.Request()

# Create the metadata plugin for inserting the authorization header.
metadata_plugin = google.auth.transport.grpc.AuthMetadataPlugin(
credentials, request, default_host=default_host,
)

# google-auth < x.x.x does not have `default_host`
# TODO: remove this try/except once google-auth >= x.x.x is required
try:
metadata_plugin = google.auth.transport.grpc.AuthMetadataPlugin(
credentials, request, default_host=default_host,
)
except:
metadata_plugin = google.auth.transport.grpc.AuthMetadataPlugin(
credentials, request
)

# Create a set of grpc.CallCredentials using the metadata plugin.
google_auth_credentials = grpc.metadata_call_credentials(metadata_plugin)
Expand Down
2 changes: 1 addition & 1 deletion noxfile.py
Expand Up @@ -51,7 +51,7 @@ def default(session):
session.install("-e", ".", "-c", constraints_path)

# REMOVE ME: Temporarily install google-auth from a branch
session.install("-e", "git+https://github.com/googleapis/google-auth-library-python.git@self-signed-jwt#egg=google-auth")
# session.install("-e", "git+https://github.com/googleapis/google-auth-library-python.git@self-signed-jwt#egg=google-auth")

pytest_args = [
"python",
Expand Down

0 comments on commit 46d6483

Please sign in to comment.