FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat(api-core): add client_cert_source to ClientOptions by arithmetic1728 · Pull Request #17 · googleapis/python-api-core · GitHub

This repository was archived by the owner on Feb 23, 2026. It is now read-only.
/ python-api-core Public archive
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (4) All 1 file type selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
19 changes: 16 additions & 3 deletions google/api_core/client_options.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
})


"""
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
2 changes: 1 addition & 1 deletion noxfile.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def docs(session):

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

shutil.rmtree(os.path.join("docs", "_build"), ignore_errors=True)
session.run(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
dependencies = [
"googleapis-common-protos >= 1.6.0, < 2.0dev",
"protobuf >= 3.4.0",
"google-auth >= 0.4.0, < 2.0dev",
"google-auth >= 1.14.0, < 2.0dev",
"requests >= 2.18.0, < 3.0.0dev",
"setuptools >= 34.0.0",
"six >= 1.10.0",
Expand Down
28 changes: 24 additions & 4 deletions tests/unit/test_client_options.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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'}"
)

Back | FazBrowse Home | New Git URL