google-cloud-pubsub 0.20.1 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29,10 +29,9 @@ module Google
29
29
  # delayed.
30
30
  #
31
31
  # @example
32
- # require "google/cloud"
32
+ # require "google/cloud/pubsub"
33
33
  #
34
- # gcloud = Google::Cloud.new
35
- # pubsub = gcloud.pubsub
34
+ # pubsub = Google::Cloud::Pubsub.new
36
35
  #
37
36
  # # Publish a message
38
37
  # topic = pubsub.topic "my-topic"
@@ -42,7 +42,7 @@ module Google
42
42
  #
43
43
  # @see https://cloud.google.com/iam/docs/managing-policies Managing
44
44
  # policies
45
- # @see https://cloud.google.com/pubsub/reference/rpc/google.iam.v1#iampolicy
45
+ # @see https://cloud.google.com/pubsub/docs/reference/rpc/google.iam.v1#iampolicy
46
46
  # google.iam.v1.IAMPolicy
47
47
  #
48
48
  # @attr [String] etag Used to verify whether the policy has changed since
@@ -52,14 +52,13 @@ module Google
52
52
  # roles with an array of members. See [Understanding
53
53
  # Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
54
54
  # listing of primitive and curated roles.
55
- # See [Binding](https://cloud.google.com/pubsub/reference/rpc/google.iam.v1#binding)
55
+ # See [Binding](https://cloud.google.com/pubsub/docs/reference/rpc/google.iam.v1#binding)
56
56
  # for a listing of values and patterns for members.
57
57
  #
58
58
  # @example
59
- # require "google/cloud"
59
+ # require "google/cloud/pubsub"
60
60
  #
61
- # gcloud = Google::Cloud.new
62
- # pubsub = gcloud.pubsub
61
+ # pubsub = Google::Cloud::Pubsub.new
63
62
  # topic = pubsub.topic "my-topic"
64
63
  #
65
64
  # policy = topic.policy # API call
@@ -85,7 +84,7 @@ module Google
85
84
  # See [Understanding
86
85
  # Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
87
86
  # listing of primitive and curated roles.
88
- # See [Binding](https://cloud.google.com/pubsub/reference/rpc/google.iam.v1#binding)
87
+ # See [Binding](https://cloud.google.com/pubsub/docs/reference/rpc/google.iam.v1#binding)
89
88
  # for a listing of values and patterns for members.
90
89
  #
91
90
  # @param [String] role_name A Cloud IAM role, such as
@@ -94,10 +93,9 @@ module Google
94
93
  # `"user:owner@example.com"`.
95
94
  #
96
95
  # @example
97
- # require "google/cloud"
96
+ # require "google/cloud/pubsub"
98
97
  #
99
- # gcloud = Google::Cloud.new
100
- # pubsub = gcloud.pubsub
98
+ # pubsub = Google::Cloud::Pubsub.new
101
99
  # topic = pubsub.topic "my-topic"
102
100
  #
103
101
  # policy = topic.policy # API call
@@ -115,7 +113,7 @@ module Google
115
113
  # policy. See [Understanding
116
114
  # Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
117
115
  # listing of primitive and curated roles. See
118
- # [Binding](https://cloud.google.com/pubsub/reference/rpc/google.iam.v1#binding)
116
+ # [Binding](https://cloud.google.com/pubsub/docs/reference/rpc/google.iam.v1#binding)
119
117
  # for a listing of values and patterns for members.
120
118
  #
121
119
  # @param [String] role_name A Cloud IAM role, such as
@@ -124,10 +122,9 @@ module Google
124
122
  # `"user:owner@example.com"`.
125
123
  #
126
124
  # @example
127
- # require "google/cloud"
125
+ # require "google/cloud/pubsub"
128
126
  #
129
- # gcloud = Google::Cloud.new
130
- # pubsub = gcloud.pubsub
127
+ # pubsub = Google::Cloud::Pubsub.new
131
128
  # topic = pubsub.topic "my-topic"
132
129
  #
133
130
  # policy = topic.policy # API call
@@ -146,16 +143,15 @@ module Google
146
143
  # {#roles}. See [Understanding
147
144
  # Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
148
145
  # listing of primitive and curated roles. See
149
- # [Binding](https://cloud.google.com/pubsub/reference/rpc/google.iam.v1#binding)
146
+ # [Binding](https://cloud.google.com/pubsub/docs/reference/rpc/google.iam.v1#binding)
150
147
  # for a listing of values and patterns for members.
151
148
  #
152
149
  # @return [Array<String>] The members strings, or an empty array.
153
150
  #
154
151
  # @example
155
- # require "google/cloud"
152
+ # require "google/cloud/pubsub"
156
153
  #
