1717from gcloud .exceptions import NotFound
1818from gcloud .pubsub .message import Message
1919from gcloud .pubsub .topic import Topic
20+ from gcloud .pubsub ._implicit_environ import _require_connection
2021
2122
2223class 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