google-cloud-pubsub 2.22.0 → 3.2.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 (36) hide show
  1. checksums.yaml +4 -4
  2. data/AUTHENTICATION.md +12 -1
  3. data/CHANGELOG.md +69 -0
  4. data/OVERVIEW.md +189 -145
  5. data/lib/google/cloud/pubsub/admin_clients.rb +116 -0
  6. data/lib/google/cloud/pubsub/async_publisher.rb +20 -19
  7. data/lib/google/cloud/pubsub/batch_publisher.rb +7 -5
  8. data/lib/google/cloud/pubsub/errors.rb +3 -3
  9. data/lib/google/cloud/pubsub/internal_logger.rb +76 -0
  10. data/lib/google/cloud/pubsub/message.rb +8 -8
  11. data/lib/google/cloud/pubsub/{subscriber → message_listener}/enumerator_queue.rb +1 -1
  12. data/lib/google/cloud/pubsub/{subscriber → message_listener}/inventory.rb +13 -10
  13. data/lib/google/cloud/pubsub/{subscriber → message_listener}/sequencer.rb +1 -1
  14. data/lib/google/cloud/pubsub/{subscriber → message_listener}/stream.rb +61 -18
  15. data/lib/google/cloud/pubsub/{subscriber → message_listener}/timed_unary_buffer.rb +29 -9
  16. data/lib/google/cloud/pubsub/message_listener.rb +414 -0
  17. data/lib/google/cloud/pubsub/project.rb +102 -530
  18. data/lib/google/cloud/pubsub/publisher.rb +424 -0
  19. data/lib/google/cloud/pubsub/received_message.rb +50 -45
  20. data/lib/google/cloud/pubsub/service.rb +34 -385
  21. data/lib/google/cloud/pubsub/subscriber.rb +442 -279
  22. data/lib/google/cloud/pubsub/version.rb +1 -1
  23. data/lib/google/cloud/pubsub.rb +37 -19
  24. data/lib/google-cloud-pubsub.rb +27 -8
  25. metadata +19 -26
  26. data/lib/google/cloud/pubsub/policy.rb +0 -188
  27. data/lib/google/cloud/pubsub/retry_policy.rb +0 -88
  28. data/lib/google/cloud/pubsub/schema/list.rb +0 -180
  29. data/lib/google/cloud/pubsub/schema.rb +0 -378
  30. data/lib/google/cloud/pubsub/snapshot/list.rb +0 -178
  31. data/lib/google/cloud/pubsub/snapshot.rb +0 -205
  32. data/lib/google/cloud/pubsub/subscription/list.rb +0 -205
  33. data/lib/google/cloud/pubsub/subscription/push_config.rb +0 -268
  34. data/lib/google/cloud/pubsub/subscription.rb +0 -1467
  35. data/lib/google/cloud/pubsub/topic/list.rb +0 -171
  36. data/lib/google/cloud/pubsub/topic.rb +0 -1100
