Skip to content

Commit 14b8935

Browse files
arithmetic1728lidizheng
authored andcommitted
feat(api-core): add client_cert_source to ClientOptions
1 parent a2e9f90 commit 14b8935

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

google/api_core/client_options.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
from google.api_core.client_options import ClientOptions
2525
from google.cloud.vision_v1 import ImageAnnotatorClient
2626
27-
options = ClientOptions(api_endpoint="foo.googleapis.com")
27+
def get_client_cert():
28+
# code to load client certificate and private key.
29+
return client_cert_bytes, client_private_key_bytes
30+
31+
options = ClientOptions(api_endpoint="foo.googleapis.com",
32+
client_cert_source=get_client_cert)
2833
2934
client = ImageAnnotatorClient(client_options=options)
3035
@@ -34,7 +39,11 @@
3439
3540
from google.cloud.vision_v1 import ImageAnnotatorClient
3641
37-
client = ImageAnnotatorClient(client_options={"api_endpoint": "foo.googleapis.com"})
42+
client = ImageAnnotatorClient(
43+
client_options={
44+
"api_endpoint": "foo.googleapis.com",
45+
client_cert_source : get_client_cert
46+
})
3847
3948
4049
"""
@@ -45,10 +54,14 @@ class ClientOptions(object):
4554
4655
Args:
4756
api_endpoint (str): The desired API endpoint, e.g., compute.googleapis.com
57+
client_cert_source (Callable[[], (bytes, bytes)]): An optional callback
58+
which returns client certificate bytes and private key bytes both in
59+
PEM format.
4860
"""
4961

50-
def __init__(self, api_endpoint=None):
62+
def __init__(self, api_endpoint=None, client_cert_source=None):
5163
self.api_endpoint = api_endpoint
64+
self.client_cert_source = client_cert_source
5265

5366
def __repr__(self):
5467
return "ClientOptions: " + repr(self.__dict__)

tests/unit/test_client_options.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,46 @@
1717
from google.api_core import client_options
1818

1919

20+
def get_client_cert():
21+
return b"cert", b"key"
22+
23+
2024
def test_constructor():
21-
options = client_options.ClientOptions(api_endpoint="foo.googleapis.com")
25+
26+
options = client_options.ClientOptions(
27+
api_endpoint="foo.googleapis.com", client_cert_source=get_client_cert
28+
)
2229

2330
assert options.api_endpoint == "foo.googleapis.com"
31+
assert options.client_cert_source() == (b"cert", b"key")
2432

2533

2634
def test_from_dict():
27-
options = client_options.from_dict({"api_endpoint": "foo.googleapis.com"})
35+
options = client_options.from_dict(
36+
{"api_endpoint": "foo.googleapis.com", "client_cert_source": get_client_cert}
37+
)
2838

2939
assert options.api_endpoint == "foo.googleapis.com"
40+
# assert options.client_cert_source == get_client_cert
41+
assert options.client_cert_source() == (b"cert", b"key")
3042

3143

3244
def test_from_dict_bad_argument():
3345
with pytest.raises(ValueError):
3446
client_options.from_dict(
35-
{"api_endpoint": "foo.googleapis.com", "bad_arg": "1234"}
47+
{
48+
"api_endpoint": "foo.googleapis.com",
49+
"bad_arg": "1234",
50+
"client_cert_source": get_client_cert,
51+
}
3652
)
3753

3854

3955
def test_repr():
4056
options = client_options.ClientOptions(api_endpoint="foo.googleapis.com")
4157

42-
assert repr(options) == "ClientOptions: {'api_endpoint': 'foo.googleapis.com'}"
58+
assert (
59+
repr(options)
60+
== "ClientOptions: {'api_endpoint': 'foo.googleapis.com', 'client_cert_source': None}"
61+
or "ClientOptions: {'client_cert_source': None, 'api_endpoint': 'foo.googleapis.com'}"
62+
)

0 commit comments

Comments
 (0)