gapic-common 0.4.3 → 0.8.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c317dee8aa769cec71d8bbac90f2dda4d550d5f499e2fe57cdd8ebc8c1f1e8f8
4
- data.tar.gz: 6be1ba8f2f3c9cb6247b951e72516313ead43250463c5dc90939cc294dcbcea2
3
+ metadata.gz: 5b3ff94cc009d187db3bbcaa3268adc302baf7918c4027da5b6b7eeb35156f9b
4
+ data.tar.gz: 48ffb516c68d49a72203f76f99e5bebd6cdd9760d96520f754cb6f51e9b39d52
5
5
  SHA512:
6
- metadata.gz: b9f631f39da2577cf557c6ad617fefd56615274028e226bf68dcbb7bb56f3a1642a29a0bebe8c949b37c95360ce869c18d09a64bd8b8baaeee86be8a3918451f
7
- data.tar.gz: 247fffcdbf8ef5571de60395600122ac408d89e68d9afa17e33afc2a58434311b6b523d0ae089175787906779ee7abe0247f45f67ba4e87a56b56cd36da896ac
6
+ metadata.gz: f347fc6aae5fba58ab1fba7d090d34f116cbad71d02d8eaa7f7d6b498d830fb6fe0fdfc8fa80c2605a5077a194ee3b15c5ddb6d4d2be22f96f23c7e620afc36f
7
+ data.tar.gz: dd712b166b3c4c2d5946fc4498fc36cc1758d236a987ff8f9efce7ccf8a8af75b07e447e35f29d98db7c00b42a2e67335ca84a9e55d29aa9606b48fb9a714f11
data/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
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
+
7
+ ### 0.7.0 / 2021-08-03
8
+
9
+ * Require googleauth 0.17 for proper support of JWT credentials with custom scopes
10
+
11
+ ### 0.6.0 / 2021-07-22
12
+
13
+ * Added helper for REST pagination
14
+
15
+ ### 0.5.0 / 2021-06-15
16
+
17
+ * Provide a way to create `x-goog-api-client` headers with rest library version and/or without grpc library version
18
+
3
19
  ### 0.4.3 / 2021-06-10
4
20
 
5
21
  * Fix file permissions.
@@ -14,6 +14,6 @@
14
14
 
15
15
  module Gapic
16
16
  module Common
17
- VERSION = "0.4.3".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
data/lib/gapic/headers.rb CHANGED
@@ -26,17 +26,27 @@ module Gapic
26
26
  # @param gax_version [String] The Gapic version. Defaults to `Gapic::Common::VERSION`.
27
27
  # @param gapic_version [String] The Gapic version.
28
28
  # @param grpc_version [String] The GRPC version. Defaults to `GRPC::VERSION`.
29
- def self.x_goog_api_client ruby_version: nil, lib_name: nil, lib_version: nil,
30
- gax_version: nil, gapic_version: nil, grpc_version: nil
29
+ # @param rest_version [String] The Rest Library (Faraday) version. Defaults to `Faraday::VERSION`.
30
+ # @param transports_version_send [Array] Which transports to send versions for.
31
+ # Allowed values to contain are:
32
+ # `:grpc` to send the GRPC library version (if defined)
33
+ # `:rest` to send the REST library version (if defined)
34
+ # Defaults to `[:grpc]`
35
+ def self.x_goog_api_client ruby_version: nil, lib_name: nil, lib_version: nil, gax_version: nil,
36
+ gapic_version: nil, grpc_version: nil, rest_version: nil,
37
+ transports_version_send: [:grpc]
38
+
31
39
  ruby_version ||= ::RUBY_VERSION
32
40
  gax_version ||= ::Gapic::Common::VERSION
33
41
  grpc_version ||= ::GRPC::VERSION if defined? ::GRPC
42
+ rest_version ||= ::Faraday::VERSION if defined? ::Faraday
34
43
 
35
44
  x_goog_api_client_header = ["gl-ruby/#{ruby_version}"]
36
45
  x_goog_api_client_header << "#{lib_name}/#{lib_version}" if lib_name
37
46
  x_goog_api_client_header << "gax/#{gax_version}"
38
47
  x_goog_api_client_header << "gapic/#{gapic_version}" if gapic_version
39
- x_goog_api_client_header << "grpc/#{grpc_version}" if grpc_version
48
+ x_goog_api_client_header << "grpc/#{grpc_version}" if grpc_version && transports_version_send.include?(:grpc)
49
+ x_goog_api_client_header << "rest/#{rest_version}" if rest_version && transports_version_send.include?(:rest)
40
50
  x_goog_api_client_header.join " ".freeze
