Skip to content

Commit 4ac1df7

Browse files
arithmetic1728parthea
authored andcommitted
feat: add api_key to client options (#248)
* feat: add api_key to client options * update
1 parent 1e2f148 commit 4ac1df7

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

google/api_core/client_options.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,14 @@ class ClientOptions(object):
6666
quota_project_id (Optional[str]): A project name that a client's
6767
quota belongs to.
6868
credentials_file (Optional[str]): A path to a file storing credentials.
69+
``credentials_file` and ``api_key`` are mutually exclusive.
6970
scopes (Optional[Sequence[str]]): OAuth access token override scopes.
71+
api_key (Optional[str]): Google API key. ``credentials_file`` and
72+
``api_key`` are mutually exclusive.
7073
7174
Raises:
7275
ValueError: If both ``client_cert_source`` and ``client_encrypted_cert_source``
73-
are provided.
76+
are provided, or both ``credentials_file`` and ``api_key`` are provided.
7477
"""
7578

7679
def __init__(
@@ -81,17 +84,21 @@ def __init__(
8184
quota_project_id=None,
8285
credentials_file=None,
8386
scopes=None,
87+
api_key=None,
8488
):
8589
if client_cert_source and client_encrypted_cert_source:
8690
raise ValueError(
8791
"client_cert_source and client_encrypted_cert_source are mutually exclusive"
8892
)
93+
if api_key and credentials_file:
94+
raise ValueError("api_key and credentials_file are mutually exclusive")
8995
self.api_endpoint = api_endpoint
9096
self.client_cert_source = client_cert_source
9197
self.client_encrypted_cert_source = client_encrypted_cert_source
9298
self.quota_project_id = quota_project_id
9399
self.credentials_file = credentials_file
94100
self.scopes = scopes
101+
self.api_key = api_key
95102

96103
def __repr__(self):
97104
return "ClientOptions: " + repr(self.__dict__)

tests/unit/test_client_options.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,36 @@ def test_constructor_with_both_cert_sources():
7272
)
7373

7474

75+
def test_constructor_with_api_key():
76+
77+
options = client_options.ClientOptions(
78+
api_endpoint="foo.googleapis.com",
79+
client_cert_source=get_client_cert,
80+
quota_project_id="quote-proj",
81+
api_key="api-key",
82+
scopes=[
83+
"https://www.googleapis.com/auth/cloud-platform",
84+
"https://www.googleapis.com/auth/cloud-platform.read-only",
85+
],
86+
)
87+
88+
assert options.api_endpoint == "foo.googleapis.com"
89+
assert options.client_cert_source() == (b"cert", b"key")
90+
assert options.quota_project_id == "quote-proj"
91+
assert options.api_key == "api-key"
92+
assert options.scopes == [
93+
"https://www.googleapis.com/auth/cloud-platform",
94+
"https://www.googleapis.com/auth/cloud-platform.read-only",
95+
]
96+
97+
98+
def test_constructor_with_both_api_key_and_credentials_file():
99+
with pytest.raises(ValueError):
100+
client_options.ClientOptions(
101+
api_key="api-key", credentials_file="path/to/credentials.json",
102+
)
103+
104+
75105
def test_from_dict():
76106
options = client_options.from_dict(
77107
{
@@ -94,6 +124,7 @@ def test_from_dict():
94124
"https://www.googleapis.com/auth/cloud-platform",
95125
"https://www.googleapis.com/auth/cloud-platform.read-only",
96126
]
127+
assert options.api_key is None
97128

98129

99130
def test_from_dict_bad_argument():
@@ -112,6 +143,6 @@ def test_repr():
112143

113144
assert (
114145
repr(options)
115-
== "ClientOptions: {'api_endpoint': 'foo.googleapis.com', 'client_cert_source': None, 'client_encrypted_cert_source': None}"
116-
or "ClientOptions: {'client_encrypted_cert_source': None, 'client_cert_source': None, 'api_endpoint': 'foo.googleapis.com'}"
146+
== "ClientOptions: {'api_endpoint': 'foo.googleapis.com', 'client_cert_source': None, 'client_encrypted_cert_source': None, 'api_key': None}"
147+
or "ClientOptions: {'client_encrypted_cert_source': None, 'client_cert_source': None, 'api_endpoint': 'foo.googleapis.com', 'api_key': None}"
117148
)

0 commit comments

Comments
 (0)