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
6 changes: 5 additions & 1 deletion google/cloud/pubsub_v1/publisher/client.py
Expand Up @@ -134,7 +134,11 @@ def __init__(self, batch_settings=(), publisher_options=(), **kwargs):
)

client_options = kwargs.pop("client_options", None)
if client_options and type(client_options["api_endpoint"]) is str:
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
Expand Down
7 changes: 6 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 @@ -81,7 +82,11 @@ def __init__(self, **kwargs):

# 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:
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
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/pubsub_v1/publisher/test_publisher_client.py
Expand Up @@ -63,6 +63,25 @@ def test_init_w_api_endpoint():
) == "testendpoint.google.com"


def test_init_w_unicode_api_endpoint():
client_options = {"api_endpoint": u"testendpoint.google.com"}
pradn marked this conversation as resolved.
Show resolved Hide resolved
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
19 changes: 19 additions & 0 deletions tests/unit/pubsub_v1/subscriber/test_subscriber_client.py
Expand Up @@ -44,6 +44,25 @@ def test_init_w_api_endpoint():
) == "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