Skip to content

Commit

Permalink
fix: use client_options.api_endpoint parameter instead of ignoring it (
Browse files Browse the repository at this point in the history
…#59)

- The client_options.api-endpoint parameter was being ignored. This bug prevented users from using regional endpoints. It also made it harder to test code against nonprod endpoints.

Fixes #61
  • Loading branch information
pradn committed Mar 25, 2020
1 parent a3ee941 commit 56b8d7b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
12 changes: 11 additions & 1 deletion google/cloud/pubsub_v1/publisher/client.py
Expand Up @@ -133,6 +133,16 @@ 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 "api_endpoint" in client_options
and isinstance(client_options["api_endpoint"], six.string_types)
):
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 +227,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
14 changes: 13 additions & 1 deletion google/cloud/pubsub_v1/subscriber/client.py
Expand Up @@ -16,6 +16,7 @@

import os
import pkg_resources
import six

import grpc

Expand Down Expand Up @@ -79,6 +80,17 @@ 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 "api_endpoint" in client_options
and isinstance(client_options["api_endpoint"], six.string_types)
):
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 +145,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
29 changes: 29 additions & 0 deletions tests/unit/pubsub_v1/publisher/test_publisher_client.py
Expand Up @@ -53,6 +53,35 @@ 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_w_unicode_api_endpoint():
client_options = {"api_endpoint": u"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_w_empty_client_options():
client = publisher.Client(client_options={})

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


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
29 changes: 29 additions & 0 deletions tests/unit/pubsub_v1/subscriber/test_subscriber_client.py
Expand Up @@ -34,6 +34,35 @@ 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_w_unicode_api_endpoint():
client_options = {"api_endpoint": u"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_w_empty_client_options():
client = subscriber.Client(client_options={})

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


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

0 comments on commit 56b8d7b

Please sign in to comment.