gapic-common 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fb55a8128893e307e6f1237157b9456481ce7aa2e09a6dbeb19786359cf774ef
4
- data.tar.gz: 37b93b8e7b2d444679279d6f9e4b87b1ccfc460d4c9fc041ca390a22f115e4f5
3
+ metadata.gz: 5b3ff94cc009d187db3bbcaa3268adc302baf7918c4027da5b6b7eeb35156f9b
4
+ data.tar.gz: 48ffb516c68d49a72203f76f99e5bebd6cdd9760d96520f754cb6f51e9b39d52
5
5
  SHA512:
6
- metadata.gz: b635e99c7a32cd3926e278312103197299a48679ea3d5ce6286ab7e5a5873cbb718106e3e69ac28b33316c5ff61c7b22fe67d5499b62298d1dc75c1f7f70eda6
7
- data.tar.gz: 81cefba38844981e6c137aff1d2de3fc5282e77b93c7761d20ae04007e7d46d07529b29fcf8315e98505268d101c500ad8e8d8e3df38fab5e0f4d8edbb914e4d
6
+ metadata.gz: f347fc6aae5fba58ab1fba7d090d34f116cbad71d02d8eaa7f7d6b498d830fb6fe0fdfc8fa80c2605a5077a194ee3b15c5ddb6d4d2be22f96f23c7e620afc36f
7
+ data.tar.gz: dd712b166b3c4c2d5946fc4498fc36cc1758d236a987ff8f9efce7ccf8a8af75b07e447e35f29d98db7c00b42a2e67335ca84a9e55d29aa9606b48fb9a714f11
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Release History
2
2
 
3
+ ### 0.8.0 / 2022-01-20
4
+
5
+ * Add generic LROs helpers. These are used for the Nonstandard (not conforming to AIP-151) Cloud LROs.
6
+
3
7
  ### 0.7.0 / 2021-08-03
4
8
 
5
9
  * Require googleauth 0.17 for proper support of JWT credentials with custom scopes
@@ -14,6 +14,6 @@
14
14
 
15
15
  module Gapic
16
16
  module Common
17
- VERSION = "0.7.0".freeze
17
+ VERSION = "0.8.0".freeze
18
18
  end
19
19
  end