157
- # gcloud = Google::Cloud.new
158
- # pubsub = gcloud.pubsub
154
+ # pubsub = Google::Cloud::Pubsub.new
159
155
  # topic = pubsub.topic "my-topic"
160
156
  #
161
157
  # policy = topic.policy
@@ -14,7 +14,7 @@
14
14
 
15
15
 
16
16
  require "google/cloud/errors"
17
- require "google/cloud/core/gce"
17
+ require "google/cloud/core/environment"
18
18
  require "google/cloud/pubsub/service"
19
19
  require "google/cloud/pubsub/credentials"
20
20
  require "google/cloud/pubsub/topic"
@@ -36,17 +36,16 @@ module Google
36
36
  # See {Google::Cloud#pubsub}
37
37
  #
38
38
  # @example
39
- # require "google/cloud"
39
+ # require "google/cloud/pubsub"
40
40
  #
41
- # gcloud = Google::Cloud.new
42
- # pubsub = gcloud.pubsub
41
+ # pubsub = Google::Cloud::Pubsub.new
43
42
  #
44
43
  # topic = pubsub.topic "my-topic"
45
44
  # topic.publish "task completed"
46
45
  #
47
46
  class Project
48
47
  ##
49
- # @private The gRPC Service object.
48
+ # @private The Service object.
50
49
  attr_accessor :service
51
50
 
52
51
  ##
@@ -58,11 +57,12 @@ module Google
58
57
  # The Pub/Sub project connected to.
59
58
  #
60
59
  # @example
61
- # require "google/cloud"
60
+ # require "google/cloud/pubsub"
62
61
  #
63
- # gcloud = Google::Cloud.new "my-todo-project",
64
- # "/path/to/keyfile.json"
65
- # pubsub = gcloud.pubsub
62
+ # pubsub = Google::Cloud::Pubsub.new(
63
+ # project: "my-todo-project",
64
+ # keyfile: "/path/to/keyfile.json"
65
+ # )
66
66
  #
67
67
  # pubsub.project #=> "my-todo-project"
68
68
  #
@@ -76,7 +76,7 @@ module Google
76
76
  ENV["PUBSUB_PROJECT"] ||
77
77
  ENV["GOOGLE_CLOUD_PROJECT"] ||
78
78
  ENV["GCLOUD_PROJECT"] ||
79
- Google::Cloud::Core::GCE.project_id
79
+ Google::Cloud::Core::Environment.project_id
80
80
  end
81
81
 
82
82
  ##
@@ -103,38 +103,33 @@ module Google
103
103
  # `autocreate` is set to `true`.
104
104
  #
105
105
  # @example
106
- # require "google/cloud"
106
+ # require "google/cloud/pubsub"
107
107
  #
108
- # gcloud = Google::Cloud.new
109
- # pubsub = gcloud.pubsub
108
+ # pubsub = Google::Cloud::Pubsub.new
110
109
  # topic = pubsub.topic "existing-topic"
111
110
  #
112
111
  # @example By default `nil` will be returned if topic does not exist.
113
- # require "google/cloud"
112
+ # require "google/cloud/pubsub"
114
113
  #
115
- # gcloud = Google::Cloud.new
116
- # pubsub = gcloud.pubsub
114
+ # pubsub = Google::Cloud::Pubsub.new
117
115
  # topic = pubsub.topic "non-existing-topic" #=> nil
118
116
  #
119
117
  # @example With the `autocreate` option set to `true`.
120
- # require "google/cloud"
118
+ # require "google/cloud/pubsub"
121
119
  #
122
- # gcloud = Google::Cloud.new
123
- # pubsub = gcloud.pubsub
120
+ # pubsub = Google::Cloud::Pubsub.new
124
121
  # topic = pubsub.topic "non-existing-topic", autocreate: true
125
122
  #
126
123
  # @example Create topic in a different project with the `project` flag.
127
- # require "google/cloud"
124
+ # require "google/cloud/pubsub"
128
125
  #
129
- # gcloud = Google::Cloud.new
130
- # pubsub = gcloud.pubsub
126
+ # pubsub = Google::Cloud::Pubsub.new
131
127
  # topic = pubsub.topic "another-topic", project: "another-project"
132
128
  #
133
129
  # @example Skip the lookup against the service with `skip_lookup`:
134
- # require "google/cloud"
130
+ # require "google/cloud/pubsub"
135
131
  #
136
- # gcloud = Google::Cloud.new
137
- # pubsub = gcloud.pubsub
132
+ # pubsub = Google::Cloud::Pubsub.new
138
133
  # topic = pubsub.topic "another-topic", skip_lookup: true
139
134
  #
140
135
  def topic topic_name, autocreate: nil, project: nil, skip_lookup: nil
