Skip to content

Commit a582936

Browse files
feat(client_options): add new client options 'quota_project_id', 'scopes', and 'credentials_file'
Co-authored-by: Bu Sun Kim <[email protected]>
1 parent 33ab7fa commit a582936

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

google/api_core/client_options.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,20 @@ class ClientOptions(object):
5353
"""Client Options used to set options on clients.
5454
5555
Args:
56-
api_endpoint (str): The desired API endpoint, e.g., compute.googleapis.com
57-
client_cert_source (Callable[[], (bytes, bytes)]): An optional callback
56+
api_endpoint (Optional[str]): The desired API endpoint, e.g.,
57+
compute.googleapis.com
58+
client_cert_source (Optional[Callable[[], (bytes, bytes)]]): A callback
5859
which returns client certificate bytes and private key bytes both in
5960
PEM format. ``client_cert_source`` and ``client_encrypted_cert_source``
6061
are mutually exclusive.
61-
client_encrypted_cert_source (Callable[[], (str, str, bytes)]): An optional
62-
callback which returns client certificate file path, encrypted private
63-
key file path, and the passphrase bytes.``client_cert_source`` and
64-
``client_encrypted_cert_source`` are mutually exclusive.
62+
client_encrypted_cert_source (Optional[Callable[[], (str, str, bytes)]]):
63+
A callback which returns client certificate file path, encrypted
64+
private key file path, and the passphrase bytes.``client_cert_source``
65+
and ``client_encrypted_cert_source`` are mutually exclusive.
66+
quota_project_id (Optional[str]): A project name that a client's
67+
quota belongs to.
68+
credentials_file (Optional[str]): A path to a file storing credentials.
69+
scopes (Optional[Sequence[str]]): OAuth access token override scopes.
6570
6671
Raises:
6772
ValueError: If both ``client_cert_source`` and ``client_encrypted_cert_source``
@@ -73,6 +78,9 @@ def __init__(
7378
api_endpoint=None,
7479
client_cert_source=None,
7580
client_encrypted_cert_source=None,
81+
quota_project_id=None,
82+
credentials_file=None,
83+
scopes=None,
7684
):
7785
if client_cert_source and client_encrypted_cert_source:
7886
raise ValueError(
@@ -81,6 +89,9 @@ def __init__(
8189
self.api_endpoint = api_endpoint
8290
self.client_cert_source = client_cert_source
8391
self.client_encrypted_cert_source = client_encrypted_cert_source
92+
self.quota_project_id = quota_project_id
93+
self.credentials_file = credentials_file
94+
self.scopes = scopes
8495

8596
def __repr__(self):
8697
return "ClientOptions: " + repr(self.__dict__)
@@ -90,7 +101,8 @@ def from_dict(options):
90101
"""Construct a client options object from a dictionary.
91102
92103
Args:
93-
options (dict): A dictionary with client options.
104+
options (Dict[str, Any]): A dictionary with client options.
105+
See the docstring for ClientOptions for details on valid arguments.
94106
"""
95107

96108
client_options = ClientOptions()

tests/unit/test_client_options.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,24 @@ def get_client_encrypted_cert():
2828
def test_constructor():
2929

3030
options = client_options.ClientOptions(
31-
api_endpoint="foo.googleapis.com", client_cert_source=get_client_cert
31+
api_endpoint="foo.googleapis.com",
32+
client_cert_source=get_client_cert,
33+
quota_project_id="quote-proj",
34+
credentials_file="path/to/credentials.json",
35+
scopes=[
36+
"https://www.googleapis.com/auth/cloud-platform",
37+
"https://www.googleapis.com/auth/cloud-platform.read-only",
38+
]
3239
)
3340

3441
assert options.api_endpoint == "foo.googleapis.com"
3542
assert options.client_cert_source() == (b"cert", b"key")
43+
assert options.quota_project_id == "quote-proj"
44+
assert options.credentials_file == "path/to/credentials.json"
45+
assert options.scopes == [
46+
"https://www.googleapis.com/auth/cloud-platform",
47+
"https://www.googleapis.com/auth/cloud-platform.read-only",
48+
]
3649

3750

3851
def test_constructor_with_encrypted_cert_source():
@@ -61,12 +74,26 @@ def test_constructor_with_both_cert_sources():
6174

6275
def test_from_dict():
6376
options = client_options.from_dict(
64-
{"api_endpoint": "foo.googleapis.com", "client_cert_source": get_client_cert}
77+
{
78+
"api_endpoint": "foo.googleapis.com",
79+
"client_cert_source": get_client_cert,
80+
"quota_project_id": "quote-proj",
81+
"credentials_file": "path/to/credentials.json",
82+
"scopes": [
83+
"https://www.googleapis.com/auth/cloud-platform",
84+
"https://www.googleapis.com/auth/cloud-platform.read-only",
85+
]
86+
}
6587
)
6688

6789
assert options.api_endpoint == "foo.googleapis.com"
68-
# assert options.client_cert_source == get_client_cert
6990
assert options.client_cert_source() == (b"cert", b"key")
91+
assert options.quota_project_id == "quote-proj"
92+
assert options.credentials_file == "path/to/credentials.json"
93+
assert options.scopes == [
94+
"https://www.googleapis.com/auth/cloud-platform",
95+
"https://www.googleapis.com/auth/cloud-platform.read-only",
96+
]
7097

7198

7299
def test_from_dict_bad_argument():

0 commit comments

Comments
 (0)