google-cloud-pubsub 2.22.0 → 3.0.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.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +27 -0
  3. data/OVERVIEW.md +188 -144
  4. data/lib/google/cloud/pubsub/admin_clients.rb +116 -0
  5. data/lib/google/cloud/pubsub/async_publisher.rb +9 -9
  6. data/lib/google/cloud/pubsub/batch_publisher.rb +4 -4
  7. data/lib/google/cloud/pubsub/errors.rb +3 -3
  8. data/lib/google/cloud/pubsub/message.rb +8 -8
  9. data/lib/google/cloud/pubsub/{subscriber → message_listener}/enumerator_queue.rb +1 -1
  10. data/lib/google/cloud/pubsub/{subscriber → message_listener}/inventory.rb +2 -4
  11. data/lib/google/cloud/pubsub/{subscriber → message_listener}/sequencer.rb +1 -1
  12. data/lib/google/cloud/pubsub/{subscriber → message_listener}/stream.rb +10 -10
  13. data/lib/google/cloud/pubsub/{subscriber → message_listener}/timed_unary_buffer.rb +1 -1
  14. data/lib/google/cloud/pubsub/message_listener.rb +413 -0
  15. data/lib/google/cloud/pubsub/project.rb +98 -531
  16. data/lib/google/cloud/pubsub/publisher.rb +373 -0
  17. data/lib/google/cloud/pubsub/received_message.rb +50 -45
  18. data/lib/google/cloud/pubsub/service.rb +24 -386
  19. data/lib/google/cloud/pubsub/subscriber.rb +442 -279
  20. data/lib/google/cloud/pubsub/version.rb +1 -1
  21. data/lib/google/cloud/pubsub.rb +8 -15
  22. data/lib/google-cloud-pubsub.rb +5 -4
  23. metadata +18 -26
  24. data/lib/google/cloud/pubsub/policy.rb +0 -188
  25. data/lib/google/cloud/pubsub/retry_policy.rb +0 -88
  26. data/lib/google/cloud/pubsub/schema/list.rb +0 -180
  27. data/lib/google/cloud/pubsub/schema.rb +0 -378
  28. data/lib/google/cloud/pubsub/snapshot/list.rb +0 -178
  29. data/lib/google/cloud/pubsub/snapshot.rb +0 -205
  30. data/lib/google/cloud/pubsub/subscription/list.rb +0 -205
  31. data/lib/google/cloud/pubsub/subscription/push_config.rb +0 -268
  32. data/lib/google/cloud/pubsub/subscription.rb +0 -1467
  33. data/lib/google/cloud/pubsub/topic/list.rb +0 -171
  34. data/lib/google/cloud/pubsub/topic.rb +0 -1100