@@ -0,0 +1,38 @@
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
+ module Gapic
16
+ module GenericLRO
17
+ ##
18
+ # A base class for the wrappers over the long-running operations.
19
+ #
20
+ # @attribute [r] operation
21
+ # @return [Object] The wrapped operation object.
22
+ #
23
+ class BaseOperation
24
+ attr_reader :operation
25
+
26
+ ##
27
+ # @private
28
+ # @param operation [Object] The operation object to be wrapped
29
+ def initialize operation
30
+ @operation = operation
31
+ end
32
+
33
+ protected
34
+
35
+ attr_writer :operation
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,325 @@
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
+ require "gapic/generic_lro/base_operation"
16
+ require "gapic/operation/retry_policy"
17
+
18
+ module Gapic
19
+ module GenericLRO
20
+ ##
21
+ # A class used to wrap the longrunning operation objects, including the nonstandard ones
22
+ # (`nonstandard` meaning not conforming to the AIP-151).
23
+ # It provides helper methods to poll and check for status of these operations.
24
+ #
25
+ class Operation < Gapic::GenericLRO::BaseOperation
26
+ ##
27
+ # @param operation [Object] The long-running operation object that is returned by the initial method call.
28
+ #
29
+ # @param client [Object] The client that handles the polling for the longrunning operation.
30
+ #
31
+ # @param polling_method_name [String] The name of the methods on the client that polls the longrunning operation.
32
+ #
33
+ # @param operation_status_field [String] The name of the `status` field in the underlying long-running operation
34
+ # object. The `status` field signals that the operation has finished. It should either contain symbols, and
35
+ # be set to `:DONE` when finished or contain a boolean and be set to `true` when finished.
36
+ #
37
+ # @param request_values [Map<String, String>] The values that are to be copied from the request that
38
+ # triggered the longrunning operation, into the request that polls for the longrunning operation.
39
+ # The format is `name of the request field` -> `value`
40
+ #
41
+ # @param operation_name_field [String, nil] The name of the `name` field in the underlying long-running operation
42
+ # object. Optional.
43
+ #
44
+ # @param operation_err_field [String, nil] The name of the `error` field in the underlying long-running operation
45
+ # object. The `error` field should be a message-type, and have same semantics as `google.rpc.Status`, including
46
+ # an integer `code` subfield, that carries an error code. If the `operation_err_field` field is given,
47
+ # the `operation_err_code_field` and `operation_err_msg_field` parameters are ignored. Optional.
48
+ #
49
+ # @param operation_err_code_field [String, nil] The name of the `error_code` field in the underlying
50
+ # long-running operation object. It is ignored if `operation_err_field` is given. Optional.
51
+ #
52
+ # @param operation_err_msg_field [String, nil] The name of the `error_message` field in the underlying
53
+ # long-running operation object. It is ignored if `operation_err_field` is given. Optional.
54
+ #
55
+ # @param operation_copy_fields [Map<String, String>] The map of the fields that need to be copied from the
56
+ # long-running operation object that the polling method returns to the polling request.
57
+ # The format is `name of the operation object field` -> `name of the request field` (`from` -> `to`)
58
+ #
59
+ # @param options [Gapic::CallOptions] call options for this operation
60
+ #
61
+ def initialize operation, client:, polling_method_name:, operation_status_field:,
62
+ request_values: {}, operation_name_field: nil, operation_err_field: nil,
63
+ operation_err_code_field: nil, operation_err_msg_field: nil, operation_copy_fields: {},
64
+ options: {}
65
+ @client = client
66
+ @polling_method_name = polling_method_name
67
+ @operation_status_field = operation_status_field
68
+
69
+ @request_values = request_values || {}
70
+
71
+ @operation_name_field = operation_name_field
72
+ @operation_err_field = operation_err_field
73
+ @operation_err_code_field = operation_err_code_field
74
+ @operation_err_msg_field = operation_err_msg_field
75
+
76
+ @operation_copy_fields = operation_copy_fields || {}
77
+
78
+ @on_done_callbacks = []
79
+ @on_reload_callbacks = []
80
+ @options = options || {}
81
+
82
+ super operation
83
+ end
84
+
85
+ ##
86
+ # If the operation is done, returns the response. If the operation response is an error, the error will be
87
+ # returned. Otherwise returns nil.
88
+ #
89
+ # @return [Object, nil] The result of the operation or an error.
90
+ #
91
+ def results
92
+ return error if error?
93
+ return response if response?
94
+ end
95
+
96
+ ##
97
+ # Returns the name of the operation, if specified.
98
+ #
99
+ # @return [String, nil] The name of the operation.
100
+ #
101
+ def name
102
+ return nil if @operation_name_field.nil?
103
+ operation.send @operation_name_field if operation.respond_to? @operation_name_field
104
+ end
105
+
106
+ ##
107
+ # Checks if the operation is done. This does not send a new api call, but checks the result of the previous api
108
+ # call to see if done.
109
+ #
110
+ # @return [Boolean] Whether the operation is done.
111
+ #
112
+ def done?
113
+ return status if [true, false].include? status
114
+
115
+ status == :DONE
116
+ end
117
+
118
+ ##
119
+ # Checks if the operation is done and the result is not an error. If the operation is not finished then this will
120
+ # return false.
121
+ #
122
+ # @return [Boolean] Whether a response has been returned.
123
+ #
124
+ def response?
125
+ done? && !error?
126
+ end
127
+
128
+ ##
129
+ # If the operation is completed successfully, returns the underlying operation object, otherwise returns nil.
130
+ #
131
+ # @return [Object, nil] The response of the operation.
132
+ def response
133
+ operation if response?
134
+ end
135
+
136
+ ##
137
+ # Checks if the operation is done and the result is an error. If the operation is not finished then this will
138
+ # return false.
139
+ #
140
+ # @return [Boolean] Whether an error has been returned.
141
+ #
142
+ def error?
143
+ done? && (!err.nil? || !error_code.nil?)
144
+ end
145
+
146
+ ##
147
+ # If the operation response is an error, the error will be returned, otherwise returns nil.
148
+ #
149
+ # @return [Object, nil] The error object.
150
+ #
151
+ def error
152
+ return unless error?
153
+ err || GenericError.new(error_code, error_msg)
154
+ end
155
+
156
+ ##
157
+ # Reloads the operation object.
158
+ #
159
+ # @param options [Gapic::CallOptions, Hash] The options for making the RPC call. A Hash can be provided
160
+ # to customize the options object, using keys that match the arguments for {Gapic::CallOptions.new}.
161
+ #
162
+ # @return [Gapic::GenericLRO::Operation] Since this method changes internal state, it returns itself.
163
+ #
164
+ def reload! options: nil
165
+ return self if done?
166
+
167
+ @on_reload_callbacks.each { |proc| proc.call self }
168
+
169
+ request_hash = @request_values.transform_keys(&:to_sym)
170
+ @operation_copy_fields.each do |field_from, field_to|
171
+ request_hash[field_to.to_sym] = operation.send field_from.to_s if operation.respond_to? field_from.to_s
172
+ end
173
+
174
+ options = merge_options options, @options
175
+
176
+ ops = @client.send @polling_method_name, request_hash, options
177
+ ops = ops.operation if ops.is_a? Gapic::GenericLRO::BaseOperation
178
+
179
+ self.operation = ops
180
+
181
+ if done?
182
+ @on_reload_callbacks.clear
183
+ @on_done_callbacks.each { |proc| proc.call self }
184
+ @on_done_callbacks.clear
185
+ end
186
+
187
+ self
188
+ end
189
+ alias refresh! reload!
190
+
191
+ ##
192
+ # Blocking method to wait until the operation has completed or the maximum timeout has been reached. Upon
193
+ # completion, registered callbacks will be called, then - if a block is given - the block will be called.
194
+ #
195
+ # @param retry_policy [RetryPolicy, Hash, Proc] The policy for retry. A custom proc that takes the error as an
196
+ # argument and blocks can also be provided.
197
+ #
198
+ # @yield operation [Gapic::GenericLRO::Operation] Yields the finished Operation.
199
+ #
200
+ def wait_until_done! retry_policy: nil
201
+ retry_policy = ::Gapic::Operation::RetryPolicy.new retry_policy if retry_policy.is_a? Hash
202
+ retry_policy ||= ::Gapic::Operation::RetryPolicy.new
203
+
204
+ until done?
205
+ reload!
206
+ break unless retry_policy.call
207
+ end
208
+
209
+ yield self if block_given?
210
+
211
+ self
212
+ end
213
+
214
+ ##
215
+ # Registers a callback to be run when an operation is being reloaded. If the operation has completed
216
+ # prior to a call to this function the callback will NOT be called or registered.
217
+ #
218
+ # @yield operation [Gapic::Operation] Yields the finished Operation.
219
+ #
220
+ def on_reload &block
221
+ return if done?
222
+ @on_reload_callbacks.push block
223
+ end
224
+
225
+ ##
226
+ # Registers a callback to be run when a refreshed operation is marked as done. If the operation has completed
227
+ # prior to a call to this function the callback will be called instead of registered.
228
+ #
229
+ # @yield operation [Gapic::Operation] Yields the finished Operation.
230
+ #
231
+ def on_done &block
232
+ if done?
233
+ yield self
234
+ else
235
+ @on_done_callbacks.push block
236
+ end
237
+ end
238
+
239
+ private
240
+
241
+ ##
242
+ # @return [String, Boolean, nil] A status, whether operation is Done,
243
+ # as either a boolean (`true` === Done) or a symbol (`:DONE` === Done)
244
+ #
245
+ def status
246
+ return nil if @operation_status_field.nil?
247
+ operation.send @operation_status_field
248
+ end
249
+
250
+ ##
251
+ # @return [String, nil] An error message if the error message field is specified
252
+ #
253
+ def err
254
+ return nil if @operation_err_field.nil?
255
+ operation.send @operation_err_field if operation.respond_to? @operation_err_field
256
+ end
257
+
258
+ ##
259
+ # @return [String, nil] An error code if the error code field is specified
260
+ #
261
+ def error_code
262
+ return nil if @operation_err_code_field.nil?
263
+ operation.send @operation_err_code_field if operation.respond_to? @operation_err_code_field
264
+ end
265
+
266
+ ##
267
+ # @return [String, nil] An error message if the error message field is specified
268
+ #
269
+ def error_msg
270
+ return nil if @operation_err_msg_field.nil?
271
+ operation.send @operation_err_msg_field if operation.respond_to? @operation_err_msg_field
272
+ end
273
+
274
+ ##
275
+ # Merges options given to the method with a baseline Gapic::Options object
276
+ #
277
+ # @param method_opts [Gapic::CallOptions, Hash] The options for making the RPC call given to a method invocation.
278
+ # A Hash can be provided to customize the options object, using keys that match the arguments
279
+ # for {Gapic::CallOptions.new}.
280
+ #
281
+ # @param baseline_opts [Gapic::CallOptions, Hash] The baseline options for making the RPC call.
282
+ # A Hash can be provided to customize the options object, using keys that match the arguments
283
+ # for {Gapic::CallOptions.new}.
284
+ #
285
+ def merge_options method_opts, baseline_opts
286
+ options = if method_opts.respond_to? :to_h
287
+ method_opts.to_h.merge baseline_opts.to_h
288
+ else
289
+ baseline_opts.to_h
290
+ end
291
+
292
+ Gapic::CallOptions.new(**options)
293
+ end
294
+
295
+ ##
296
+ # Represents a generic error that a generic LRO can report
297
+ #
298
+ # @!attribute [r] code
299
+ # @return [String] An error code
300
+ #
301
+ # @!attribute [r] message
302
+ # @return [String] An error message
303
+ #
304
+ class GenericError
305
+ attr_accessor :code
306
+ attr_accessor :message
307
+
308
+ ##
309
+ # @param code [String] An error code
310
+ # @param message [String] An error message
311
+ def initialize code, message
312
+ @code = code
313
+ @message = message
314
+ end
315
+ end
316
+
317
+ protected
318
+
319
+ ##
320
+ # @private
321
+ # @return [Object] The client that handles the polling for the longrunning operation.
322
+ attr_accessor :client
323
+ end
324
+ end
325
+ end
@@ -12,22 +12,14 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+ require "gapic/generic_lro/base_operation"
16
+
15
17
  module Gapic
