17
17
from gcloud .exceptions import NotFound
18
18
from gcloud .pubsub .message import Message
19
19
from gcloud .pubsub .topic import Topic
20
+ from gcloud .pubsub ._implicit_environ import _require_connection
20
21
21
22
22
23
class Subscription (object ):
@@ -46,17 +47,12 @@ def __init__(self, name, topic, ack_deadline=None, push_endpoint=None):
46
47
self .push_endpoint = push_endpoint
47
48
48
49
@classmethod
49
- def from_api_repr (cls , resource , connection = None , topics = None ):
50
+ def from_api_repr (cls , resource , topics = None ):
50
51
"""Factory: construct a topic given its API representation
51
52
52
53
:type resource: dict
53
54
:param resource: topic resource representation returned from the API
54
55
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
-
60
56
:type topics: dict or None
61
57
:param topics: A mapping of topic names -> topics. If not passed,
62
58
the subscription will have a newly-created topic.
@@ -68,8 +64,7 @@ def from_api_repr(cls, resource, connection=None, topics=None):
68
64
t_name = resource ['topic' ]
69
65
topic = topics .get (t_name )
70
66
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 })
73
68
_ , _ , _ , name = resource ['name' ].split ('/' )
74
69
ack_deadline = resource .get ('ackDeadlineSeconds' )
75
70
push_config = resource .get ('pushConfig' , {})
@@ -82,11 +77,15 @@ def path(self):
82
77
project = self .topic .project
83
78
return '/projects/%s/subscriptions/%s' % (project , self .name )
84
79
85
- def create (self ):
80
+ def create (self , connection = None ):
86
81
"""API call: create the subscription via a PUT request
87
82
88
83
See:
89
84
https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/subscriptions/create
85
+
86
+ :type connection: :class:`gcloud.pubsub.connection.Connection` or None
87
+ :param connection: the connection to use. If not passed,
88
+ falls back to the topic's connection.
90
89
"""
91
90
data = {'topic' : self .topic .full_name }
92
91
@@ -96,36 +95,44 @@ def create(self):
96
95
if self .push_endpoint is not None :
97
96
data ['pushConfig' ] = {'pushEndpoint' : self .push_endpoint }
98
97
99
- conn = self . topic . connection
100
- conn .api_request (method = 'PUT' , path = self .path , data = data )
98
+ connection = _require_connection ( connection )
99
+ connection .api_request (method = 'PUT' , path = self .path , data = data )
101
100
102
- def exists (self ):
101
+ def exists (self , connection = None ):
103
102
"""API call: test existence of the subscription via a GET request
104
103
105
104
See
106
105
https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/subscriptions/get
106
+
107
+ :type connection: :class:`gcloud.pubsub.connection.Connection` or None
108
+ :param connection: the connection to use. If not passed,
109
+ falls back to the topic's connection.
107
110
"""
108
- conn = self . topic . connection
111
+ connection = _require_connection ( connection )
109
112
try :
110
- conn .api_request (method = 'GET' , path = self .path )
113
+ connection .api_request (method = 'GET' , path = self .path )
111
114
except NotFound :
112
115
return False
113
116
else :
114
117
return True
115
118
116
- def reload (self ):
119
+ def reload (self , connection = None ):
117
120
"""API call: sync local subscription configuration via a GET request
118
121
119
122
See
120
123
https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/subscriptions/get
124
+
125
+ :type connection: :class:`gcloud.pubsub.connection.Connection` or None
126
+ :param connection: the connection to use. If not passed,
127
+ falls back to the topic's connection.
121
128
"""
122
- conn = self . topic . connection
123
- data = conn .api_request (method = 'GET' , path = self .path )
129
+ connection = _require_connection ( connection )
130
+ data = connection .api_request (method = 'GET' , path = self .path )
124
131
self .ack_deadline = data .get ('ackDeadline' )
125
132
push_config = data .get ('pushConfig' , {})
126
133
self .push_endpoint = push_config .get ('pushEndpoint' )
127
134
128
- def modify_push_configuration (self , push_endpoint ):
135
+ def modify_push_configuration (self , push_endpoint , connection = None ):
129
136
"""API call: update the push endpoint for the subscription.
130
137
131
138
See:
@@ -135,18 +142,22 @@ def modify_push_configuration(self, push_endpoint):
135
142
:param push_endpoint: URL to which messages will be pushed by the
136
143
back-end. If None, the application must pull
137
144
messages.
145
+
146
+ :type connection: :class:`gcloud.pubsub.connection.Connection` or None
147
+ :param connection: the connection to use. If not passed,
148
+ falls back to the topic's connection.
138
149
"""
150
+ connection = _require_connection (connection )
139
151
data = {}
140
152
config = data ['pushConfig' ] = {}
141
153
if push_endpoint is not None :
142
154
config ['pushEndpoint' ] = push_endpoint
143
- conn = self .topic .connection
144
- conn .api_request (method = 'POST' ,
145
- path = '%s:modifyPushConfig' % self .path ,
146
- data = data )
155
+ connection .api_request (method = 'POST' ,
156
+ path = '%s:modifyPushConfig' % self .path ,
157
+ data = data )
147
158
self .push_endpoint = push_endpoint
148
159
149
- def pull (self , return_immediately = False , max_messages = 1 ):
160
+ def pull (self , return_immediately = False , max_messages = 1 , connection = None ):
150
161
"""API call: retrieve messages for the subscription.
151
162
152
163
See:
@@ -161,36 +172,44 @@ def pull(self, return_immediately=False, max_messages=1):
161
172
:type max_messages: int
162
173
:param max_messages: the maximum number of messages to return.
163
174
175
+ :type connection: :class:`gcloud.pubsub.connection.Connection` or None
176
+ :param connection: the connection to use. If not passed,
177
+ falls back to the topic's connection.
178
+
164
179
:rtype: list of (ack_id, message) tuples
165
180
:returns: sequence of tuples: ``ack_id`` is the ID to be used in a
166
181
subsequent call to :meth:`acknowledge`, and ``message``
167
182
is an instance of :class:`gcloud.pubsub.message.Message`.
168
183
"""
184
+ connection = _require_connection (connection )
169
185
data = {'returnImmediately' : return_immediately ,
170
186
'maxMessages' : max_messages }
171
- conn = self .topic .connection
172
- response = conn .api_request (method = 'POST' ,
173
- path = '%s:pull' % self .path ,
174
- data = data )
187
+ response = connection .api_request (method = 'POST' ,
188
+ path = '%s:pull' % self .path ,
189
+ data = data )
175
190
return [(info ['ackId' ], Message .from_api_repr (info ['message' ]))
176
191
for info in response ['receivedMessages' ]]
177
192
178
- def acknowledge (self , ack_ids ):
193
+ def acknowledge (self , ack_ids , connection = None ):
179
194
"""API call: acknowledge retrieved messages for the subscription.
180
195
181
196
See:
182
197
https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/subscriptions/acknowledge
183
198
184
199
:type ack_ids: list of string
185
200
:param ack_ids: ack IDs of messages being acknowledged
201
+
202
+ :type connection: :class:`gcloud.pubsub.connection.Connection` or None
203
+ :param connection: the connection to use. If not passed,
204
+ falls back to the topic's connection.
186
205
"""
206
+ connection = _require_connection (connection )
187
207
data = {'ackIds' : ack_ids }
188
- conn = self .topic .connection
189
- conn .api_request (method = 'POST' ,
190
- path = '%s:acknowledge' % self .path ,
191
- data = data )
208
+ connection .api_request (method = 'POST' ,
209
+ path = '%s:acknowledge' % self .path ,
210
+ data = data )
192
211
193
- def modify_ack_deadline (self , ack_id , ack_deadline ):
212
+ def modify_ack_deadline (self , ack_id , ack_deadline , connection = None ):
194
213
"""API call: update acknowledgement deadline for a retrieved message.
195
214
196
215
See:
@@ -201,18 +220,26 @@ def modify_ack_deadline(self, ack_id, ack_deadline):
201
220
202
221
:type ack_deadline: int
203
222
:param ack_deadline: new deadline for the message, in seconds
223
+
224
+ :type connection: :class:`gcloud.pubsub.connection.Connection` or None
225
+ :param connection: the connection to use. If not passed,
226
+ falls back to the topic's connection.
204
227
"""
228
+ connection = _require_connection (connection )
205
229
data = {'ackId' : ack_id , 'ackDeadlineSeconds' : ack_deadline }
206
- conn = self .topic .connection
207
- conn .api_request (method = 'POST' ,
208
- path = '%s:modifyAckDeadline' % self .path ,
209
- data = data )
230
+ connection .api_request (method = 'POST' ,
231
+ path = '%s:modifyAckDeadline' % self .path ,
232
+ data = data )
210
233
211
- def delete (self ):
234
+ def delete (self , connection = None ):
212
235
"""API call: delete the subscription via a DELETE request.
213
236
214
237
See:
215
238
https://cloud.google.com/pubsub/reference/rest/v1beta2/projects/subscriptions/delete
239
+
240
+ :type connection: :class:`gcloud.pubsub.connection.Connection` or None
241
+ :param connection: the connection to use. If not passed,
242
+ falls back to the topic's connection.
216
243
"""
217
- conn = self . topic . connection
218
- conn .api_request (method = 'DELETE' , path = self .path )
244
+ connection = _require_connection ( connection )
245
+ connection .api_request (method = 'DELETE' , path = self .path )
0 commit comments