41
51
  end
42
52
  end
@@ -0,0 +1,25 @@
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
+
17
+ module Gapic
18
+ module Rest
19
+ ##
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
24
+ end
25
+ end
@@ -0,0 +1,225 @@
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 Rest
17
+ ##
18
+ # A class to provide the Enumerable interface to the response of a REST paginated method.
19
+ # PagedEnumerable assumes response message holds a list of resources and the token to the next page.
20
+ #
21
+ # PagedEnumerable provides the enumerations over the resource data, and also provides the enumerations over the
22
+ # pages themselves.
23
+ #
24
+ # @example normal iteration over resources.
25
+ # paged_enumerable.each { |resource| puts resource }
26
+ #
27
+ # @example per-page iteration.
28
+ # paged_enumerable.each_page { |page| puts page }
29
+ #
30
+ # @example Enumerable over pages.
31
+ # paged_enumerable.each_page do |page|
32
+ # page.each { |resource| puts resource }
33
+ # end
34
+ #
35
+ # @example more exact operations over pages.
36
+ # while some_condition()
37
+ # page = paged_enumerable.page
38
+ # do_something(page)
39
+ # break if paged_enumerable.next_page?
40
+ # paged_enumerable.next_page
41
+ # end
42
+ #
43
+ # @attribute [r] page
44
+ # @return [Page] The current page object.
45
+ #
46
+ class PagedEnumerable
47
+ include Enumerable
48
+
49
+ attr_reader :page
50
+
51
+ ##
52
+ # @private
53
+ # @param service_stub [Object] The REST service_stub with the baseline implementation for the wrapped method.
54
+ # @param method_name [Symbol] The REST method name that is being wrapped.
55
+ # @param request [Object] The request object.
56
+ # @param response [Object] The response object.
57
+ # @param options [Gapic::CallOptions] The options for making the RPC call.
58
+ # @param format_resource [Proc] A Proc object to format the resource object. The Proc should accept response as an
59
+ # argument, and return a formatted resource object. Optional.
60
+ #
61
+ def initialize service_stub, method_name, resource_field_name, request, response, options, format_resource: nil
62
+ @service_stub = service_stub
63
+ @method_name = method_name
64
+ @resource_field_name = resource_field_name
65
+ @request = request
66
+ @response = response
67
+ @options = options
68
+ @format_resource = format_resource
69
+
70
+ @page = Page.new response, resource_field_name, format_resource: @format_resource
71
+ end
72
+
73
+ ##
74
+ # Iterate over the individual resources, automatically requesting new pages as needed.
75
+ #
76
+ # @yield [Object] Gives the resource objects in the stream.
77
+ #
78
+ # @return [Enumerator] if no block is provided
79
+ #
80
+ def each &block
81
+ return enum_for :each unless block_given?
82
+
83
+ each_page do |page|
84
+ page.each(&block)
85
+ end
86
+ end
87
+
88
+ ##
89
+ # Iterate over the pages.
90
+ #
91
+ # @yield [Page] Gives the pages in the stream.
92
+ #
93
+ # @return [Enumerator] if no block is provided
94
+ #
95
+ def each_page
96
+ return enum_for :each_page unless block_given?
97
+
98
+ loop do
99
+ break if @page.nil?
100
+ yield @page
101
+ next_page!
102
+ end
103
+ end
104
+
105
+ ##
106
+ # True if there is at least one more page of results.
107
+ #
108
+ # @return [Boolean]
109
+ #
110
+ def next_page?
111
+ !next_page_token.nil? && !next_page_token.empty?
112
+ end
113
+
114
+ ##
115
+ # Load the next page and set it as the current page.
116
+ # If there is no next page, sets nil as a current page.
117
+ #
118
+ # @return [Page, nil] the new page object.
119
+ #
120
+ def next_page!
121
+ unless next_page?
122
+ @page = nil
123
+ return @page
124
+ end
125
+
126
+ next_request = @request.dup
127
+ next_request.page_token = @page.next_page_token
128
+
129
+ @response = @service_stub.send @method_name, next_request, @options
130
+ @page = Page.new @response, @resource_field_name, format_resource: @format_resource
131
+ end
132
+ alias next_page next_page!
133
+
134
+ ##
135
+ # The page token to be used for the next RPC call, or the empty string if there is no next page.
136
+ # nil if the iteration is complete.
137
+ #
138
+ # @return [String, nil]
139
+ #
140
+ def next_page_token
141
+ @page&.next_page_token
142
+ end
143
+
144
+ ##
145
+ # The current response object, for the current page.
146
+ # nil if the iteration is complete.
147
+ #
148
+ # @return [Object, nil]
149
+ #
150
+ def response
151
+ @page&.response
152
+ end
153
+
154
+ ##
155
+ # A class to represent a page in a PagedEnumerable. This also implements Enumerable, so it can iterate over the
156
+ # resource elements.
157
+ #
158
+ # @attribute [r] response
159
+ # @return [Object] the response object for the page.
160
+ class Page
161
+ include Enumerable
162
+ attr_reader :response
163
+
164
+ ##
165
+ # @private
166
+ # @param response [Object] The response object for the page.
167
+ # @param resource_field [String] The name of the field in response which holds the resources.
168
+ # @param format_resource [Proc, nil] A Proc object to format the resource object. Default nil (no formatting).
169
+ # The Proc should accept response as an argument, and return a formatted resource object. Optional.
170
+ #
171
+ def initialize response, resource_field, format_resource: nil
172
+ @response = response
173
+ @resource_field = resource_field
174
+ @format_resource = format_resource
175
+ end
176
+
177
+ ##
178
+ # Iterate over the resources.
179
+ #
180
+ # @yield [Object] Gives the resource objects in the page.
181
+ #
182
+ # @return [Enumerator] if no block is provided
183
+ #
184
+ def each
185
+ return enum_for :each unless block_given?
186
+
187
+ # We trust that the field exists and is an Enumerable
188
+ resources.each do |resource|
189
+ resource = @format_resource.call resource if @format_resource
190
+ yield resource
191
+ end
192
+ end
193
+
194
+ ##
195
+ # The page token to be used for the next RPC call, or the empty string if there is no next page.
196
+ #
197
+ # @return [String]
198
+ #
199
+ def next_page_token
200
+ @response.next_page_token
201
+ end
202
+
203
+ ##
204
+ # Whether the next_page_token exists and is not empty
205
+ #
206
+ # @return [Boolean]
207
+ #
208
+ def next_page_token?
209
+ !next_page_token.empty?
210
+ end
211
+
212
+ ##
213
+ # Resources in this page presented as an array.
214
+ # When the iterable is a protobuf map, the `.each |item|` gives just the keys
215
+ # to iterate like a normal hash it should be converted to an array first
216
+ #
217
+ # @return [Array]
218
+ #
219
+ def resources
220
+ @resources ||= @response[@resource_field].to_a
221
+ end
222
+ end
223
+ end
224
+ end
225
+ end
data/lib/gapic/rest.rb CHANGED
@@ -24,4 +24,6 @@ require "gapic/protobuf"
24
24
  require "gapic/rest/client_stub"