@@ -1,378 +0,0 @@
1
- # Copyright 2021 Google LLC
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # https://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- require "google/cloud/pubsub/schema/list"
17
- require "google/cloud/pubsub/v1"
18
-
19
- module Google
20
- module Cloud
21
- module PubSub
22
- ##
23
- # # Schema
24
- #
25
- # A schema resource.
26
- #
27
- # @example
28
- # require "google/cloud/pubsub"
29
- #
30
- # pubsub = Google::Cloud::PubSub.new
31
- #
32
- # schema = pubsub.schema "my-schema"
33
- # schema.name #=> "projects/my-project/schemas/my-schema"
34
- # schema.type #=> :PROTOCOL_BUFFER
35
- #
36
- class Schema
37
- ##
38
- # @private The Service object.
39
- attr_accessor :service
40
-
41
- ##
42
- # @private The gRPC Google::Cloud::PubSub::V1::Schema object.
43
- attr_accessor :grpc
44
-
45
- ##
46
- # @private Create a new Schema instance.
47
- def initialize grpc, service, view: nil
48
- @grpc = grpc
49
- @service = service
50
- @exists = nil
51
- @view = view || :FULL
52
- end
53
-
54
- ##
55
- # The name of the schema.
56
- #
57
- # @return [String] A fully-qualified schema name in the form `projects/{project_id}/schemas/{schema_id}`.
58
- #
59
- def name
60
- @grpc.name
61
- end
62
-
63
- ##
64
- # The type of the schema. Possible values include:
65
- #
66
- # * `PROTOCOL_BUFFER` - A Protocol Buffer schema definition.
67
- # * `AVRO` - An Avro schema definition.
68
- #
69
- # @return [String, nil] The upper-case type name.
70
- #
71
- def type
72
- return nil if reference?
73
- @grpc.type
74
- end
75
-
76
- ##
77
- # The definition of the schema. This should be a string representing the full definition of the schema that is a
78
- # valid schema definition of the type specified in {#type}.
79
- #
80
- # @return [String, nil] The schema definition.
81
- #
82
- def definition
83
- return nil if reference?
84
- @grpc.definition if @grpc.definition && !@grpc.definition.empty?
85
- end
86
-
87
- ##
88
- # The revision ID of the schema.
89
- #
90
- # @return [String] The revision id.
91
- # @return [nil] If this object is a reference.
92
- #
93
- def revision_id
94
- return nil if reference?
95
- @grpc.revision_id if @grpc.revision_id && !@grpc.revision_id.empty?
96
- end
97
-
98
- ##
99
- # Validates a message against a schema.
100
- #
101
- # @param message_data [String] Message to validate against the provided `schema_spec`.
102
- # @param message_encoding [Symbol, String] The encoding of the message validated against the schema. Values
103
- # include:
104
- #
105
- # * `JSON` - JSON encoding.
106
- # * `BINARY` - Binary encoding, as defined by the schema type. For some schema types, binary encoding may not
107
- # be available.
108
- #
109
- # @return [Boolean] Returns `true` if the message validiation succeeds, `false` otherwise.
110
- #
111
- # @example
112
- # require "google/cloud/pubsub"
113
- #
114
- # pubsub = Google::Cloud::PubSub.new
115
- # schema = pubsub.schema "my-schema"
116
- #
117
- # message_data = { "name" => "Alaska", "post_abbr" => "AK" }.to_json
118
- # schema.validate_message message_data, :json
119
- #
120
- def validate_message message_data, message_encoding
121
- message_encoding = message_encoding.to_s.upcase
122
- service.validate_message message_data, message_encoding, schema_name: name
123
- true
124
- rescue Google::Cloud::InvalidArgumentError
125
- false
126
- end
127
-
128
- ##
129
- # Removes the schema, if it exists.
130
- #
131
- # @return [Boolean] Returns `true` if the schema was deleted.
132
- #
133
- # @example
134
- # require "google/cloud/pubsub"
135
- #
136
- # pubsub = Google::Cloud::PubSub.new
137
- # schema = pubsub.schema "my-schema"
138
- #
139
- # schema.delete
140
- #
141
- def delete
142
- ensure_service!
143
- service.delete_schema name
144
- true
145
- end
146
-
147
- ##
148
- # Reloads the schema with current data from the Pub/Sub service.
149
- #
150
- # @param view [Symbol, String, nil] The set of fields to return in the response. Possible values:
151
- # * `BASIC` - Include the `name` and `type` of the schema, but not the `definition`.
152
- # * `FULL` - Include all Schema object fields.
153
- #
154
- # Optional. If not provided or `nil`, the last non-nil `view` argument to this method will be used if one has
155
- # been given, othewise `FULL` will be used.
156
- #
157
- # @return [Google::Cloud::PubSub::Schema] Returns the reloaded schema.
158
- #
159
- # @example Skip retrieving the schema from the service, then load it:
160
- # require "google/cloud/pubsub"
161
- #
162
- # pubsub = Google::Cloud::PubSub.new
163
- # schema = pubsub.schema "my-schema", skip_lookup: true
164
- #
165
- # schema.reload!
166
- #
167
- # @example Use the `view` option to load the basic or full resource:
168
- # require "google/cloud/pubsub"
169
- #
170
- # pubsub = Google::Cloud::PubSub.new
171
- # schema = pubsub.schema "my-schema", view: :basic
172
- # schema.resource_partial? #=> true
173
- #
174
- # schema.reload! view: :full
175
- # schema.resource_partial? #=> false
176
- #
177
- # @!group Lifecycle
178
- #
179
- def reload! view: nil
180
- ensure_service!
181
- @view = view || @view
182
- @grpc = service.get_schema name, @view
183
- @reference = nil
184
- @exists = nil
185
- self
186
- end
187
- alias refresh! reload!
188
-
189
- ##
190
- # Determines whether the schema exists in the Pub/Sub service.
191
- #
192
- # @example
193
- # require "google/cloud/pubsub"
194
- #
195
- # pubsub = Google::Cloud::PubSub.new
196
- #
197
- # schema = pubsub.schema "my-schema"
198
- # schema.exists? #=> true
199
- #
200
- def exists?
201
- # Always true if the object is not set as reference
202
- return true unless reference?
203
- # If we have a value, return it
204
- return @exists unless @exists.nil?
205
- ensure_grpc!
206
- @exists = true
207
- rescue Google::Cloud::NotFoundError
208
- @exists = false
209
- end
210
-
211
- ##
212
- # Commits a new schema revision to an existing schema.
213
- #
214
- # @param definition [String] The definition of the schema. This should
215
- # contain a string representing the full definition of the schema that
216
- # is a valid schema definition of the type specified in `type`. See
217
- # https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.schemas#Schema
218
- # for details.
219
- # @param type [String, Symbol] The type of the schema. Possible values are
220
- # case-insensitive and include:
221
- #
222
- # * `PROTOCOL_BUFFER` - A Protocol Buffer schema definition.
223
- # * `AVRO` - An Avro schema definition.
224
- #
225
- # @return [Google::Cloud::PubSub::Schema]
226
- #
227
- # @example
228
- # require "google/cloud/pubsub"
229
- #
230
- # pubsub = Google::Cloud::PubSub.new
231
- #
232
- # schema = pubsub.schema "my-schema"
233
- #
234
- # definition = File.read /path/to/file
235
- #
236
- # revision_schema = schema.commit definition, type: :protocol_buffer
237
- #
238
- def commit definition, type
239
- type = type.to_s.upcase
240
- grpc = service.commit_schema name, definition, type
241
- Schema.from_grpc grpc, service, view: @view
242
- end
243
-
244
- ##
245
- # Lists all schema revisions for the named schema.
246
- #
247
- # @param view [Symbol, String, nil] The set of fields to return in the response. Possible values:
248
- # * `BASIC` - Include the `name` and `type` of the schema, but not the `definition`.
249
- # * `FULL` - Include all Schema object fields.
250
- #
251
- # @return [Google::Cloud::PubSub::V1::ListSchemaRevisionsResponse]
252
- #
253
- # @example
254
- # require "google/cloud/pubsub"
255
- #
256
- # pubsub = Google::Cloud::PubSub.new
257
- #
258
- # schema = pubsub.schema "my-schema"
259
- # schema.list_revisions
260
- #
261
- def list_revisions view: nil, page_size: nil, page_token: nil
262
- service.list_schema_revisions name,
263
- view || @view,
264
- page_size,
265
- page_token
266
- end
267
-
268
- ##
269
- # Determines whether the schema object was created without retrieving the
270
- # resource representation from the Pub/Sub service.
271
- #
272
- # @return [Boolean] `true` when the schema was created without a resource
273
- # representation, `false` otherwise.
274
- #
275
- # @example
276
- # require "google/cloud/pubsub"
277
- #
278
- # pubsub = Google::Cloud::PubSub.new
279
- #
280
- # schema = pubsub.schema "my-schema", skip_lookup: true
281
- # schema.reference? #=> true
282
- #
283
- def reference?
284
- @grpc.type.nil? || @grpc.type == :TYPE_UNSPECIFIED
285
- end
286
-
287
- ##
288
- # Determines whether the schema object was created with a resource
289
- # representation from the Pub/Sub service.
290
- #
291
- # @return [Boolean] `true` when the schema was created with a resource
292
- # representation, `false` otherwise.
293
- #
294
- # @example
295
- # require "google/cloud/pubsub"
296
- #
297
- # pubsub = Google::Cloud::PubSub.new
298
- #
299
- # schema = pubsub.schema "my-schema"
300
- # schema.resource? #=> true
301
- #
302
- def resource?
303
- !reference?
304
- end
305
-
306
- ##
307
- # Whether the schema was created with a partial resource representation
308
- # from the Pub/Sub service.
309
- #
310
- # @return [Boolean] `true` when the schema was created with a partial
311
- # resource representation, `false` otherwise.
312
- #
313
- # @example
314
- # require "google/cloud/pubsub"
315
- #
316
- # pubsub = Google::Cloud::PubSub.new
317
- # schema = pubsub.schema "my-schema", view: :basic
318
- #
319
- # schema.resource_partial? #=> true
320
- # schema.reload! view: :full # Loads the full resource.
321
- # schema.resource_partial? #=> false
322
- #
323
- def resource_partial?
324
- resource? && !resource_full?
325
- end
326
-
327
- ##
328
- # Whether the schema was created with a full resource representation
329
- # from the Pub/Sub service.
330
- #
331
- # @return [Boolean] `true` when the schema was created with a full
332
- # resource representation, `false` otherwise.
333
- #
334
- # @example
335
- # require "google/cloud/pubsub"
336
- #
337
- # pubsub = Google::Cloud::PubSub.new
338
- # schema = pubsub.schema "my-schema"
339
- #
340
- # schema.resource_full? #=> true
341
- #
342
- def resource_full?
343
- resource? && @grpc.definition && !@grpc.definition.empty?
344
- end
345
-
346
- ##
347
- # @private New Schema from a Google::Cloud::PubSub::V1::Schema object.
348
- def self.from_grpc grpc, service, view: nil
349
- new grpc, service, view: view
350
- end
351
-
352
- ##
353
- # @private New reference Schema object without making an HTTP request.
354
- def self.from_name name, view, service, options = {}
355
- grpc = Google::Cloud::PubSub::V1::Schema.new name: service.schema_path(name, options)
356
- from_grpc grpc, service, view: view
357
- end
358
-
359
- protected
360
-
361
- ##
362
- # @private Raise an error unless an active connection to the service is available.
363
- def ensure_service!
364
- raise "Must have active connection to service" unless service
365
- end
366
-
367
- ##
368
- # Ensures a Google::Cloud::PubSub::V1::Schema object exists.
369
- def ensure_grpc!
370
- ensure_service!
371
- reload! if reference?
372
- end
373
- end
374
- end
375
-
376
- Pubsub = PubSub unless const_defined? :Pubsub
377
- end
378
- end
@@ -1,178 +0,0 @@
1
- # Copyright 2017 Google LLC
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # https://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- require "delegate"
17
-
18
- module Google
19
- module Cloud
20
- module PubSub
21
- class Snapshot
22
- ##
23
- # Snapshot::List is a special case Array with additional values.
24
- class List < DelegateClass(::Array)
25
- ##
26
- # If not empty, indicates that there are more snapshots
27
- # that match the request and this value should be passed to
28
- # the next {Google::Cloud::PubSub::Project#snapshots} to continue.
29
- attr_accessor :token
30
-
31
- ##
32
- # @private Create a new Snapshot::List with an array of values.
33
- def initialize arr = []
34
- @prefix = nil
35
- @token = nil
36
- @max = nil
37
- super arr
38
- end
39
-
40
- ##
41
- # Whether there a next page of snapshots.
42
- #
43
- # @return [Boolean]
44
- #
45
- # @example
46
- # require "google/cloud/pubsub"
47
- #
48
- # pubsub = Google::Cloud::PubSub.new
49
- #
50
- # snapshots = pubsub.snapshots
51
- # if snapshots.next?
52
- # next_snapshots = snapshots.next
53
- # end
54
- #
55
- def next?
56
- !token.nil?
57
- end
58
-
59
- ##
60
- # Retrieve the next page of snapshots.
61
- #
62
- # @return [Snapshot::List]
63
- #
64
- # @example
65
- # require "google/cloud/pubsub"
66
- #
67
- # pubsub = Google::Cloud::PubSub.new
68
- #
69
- # snapshots = pubsub.snapshots
70
- # if snapshots.next?
71
- # next_snapshots = snapshots.next
72
- # end
73
- #
74
- def next
75
- return nil unless next?
76
- ensure_service!
77
- next_snapshots
78
- end
79
-
80
- ##
81
- # Retrieves remaining results by repeatedly invoking {#next} until
82
- # {#next?} returns `false`. Calls the given block once for each
83
- # result, which is passed as the argument to the block.
84
- #
85
- # An Enumerator is returned if no block is given.
86
- #
87
- # This method will make repeated API calls until all remaining results
88
- # are retrieved. (Unlike `#each`, for example, which merely iterates
89
- # over the results returned by a single API call.) Use with caution.
90
- #
91
- # @param [Integer] request_limit The upper limit of API requests to
92
- # make to load all snapshots. Default is no limit.
93
- # @yield [snapshot] The block for accessing each snapshot.
94
- # @yieldparam [Snapshot] snapshot The snapshot object.
95
- #
96
- # @return [Enumerator]
97
- #
98
- # @example Iterating each snapshot by passing a block:
99
- # require "google/cloud/pubsub"
100
- #
101
- # pubsub = Google::Cloud::PubSub.new
102
- #
103
- # snapshots = pubsub.snapshots
104
- # snapshots.all do |snapshot|
105
- # puts snapshot.name
106
- # end
107
- #
108
- # @example Using the enumerator by not passing a block:
109
- # require "google/cloud/pubsub"
110
- #
111
- # pubsub = Google::Cloud::PubSub.new
112
- #
113
- # snapshots = pubsub.snapshots
114
- # all_names = snapshots.all.map do |snapshot|
115
- # snapshot.name
116
- # end
117
- #
118
- # @example Limit the number of API calls made:
119
- # require "google/cloud/pubsub"
120
- #
121
- # pubsub = Google::Cloud::PubSub.new
122
- #
123
- # snapshots = pubsub.snapshots
124
- # snapshots.all(request_limit: 10) do |snapshot|
125
- # puts snapshot.name
126
- # end
127
- #
128
- def all request_limit: nil, &block
129
- request_limit = request_limit.to_i if request_limit
130
- return enum_for :all, request_limit: request_limit unless block_given?
131
- results = self
132
- loop do
133
- results.each(&block)
134
- if request_limit
135
- request_limit -= 1
136
- break if request_limit.negative?
137
- end
138
- break unless results.next?
139
- results = results.next
140
- end
141
- end
142
-
143
- ##
144
- # @private New Snapshots::List from a
145
- # Google::Cloud::PubSub::V1::ListSnapshotsRequest object.
146
- def self.from_grpc grpc_list, service, max = nil
147
- subs = new(Array(grpc_list.snapshots).map do |grpc|
148
- Snapshot.from_grpc grpc, service
149
- end)
150
- token = grpc_list.next_page_token
151
- token = nil if token == "".freeze
152
- subs.instance_variable_set :@token, token
153
- subs.instance_variable_set :@service, service
154
- subs.instance_variable_set :@max, max
155
- subs
156
- end
157
-
158
- protected
159
-
160
- ##
161
- # @private Raise an error unless an active connection to the service
162
- # is available.
163
- def ensure_service!
164
- raise "Must have active connection to service" unless @service
165
- end
166
-
167
- def next_snapshots
168
- options = { prefix: @prefix, token: @token, max: @max }
169
- grpc = @service.list_snapshots options
170
- self.class.from_grpc grpc, @service, @max
171
- end
172
- end
173
- end
174
- end
175
-
176
- Pubsub = PubSub unless const_defined? :Pubsub
177
- end
178
- end