Skip to content

Commit 4834224

Browse files
authored
feat: adds support for audience in client_options in v1 branch (#411)
feat: add api_key to client options in v1 branch
1 parent 1e2f148 commit 4834224

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

google/api_core/client_options.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,19 @@ 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.
73+
api_audience (Optional[str]): The intended audience for the API calls
74+
to the service that will be set when using certain 3rd party
75+
authentication flows. Audience is typically a resource identifier.
76+
If not set, the service endpoint value will be used as a default.
77+
An example of a valid ``api_audience`` is: "https://language.googleapis.com".
7078
7179
Raises:
7280
ValueError: If both ``client_cert_source`` and ``client_encrypted_cert_source``
73-
are provided.
81+
are provided, or both ``credentials_file`` and ``api_key`` are provided.
7482
"""
7583

7684
def __init__(
@@ -81,17 +89,23 @@ def __init__(
8189
quota_project_id=None,
8290
credentials_file=None,
8391
scopes=None,
92+
api_key=None,
93+
api_audience=None,
8494
):
8595
if client_cert_source and client_encrypted_cert_source:
8696
raise ValueError(
8797
"client_cert_source and client_encrypted_cert_source are mutually exclusive"
8898
)
99+
if api_key and credentials_file:
100+
raise ValueError("api_key and credentials_file are mutually exclusive")
89101
self.api_endpoint = api_endpoint
90102
self.client_cert_source = client_cert_source
91103
self.client_encrypted_cert_source = client_encrypted_cert_source
92104
self.quota_project_id = quota_project_id
93105
self.credentials_file = credentials_file
94106
self.scopes = scopes
107+
self.api_key = api_key
108+
self.api_audience = api_audience
95109

96110
def __repr__(self):
97111
return "ClientOptions: " + repr(self.__dict__)

tests/unit/test_client_options.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def test_constructor():
3636
"https://www.googleapis.com/auth/cloud-platform",
3737
"https://www.googleapis.com/auth/cloud-platform.read-only",
3838
],
39+
api_audience="foo2.googleapis.com",
3940
)
4041

4142
assert options.api_endpoint == "foo.googleapis.com"
@@ -46,6 +47,7 @@ def test_constructor():
4647
"https://www.googleapis.com/auth/cloud-platform",
4748
"https://www.googleapis.com/auth/cloud-platform.read-only",
4849
]
50+
assert options.api_audience == "foo2.googleapis.com"
4951

5052

5153
def test_constructor_with_encrypted_cert_source():
@@ -72,6 +74,36 @@ def test_constructor_with_both_cert_sources():
7274
)
7375

7476

77+
def test_constructor_with_api_key():
78+
79+
options = client_options.ClientOptions(
80+
api_endpoint="foo.googleapis.com",
81+
client_cert_source=get_client_cert,
82+
quota_project_id="quote-proj",
83+
api_key="api-key",
84+
scopes=[
85+
"https://www.googleapis.com/auth/cloud-platform",
86+
"https://www.googleapis.com/auth/cloud-platform.read-only",
87+
],
88+
)
89+
90+
assert options.api_endpoint == "foo.googleapis.com"
91+
assert options.client_cert_source() == (b"cert", b"key")
92+
assert options.quota_project_id == "quote-proj"
93+
assert options.api_key == "api-key"
94+
assert options.scopes == [
95+
"https://www.googleapis.com/auth/cloud-platform",
96+
"https://www.googleapis.com/auth/cloud-platform.read-only",
97+
]
98+
99+
100+
def test_constructor_with_both_api_key_and_credentials_file():
101+
with pytest.raises(ValueError):
102+
client_options.ClientOptions(
103+
api_key="api-key", credentials_file="path/to/credentials.json",
104+
)
105+
106+
75107
def test_from_dict():
76108
options = client_options.from_dict(
77109
{
@@ -83,6 +115,7 @@ def test_from_dict():
83115
"https://www.googleapis.com/auth/cloud-platform",
84116
"https://www.googleapis.com/auth/cloud-platform.read-only",
85117
],
118+
"api_audience": "foo2.googleapis.com",
86119
}
87120
)
88121

@@ -94,6 +127,8 @@ def test_from_dict():
94127
"https://www.googleapis.com/auth/cloud-platform",
95128
"https://www.googleapis.com/auth/cloud-platform.read-only",
96129
]
130+
assert options.api_key is None
131+
assert options.api_audience == "foo2.googleapis.com"
97132

98133

99134
def test_from_dict_bad_argument():
@@ -112,6 +147,6 @@ def test_repr():
112147

113148
assert (
114149
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'}"
150+
== "ClientOptions: {'api_endpoint': 'foo.googleapis.com', 'client_cert_source': None, 'client_encrypted_cert_source': None, 'api_key': None}"
151+
or "ClientOptions: {'client_encrypted_cert_source': None, 'client_cert_source': None, 'api_endpoint': 'foo.googleapis.com', 'api_key': None}"
117152
)

0 commit comments

Comments
 (0)