Skip to content

Commit 042e062

Browse files
committed
Removing references to cnxn in Pub/Sub _gax module.
1 parent e26b437 commit 042e062

File tree

4 files changed

+58
-51
lines changed

4 files changed

+58
-51
lines changed

pubsub/google/cloud/pubsub/_gax.py

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -512,48 +512,60 @@ def _received_message_pb_to_mapping(received_message_pb):
512512
}
513513

514514

515-
def make_gax_publisher_api(connection):
515+
def make_gax_publisher_api(credentials, host=None, secure=True):
516516
"""Create an instance of the GAX Publisher API.
517517
518-
If the ``connection`` is intended for a local emulator, then
519-
an insecure ``channel`` is created pointing at the local
520-
Pub / Sub server.
518+
If the ``secure=False`` then we create an insecure ``channel``
519+
pointing at the local Pub / Sub emulator.
521520
522-
:type connection: :class:`~google.cloud.pubsub._http.Connection`
523-
:param connection: The connection that holds configuration details.
521+
:type credentials: :class:`~google.auth.credentials.Credentials`
522+
:param credentials: Credentials for getting access tokens.
523+
524+
:type host: str
525+
:param host: (Optional) The host for an insecure channel. Only
526+
used if ``secure=False``.
527+
528+
:type secure: bool
529+
:param secure: (Optional) Indicates if we should create a secure
530+
or insecure channel. Defaults to :data:`True`.
524531
525532
:rtype: :class:`.publisher_client.PublisherClient`
526-
:returns: A publisher API instance with the proper connection
527-
configuration.
533+
:returns: A publisher API instance with the proper channel.
528534
"""
529-
if connection.in_emulator:
530-
channel = insecure_channel(connection.host)
535+
if not secure:
536+
channel = insecure_channel(host)
531537
else:
532538
channel = make_secure_channel(
533-
connection.credentials, DEFAULT_USER_AGENT,
539+
credentials, DEFAULT_USER_AGENT,
534540
PublisherClient.SERVICE_ADDRESS)
535541
return PublisherClient(channel=channel)
536542

537543

538-
def make_gax_subscriber_api(connection):
544+
def make_gax_subscriber_api(credentials, host=None, secure=True):
539545
"""Create an instance of the GAX Subscriber API.
540546
541-
If the ``connection`` is intended for a local emulator, then
542-
an insecure ``channel`` is created pointing at the local
543-
Pub / Sub server.
547+
If the ``secure=False`` then we create an insecure ``channel``
548+
pointing at the local Pub / Sub emulator.
549+
550+
:type credentials: :class:`~google.auth.credentials.Credentials`
551+
:param credentials: Credentials for getting access tokens.
552+
553+
:type host: str
554+
:param host: (Optional) The host for an insecure channel. Only
555+
used if ``secure=False``.
544556
545-
:type connection: :class:`~google.cloud.pubsub._http.Connection`
546-
:param connection: The connection that holds configuration details.
557+
:type secure: bool
558+
:param secure: (Optional) Indicates if we should create a secure
559+
or insecure channel. Defaults to :data:`True`.
547560
548561
:rtype: :class:`.subscriber_client.SubscriberClient`
549-
:returns: A subscriber API instance with the proper connection
550-
configuration.
562+
:returns: A subscriber API instance with the proper channel.
551563
"""
552-
if connection.in_emulator:
553-
channel = insecure_channel(connection.host)
564+
if not secure:
565+
channel = insecure_channel(host)
554566
else:
555567
channel = make_secure_channel(
556-
connection.credentials, DEFAULT_USER_AGENT,
568+
credentials, DEFAULT_USER_AGENT,
557569
SubscriberClient.SERVICE_ADDRESS)
558570
return SubscriberClient(channel=channel)
559571

pubsub/google/cloud/pubsub/client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ def publisher_api(self):
9191
"""Helper for publisher-related API calls."""
9292
if self._publisher_api is None:
9393
if self._use_gax:
94-
generated = make_gax_publisher_api(self._connection)
94+
generated = make_gax_publisher_api(
95+
self._credentials, host=self._connection.host,
96+
secure=not self._connection.in_emulator)
9597
self._publisher_api = GAXPublisherAPI(generated, self)
9698
else:
9799
self._publisher_api = JSONPublisherAPI(self)
@@ -102,7 +104,9 @@ def subscriber_api(self):
102104
"""Helper for subscriber-related API calls."""
103105
if self._subscriber_api is None:
104106
if self._use_gax:
105-
generated = make_gax_subscriber_api(self._connection)
107+
generated = make_gax_subscriber_api(
108+
self._credentials, host=self._connection.host,
109+
secure=not self._connection.in_emulator)
106110
self._subscriber_api = GAXSubscriberAPI(generated, self)
107111
else:
108112
self._subscriber_api = JSONSubscriberAPI(self)

pubsub/unit_tests/test__gax.py

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -900,9 +900,9 @@ def test_subscription_modify_ack_deadline_error(self):
900900
@unittest.skipUnless(_HAVE_GAX, 'No gax-python')
901901
class Test_make_gax_publisher_api(_Base, unittest.TestCase):
902902

903-
def _call_fut(self, connection):
903+
def _call_fut(self, *args, **kwargs):
904904
from google.cloud.pubsub._gax import make_gax_publisher_api
905-
return make_gax_publisher_api(connection)
905+
return make_gax_publisher_api(*args, **kwargs)
906906

907907
def test_live_api(self):
908908
from google.cloud.pubsub._gax import DEFAULT_USER_AGENT
@@ -924,14 +924,12 @@ def make_channel(*args):
924924
mock_publisher_api.SERVICE_ADDRESS = host
925925