@@ -158,10 +153,9 @@ module Google
158
153
  # @return [Google::Cloud::Pubsub::Topic]
159
154
  #
160
155
  # @example
161
- # require "google/cloud"
156
+ # require "google/cloud/pubsub"
162
157
  #
163
- # gcloud = Google::Cloud.new
164
- # pubsub = gcloud.pubsub
158
+ # pubsub = Google::Cloud::Pubsub.new
165
159
  # topic = pubsub.create_topic "my-topic"
166
160
  #
167
161
  def create_topic topic_name
@@ -183,10 +177,9 @@ module Google
183
177
  # {Google::Cloud::Pubsub::Topic::List})
184
178
  #
185
179
  # @example
186
- # require "google/cloud"
180
+ # require "google/cloud/pubsub"
187
181
  #
188
- # gcloud = Google::Cloud.new
189
- # pubsub = gcloud.pubsub
182
+ # pubsub = Google::Cloud::Pubsub.new
190
183
  #
191
184
  # topics = pubsub.topics
192
185
  # topics.each do |topic|
@@ -194,10 +187,9 @@ module Google
194
187
  # end
195
188
  #
196
189
  # @example Retrieve all topics: (See {Topic::List#all})
197
- # require "google/cloud"
190
+ # require "google/cloud/pubsub"
198
191
  #
199
- # gcloud = Google::Cloud.new
200
- # pubsub = gcloud.pubsub
192
+ # pubsub = Google::Cloud::Pubsub.new
201
193
  #
202
194
  # topics = pubsub.topics
203
195
  # topics.all do |topic|
@@ -235,35 +227,31 @@ module Google
235
227
  # block.
236
228
  #
237
229
  # @example
238
- # require "google/cloud"
230
+ # require "google/cloud/pubsub"
239
231
  #
240
- # gcloud = Google::Cloud.new
241
- # pubsub = gcloud.pubsub
232
+ # pubsub = Google::Cloud::Pubsub.new
242
233
  #
243
234
  # msg = pubsub.publish "my-topic", "new-message"
244
235
  #
245
236
  # @example A message can be published using a File object:
246
- # require "google/cloud"
237
+ # require "google/cloud/pubsub"
247
238
  #
248
- # gcloud = Google::Cloud.new
249
- # pubsub = gcloud.pubsub
239
+ # pubsub = Google::Cloud::Pubsub.new
250
240
  #
251
241
  # msg = pubsub.publish "my-topic", File.open("message.txt")
252
242
  #
253
243
  # @example Additionally, a message can be published with attributes:
254
- # require "google/cloud"
244
+ # require "google/cloud/pubsub"
255
245
  #
256
- # gcloud = Google::Cloud.new
257
- # pubsub = gcloud.pubsub
246
+ # pubsub = Google::Cloud::Pubsub.new
258
247
  #
259
248
  # msg = pubsub.publish "my-topic", "new-message", foo: :bar,
260
249
  # this: :that
261
250
  #
262
251
  # @example Multiple messages can be sent at the same time using a block:
263
- # require "google/cloud"
252
+ # require "google/cloud/pubsub"
264
253
  #
265
- # gcloud = Google::Cloud.new
266
- # pubsub = gcloud.pubsub
254
+ # pubsub = Google::Cloud::Pubsub.new
267
255
  #
268
256
  # msgs = pubsub.publish "my-topic" do |p|
269
257
  # p.publish "new-message-1", foo: :bar
@@ -272,10 +260,9 @@ module Google
272
260
  # end
273
261
  #
274
262
  # @example With `autocreate`:
275
- # require "google/cloud"
263
+ # require "google/cloud/pubsub"
276
264
  #
277
- # gcloud = Google::Cloud.new
278
- # pubsub = gcloud.pubsub
265
+ # pubsub = Google::Cloud::Pubsub.new
279
266
  #
280
267
  # msg = pubsub.publish "new-topic", "new-message", autocreate: true
281
268
  #
@@ -316,38 +303,34 @@ module Google
316
303
  # @return [Google::Cloud::Pubsub::Subscription]
317
304
  #
318
305
  # @example
319
- # require "google/cloud"
306
+ # require "google/cloud/pubsub"
320
307
  #
321
- # gcloud = Google::Cloud.new
322
- # pubsub = gcloud.pubsub
308
+ # pubsub = Google::Cloud::Pubsub.new
323
309
  #
324
310
  # sub = pubsub.subscribe "my-topic", "my-topic-sub"
325
311
  # puts sub.name # => "my-topic-sub"
326
312
  #
327
313
  # @example The name is optional, and will be generated if not given.
328
- # require "google/cloud"
314
+ # require "google/cloud/pubsub"
329
315
  #
