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

fix: use client_options.api_endpoint parameter instead of ignoring it #59

Merged
merged 3 commits into from Mar 25, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion google/cloud/pubsub_v1/publisher/client.py
Expand Up @@ -133,6 +133,12 @@ def __init__(self, batch_settings=(), publisher_options=(), **kwargs):
target=os.environ.get("PUBSUB_EMULATOR_HOST")
)

client_options = kwargs.pop("client_options", None)
if client_options and type(client_options["api_endpoint"]) is str:
pradn marked this conversation as resolved.
Show resolved Hide resolved
self._target = client_options["api_endpoint"]
else:
self._target = publisher_client.PublisherClient.SERVICE_ADDRESS

# Use a custom channel.
# We need this in order to set appropriate default message size and
# keepalive options.
Expand Down Expand Up @@ -217,7 +223,7 @@ def target(self):
Returns:
str: The location of the API.
"""
return publisher_client.PublisherClient.SERVICE_ADDRESS
return self._target

def _get_or_create_sequencer(self, topic, ordering_key):
""" Get an existing sequencer or create a new one given the (topic,
Expand Down
9 changes: 8 additions & 1 deletion google/cloud/pubsub_v1/subscriber/client.py
Expand Up @@ -79,6 +79,13 @@ def __init__(self, **kwargs):
target=os.environ.get("PUBSUB_EMULATOR_HOST")
)

# api_endpoint wont be applied if 'transport' is passed in.
client_options = kwargs.pop("client_options", None)
if client_options and type(client_options["api_endpoint"]) is str:
self._target = client_options["api_endpoint"]
else:
self._target = subscriber_client.SubscriberClient.SERVICE_ADDRESS

# Use a custom channel.
# We need this in order to set appropriate default message size and
# keepalive options.
Expand Down Expand Up @@ -133,7 +140,7 @@ def target(self):
Returns:
str: The location of the API.
"""
return subscriber_client.SubscriberClient.SERVICE_ADDRESS
return self._target

@property
def api(self):
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/pubsub_v1/publisher/test_publisher_client.py
Expand Up @@ -53,6 +53,16 @@ def test_init_w_custom_transport():
assert client.batch_settings.max_messages == 100


def test_init_w_api_endpoint():
client_options = {"api_endpoint": "testendpoint.google.com"}
client = publisher.Client(client_options=client_options)

assert isinstance(client.api, publisher_client.PublisherClient)
assert (client.api.transport._channel._channel.target()).decode(
"utf-8"
) == "testendpoint.google.com"


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
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/pubsub_v1/subscriber/test_subscriber_client.py
Expand Up @@ -34,6 +34,16 @@ def test_init_w_custom_transport():
assert client.api.transport is transport


def test_init_w_api_endpoint():
client_options = {"api_endpoint": "testendpoint.google.com"}
client = subscriber.Client(client_options=client_options)

assert isinstance(client.api, subscriber_client.SubscriberClient)
assert (client.api.transport._channel._channel.target()).decode(
"utf-8"
) == "testendpoint.google.com"


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
Expand Down