Skip to content

Commit 748c935

Browse files
feat(api-core): add client_cert_source to ClientOptions (googleapis#17)
* feat(api-core): add client_cert_source to ClientOptions * Update google/api_core/client_options.py Co-Authored-By: Bu Sun Kim <[email protected]> * bump google-auth version * update noxfile.py to fix docs problem Co-authored-by: Bu Sun Kim <[email protected]>
1 parent 0c2c556 commit 748c935

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
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__)

noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def docs(session):
112112

113113
session.install(".", "grpcio >= 1.8.2", "grpcio-gcp >= 0.2.2")
114114
session.install("-e", ".")
115-
session.install("sphinx", "alabaster", "recommonmark")
115+
session.install("sphinx < 3.0", "alabaster", "recommonmark")
116116

117117
shutil.rmtree(os.path.join("docs", "_build"), ignore_errors=True)
118118
session.run(

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
dependencies = [
3232
"googleapis-common-protos >= 1.6.0, < 2.0dev",
3333
"protobuf >= 3.4.0",
34-
"google-auth >= 0.4.0, < 2.0dev",
34+
"google-auth >= 1.14.0, < 2.0dev",
3535
"requests >= 2.18.0, < 3.0.0dev",
3636
"setuptools >= 34.0.0",
3737
"six >= 1.10.0",

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)