Skip to content

Commit 56b8d7b

Browse files
authored
fix: use client_options.api_endpoint parameter instead of ignoring it (#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
1 parent a3ee941 commit 56b8d7b

File tree

4 files changed

+82
-2
lines changed

4 files changed

+82
-2
lines changed

google/cloud/pubsub_v1/publisher/client.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ def __init__(self, batch_settings=(), publisher_options=(), **kwargs):
133133
target=os.environ.get("PUBSUB_EMULATOR_HOST")
134134
)
135135

136+
client_options = kwargs.pop("client_options", None)
137+
if (
138+
client_options
139+
and "api_endpoint" in client_options
140+
and isinstance(client_options["api_endpoint"], six.string_types)
141+
):
142+
self._target = client_options["api_endpoint"]
143+
else:
144+
self._target = publisher_client.PublisherClient.SERVICE_ADDRESS
145+
136146
# Use a custom channel.
137147
# We need this in order to set appropriate default message size and
138148
# keepalive options.
@@ -217,7 +227,7 @@ def target(self):
217227
Returns:
218228
str: The location of the API.
219229
"""
220-
return publisher_client.PublisherClient.SERVICE_ADDRESS
230+
return self._target
221231

222232
def _get_or_create_sequencer(self, topic, ordering_key):
223233
""" Get an existing sequencer or create a new one given the (topic,

google/cloud/pubsub_v1/subscriber/client.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import os
1818
import pkg_resources
19+
import six
1920

2021
import grpc
2122

@@ -79,6 +80,17 @@ def __init__(self, **kwargs):
7980
target=os.environ.get("PUBSUB_EMULATOR_HOST")
8081
)
8182

83+
# api_endpoint wont be applied if 'transport' is passed in.
84+
client_options = kwargs.pop("client_options", None)
85+
if (
86+
client_options
87+
and "api_endpoint" in client_options
88+
and isinstance(client_options["api_endpoint"], six.string_types)
89+
):
90+
self._target = client_options["api_endpoint"]
91+
else:
92+
self._target = subscriber_client.SubscriberClient.SERVICE_ADDRESS
93+
8294
# Use a custom channel.
8395
# We need this in order to set appropriate default message size and
8496
# keepalive options.
@@ -133,7 +145,7 @@ def target(self):
133145
Returns:
134146
str: The location of the API.
135147
"""
136-
return subscriber_client.SubscriberClient.SERVICE_ADDRESS
148+
return self._target
137149

138150
@property
139151
def api(self):

tests/unit/pubsub_v1/publisher/test_publisher_client.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,35 @@ def test_init_w_custom_transport():
5353
assert client.batch_settings.max_messages == 100
5454

5555

56+
def test_init_w_api_endpoint():
57+
client_options = {"api_endpoint": "testendpoint.google.com"}
58+
client = publisher.Client(client_options=client_options)
59+
60+
assert isinstance(client.api, publisher_client.PublisherClient)
61+
assert (client.api.transport._channel._channel.target()).decode(
62+
"utf-8"
63+
) == "testendpoint.google.com"
64+
65+
66+
def test_init_w_unicode_api_endpoint():
67+
client_options = {"api_endpoint": u"testendpoint.google.com"}
68+
client = publisher.Client(client_options=client_options)
69+
70+
assert isinstance(client.api, publisher_client.PublisherClient)
71+
assert (client.api.transport._channel._channel.target()).decode(
72+
"utf-8"
73+
) == "testendpoint.google.com"
74+
75+
76+
def test_init_w_empty_client_options():
77+
client = publisher.Client(client_options={})
78+
79+
assert isinstance(client.api, publisher_client.PublisherClient)
80+
assert (client.api.transport._channel._channel.target()).decode(
81+
"utf-8"
82+
) == publisher_client.PublisherClient.SERVICE_ADDRESS
83+
84+
5685
def test_init_emulator(monkeypatch):
5786
monkeypatch.setenv("PUBSUB_EMULATOR_HOST", "/foo/bar/")
5887
# NOTE: When the emulator host is set, a custom channel will be used, so

tests/unit/pubsub_v1/subscriber/test_subscriber_client.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,35 @@ def test_init_w_custom_transport():
3434
assert client.api.transport is transport
3535

3636

37+
def test_init_w_api_endpoint():
38+
client_options = {"api_endpoint": "testendpoint.google.com"}
39+
client = subscriber.Client(client_options=client_options)
40+
41+
assert isinstance(client.api, subscriber_client.SubscriberClient)
42+
assert (client.api.transport._channel._channel.target()).decode(
43+
"utf-8"
44+
) == "testendpoint.google.com"
45+
46+
47+
def test_init_w_unicode_api_endpoint():
48+
client_options = {"api_endpoint": u"testendpoint.google.com"}
49+
client = subscriber.Client(client_options=client_options)
50+
51+
assert isinstance(client.api, subscriber_client.SubscriberClient)
52+
assert (client.api.transport._channel._channel.target()).decode(
53+
"utf-8"
54+
) == "testendpoint.google.com"
55+
56+
57+
def test_init_w_empty_client_options():
58+
client = subscriber.Client(client_options={})
59+
60+
assert isinstance(client.api, subscriber_client.SubscriberClient)
61+
assert (client.api.transport._channel._channel.target()).decode(
62+
"utf-8"
63+
) == subscriber_client.SubscriberClient.SERVICE_ADDRESS
64+
65+
3766
def test_init_emulator(monkeypatch):
3867
monkeypatch.setenv("PUBSUB_EMULATOR_HOST", "/baz/bacon/")
3968
# NOTE: When the emulator host is set, a custom channel will be used, so

0 commit comments

Comments
 (0)