google-cloud-pubsub 2.1.1 → 2.7.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.
@@ -31,9 +31,6 @@ module Google
31
31
  # Retry Policy is implemented on a best effort basis. At times, the delay between consecutive deliveries may not
32
32
  # match the configuration. That is, delay can be more or less than configured backoff.
33
33
  #
34
- # **EXPERIMENTAL:** This API might be changed in backward-incompatible ways and is not recommended for production
35
- # use. It is not subject to any SLA or deprecation policy.
36
- #
37
34
  # @attr [Numeric] minimum_backoff The minimum delay between consecutive deliveries of a given message. Value
38
35
  # should be between 0 and 600 seconds. The default value is 10 seconds.
39
36
  # @attr [Numeric] maximum_backoff The maximum delay between consecutive deliveries of a given message. Value
@@ -52,7 +49,8 @@ module Google
52
49
  # sub.retry_policy.maximum_backoff #=> 300
53
50
  #
54
51
  class RetryPolicy
55
- attr_reader :minimum_backoff, :maximum_backoff
52
+ attr_reader :minimum_backoff
53
+ attr_reader :maximum_backoff
56
54
 
57
55
  ##
58
56
  # Creates a new, immutable RetryPolicy value object.
@@ -0,0 +1,310 @@
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
+ # Validates a message against a schema.
89
+ #
90
+ # @param message_data [String] Message to validate against the provided `schema_spec`.
91
+ # @param message_encoding [Symbol, String] The encoding of the message validated against the schema. Values
92
+ # include:
93
+ #
94
+ # * `JSON` - JSON encoding.
95
+ # * `BINARY` - Binary encoding, as defined by the schema type. For some schema types, binary encoding may not
96
+ # be available.
97
+ #
98
+ # @return [Boolean] Returns `true` if the message validiation succeeds, `false` otherwise.
99
+ #
100
+ # @example
101
+ # require "google/cloud/pubsub"
102
+ #
103
+ # pubsub = Google::Cloud::PubSub.new
104
+ # schema = pubsub.schema "my-schema"
105
+ #
106
+ # message_data = { "name" => "Alaska", "post_abbr" => "AK" }.to_json
107
+ # schema.validate_message message_data, :json
108
+ #
109
+ def validate_message message_data, message_encoding
110
+ message_encoding = message_encoding.to_s.upcase
111
+ service.validate_message message_data, message_encoding, schema_name: name
112
+ true
113
+ rescue Google::Cloud::InvalidArgumentError
114
+ false
115
+ end
116
+
117
+ ##
118
+ # Removes the schema, if it exists.
119
+ #
120
+ # @return [Boolean] Returns `true` if the schema was deleted.
121
+ #
122
+ # @example
123
+ # require "google/cloud/pubsub"
124
+ #
125
+ # pubsub = Google::Cloud::PubSub.new
126
+ # schema = pubsub.schema "my-schema"
127
+ #
128
+ # schema.delete
129
+ #
130
+ def delete
131
+ ensure_service!
132
+ service.delete_schema name
133
+ true
134
+ end
135
+
136
+ ##
137
+ # Reloads the schema with current data from the Pub/Sub service.
138
+ #
139
+ # @param view [Symbol, String, nil] The set of fields to return in the response. Possible values:
140
+ # * `BASIC` - Include the `name` and `type` of the schema, but not the `definition`.
141
+ # * `FULL` - Include all Schema object fields.
142
+ #
143
+ # Optional. If not provided or `nil`, the last non-nil `view` argument to this method will be used if one has
144
+ # been given, othewise `FULL` will be used.
145
+ #
146
+ # @return [Google::Cloud::PubSub::Schema] Returns the reloaded schema.
147
+ #
148
+ # @example Skip retrieving the schema from the service, then load it:
149
+ # require "google/cloud/pubsub"
150
+ #
151
+ # pubsub = Google::Cloud::PubSub.new
152
+ # schema = pubsub.schema "my-schema", skip_lookup: true
153
+ #
154
+ # schema.reload!
155
+ #
156
+ # @example Use the `view` option to load the basic or full resource:
157
+ # require "google/cloud/pubsub"
158
+ #
159
+ # pubsub = Google::Cloud::PubSub.new
160
+ # schema = pubsub.schema "my-schema", view: :basic
161
+ # schema.resource_partial? #=> true
162
+ #
163
+ # schema.reload! view: :full
164
+ # schema.resource_partial? #=> false
165
+ #
166
+ # @!group Lifecycle
167
+ #
168
+ def reload! view: nil
169
+ ensure_service!
170
+ @view = view || @view
171
+ @grpc = service.get_schema name, @view
172
+ @reference = nil
173
+ @exists = nil
174
+ self
175
+ end
176
+ alias refresh! reload!
177
+
178
+ ##
179
+ # Determines whether the schema exists in the Pub/Sub service.
180
+ #
181
+ # @example
182
+ # require "google/cloud/pubsub"
183
+ #
184
+ # pubsub = Google::Cloud::PubSub.new
185
+ #
186
+ # schema = pubsub.schema "my-schema"
187
+ # schema.exists? #=> true
188
+ #
189
+ def exists?
190
+ # Always true if the object is not set as reference
191
+ return true unless reference?
192
+ # If we have a value, return it
193
+ return @exists unless @exists.nil?
194
+ ensure_grpc!
195
+ @exists = true
196
+ rescue Google::Cloud::NotFoundError
197
+ @exists = false
198
+ end
199
+
200
+ ##
201
+ # Determines whether the schema object was created without retrieving the
202
+ # resource representation from the Pub/Sub service.
203
+ #
204
+ # @return [Boolean] `true` when the schema was created without a resource
205
+ # representation, `false` otherwise.
206
+ #
207
+ # @example
208
+ # require "google/cloud/pubsub"
209
+ #
210
+ # pubsub = Google::Cloud::PubSub.new
211
+ #
212
+ # schema = pubsub.schema "my-schema", skip_lookup: true
213
+ # schema.reference? #=> true
214
+ #
215
+ def reference?
216
+ @grpc.type.nil? || @grpc.type == :TYPE_UNSPECIFIED
217
+ end
218
+
219
+ ##
220
+ # Determines whether the schema object was created with a resource
221
+ # representation from the Pub/Sub service.
222
+ #
223
+ # @return [Boolean] `true` when the schema was created with a resource
224
+ # representation, `false` otherwise.
225
+ #
226
+ # @example
227
+ # require "google/cloud/pubsub"
228
+ #
229
+ # pubsub = Google::Cloud::PubSub.new
230
+ #
231
+ # schema = pubsub.schema "my-schema"
232
+ # schema.resource? #=> true
233
+ #
234
+ def resource?
235
+ !reference?
236
+ end
237
+
238
+ ##
239
+ # Whether the schema was created with a partial resource representation
240
+ # from the Pub/Sub service.
241
+ #
242
+ # @return [Boolean] `true` when the schema was created with a partial
243
+ # resource representation, `false` otherwise.
244
+ #
245
+ # @example
246
+ # require "google/cloud/pubsub"
247
+ #
248
+ # pubsub = Google::Cloud::PubSub.new
249
+ # schema = pubsub.schema "my-schema", view: :basic
250
+ #
251
+ # schema.resource_partial? #=> true
252
+ # schema.reload! view: :full # Loads the full resource.
253
+ # schema.resource_partial? #=> false
254
+ #
255
+ def resource_partial?
256
+ resource? && !resource_full?
257
+ end
258
+
259
+ ##
260
+ # Whether the schema was created with a full resource representation
261
+ # from the Pub/Sub service.
262
+ #
263
+ # @return [Boolean] `true` when the schema was created with a full
264
+ # resource representation, `false` otherwise.
265
+ #
266
+ # @example
267
+ # require "google/cloud/pubsub"
268
+ #
269
+ # pubsub = Google::Cloud::PubSub.new
270
+ # schema = pubsub.schema "my-schema"
271
+ #
272
+ # schema.resource_full? #=> true
273
+ #
274
+ def resource_full?
275
+ resource? && @grpc.definition && !@grpc.definition.empty?
276
+ end
277
+
278
+ ##
279
+ # @private New Schema from a Google::Cloud::PubSub::V1::Schema object.
280
+ def self.from_grpc grpc, service, view: nil
281
+ new grpc, service, view: view
282
+ end
283
+
284
+ ##
285
+ # @private New reference Schema object without making an HTTP request.
286
+ def self.from_name name, view, service, options = {}
287
+ grpc = Google::Cloud::PubSub::V1::Schema.new name: service.schema_path(name, options)
288
+ from_grpc grpc, service, view: view
289
+ end
290
+
291
+ protected
292
+
293
+ ##
294
+ # @private Raise an error unless an active connection to the service is available.
295
+ def ensure_service!
296
+ raise "Must have active connection to service" unless service
297
+ end
298
+
299
+ ##
300
+ # Ensures a Google::Cloud::PubSub::V1::Schema object exists.
301
+ def ensure_grpc!
302
+ ensure_service!
303
+ reload! if reference?
304
+ end
305
+ end
306
+ end
307
+
308
+ Pubsub = PubSub unless const_defined? :Pubsub
309
+ end
310
+ end
@@ -0,0 +1,180 @@
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