330
- # gcloud = Google::Cloud.new
331
- # pubsub = gcloud.pubsub
316
+ # pubsub = Google::Cloud::Pubsub.new
332
317
  #
333
318
  # sub = pubsub.subscribe "my-topic"
334
319
  # puts sub.name # => "generated-sub-name"
335
320
  #
336
321
  # @example Wait 2 minutes for acknowledgement and push all to endpoint:
337
- # require "google/cloud"
322
+ # require "google/cloud/pubsub"
338
323
  #
339
- # gcloud = Google::Cloud.new
340
- # pubsub = gcloud.pubsub
324
+ # pubsub = Google::Cloud::Pubsub.new
341
325
  #
342
326
  # sub = pubsub.subscribe "my-topic", "my-topic-sub",
343
327
  # deadline: 120,
344
328
  # endpoint: "https://example.com/push"
345
329
  #
346
330
  # @example With `autocreate`:
347
- # require "google/cloud"
331
+ # require "google/cloud/pubsub"
348
332
  #
349
- # gcloud = Google::Cloud.new
350
- # pubsub = gcloud.pubsub
333
+ # pubsub = Google::Cloud::Pubsub.new
351
334
  #
352
335
  # sub = pubsub.subscribe "new-topic", "new-topic-sub",
353
336
  # autocreate: true
@@ -387,19 +370,17 @@ module Google
387
370
  # the subscription does not exist
388
371
  #
389
372
  # @example
390
- # require "google/cloud"
373
+ # require "google/cloud/pubsub"
391
374
  #
392
- # gcloud = Google::Cloud.new
393
- # pubsub = gcloud.pubsub
375
+ # pubsub = Google::Cloud::Pubsub.new
394
376
  #
395
377
  # subscription = pubsub.subscription "my-sub"
396
378
  # puts subscription.name
397
379
  #
398
380
  # @example Skip the lookup against the service with `skip_lookup`:
399
- # require "google/cloud"
381
+ # require "google/cloud/pubsub"
400
382
  #
401
- # gcloud = Google::Cloud.new
402
- # pubsub = gcloud.pubsub
383
+ # pubsub = Google::Cloud::Pubsub.new
403
384
  #
404
385
  # # No API call is made to retrieve the subscription information.
405
386
  # subscription = pubsub.subscription "my-sub", skip_lookup: true
@@ -430,10 +411,9 @@ module Google
430
411
  # {Google::Cloud::Pubsub::Subscription::List})
431
412
  #
432
413
  # @example
433
- # require "google/cloud"
414
+ # require "google/cloud/pubsub"
434
415
  #
435
- # gcloud = Google::Cloud.new
436
- # pubsub = gcloud.pubsub
416
+ # pubsub = Google::Cloud::Pubsub.new
437
417
  #
438
418
  # subscriptions = pubsub.subscriptions
439
419
  # subscriptions.each do |subscription|
@@ -441,10 +421,9 @@ module Google
441
421
  # end
442
422
  #
443
423
  # @example Retrieve all subscriptions: (See {Subscription::List#all})
444
- # require "google/cloud"
424
+ # require "google/cloud/pubsub"
445
425
  #
446
- # gcloud = Google::Cloud.new
447
- # pubsub = gcloud.pubsub
426
+ # pubsub = Google::Cloud::Pubsub.new
448
427
  #
449
428
  # subscriptions = pubsub.subscriptions
450
429
  # subscriptions.all do |subscription|
@@ -25,10 +25,9 @@ module Google
25
25
  # Represents a Pub/Sub {Message} that can be acknowledged or delayed.
26
26
  #
27
27
  # @example
28
- # require "google/cloud"
28
+ # require "google/cloud/pubsub"
29
29
  #
30
- # gcloud = Google::Cloud.new
31
- # pubsub = gcloud.pubsub
30
+ # pubsub = Google::Cloud::Pubsub.new
32
31
  #
33
32
  # sub = pubsub.subscription "my-topic-sub"
34
33
  # received_message = sub.pull.first
@@ -90,10 +89,9 @@ module Google
90
89
  # Acknowledges receipt of the message.
91
90
  #
92
91
  # @example
93
- # require "google/cloud"
92
+ # require "google/cloud/pubsub"
94
93
  #
95
- # gcloud = Google::Cloud.new
96
- # pubsub = gcloud.pubsub
94
+ # pubsub = Google::Cloud::Pubsub.new
97
95
  #
98
96
  # sub = pubsub.subscription "my-topic-sub"
99
97
  # received_message = sub.pull.first
@@ -121,10 +119,9 @@ module Google
121
119
  # the message available for another pull request.
122
120
  #
123
121
  # @example
