Skip to content

Commit 128015e

Browse files
committed
only enable feature if auth library has support
1 parent 274d056 commit 128015e

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

google/cloud/client/__init__.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,18 @@
2525
import google.api_core.exceptions
2626
import google.auth
2727
from google.auth import environment_vars
28-
import google.auth.api_key
2928
import google.auth.credentials
3029
import google.auth.transport.requests
3130
from google.cloud._helpers import _determine_default_project
3231
from google.oauth2 import service_account
3332

33+
try:
34+
import google.auth.api_key
35+
36+
HAS_GOOGLE_AUTH_API_KEY = True
37+
except:
38+
HAS_GOOGLE_AUTH_API_KEY = False
39+
3440

3541
_GOOGLE_AUTH_CREDENTIALS_HELP = (
3642
"This library only supports credentials from google-auth-library-python. "
@@ -162,7 +168,11 @@ def __init__(self, credentials=None, _http=None, client_options=None):
162168
"'credentials' and 'client_options.credentials_file' are mutually exclusive."
163169
)
164170

165-
if client_options.api_key and (credentials or client_options.credentials_file):
171+
if (
172+
HAS_GOOGLE_AUTH_API_KEY
173+
and client_options.api_key
174+
and (credentials or client_options.credentials_file)
175+
):
166176
raise google.api_core.exceptions.DuplicateCredentialArgs(
167177
"'client_options.api_key' is mutually exclusive with 'credentials' and 'client_options.credentials_file'."
168178
)
@@ -180,7 +190,7 @@ def __init__(self, credentials=None, _http=None, client_options=None):
180190
credentials, _ = google.auth.load_credentials_from_file(
181191
client_options.credentials_file, scopes=scopes
182192
)
183-
elif client_options.api_key is not None:
193+
elif HAS_GOOGLE_AUTH_API_KEY and client_options.api_key is not None:
184194
credentials = google.auth.api_key.Credentials(client_options.api_key)
185195
else:
186196
credentials, _ = google.auth.default(scopes=scopes)

tests/unit/test_client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import unittest
1818
from unittest import mock
1919

20+
from google.cloud.client import HAS_GOOGLE_AUTH_API_KEY
21+
2022

2123
def _make_credentials():
2224
import google.auth.credentials
@@ -75,6 +77,9 @@ def test_ctor_explicit(self):
7577
self.assertIs(client_obj._credentials, credentials)
7678
self.assertIs(client_obj._http_internal, http)
7779

80+
@unittest.skipIf(
81+
not HAS_GOOGLE_AUTH_API_KEY, "Auth library version does not support API keys"
82+
)
7883
def test_ctor_client_options_w_api_key(self):
7984
from google.auth.api_key import Credentials
8085

@@ -93,6 +98,9 @@ def test_ctor_client_options_w_conflicting_creds(self):
9398
with self.assertRaises(DuplicateCredentialArgs):
9499
self._make_one(credentials=credentials, client_options=client_options)
95100

101+
@unittest.skipIf(
102+
not HAS_GOOGLE_AUTH_API_KEY, "Auth library version does not support API keys"
103+
)
96104
def test_ctor_client_options_w_api_key_and_conflicting_creds(self):
97105
from google.api_core.exceptions import DuplicateCredentialArgs
98106

0 commit comments

Comments
 (0)