Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api-core): add client_cert_source to ClientOptions #17

Merged
merged 5 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions google/api_core/client_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
from google.api_core.client_options import ClientOptions
from google.cloud.vision_v1 import ImageAnnotatorClient

options = ClientOptions(api_endpoint="foo.googleapis.com")
def get_client_cert():
# code to load client certificate and private key.
return client_cert_bytes, client_private_key_bytes

options = ClientOptions(api_endpoint="foo.googleapis.com",
client_cert_source=get_client_cert)

client = ImageAnnotatorClient(client_options=options)

Expand All @@ -34,7 +39,11 @@

from google.cloud.vision_v1 import ImageAnnotatorClient

client = ImageAnnotatorClient(client_options={"api_endpoint": "foo.googleapis.com"})
client = ImageAnnotatorClient(
client_options={
"api_endpoint": "foo.googleapis.com",
client_cert_source : get_client_cert
arithmetic1728 marked this conversation as resolved.
Show resolved Hide resolved
})


"""
Expand All @@ -45,10 +54,14 @@ class ClientOptions(object):

Args:
api_endpoint (str): The desired API endpoint, e.g., compute.googleapis.com
client_cert_source (Callable[[], (bytes, bytes)]): An optional callback
which returns client certificate bytes and private key bytes both in
PEM format.
"""

def __init__(self, api_endpoint=None):
def __init__(self, api_endpoint=None, client_cert_source=None):
self.api_endpoint = api_endpoint
self.client_cert_source = client_cert_source

def __repr__(self):
return "ClientOptions: " + repr(self.__dict__)
Expand Down
28 changes: 24 additions & 4 deletions tests/unit/test_client_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,46 @@
from google.api_core import client_options


def get_client_cert():
return b"cert", b"key"


def test_constructor():
options = client_options.ClientOptions(api_endpoint="foo.googleapis.com")

options = client_options.ClientOptions(
api_endpoint="foo.googleapis.com", client_cert_source=get_client_cert
)

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


def test_from_dict():
options = client_options.from_dict({"api_endpoint": "foo.googleapis.com"})
options = client_options.from_dict(
{"api_endpoint": "foo.googleapis.com", "client_cert_source": get_client_cert}
)

assert options.api_endpoint == "foo.googleapis.com"
# assert options.client_cert_source == get_client_cert
assert options.client_cert_source() == (b"cert", b"key")


def test_from_dict_bad_argument():
with pytest.raises(ValueError):
client_options.from_dict(
{"api_endpoint": "foo.googleapis.com", "bad_arg": "1234"}
{
"api_endpoint": "foo.googleapis.com",
"bad_arg": "1234",
"client_cert_source": get_client_cert,
}
)


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

assert repr(options) == "ClientOptions: {'api_endpoint': 'foo.googleapis.com'}"
assert (
repr(options)
== "ClientOptions: {'api_endpoint': 'foo.googleapis.com', 'client_cert_source': None}"
or "ClientOptions: {'client_cert_source': None, 'api_endpoint': 'foo.googleapis.com'}"
)