Skip to content

Commit ad82cf7

Browse files
committed
Expunge 'Topic.connection' property.
Adopt '_require_connection' pattern from storage. Addresses: #859 (comment)
1 parent d6edeb2 commit ad82cf7

File tree

5 files changed

+171
-213
lines changed

5 files changed

+171
-213
lines changed

gcloud/pubsub/api.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""Define API functions (not bound to classes)."""
1616

1717
from gcloud._helpers import get_default_project
18-
from gcloud.pubsub._implicit_environ import get_default_connection
18+
from gcloud.pubsub._implicit_environ import _require_connection
1919
from gcloud.pubsub.subscription import Subscription
2020
from gcloud.pubsub.topic import Topic
2121

@@ -53,8 +53,7 @@ def list_topics(page_size=None, page_token=None,
5353
if project is None:
5454
project = get_default_project()
5555

56-
if connection is None:
57-
connection = get_default_connection()
56+
connection = _require_connection(connection)
5857

5958
params = {}
6059

@@ -66,8 +65,7 @@ def list_topics(page_size=None, page_token=None,
6665

6766
path = '/projects/%s/topics' % project
6867
resp = connection.api_request(method='GET', path=path, query_params=params)
69-
topics = [Topic.from_api_repr(resource, connection)
70-
for resource in resp['topics']]
68+
topics = [Topic.from_api_repr(resource) for resource in resp['topics']]
7169
return topics, resp.get('nextPageToken')
7270

7371

@@ -110,8 +108,7 @@ def list_subscriptions(page_size=None, page_token=None, topic_name=None,
110108
if project is None:
111109
project = get_default_project()
112110

113-
if connection is None:
114-
connection = get_default_connection()
111+
connection = _require_connection(connection)
115112

116113
params = {}
117114

@@ -128,8 +125,6 @@ def list_subscriptions(page_size=None, page_token=None, topic_name=None,
128125

129126
resp = connection.api_request(method='GET', path=path, query_params=params)
130127
topics = {}
131-
subscriptions = [Subscription.from_api_repr(resource,
132-
connection=connection,
133-
topics=topics)
128+
subscriptions = [Subscription.from_api_repr(resource, topics=topics)
134129
for resource in resp['subscriptions']]
135130
return subscriptions, resp.get('nextPageToken')

gcloud/pubsub/subscription.py

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from gcloud.exceptions import NotFound
1818
from gcloud.pubsub.message import Message
1919
from gcloud.pubsub.topic import Topic
20+
from gcloud.pubsub._implicit_environ import _require_connection
2021

2122

2223
class Subscription(object):
@@ -46,17 +47,12 @@ def __init__(self, name, topic, ack_deadline=None, push_endpoint=None):
4647
self.push_endpoint = push_endpoint
4748

4849
@classmethod
49-
def from_api_repr(cls, resource, connection=None, topics=None):
50+
def from_api_repr(cls, resource, topics=None):
5051
"""Factory: construct a topic given its API representation
5152
5253
:type resource: dict
5354
:param resource: topic resource representation returned from the API
5455
55-
:type connection: :class:`gcloud.pubsub.connection.Connection` or None
56-
:param connection: the connection to use. If not passed,
57-
falls back to the default inferred from the
58-
environment.
59-
6056
:type topics: dict or None
6157
:param topics: A mapping of topic names -> topics. If not passed,
6258
the subscription will have a newly-created topic.
@@ -68,8 +64,7 @@ def from_api_repr(cls, resource, connection=None, topics=None):
6864
t_name = resource['topic']
6965
topic = topics.get(t_name)
7066
if topic is None:
71-
topic = topics[t_name] = Topic.from_api_repr({'name': t_name},
72-
connection)
67+
topic = topics[t_name] = Topic.from_api_repr({'name': t_name})
7368
_, _, _, name = resource['name'].split('/')
7469
ack_deadline = resource.get('ackDeadlineSeconds')
7570
push_config = resource.get('pushConfig', {})
@@ -100,9 +95,7 @@ def create(self, connection=None):
10095
if self.push_endpoint is not None:
10196
data['pushConfig'] = {'pushEndpoint': self.push_endpoint}
10297

103-
if connection is None:
104-
connection = self.topic.connection
105-
98+
connection = _require_connection(connection)
10699
connection.api_request(method='PUT', path=self.path, data=data)
107100

108101
def exists(self, connection=None):
@@ -115,8 +108,7 @@ def exists(self, connection=None):
115108
:param connection: the connection to use. If not passed,
116109
falls back to the topic's connection.
117110
"""
118-
if connection is None:
119-
connection = self.topic.connection
111+
connection = _require_connection(connection)
120112
try:
121113
connection.api_request(method='GET', path=self.path)
122114
except NotFound:
@@ -134,8 +126,7 @@ def reload(self, connection=None):
134126
:param connection: the connection to use. If not passed,
135127
falls back to the topic's connection.
136128
"""
137-
if connection is None:
138-
connection = self.topic.connection
129+
connection = _require_connection(connection)
139130
data = connection.api_request(method='GET', path=self.path)
140131
self.ack_deadline = data.get('ackDeadline')
141132
push_config = data.get('pushConfig', {})
@@ -156,8 +147,7 @@ def modify_push_configuration(self, push_endpoint, connection=None):
156147
:param connection: the connection to use. If not passed,
157148
falls back to the topic's connection.
158149
"""
159-
if connection is None:
160-
connection = self.topic.connection
150+
connection = _require_connection(connection)
161151
data = {}
162152
config = data['pushConfig'] = {}
163153
if push_endpoint is not None:
@@ -191,8 +181,7 @@ def pull(self, return_immediately=False, max_messages=1, connection=None):
191181
subsequent call to :meth:`acknowledge`, and ``message``
192182
is an instance of :class:`gcloud.pubsub.message.Message`.
193183
"""
194-
if connection is None:
195-
connection = self.topic.connection
184+
connection = _require_connection(connection)
196185
data = {'returnImmediately': return_immediately,
197186
'maxMessages': max_messages}
198187
response = connection.api_request(method='POST',
@@ -214,8 +203,7 @@ def acknowledge(self, ack_ids, connection=None):
214203
:param connection: the connection to use. If not passed,
215204
falls back to the topic's connection.
216205
"""
217-
if connection is None:
218-
connection = self.topic.connection
206+
connection = _require_connection(connection)
219207
data = {'ackIds': ack_ids}
220208
connection.api_request(method='POST',
221209
path='%s:acknowledge' % self.path,
@@ -237,8 +225,7 @@ def modify_ack_deadline(self, ack_id, ack_deadline, connection=None):
237225
:param connection: the connection to use. If not passed,
238226
falls back to the topic's connection.
239227
"""
240-
if connection is None:
241-
connection = self.topic.connection
228+
connection = _require_connection(connection)
242229
data = {'ackId': ack_id, 'ackDeadlineSeconds': ack_deadline}
243230
connection.api_request(method='POST',
244231
path='%s:modifyAckDeadline' % self.path,
@@ -254,6 +241,5 @@ def delete(self, connection=None):
254241
:param connection: the connection to use. If not passed,
255242
falls back to the topic's connection.
256243
"""
257-
if connection is None:
258-
connection = self.topic.connection
244+
connection = _require_connection(connection)
259245
connection.api_request(method='DELETE', path=self.path)

0 commit comments

Comments
 (0)