Skip to content

Commit 46d6483

Browse files
committed
refactor: tweak code to not fail on older google-auth versions
1 parent ba36db6 commit 46d6483

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

google/api_core/grpc_helpers.py

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -216,29 +216,59 @@ def _create_composite_credentials(
216216
)
217217

218218
if credentials_file:
219-
credentials, _ = google.auth.load_credentials_from_file(
220-
credentials_file,
221-
scopes=scopes,
222-
default_scopes=default_scopes
223-
)
219+
try:
220+
credentials, _ = google.auth.load_credentials_from_file(
221+
credentials_file,
222+
scopes=scopes,
223+
default_scopes=default_scopes
224+
)
225+
# google-auth < x.x.x does not have `default_scopes`
226+
# TODO: remove this try/except once google-auth >= x.x.x is required
227+
except TypeError:
228+
credentials, _ = google.auth.load_credentials_from_file(
229+
credentials_file,
230+
scopes=scopes or default_scopes,
231+
)
224232
elif credentials:
225-
credentials = google.auth.credentials.with_scopes_if_required(
226-
credentials,
227-
scopes=scopes,
228-
default_scopes=default_scopes
229-
)
233+
try:
234+
credentials = google.auth.credentials.with_scopes_if_required(
235+
credentials,
236+
scopes=scopes,
237+
default_scopes=default_scopes
238+
)
239+
# google-auth < x.x.x does not have `default_scopes`
240+
# TODO: remove this try/except once google-auth >= x.x.x is required
241+
except TypeError:
242+
credentials = google.auth.credentials.with_scopes_if_required(
243+
credentials,
244+
scopes=scopes or default_scopes,
245+
)
246+
230247
else:
231-
credentials, _ = google.auth.default(scopes=scopes, default_scopes=default_scopes)
248+
try:
249+
credentials, _ = google.auth.default(scopes=scopes, default_scopes=default_scopes)
250+
# google-auth < x.x.x does not have `default_scopes`
251+
# TODO: remove this try/except once google-auth >= x.x.x is required
252+
except TypeError:
253+
credentials, _ = google.auth.default(scopes=scopes or default_scopes)
232254

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

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

238260
# Create the metadata plugin for inserting the authorization header.
239-
metadata_plugin = google.auth.transport.grpc.AuthMetadataPlugin(
240-
credentials, request, default_host=default_host,
241-
)
261+
262+
# google-auth < x.x.x does not have `default_host`
263+
# TODO: remove this try/except once google-auth >= x.x.x is required
264+
try:
265+
metadata_plugin = google.auth.transport.grpc.AuthMetadataPlugin(
266+
credentials, request, default_host=default_host,
267+
)
268+
except:
269+
metadata_plugin = google.auth.transport.grpc.AuthMetadataPlugin(
270+
credentials, request
271+
)
242272

243273
# Create a set of grpc.CallCredentials using the metadata plugin.
244274
google_auth_credentials = grpc.metadata_call_credentials(metadata_plugin)

noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def default(session):
5151
session.install("-e", ".", "-c", constraints_path)
5252

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

5656
pytest_args = [
5757
"python",

0 commit comments

Comments
 (0)