@@ -1,180 +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 "delegate"
17
-
18
- module Google
19
- module Cloud
20
- module PubSub
21
- class Schema
22
- ##
23
- # Schema::List is a special case Array with additional values.
24
- class List < DelegateClass(::Array)
25
- ##
26
- # If not empty, indicates that there are more schemas
27
- # that match the request and this value should be passed to
28
- # the next {Google::Cloud::PubSub::Project#schemas} to continue.
29
- attr_accessor :token
30
-
31
- ##
32
- # @private Create a new Schema::List with an array of values.
33
- def initialize arr = []
34
- @prefix = nil
35
- @token = nil
36
- @view = nil
37
- @max = nil
38
- super arr
39
- end
40
-
41
- ##
42
- # Whether there a next page of schemas.
43
- #
44
- # @return [Boolean]
45
- #
46
- # @example
47
- # require "google/cloud/pubsub"
48
- #
49
- # pubsub = Google::Cloud::PubSub.new
50
- #
51
- # schemas = pubsub.schemas
52
- # if schemas.next?
53
- # next_schemas = schemas.next
54
- # end
55
- #
56
- def next?
57
- !token.nil?
58
- end
59
-
60
- ##
61
- # Retrieve the next page of schemas.
62
- #
63
- # @return [Schema::List]
64
- #
65
- # @example
66
- # require "google/cloud/pubsub"
67
- #
68
- # pubsub = Google::Cloud::PubSub.new
69
- #
70
- # schemas = pubsub.schemas
71
- # if schemas.next?
72
- # next_schemas = schemas.next
73
- # end
74
- #
75
- def next
76
- return nil unless next?
77
- ensure_service!
78
- next_schemas
79
- end
80
-
81
- ##
82
- # Retrieves remaining results by repeatedly invoking {#next} until
83
- # {#next?} returns `false`. Calls the given block once for each
84
- # result, which is passed as the argument to the block.
85
- #
86
- # An Enumerator is returned if no block is given.
87
- #
88
- # This method will make repeated API calls until all remaining results
89
- # are retrieved. (Unlike `#each`, for example, which merely iterates
90
- # over the results returned by a single API call.) Use with caution.
91
- #
92
- # @param [Integer] request_limit The upper limit of API requests to
93
- # make to load all schemas. Default is no limit.
94
- # @yield [schema] The block for accessing each schema.
95
- # @yieldparam [Schema] schema The schema object.
96
- #
97
- # @return [Enumerator]
98
- #
99
- # @example Iterating each schema by passing a block:
100
- # require "google/cloud/pubsub"
101
- #
102
- # pubsub = Google::Cloud::PubSub.new
103
- #
104
- # schemas = pubsub.schemas
105
- # schemas.all do |schema|
106
- # puts schema.name
107
- # end
108
- #
109
- # @example Using the enumerator by not passing a block:
110
- # require "google/cloud/pubsub"
111
- #
112
- # pubsub = Google::Cloud::PubSub.new
113
- #
114
- # schemas = pubsub.schemas
115
- # all_names = schemas.all.map do |schema|
116
- # schema.name
117
- # end
118
- #
119
- # @example Limit the number of API calls made:
120
- # require "google/cloud/pubsub"
121
- #
122
- # pubsub = Google::Cloud::PubSub.new
123
- #
124
- # schemas = pubsub.schemas
125
- # schemas.all(request_limit: 10) do |schema|
126
- # puts schema.name
127
- # end
128
- #
129
- def all request_limit: nil, &block
130
- request_limit = request_limit.to_i if request_limit
131
- return enum_for :all, request_limit: request_limit unless block_given?
132
- results = self
133
- loop do
134
- results.each(&block)
135
- if request_limit
136
- request_limit -= 1
137
- break if request_limit.negative?
138
- end
139
- break unless results.next?
140
- results = results.next
141
- end
142
- end
143
-
144
- ##
145
- # @private New Schemas::List from a
146
- # Google::Cloud::PubSub::V1::ListSchemasRequest object.
147
- def self.from_grpc grpc_list, service, view, max = nil
148
- subs = new(Array(grpc_list.schemas).map do |grpc|
149
- Schema.from_grpc grpc, service
150
- end)
151
- token = grpc_list.next_page_token
152
- token = nil if token == "".freeze
153
- subs.instance_variable_set :@token, token
154
- subs.instance_variable_set :@service, service
155
- subs.instance_variable_set :@view, view
156
- subs.instance_variable_set :@max, max
157
- subs
158
- end
159
-
160
- protected
161
-
162
- ##
163
- # @private Raise an error unless an active connection to the service
164
- # is available.
165
- def ensure_service!
166
- raise "Must have active connection to service" unless @service
167
- end
168
-
169
- def next_schemas
170
- options = { prefix: @prefix, token: @token, max: @max }
171
- grpc = @service.list_schemas @view, options
172
- self.class.from_grpc grpc, @service, @view, @max
173
- end
174
- end
175
- end
176
- end
177
-
178
- Pubsub = PubSub unless const_defined? :Pubsub
179
- end
180
- end
@@ -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