124
- # require "google/cloud"
122
+ # require "google/cloud/pubsub"
125
123
  #
126
- # gcloud = Google::Cloud.new
127
- # pubsub = gcloud.pubsub
124
+ # pubsub = Google::Cloud::Pubsub.new
128
125
  #
129
126
  # sub = pubsub.subscription "my-topic-sub"
130
127
  # received_message = sub.pull.first
@@ -14,34 +14,39 @@
14
14
 
15
15
 
16
16
  require "google/cloud/errors"
17
- require "google/cloud/core/grpc_backoff"
18
- require "google/pubsub/v1/pubsub_pb"
19
- require "google/iam/v1/iam_policy"
20
- require "google/cloud/core/grpc_utils"
21
- require "json"
17
+ require "google/cloud/pubsub/credentials"
18
+ require "google/cloud/pubsub/version"
19
+ require "google/cloud/pubsub/v1"
20
+ require "google/gax/errors"
22
21
 
23
22
  module Google
24
23
  module Cloud
25
24
  module Pubsub
26
25
  ##
27
- # @private Represents the gRPC Pub/Sub service, including all the API
26
+ # @private Represents the GAX Pub/Sub service, including all the API
28
27
  # methods.
29
28
  class Service
30
- attr_accessor :project, :credentials, :host, :retries, :timeout
29
+ attr_accessor :project, :credentials, :host, :timeout, :client_config
31
30
 
32
31
  ##
33
32
  # Creates a new Service instance.
34
- def initialize project, credentials, host: nil, retries: nil,
35
- timeout: nil
33
+ def initialize project, credentials, host: nil, timeout: nil,
34
+ client_config: nil
36
35
  @project = project
37
36
  @credentials = credentials
38
37
  @host = host || "pubsub.googleapis.com"
39
- @retries = retries
40
38
  @timeout = timeout
39
+ @client_config = client_config || {}
41
40
  end
42
41
 
43
- def creds
42
+ def channel
43
+ require "grpc"
44
+ GRPC::Core::Channel.new host, nil, chan_creds
45
+ end
46
+
47
+ def chan_creds
44
48
  return credentials if insecure?
49
+ require "grpc"
45
50
  GRPC::Core::ChannelCredentials.new.compose \
46
51
  GRPC::Core::CallCredentials.new credentials.client.updater_proc
47
52
  end
@@ -49,10 +54,13 @@ module Google
49
54
  def subscriber
50
55
  return mocked_subscriber if mocked_subscriber
51
56
  @subscriber ||= begin
52
- require "google/pubsub/v1/pubsub_services_pb"
53
-
54
- Google::Pubsub::V1::Subscriber::Stub.new(
55
- host, creds, timeout: timeout)
57
+ V1::SubscriberApi.new(
58
+ service_path: host,
59
+ channel: channel,
60
+ timeout: timeout,
61
+ client_config: client_config,
62
+ app_name: "gcloud-ruby",
63
+ app_version: Google::Cloud::Pubsub::VERSION)
56
64
  end
57
65
  end
58
66
  attr_accessor :mocked_subscriber
@@ -60,25 +68,16 @@ module Google
60
68
  def publisher
61
69
  return mocked_publisher if mocked_publisher
62
70
  @publisher ||= begin
63
- require "google/pubsub/v1/pubsub_services_pb"
64
-
65
- Google::Pubsub::V1::Publisher::Stub.new(
66
- host, creds, timeout: timeout)
71
+ V1::PublisherApi.new(
72
+ service_path: host,
73
+ channel: channel,
74
+ timeout: timeout,
75
+ app_name: "gcloud-ruby",
76
+ app_version: Google::Cloud::Pubsub::VERSION)
67
77
  end
68
78
  end
69
79
  attr_accessor :mocked_publisher
70
80
 
71
- def iam
72
- return mocked_iam if mocked_iam
73
- @iam ||= begin
74
- require "google/iam/v1/iam_policy_services"
75
-
76
- Google::Iam::V1::IAMPolicy::Stub.new(
77
- host, creds, timeout: timeout)
78
- end
79
- end
80
- attr_accessor :mocked_iam
81
-
82
81
  def insecure?
83
82
  credentials == :this_channel_is_insecure
84
83
  end
@@ -90,33 +89,37 @@ module Google
90
89
  # If other attributes are added in the future,
91
90
  # they will be returned here.
92
91
  def get_topic topic_name, options = {}
93
- topic_req = Google::Pubsub::V1::GetTopicRequest.new.tap do |r|
94
- r.topic = topic_path(topic_name, options)
92
+ execute do
93
+ publisher.get_topic topic_path(topic_name, options),
94
+ options: default_options
95
95
  end
