From 923c4d8f3ec059b8919ed8d1a55c7fba48e58223 Mon Sep 17 00:00:00 2001 From: Carlos de la Guardia Date: Mon, 14 Sep 2020 02:51:53 -0500 Subject: [PATCH] fix: pass client options to publisher and subscriber clients (#166) --- google/cloud/pubsub_v1/publisher/client.py | 2 +- google/cloud/pubsub_v1/subscriber/client.py | 2 +- .../publisher/test_publisher_client.py | 18 ++++++++++++++++++ .../subscriber/test_subscriber_client.py | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/google/cloud/pubsub_v1/publisher/client.py b/google/cloud/pubsub_v1/publisher/client.py index ea371190c..f1e198b1a 100644 --- a/google/cloud/pubsub_v1/publisher/client.py +++ b/google/cloud/pubsub_v1/publisher/client.py @@ -130,7 +130,7 @@ def __init__(self, batch_settings=(), publisher_options=(), **kwargs): target=os.environ.get("PUBSUB_EMULATOR_HOST") ) - client_options = kwargs.pop("client_options", None) + client_options = kwargs.get("client_options", None) if ( client_options and "api_endpoint" in client_options diff --git a/google/cloud/pubsub_v1/subscriber/client.py b/google/cloud/pubsub_v1/subscriber/client.py index 98d6d75c7..e0b10c888 100644 --- a/google/cloud/pubsub_v1/subscriber/client.py +++ b/google/cloud/pubsub_v1/subscriber/client.py @@ -83,7 +83,7 @@ def __init__(self, **kwargs): ) # api_endpoint wont be applied if 'transport' is passed in. - client_options = kwargs.pop("client_options", None) + client_options = kwargs.get("client_options", None) if ( client_options and "api_endpoint" in client_options diff --git a/tests/unit/pubsub_v1/publisher/test_publisher_client.py b/tests/unit/pubsub_v1/publisher/test_publisher_client.py index 1760482ac..3b6aa1477 100644 --- a/tests/unit/pubsub_v1/publisher/test_publisher_client.py +++ b/tests/unit/pubsub_v1/publisher/test_publisher_client.py @@ -103,6 +103,24 @@ def test_init_w_empty_client_options(): ) == publisher_client.PublisherClient.SERVICE_ADDRESS +def test_init_client_options_pass_through(): + def init(self, *args, **kwargs): + self.kwargs = kwargs + + with mock.patch.object(publisher_client.PublisherClient, "__init__", init): + client = publisher.Client( + client_options={ + "quota_project_id": "42", + "scopes": [], + "credentials_file": "file.json", + } + ) + client_options = client.api.kwargs["client_options"] + assert client_options.get("quota_project_id") == "42" + assert client_options.get("scopes") == [] + assert client_options.get("credentials_file") == "file.json" + + def test_init_emulator(monkeypatch): monkeypatch.setenv("PUBSUB_EMULATOR_HOST", "/foo/bar/") # NOTE: When the emulator host is set, a custom channel will be used, so diff --git a/tests/unit/pubsub_v1/subscriber/test_subscriber_client.py b/tests/unit/pubsub_v1/subscriber/test_subscriber_client.py index f75f1dae2..634351757 100644 --- a/tests/unit/pubsub_v1/subscriber/test_subscriber_client.py +++ b/tests/unit/pubsub_v1/subscriber/test_subscriber_client.py @@ -64,6 +64,24 @@ def test_init_w_empty_client_options(): ) == subscriber_client.SubscriberClient.SERVICE_ADDRESS +def test_init_client_options_pass_through(): + def init(self, *args, **kwargs): + self.kwargs = kwargs + + with mock.patch.object(subscriber_client.SubscriberClient, "__init__", init): + client = subscriber.Client( + client_options={ + "quota_project_id": "42", + "scopes": [], + "credentials_file": "file.json", + } + ) + client_options = client._api.kwargs["client_options"] + assert client_options.get("quota_project_id") == "42" + assert client_options.get("scopes") == [] + assert client_options.get("credentials_file") == "file.json" + + def test_init_emulator(monkeypatch): monkeypatch.setenv("PUBSUB_EMULATOR_HOST", "/baz/bacon/") # NOTE: When the emulator host is set, a custom channel will be used, so