16
18
  module Rest
17
19
  ##
18
- # A base class for the wrappers over the long-running operations to the response of a REST LRO method.
19
- #
20
- # @attribute [r] operation
21
- # @return [Object] The wrapped operation object.
22
- class BaseOperation
23
- attr_reader :operation
24
-
25
- ##
26
- # @private
27
- # @param operation [Object] The wrapped operation object.
28
- def initialize operation
29
- @operation = operation
30
- end
31
- end
20
+ # This alias is left here for the backwards compatibility purposes.
21
+ # Rest LROs now use the same GenericLRO base as the GRPC LROs.
22
+ # @deprecated Use {Gapic::GenericLRO::BaseOperation} instead.
23
+ BaseOperation = Gapic::GenericLRO::BaseOperation
32
24
  end
33
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gapic-common
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Google API Authors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-05 00:00:00.000000000 Z
11
+ date: 2022-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -196,6 +196,20 @@ dependencies:
196
196
  - - "~>"
197
197
  - !ruby/object:Gem::Version
198
198
  version: '5.2'
199
+ - !ruby/object:Gem::Dependency
200
+ name: pry
201
+ requirement: !ruby/object:Gem::Requirement
202
+ requirements:
203
+ - - ">="
204
+ - !ruby/object:Gem::Version
205
+ version: '0.14'
206
+ type: :development
207
+ prerelease: false
208
+ version_requirements: !ruby/object:Gem::Requirement
209
+ requirements:
210
+ - - ">="
211
+ - !ruby/object:Gem::Version
212
+ version: '0.14'
199
213
  - !ruby/object:Gem::Dependency
200
214
  name: rake
201
215
  requirement: !ruby/object:Gem::Requirement
@@ -259,6 +273,8 @@ files:
259
273
  - lib/gapic/common/version.rb
260
274
  - lib/gapic/config.rb
261
275
  - lib/gapic/config/method.rb
276
+ - lib/gapic/generic_lro/base_operation.rb
277
+ - lib/gapic/generic_lro/operation.rb
262
278
  - lib/gapic/grpc.rb
263
279
  - lib/gapic/grpc/service_stub.rb
264
280
  - lib/gapic/grpc/service_stub/rpc_call.rb
@@ -294,7 +310,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
294
310
  - !ruby/object:Gem::Version
295
311
  version: '0'
296
312
  requirements: []
297
- rubygems_version: 3.1.6
313
+ rubygems_version: 3.1.2
298
314
  signing_key:
299
315
  specification_version: 4
300
316
  summary: Common code for GAPIC-generated API clients