96
-
97
- execute { publisher.get_topic topic_req }
98
96
  end
99
97
 
100
98
  ##
101
99
  # Lists matching topics.
102
100
  def list_topics options = {}
103
- topics_req = Google::Pubsub::V1::ListTopicsRequest.new.tap do |r|
104
- r.project = project_path(options)
105
- r.page_token = options[:token] if options[:token]
106
- r.page_size = options[:max] if options[:max]
101
+ call_options = default_options
102
+ if (token = options[:token])
103
+ call_options = Google::Gax::CallOptions.new kwargs: default_headers,
104
+ page_token: token
107
105
  end
108
106
 
109
- execute { publisher.list_topics topics_req }
107
+ execute do
108
+ paged_enum = publisher.list_topics project_path(options),
109
+ page_size: options[:max],
110
+ options: call_options
111
+
112
+ paged_enum.page.response
113
+ end
110
114
  end
111
115
 
112
116
  ##
113
117
  # Creates the given topic with the given name.
114
118
  def create_topic topic_name, options = {}
115
- topic_req = Google::Pubsub::V1::Topic.new.tap do |r|
116
- r.name = topic_path(topic_name, options)
119
+ execute do
120
+ publisher.create_topic topic_path(topic_name, options),
121
+ options: default_options
117
122
  end
118
-
119
- execute { publisher.create_topic topic_req }
120
123
  end
121
124
 
122
125
  ##
@@ -125,11 +128,10 @@ module Google
125
128
  # exist. After a topic is deleted, a new topic may be created with the
126
129
  # same name.
127
130
  def delete_topic topic_name
128
- topic_req = Google::Pubsub::V1::DeleteTopicRequest.new.tap do |r|
129
- r.topic = topic_path(topic_name)
131
+ execute do
132
+ publisher.delete_topic topic_path(topic_name),
133
+ options: default_options
130
134
  end
131
-
132
- execute { publisher.delete_topic topic_req }
133
135
  end
134
136
 
135
137
  ##
@@ -138,184 +140,195 @@ module Google
138
140
  # The messages parameter is an array of arrays.
139
141
  # The first element is the data, second is attributes hash.
140
142
  def publish topic, messages
141
- publish_req = Google::Pubsub::V1::PublishRequest.new(
142
- topic: topic_path(topic),
143
- messages: messages.map do |data, attributes|
144
- Google::Pubsub::V1::PubsubMessage.new(
145
- data: data, attributes: attributes)
146
- end
147
- )
143
+ messages = messages.map do |data, attributes|
144
+ Google::Pubsub::V1::PubsubMessage.new(
145
+ data: data, attributes: attributes)
146
+ end
148
147
 
149
- execute { publisher.publish publish_req }
148
+ execute do
149
+ publisher.publish topic_path(topic), messages,
150
+ options: default_options
151
+ end
150
152
  end
151
153
 
152
154
  ##
153
155
  # Gets the details of a subscription.
154
156
  def get_subscription subscription_name, options = {}
155
- sub_req = Google::Pubsub::V1::GetSubscriptionRequest.new(
156
- subscription: subscription_path(subscription_name, options)
157
- )
158
-
159
- execute { subscriber.get_subscription sub_req }
157
+ subscription = subscription_path(subscription_name, options)
158
+ execute do
159
+ subscriber.get_subscription subscription, options: default_options
160
+ end
160
161
  end
161
162
 
162
163
  ##
163
164
  # Lists matching subscriptions by project and topic.
164
165
  def list_topics_subscriptions topic, options = {}
165
- list_params = { topic: topic_path(topic, options),
166
- page_token: options[:token],
167
- page_size: options[:max]
168
- }.delete_if { |_, v| v.nil? }
169
- list_req = Google::Pubsub::V1::ListTopicSubscriptionsRequest.new \
170
- list_params
166
+ call_options = default_options
167
+ if (token = options[:token])
168
+ call_options = Google::Gax::CallOptions.new kwargs: default_headers,
169
+ page_token: token
170
+ end
171
+
172
+ execute do
173
+ paged_enum = publisher.list_topic_subscriptions \
174
+ topic_path(topic, options),
175
+ page_size: options[:max],
176
+ options: call_options
171
177
 
172
- execute { publisher.list_topic_subscriptions list_req }
178
+ paged_enum.page.response
179
+ end
173
180
  end
174
181
 
175
182
  ##
176
183
  # Lists matching subscriptions by project.
177
184
  def list_subscriptions options = {}
