/
subscriber_grpc_transport.py
434 lines (359 loc) · 17.5 KB
/
subscriber_grpc_transport.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# -*- coding: utf-8 -*-
#
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import google.api_core.grpc_helpers
from google.cloud.pubsub_v1.proto import pubsub_pb2_grpc
from google.iam.v1 import iam_policy_pb2_grpc as iam_policy_pb2_grpc
class SubscriberGrpcTransport(object):
"""gRPC transport class providing stubs for
google.pubsub.v1 Subscriber API.
The transport provides access to the raw gRPC stubs,
which can be used to take advantage of advanced
features of gRPC.
"""
# The scopes needed to make gRPC calls to all of the methods defined
# in this service.
_OAUTH_SCOPES = (
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/pubsub",
)
def __init__(
self, channel=None, credentials=None, address="pubsub.googleapis.com:443"
):
"""Instantiate the transport class.
Args:
channel (grpc.Channel): A ``Channel`` instance through
which to make calls. This argument is mutually exclusive
with ``credentials``; providing both will raise an exception.
credentials (google.auth.credentials.Credentials): The
authorization credentials to attach to requests. These
credentials identify this application to the service. If none
are specified, the client will attempt to ascertain the
credentials from the environment.
address (str): The address where the service is hosted.
"""
# If both `channel` and `credentials` are specified, raise an
# exception (channels come with credentials baked in already).
if channel is not None and credentials is not None:
raise ValueError(
"The `channel` and `credentials` arguments are mutually " "exclusive."
)
# Create the channel.
if channel is None:
channel = self.create_channel(
address=address,
credentials=credentials,
options={
"grpc.max_send_message_length": -1,
"grpc.max_receive_message_length": -1,
}.items(),
)
self._channel = channel
# gRPC uses objects called "stubs" that are bound to the
# channel and provide a basic method for each RPC.
self._stubs = {
"iam_policy_stub": iam_policy_pb2_grpc.IAMPolicyStub(channel),
"subscriber_stub": pubsub_pb2_grpc.SubscriberStub(channel),
}
@classmethod
def create_channel(
cls, address="pubsub.googleapis.com:443", credentials=None, **kwargs
):
"""Create and return a gRPC channel object.
Args:
address (str): The host for the channel to use.
credentials (~.Credentials): The
authorization credentials to attach to requests. These
credentials identify this application to the service. If
none are specified, the client will attempt to ascertain
the credentials from the environment.
kwargs (dict): Keyword arguments, which are passed to the
channel creation.
Returns:
grpc.Channel: A gRPC channel object.
"""
return google.api_core.grpc_helpers.create_channel(
address, credentials=credentials, scopes=cls._OAUTH_SCOPES, **kwargs
)
@property
def channel(self):
"""The gRPC channel used by the transport.
Returns:
grpc.Channel: A gRPC channel object.
"""
return self._channel
@property
def create_subscription(self):
"""Return the gRPC stub for :meth:`SubscriberClient.create_subscription`.
Creates a subscription to a given topic. See the resource name rules. If
the subscription already exists, returns ``ALREADY_EXISTS``. If the
corresponding topic doesn't exist, returns ``NOT_FOUND``.
If the name is not provided in the request, the server will assign a
random name for this subscription on the same project as the topic,
conforming to the `resource name
format <https://cloud.google.com/pubsub/docs/admin#resource_names>`__.
The generated name is populated in the returned Subscription object.
Note that for REST API requests, you must specify a name in the request.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["subscriber_stub"].CreateSubscription
@property
def get_subscription(self):
"""Return the gRPC stub for :meth:`SubscriberClient.get_subscription`.
Gets the configuration details of a subscription.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["subscriber_stub"].GetSubscription
@property
def update_subscription(self):
"""Return the gRPC stub for :meth:`SubscriberClient.update_subscription`.
Updates an existing subscription. Note that certain properties of a
subscription, such as its topic, are not modifiable.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["subscriber_stub"].UpdateSubscription
@property
def list_subscriptions(self):
"""Return the gRPC stub for :meth:`SubscriberClient.list_subscriptions`.
Lists matching subscriptions.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["subscriber_stub"].ListSubscriptions
@property
def delete_subscription(self):
"""Return the gRPC stub for :meth:`SubscriberClient.delete_subscription`.
Deletes an existing subscription. All messages retained in the
subscription are immediately dropped. Calls to ``Pull`` after deletion
will return ``NOT_FOUND``. After a subscription is deleted, a new one
may be created with the same name, but the new one has no association
with the old subscription or its topic unless the same topic is
specified.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["subscriber_stub"].DeleteSubscription
@property
def modify_ack_deadline(self):
"""Return the gRPC stub for :meth:`SubscriberClient.modify_ack_deadline`.
Modifies the ack deadline for a specific message. This method is useful
to indicate that more time is needed to process a message by the
subscriber, or to make the message available for redelivery if the
processing was interrupted. Note that this does not modify the
subscription-level ``ackDeadlineSeconds`` used for subsequent messages.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["subscriber_stub"].ModifyAckDeadline
@property
def acknowledge(self):
"""Return the gRPC stub for :meth:`SubscriberClient.acknowledge`.
Acknowledges the messages associated with the ``ack_ids`` in the
``AcknowledgeRequest``. The Pub/Sub system can remove the relevant
messages from the subscription.
Acknowledging a message whose ack deadline has expired may succeed, but
such a message may be redelivered later. Acknowledging a message more
than once will not result in an error.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["subscriber_stub"].Acknowledge
@property
def pull(self):
"""Return the gRPC stub for :meth:`SubscriberClient.pull`.
Pulls messages from the server. The server may return ``UNAVAILABLE`` if
there are too many concurrent pull requests pending for the given
subscription.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["subscriber_stub"].Pull
@property
def streaming_pull(self):
"""Return the gRPC stub for :meth:`SubscriberClient.streaming_pull`.
Establishes a stream with the server, which sends messages down to the
client. The client streams acknowledgements and ack deadline
modifications back to the server. The server will close the stream and
return the status on any error. The server may close the stream with
status ``UNAVAILABLE`` to reassign server-side resources, in which case,
the client should re-establish the stream. Flow control can be achieved
by configuring the underlying RPC channel.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["subscriber_stub"].StreamingPull
@property
def modify_push_config(self):
"""Return the gRPC stub for :meth:`SubscriberClient.modify_push_config`.
Modifies the ``PushConfig`` for a specified subscription.
This may be used to change a push subscription to a pull one (signified
by an empty ``PushConfig``) or vice versa, or change the endpoint URL
and other attributes of a push subscription. Messages will accumulate
for delivery continuously through the call regardless of changes to the
``PushConfig``.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["subscriber_stub"].ModifyPushConfig
@property
def list_snapshots(self):
"""Return the gRPC stub for :meth:`SubscriberClient.list_snapshots`.
Lists the existing snapshots. Snapshots are used in
<a href="https://cloud.google.com/pubsub/docs/replay-overview">Seek</a>
operations, which allow
you to manage message acknowledgments in bulk. That is, you can set the
acknowledgment state of messages in an existing subscription to the state
captured by a snapshot.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["subscriber_stub"].ListSnapshots
@property
def create_snapshot(self):
"""Return the gRPC stub for :meth:`SubscriberClient.create_snapshot`.
Creates a snapshot from the requested subscription. Snapshots are used
in Seek operations, which allow you to manage message acknowledgments in
bulk. That is, you can set the acknowledgment state of messages in an
existing subscription to the state captured by a snapshot. If the
snapshot already exists, returns ``ALREADY_EXISTS``. If the requested
subscription doesn't exist, returns ``NOT_FOUND``. If the backlog in the
subscription is too old -- and the resulting snapshot would expire in
less than 1 hour -- then ``FAILED_PRECONDITION`` is returned. See also
the ``Snapshot.expire_time`` field. If the name is not provided in the
request, the server will assign a random name for this snapshot on the
same project as the subscription, conforming to the `resource name
format <https://cloud.google.com/pubsub/docs/admin#resource_names>`__.
The generated name is populated in the returned Snapshot object. Note
that for REST API requests, you must specify a name in the request.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["subscriber_stub"].CreateSnapshot
@property
def update_snapshot(self):
"""Return the gRPC stub for :meth:`SubscriberClient.update_snapshot`.
Updates an existing snapshot. Snapshots are used in
<a href="https://cloud.google.com/pubsub/docs/replay-overview">Seek</a>
operations, which allow
you to manage message acknowledgments in bulk. That is, you can set the
acknowledgment state of messages in an existing subscription to the state
captured by a snapshot.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["subscriber_stub"].UpdateSnapshot
@property
def delete_snapshot(self):
"""Return the gRPC stub for :meth:`SubscriberClient.delete_snapshot`.
Removes an existing snapshot. Snapshots are used in
<a href="https://cloud.google.com/pubsub/docs/replay-overview">Seek</a>
operations, which allow
you to manage message acknowledgments in bulk. That is, you can set the
acknowledgment state of messages in an existing subscription to the state
captured by a snapshot.<br><br>
When the snapshot is deleted, all messages retained in the snapshot
are immediately dropped. After a snapshot is deleted, a new one may be
created with the same name, but the new one has no association with the old
snapshot or its subscription, unless the same subscription is specified.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["subscriber_stub"].DeleteSnapshot
@property
def seek(self):
"""Return the gRPC stub for :meth:`SubscriberClient.seek`.
Seeks an existing subscription to a point in time or to a given snapshot,
whichever is provided in the request. Snapshots are used in
<a href="https://cloud.google.com/pubsub/docs/replay-overview">Seek</a>
operations, which allow
you to manage message acknowledgments in bulk. That is, you can set the
acknowledgment state of messages in an existing subscription to the state
captured by a snapshot. Note that both the subscription and the snapshot
must be on the same topic.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["subscriber_stub"].Seek
@property
def set_iam_policy(self):
"""Return the gRPC stub for :meth:`SubscriberClient.set_iam_policy`.
Sets the access control policy on the specified resource. Replaces any
existing policy.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["iam_policy_stub"].SetIamPolicy
@property
def get_iam_policy(self):
"""Return the gRPC stub for :meth:`SubscriberClient.get_iam_policy`.
Gets the access control policy for a resource.
Returns an empty policy if the resource exists and does not have a policy
set.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["iam_policy_stub"].GetIamPolicy
@property
def test_iam_permissions(self):
"""Return the gRPC stub for :meth:`SubscriberClient.test_iam_permissions`.
Returns permissions that a caller has on the specified resource. If the
resource does not exist, this will return an empty set of permissions,
not a NOT\_FOUND error.
Note: This operation is designed to be used for building
permission-aware UIs and command-line tools, not for authorization
checking. This operation may "fail open" without warning.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["iam_policy_stub"].TestIamPermissions