926926
creds = _make_credentials()
927-
connection = _Connection(in_emulator=False,
928-
credentials=creds)
929927
patch = mock.patch.multiple(
930928
'google.cloud.pubsub._gax',
931929
PublisherClient=mock_publisher_api,
932930
make_secure_channel=make_channel)
933931
with patch:
934-
result = self._call_fut(connection)
932+
result = self._call_fut(creds)
935933

936934
self.assertIs(result, mock_result)
937935
self.assertEqual(channels, [channel_obj])
@@ -953,13 +951,12 @@ def mock_insecure_channel(host):
953951
return mock_channel
954952

955953
host = 'CURR_HOST:1234'
956-
connection = _Connection(in_emulator=True, host=host)
957954
patch = mock.patch.multiple(
958955
'google.cloud.pubsub._gax',
959956
PublisherClient=mock_publisher_api,
960957
insecure_channel=mock_insecure_channel)
961958
with patch:
962-
result = self._call_fut(connection)
959+
result = self._call_fut(None, host=host, secure=False)
963960

964961
self.assertIs(result, mock_result)
965962
self.assertEqual(channels, [mock_channel])
@@ -969,9 +966,9 @@ def mock_insecure_channel(host):
969966
@unittest.skipUnless(_HAVE_GAX, 'No gax-python')
970967
class Test_make_gax_subscriber_api(_Base, unittest.TestCase):
971968

972-
def _call_fut(self, connection):
969+
def _call_fut(self, *args, **kwargs):
973970
from google.cloud.pubsub._gax import make_gax_subscriber_api
974-
return make_gax_subscriber_api(connection)
971+
return make_gax_subscriber_api(*args, **kwargs)
975972

976973
def test_live_api(self):
977974
from google.cloud.pubsub._gax import DEFAULT_USER_AGENT
@@ -993,14 +990,12 @@ def make_channel(*args):
993990
mock_subscriber_api.SERVICE_ADDRESS = host
994991

995992
creds = _make_credentials()
996-
connection = _Connection(in_emulator=False,
997-
credentials=creds)
998993
patch = mock.patch.multiple(
999994
'google.cloud.pubsub._gax',
1000995
SubscriberClient=mock_subscriber_api,
1001996
make_secure_channel=make_channel)
1002997
with patch:
1003-
result = self._call_fut(connection)
998+
result = self._call_fut(creds)
1004999

10051000
self.assertIs(result, mock_result)
10061001
self.assertEqual(channels, [channel_obj])
@@ -1022,13 +1017,12 @@ def mock_insecure_channel(host):
10221017
return mock_channel
10231018

10241019
host = 'CURR_HOST:1234'
1025-
connection = _Connection(in_emulator=True, host=host)
10261020
patch = mock.patch.multiple(
10271021
'google.cloud.pubsub._gax',
10281022
SubscriberClient=mock_subscriber_api,
10291023
insecure_channel=mock_insecure_channel)
10301024
with patch:
1031-
result = self._call_fut(connection)
1025+
result = self._call_fut(None, host=host, secure=False)
10321026

10331027
self.assertIs(result, mock_result)
10341028
self.assertEqual(channels, [mock_channel])
@@ -1207,15 +1201,6 @@ def __init__(self, received_messages):
12071201
self.received_messages = received_messages
12081202

12091203

1210-
class _Connection(object):
1211-
1212-
def __init__(self, in_emulator=False, host=None,
1213-
credentials=None):
1214-
self.in_emulator = in_emulator
1215-
self.host = host
1216-
self.credentials = credentials
1217-
1218-
12191204
class _Client(object):
12201205

12211206
def __init__(self, project):

pubsub/unit_tests/test_client.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ def test_no_gax_ctor(self):
6969
self.assertIsInstance(api, _PublisherAPI)
7070

7171
def test_publisher_api_w_gax(self):
72+
from google.cloud.pubsub import _http
73+
7274
wrapped = object()
7375
_called_with = []
7476

@@ -100,8 +102,9 @@ def __init__(self, _wrapped, client):
100102
# API instance is cached
101103
again = client.publisher_api
102104
self.assertIs(again, api)
103-
args = (client._connection,)
104-
self.assertEqual(_called_with, [(args, {})])
105+
args = (creds,)
106+
kwargs = {'host': _http.Connection.API_BASE_URL, 'secure': True}
107+
self.assertEqual(_called_with, [(args, kwargs)])
105108

106109
def test_subscriber_api_wo_gax(self):
107110
from google.cloud.pubsub._http import _SubscriberAPI
@@ -121,6 +124,8 @@ def test_subscriber_api_wo_gax(self):
121124
self.assertIs(again, api)
122125

123126
def test_subscriber_api_w_gax(self):
127+
from google.cloud.pubsub import _http
128+
124129
wrapped = object()
125130
_called_with = []
126131

@@ -152,8 +157,9 @@ def __init__(self, _wrapped, client):
152157
# API instance is cached
153158
again = client.subscriber_api
154159
self.assertIs(again, api)
155-
args = (client._connection,)
156-
self.assertEqual(_called_with, [(args, {})])
160+
args = (creds,)
161+
kwargs = {'host': _http.Connection.API_BASE_URL, 'secure': True}
162+
self.assertEqual(_called_with, [(args, kwargs)])
157163

158164
def test_iam_policy_api(self):
159165
from google.cloud.pubsub._http import _IAMPolicyAPI

0 commit comments

Comments
 (0)