178
- list_params = { project: project_path(options),
179
- page_token: options[:token],
180
- page_size: options[:max]
181
- }.delete_if { |_, v| v.nil? }
182
- list_req = Google::Pubsub::V1::ListSubscriptionsRequest.new(
183
- list_params)
185
+ call_options = default_options
186
+ if (token = options[:token])
187
+ call_options = Google::Gax::CallOptions.new kwargs: default_headers,
188
+ page_token: token
189
+ end
190
+
191
+ execute do
192
+ paged_enum = subscriber.list_subscriptions project_path(options),
193
+ page_size: options[:max],
194
+ options: call_options
184
195
 
185
- execute { subscriber.list_subscriptions list_req }
196
+ paged_enum.page.response
197
+ end
186
198
  end
187
199
 
188
200
  ##
189
201
  # Creates a subscription on a given topic for a given subscriber.
190
202
  def create_subscription topic, subscription_name, options = {}
191
- sub_params = { name: subscription_path(subscription_name, options),
192
- topic: topic_path(topic),
193
- ack_deadline_seconds: options[:deadline]
194
- }.delete_if { |_, v| v.nil? }
195
- sub_req = Google::Pubsub::V1::Subscription.new sub_params
196
- if options[:endpoint]
197
- sub_req.push_config = Google::Pubsub::V1::PushConfig.new(
198
- push_endpoint: options[:endpoint],
199
- attributes: (options[:attributes] || {}).to_h)
203
+ name = subscription_path(subscription_name, options)
204
+ topic = topic_path(topic)
205
+ push_config = if options[:endpoint]
206
+ Google::Pubsub::V1::PushConfig.new \
207
+ push_endpoint: options[:endpoint],
208
+ attributes: (options[:attributes] || {}).to_h
209
+ end
210
+ deadline = options[:deadline]
211
+
212
+ execute do
213
+ subscriber.create_subscription name,
214
+ topic,
215
+ push_config: push_config,
216
+ ack_deadline_seconds: deadline,
217
+ options: default_options
200
218
  end
201
-
202
- execute { subscriber.create_subscription sub_req }
203
219
  end
204
220
 
205
221
  ##
206
222
  # Deletes an existing subscription.
207
223
  # All pending messages in the subscription are immediately dropped.
208
224
  def delete_subscription subscription
209
- del_req = Google::Pubsub::V1::DeleteSubscriptionRequest.new(
210
- subscription: subscription_path(subscription)
211
- )
212
-
213
- execute { subscriber.delete_subscription del_req }
225
+ execute do
226
+ subscriber.delete_subscription subscription_path(subscription),
227
+ options: default_options
228
+ end
214
229
  end
215
230
 
216
231
  ##
217
232
  # Pulls a single message from the server.
218
233
  def pull subscription, options = {}
219
- pull_req = Google::Pubsub::V1::PullRequest.new(
220
- subscription: subscription_path(subscription, options),
221
- return_immediately: !(!options.fetch(:immediate, true)),
222
- max_messages: options.fetch(:max, 100).to_i
223
- )
224
-
225
- execute { subscriber.pull pull_req }
234
+ subscription = subscription_path(subscription, options)
235
+ max_messages = options.fetch(:max, 100).to_i
236
+ return_immediately = !(!options.fetch(:immediate, true))
237
+
238
+ execute do
239
+ subscriber.pull subscription,
240
+ max_messages,
241
+ return_immediately: return_immediately,
242
+ options: default_options
243
+ end
226
244
  end
227
245
 
228
246
  ##
229
247
  # Acknowledges receipt of a message.
230
248
  def acknowledge subscription, *ack_ids
231
- ack_req = Google::Pubsub::V1::AcknowledgeRequest.new(
232
- subscription: subscription_path(subscription),
233
- ack_ids: ack_ids
234
- )
235
-
236
- execute { subscriber.acknowledge ack_req }
249
+ execute do
250
+ subscriber.acknowledge subscription_path(subscription), ack_ids,
251
+ options: default_options
252
+ end
237
253
  end
238
254
 
239
255
  ##
240
256
  # Modifies the PushConfig for a specified subscription.
241
257
  def modify_push_config subscription, endpoint, attributes
258
+ subscription = subscription_path(subscription)
242
259
  # Convert attributes to strings to match the protobuf definition
243
260
  attributes = Hash[attributes.map { |k, v| [String(k), String(v)] }]