25
25
  require "gapic/rest/error"
26
26
  require "gapic/rest/faraday_middleware"
27
+ require "gapic/rest/operation"
28
+ require "gapic/rest/paged_enumerable"
27
29
  require "json"
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.4.3
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-06-10 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
@@ -70,7 +70,7 @@ dependencies:
70
70
  requirements:
71
71
  - - ">="
72
72
  - !ruby/object:Gem::Version
73
- version: 0.16.2
73
+ version: 0.17.0
74
74
  - - "<"
75
75
  - !ruby/object:Gem::Version
76
76
  version: 2.a
@@ -80,7 +80,7 @@ dependencies:
80
80
  requirements:
81
81
  - - ">="
82
82
  - !ruby/object:Gem::Version
83
- version: 0.16.2
83
+ version: 0.17.0
84
84
  - - "<"
85
85
  - !ruby/object:Gem::Version
86
86
  version: 2.a
@@ -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
@@ -272,6 +288,8 @@ files:
272
288
  - lib/gapic/rest/client_stub.rb
273
289
  - lib/gapic/rest/error.rb
274
290
  - lib/gapic/rest/faraday_middleware.rb
291
+ - lib/gapic/rest/operation.rb
292
+ - lib/gapic/rest/paged_enumerable.rb
275
293
  - lib/gapic/stream_input.rb
276
294
  homepage: https://github.com/googleapis/gapic-generator-ruby
277
295
  licenses:
@@ -292,7 +310,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
292
310
  - !ruby/object:Gem::Version
293
311
  version: '0'
294
312
  requirements: []
295
- rubygems_version: 3.1.6
313
+ rubygems_version: 3.1.2
296
314
  signing_key:
297
315
  specification_version: 4
298
316
  summary: Common code for GAPIC-generated API clients