244
-
245
- mpc_req = Google::Pubsub::V1::ModifyPushConfigRequest.new(
246
- subscription: subscription_path(subscription),
247
- push_config: Google::Pubsub::V1::PushConfig.new(
248
- push_endpoint: endpoint,
249
- attributes: attributes
250
- )
261
+ push_config = Google::Pubsub::V1::PushConfig.new(
262
+ push_endpoint: endpoint,
263
+ attributes: attributes
251
264
  )
252
265
 
253
- execute { subscriber.modify_push_config mpc_req }
266
+ execute do
267
+ subscriber.modify_push_config subscription, push_config,
268
+ options: default_options
269
+ end
254
270
  end
255
271
 
256
272
  ##
257
273
  # Modifies the ack deadline for a specific message.
258
274
  def modify_ack_deadline subscription, ids, deadline
259
- mad_req = Google::Pubsub::V1::ModifyAckDeadlineRequest.new(
260
- subscription: subscription_path(subscription),
261
- ack_ids: Array(ids),
262
- ack_deadline_seconds: deadline
263
- )
264
-
265
- execute { subscriber.modify_ack_deadline mad_req }
275
+ execute do
276
+ subscriber.modify_ack_deadline subscription_path(subscription),
277
+ Array(ids),
278
+ deadline, options: default_options
279
+ end
266
280
  end
267
281
 
268
282
  def get_topic_policy topic_name, options = {}
269
- get_req = Google::Iam::V1::GetIamPolicyRequest.new(
270
- resource: topic_path(topic_name, options)
271
- )
272
-
273
- execute { iam.get_iam_policy get_req }
283
+ execute do
284
+ publisher.get_iam_policy topic_path(topic_name, options),
285
+ options: default_options
286
+ end
274
287
  end
275
288
 
276
289
  def set_topic_policy topic_name, new_policy, options = {}
277
- set_req = Google::Iam::V1::SetIamPolicyRequest.new(
278
- resource: topic_path(topic_name, options),
279
- policy: new_policy
280
- )
290
+ resource = topic_path(topic_name, options)
281
291
 
282
- execute { iam.set_iam_policy set_req }
292
+ execute do
293
+ publisher.set_iam_policy resource, new_policy,
294
+ options: default_options
295
+ end
283
296
  end
284
297
 
285
298
  def test_topic_permissions topic_name, permissions, options = {}
286
- test_req = Google::Iam::V1::TestIamPermissionsRequest.new(
287
- resource: topic_path(topic_name, options),
288
- permissions: permissions
289
- )
299
+ resource = topic_path(topic_name, options)
290
300
 
291
- execute { iam.test_iam_permissions test_req }
301
+ execute do
302
+ publisher.test_iam_permissions resource, permissions,
303
+ options: default_options
304
+ end
292
305
  end
293
306
 
294
307
  def get_subscription_policy subscription_name, options = {}
295
- get_req = Google::Iam::V1::GetIamPolicyRequest.new(
296
- resource: subscription_path(subscription_name, options)
297
- )
308
+ resource = subscription_path(subscription_name, options)
298
309
 
299
- execute { iam.get_iam_policy get_req }
310
+ execute do
311
+ subscriber.get_iam_policy resource, options: default_options
312
+ end
300
313
  end
301
314
 
302
315
  def set_subscription_policy subscription_name, new_policy, options = {}
303
- set_req = Google::Iam::V1::SetIamPolicyRequest.new(
304
- resource: subscription_path(subscription_name, options),
305
- policy: new_policy
306
- )
316
+ resource = subscription_path(subscription_name, options)
307
317
 
308
- execute { iam.set_iam_policy set_req }
318
+ execute do
319
+ subscriber.set_iam_policy resource, new_policy,
320
+ options: default_options
321
+ end
309
322
  end
310
323
 
311
324
  def test_subscription_permissions subscription_name,
312
325
  permissions, options = {}
313
- test_req = Google::Iam::V1::TestIamPermissionsRequest.new(
314
- resource: subscription_path(subscription_name, options),
315
- permissions: permissions
316
- )
326
+ resource = subscription_path(subscription_name, options)
317
327
 
318
- execute { iam.test_iam_permissions test_req }
328
+ execute do
329
+ subscriber.test_iam_permissions resource, permissions,
330
+ options: default_options
331
+ end
319
332
  end
320
333
 
321
334
  def project_path options = {}
@@ -339,12 +352,19 @@ module Google
339
352
 
340
353
  protected
341
354
 
355
+ def default_headers
356
+ { "google-cloud-resource-prefix" => "projects/#{@project}" }
357
+ end
358
+
359
+ def default_options
360
+ Google::Gax::CallOptions.new kwargs: default_headers
361
+ end
362
+
342
363
  def execute
343
- Google::Cloud::Core::GrpcBackoff.new(retries: retries).execute do
344
- yield
345
- end
346
- rescue GRPC::BadStatus => e
347
- raise Error.from_error(e)
364
+ yield
365
+ rescue Google::Gax::GaxError => e
366
+ # GaxError wraps BadStatus, but exposes it as #cause
367
+ raise Google::Cloud::Error.from_error(e.cause)
348
368
  end
349
369
  end
350
370
  end