google-gax 0.8.5 → 0.8.7

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
  SHA1:
3
- metadata.gz: 798b058e269c1439eae63e4902b7558b5be005c0
4
- data.tar.gz: 3ee51d01d9addb3d2edb034efb196478fcf0c4ae
3
+ metadata.gz: 716e8277e7cc5439a3f53c73c6cde2a8299ece01
4
+ data.tar.gz: ddb28ab9299ec05325ca30cdd40d13b5791d03f1
5
5
  SHA512:
6
- metadata.gz: 84f4d99c7830db006033237c1f92f3de3b7d8dc4a43e4357b182f0cfabc52df76e6c7d3f0863ce6b89307221c31c3a50cf39573d116e8b4e78d28e42e4a11fb3
7
- data.tar.gz: 95ab0e0670ba6eeb51cc6a2a98ba93f4beda3246be5ac43a9e6b3b344aedb9a369d6d01ccb32710f56ac34797a1f22d2d7ecb28f9beb221753f2af8d636c3835
6
+ metadata.gz: 7b39b230f9fe8cd4eb4ce9a37f1b5290a2f616e05fa0a646b56287ae9a56f0543687a987db3fa1ec0996cbece43f67cb0cd957d68167c80e8f7b3faa1b035550
7
+ data.tar.gz: b74ca9b64dcf91794360de8b7055f10f4a70cce756629298fb75273b2e73004c362609b07067e30eeab5765365dde23ddded22775898d4f70d8fe8da68f9ede8
data/README.md CHANGED
File without changes
data/Rakefile CHANGED
File without changes
@@ -32,8 +32,6 @@ require 'time'
32
32
  require 'google/gax/errors'
33
33
  require 'google/gax/bundling'
34
34
 
35
- # rubocop:disable Metrics/ModuleLength
36
-
37
35
  module Google
38
36
  module Gax
39
37
  # A class to provide the Enumerable interface for page-streaming method.
@@ -246,21 +244,9 @@ module Google
246
244
  add_timeout_arg(func, this_settings.timeout,
247
245
  this_settings.kwargs)
248
246
  end
249
- api_call = catch_errors(api_call, settings.errors)
250
- api_caller.call(api_call, request, this_settings)
251
- end
252
- end
253
-
254
- # Updates a_func to wrap exceptions with GaxError
255
- #
256
- # @param a_func [Proc]
257
- # @param errors [Array<Exception>] Configures the exceptions to wrap.
258
- # @return [Proc] A proc that will wrap certain exceptions with GaxError.
259
- def catch_errors(a_func, errors)
260
- proc do |request|
261
247
  begin
262
- a_func.call(request)
263
- rescue *errors
248
+ api_caller.call(api_call, request, this_settings)
249
+ rescue *settings.errors
264
250
  raise GaxError, 'RPC failed'
265
251
  end
266
252
  end
@@ -312,8 +298,6 @@ module Google
312
298
  enumerable.method(:start)
313
299
  end
314
300
 
315
- # rubocop:disable Metrics/MethodLength
316
-
317
301
  # Creates a proc equivalent to a_func, but that retries on certain
318
302
  # exceptions.
319
303
  #
@@ -336,29 +320,24 @@ module Google
336
320
  delay = retry_options.backoff_settings.initial_retry_delay_millis
337
321
  timeout = (retry_options.backoff_settings.initial_rpc_timeout_millis /
338
322
  MILLIS_PER_SECOND)
339
- result = nil
340
- now = Time.now
341
- deadline = now + total_timeout
342
- loop do
343
- begin
344
- result = add_timeout_arg(a_func, timeout, kwargs).call(request)
345
- break
346
- rescue => exception
347
- unless exception.respond_to?(:code) &&
348
- retry_options.retry_codes.include?(exception.code)
349
- raise RetryError, 'Exception occurred in retry method that ' \
350
- 'was not classified as transient'
351
- end
352
- sleep(rand(delay) / MILLIS_PER_SECOND)
353
- now = Time.now
354
- delay = [delay * delay_mult, max_delay].min
355
- timeout = [timeout * timeout_mult, max_timeout, deadline - now].min
356
- if now >= deadline
357
- raise RetryError, 'Retry total timeout exceeded with exception'
358
- end
323
+ deadline = Time.now + total_timeout
324
+ begin
325
+ a_func.call(request, deadline: Time.now + timeout, metadata: kwargs)
326
+ rescue => exception
327
+ unless exception.respond_to?(:code) &&
328
+ retry_options.retry_codes.include?(exception.code)
329
+ raise RetryError, 'Exception occurred in retry method that ' \
330
+ 'was not classified as transient'
331
+ end
332
+ sleep(rand(delay) / MILLIS_PER_SECOND)
333
+ now = Time.now
334
+ delay = [delay * delay_mult, max_delay].min
335
+ timeout = [timeout * timeout_mult, max_timeout, deadline - now].min
336
+ if now >= deadline
337
+ raise RetryError, 'Retry total timeout exceeded with exception'
359
338
  end
339
+ retry
360
340
  end
361
- result
362
341
  end
363
342
  end
364
343
 
@@ -377,9 +356,9 @@ module Google
377
356
  end
378
357
  end
379
358
 
380
- module_function :create_api_call, :catch_errors, :bundleable,
359
+ module_function :create_api_call, :bundleable,
381
360
  :page_streamable, :retryable, :add_timeout_arg
382
- private_class_method :catch_errors, :bundleable, :page_streamable,
361
+ private_class_method :bundleable, :page_streamable,
383
362
  :retryable, :add_timeout_arg
384
363
  end
385
364
  end
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -90,7 +90,7 @@ module Google
90
90
  #
91
91
  # @return [Object] The coerced version of the given value.
92
92
  def coerce_submessage(val, field_descriptor)
93
- if field_descriptor.label == :repeated
93
+ if (field_descriptor.label == :repeated) && !(map_field? field_descriptor)
94
94
  coerce_array(val, field_descriptor)
95
95
  else
96
96
  coerce(val, field_descriptor)
@@ -108,12 +108,24 @@ module Google
108
108
  #
109
109
  # @return [Array<Object>] The coerced version of the given values.
110
110
  def coerce_array(array, field_descriptor)
111
- raise ArgumentError unless array.is_a? Array
111
+ unless array.is_a? Array
112
+ raise ArgumentError, 'Value ' + array.to_s + ' must be an array'
113
+ end
112
114
  array.map do |val|
113
115
  coerce(val, field_descriptor)
114
116
  end
115
117
  end
116
118
 
119
+ # Hack to determine if field_descriptor is for a map.
120
+ #
121
+ # TODO(geigerj): Remove this once protobuf Ruby supports an official way
122
+ # to determine if a FieldDescriptor represents a map.
123
+ # See: https://github.com/google/protobuf/issues/3425
124
+ def map_field?(field_descriptor)
125
+ (field_descriptor.label == :repeated) &&
126
+ (field_descriptor.subtype.name.include? '_MapEntry_')
127
+ end
128
+
117
129
  # Coerces the value of a field to be acceptable by the instantiation method
118
130
  # of the wrapping message.
119
131
  #
@@ -125,13 +137,13 @@ module Google
125
137
  #
126
138
  # @return [Object] The coerced version of the given value.
127
139
  def coerce(val, field_descriptor)
128
- return val unless val.is_a? Hash
140
+ return val unless (val.is_a? Hash) && !(map_field? field_descriptor)
129
141
  to_proto(val, field_descriptor.subtype.msgclass)
130
142
  end
131
143
 
132
144
  module_function :to_proto, :coerce_submessages, :coerce_submessage,
133
- :coerce_array, :coerce
145
+ :coerce_array, :coerce, :map_field?
134
146
  private_class_method :coerce_submessages, :coerce_submessage, :coerce_array,
135
- :coerce
147
+ :coerce, :map_field?
136
148
  end
137
149
  end
@@ -29,6 +29,6 @@
29
29
 
30
30
  module Google
31
31
  module Gax
32
- VERSION = '0.8.5'.freeze
32
+ VERSION = '0.8.7'.freeze
33
33
  end
34
34
  end
data/lib/google/gax.rb CHANGED
File without changes
@@ -48,7 +48,7 @@ module Google
48
48
  # Manages long-running operations with an API service.
49
49
  #
50
50
  # When an API method normally takes long time to complete, it can be designed
51
- # to return Operation to the client, and the client can use this
51
+ # to return {Google::Longrunning::Operation Operation} to the client, and the client can use this
52
52
  # interface to receive the real response asynchronously by polling the
53
53
  # operation resource, or pass the operation resource to another API (such as
54
54
  # Google Cloud Pub/Sub API) to receive the response. Any API service that
@@ -80,19 +80,28 @@ module Google
80
80
  # The scopes needed to make gRPC calls to all of the methods defined in
81
81
  # this service.
82
82
  ALL_SCOPES = [
83
+ "https://www.googleapis.com/auth/cloud-platform",
83
84
  ].freeze
84
85
 
85
86
  # @param service_path [String]
86
87
  # The domain name of the API remote host.
87
88
  # @param port [Integer]
88
89
  # The port on which to connect to the remote host.
89
- # @param channel [Channel]
90
- # A Channel object through which to make calls.
91
- # @param chan_creds [Grpc::ChannelCredentials]
92
- # A ChannelCredentials for the setting up the RPC client.
93
- # @param updater_proc [Proc]
94
- # A function that transforms the metadata for requests, e.g., to give
95
- # OAuth credentials.
90
+ # @param credentials
91
+ # [Google::Gax::Credentials, String, Hash, GRPC::Core::Channel, GRPC::Core::ChannelCredentials, Proc]
92
+ # Provides the means for authenticating requests made by the client. This parameter can
93
+ # be many types.
94
+ # A `Google::Gax::Credentials` uses a the properties of its represented keyfile for
95
+ # authenticating requests made by this client.
96
+ # A `String` will be treated as the path to the keyfile to be used for the construction of
97
+ # credentials for this client.
98
+ # A `Hash` will be treated as the contents of a keyfile to be used for the construction of
99
+ # credentials for this client.
100
+ # A `GRPC::Core::Channel` will be used to make calls through.
101
+ # A `GRPC::Core::ChannelCredentials` for the setting up the RPC client. The channel credentials
102
+ # should already be composed with a `GRPC::Core::CallCredentials` object.
103
+ # A `Proc` will be used as an updater_proc for the Grpc channel. The proc transforms the
104
+ # metadata for requests, generally, to give OAuth credentials.
96
105
  # @param scopes [Array<String>]
97
106
  # The OAuth scopes for this service. This parameter is ignored if
98
107
  # an updater_proc is supplied.
@@ -109,6 +118,7 @@ module Google
109
118
  channel: nil,
110
119
  chan_creds: nil,
111
120
  updater_proc: nil,
121
+ credentials: nil,
112
122
  scopes: ALL_SCOPES,
113
123
  client_config: {},
114
124
  timeout: DEFAULT_TIMEOUT,
@@ -122,11 +132,35 @@ module Google
122
132
  require "google/gax/grpc"
123
133
  require "google/longrunning/operations_services_pb"
124
134
 
125
-
135
+ if channel || chan_creds || updater_proc
136
+ warn "The `channel`, `chan_creds`, and `updater_proc` parameters will be removed " \
137
+ "on 2017/09/08"
138
+ credentials ||= channel
139
+ credentials ||= chan_creds
140
+ credentials ||= updater_proc
141
+ end
126
142
  if app_name || app_version
127
143
  warn "`app_name` and `app_version` are no longer being used in the request headers."
128
144
  end
129
145
 
146
+ credentials ||= Google::Gax::Credentials.default(scope: scopes)
147
+
148
+ if credentials.is_a?(String) || credentials.is_a?(Hash)
149
+ updater_proc = Google::Gax::Credentials.new(credentials).updater_proc
150
+ end
151
+ if credentials.is_a?(GRPC::Core::Channel)
152
+ channel = credentials
153
+ end
154
+ if credentials.is_a?(GRPC::Core::ChannelCredentials)
155
+ chan_creds = credentials
156
+ end
157
+ if credentials.is_a?(Proc)
158
+ updater_proc = credentials
159
+ end
160
+ if credentials.is_a?(Google::Gax::Credentials)
161
+ updater_proc = credentials.updater_proc
162
+ end
163
+
130
164
  google_api_client = "gl-ruby/#{RUBY_VERSION}"
131
165
  google_api_client << " #{lib_name}/#{lib_version}" if lib_name
132
166
  google_api_client << " gapic/0.6.8 gax/#{Google::Gax::VERSION}"
@@ -193,16 +227,17 @@ module Google
193
227
  # @example
194
228
  # require "google/longrunning"
195
229
  #
196
- # operations_client = Google::Longrunning::OperationsClient.new
230
+ # operations_client = Google::Longrunning.new
197
231
  # name = ''
198
232
  # response = operations_client.get_operation(name)
199
233
 
200
234
  def get_operation \
201
235
  name,
202
236
  options: nil
203
- req = Google::Longrunning::GetOperationRequest.new({
237
+ req = {
204
238
  name: name
205
- }.delete_if { |_, v| v.nil? })
239
+ }.delete_if { |_, v| v.nil? }
240
+ req = Google::Gax::to_proto(req, Google::Longrunning::GetOperationRequest)
206
241
  @get_operation.call(req, options)
207
242
  end
208
243
 
@@ -234,7 +269,7 @@ module Google
234
269
  # @example
235
270
  # require "google/longrunning"
236
271
  #
237
- # operations_client = Google::Longrunning::OperationsClient.new
272
+ # operations_client = Google::Longrunning.new
238
273
  # name = ''
239
274
  # filter = ''
240
275
  #
@@ -256,11 +291,12 @@ module Google
256
291
  filter,
257
292
  page_size: nil,
258
293
  options: nil
259
- req = Google::Longrunning::ListOperationsRequest.new({
294
+ req = {
260
295
  name: name,
261
296
  filter: filter,
262
297
  page_size: page_size
263
- }.delete_if { |_, v| v.nil? })
298
+ }.delete_if { |_, v| v.nil? }
299
+ req = Google::Gax::to_proto(req, Google::Longrunning::ListOperationsRequest)
264
300
  @list_operations.call(req, options)
265
301
  end
266
302
 
@@ -268,11 +304,11 @@ module Google
268
304
  # makes a best effort to cancel the operation, but success is not
269
305
  # guaranteed. If the server doesn't support this method, it returns
270
306
  # +google.rpc.Code.UNIMPLEMENTED+. Clients can use
271
- # Operations::GetOperation or
307
+ # {Google::Longrunning::Operations::GetOperation Operations::GetOperation} or
272
308
  # other methods to check whether the cancellation succeeded or whether the
273
309
  # operation completed despite cancellation. On successful cancellation,
274
310
  # the operation is not deleted; instead, it becomes an operation with
275
- # an Operation#error value with a Google::Rpc::Status#code of 1,
311
+ # an {Google::Longrunning::Operation#error Operation#error} value with a {Google::Rpc::Status#code} of 1,
276
312
  # corresponding to +Code.CANCELLED+.
277
313
  #
278
314
  # @param name [String]
@@ -284,16 +320,17 @@ module Google
284
320
  # @example
285
321
  # require "google/longrunning"
286
322
  #
287
- # operations_client = Google::Longrunning::OperationsClient.new
323
+ # operations_client = Google::Longrunning.new
288
324
  # name = ''
289
325
  # operations_client.cancel_operation(name)
290
326
 
291
327
  def cancel_operation \
292
328
  name,
293
329
  options: nil
294
- req = Google::Longrunning::CancelOperationRequest.new({
330
+ req = {
295
331
  name: name
296
- }.delete_if { |_, v| v.nil? })
332
+ }.delete_if { |_, v| v.nil? }
333
+ req = Google::Gax::to_proto(req, Google::Longrunning::CancelOperationRequest)
297
334
  @cancel_operation.call(req, options)
298
335
  nil
299
336
  end
@@ -312,16 +349,17 @@ module Google
312
349
  # @example
313
350
  # require "google/longrunning"
314
351
  #
315
- # operations_client = Google::Longrunning::OperationsClient.new
352
+ # operations_client = Google::Longrunning.new
316
353
  # name = ''
317
354
  # operations_client.delete_operation(name)
318
355
 
319
356
  def delete_operation \
320
357
  name,
321
358
  options: nil
322
- req = Google::Longrunning::DeleteOperationRequest.new({
359
+ req = {
323
360
  name: name
324
- }.delete_if { |_, v| v.nil? })
361
+ }.delete_if { |_, v| v.nil? }
362
+ req = Google::Gax::to_proto(req, Google::Longrunning::DeleteOperationRequest)
325
363
  @delete_operation.call(req, options)
326
364
  nil
327
365
  end
@@ -6,9 +6,7 @@
6
6
  "DEADLINE_EXCEEDED",
7
7
  "UNAVAILABLE"
8
8
  ],
9
- "non_idempotent": [
10
- "UNAVAILABLE"
11
- ]
9
+ "non_idempotent": []
12
10
  },
13
11
  "retry_params": {
14
12
  "default": {
@@ -28,6 +28,7 @@ message User {
28
28
  string name = 1;
29
29
  UserType type = 2;
30
30
  repeated Post posts = 3;
31
+ map<string, string> map_field = 4;
31
32
  }
32
33
 
33
34
  message Post {
@@ -12,6 +12,7 @@ Google::Protobuf::DescriptorPool.generated_pool.build do
12
12
  optional :name, :string, 1
13
13
  optional :type, :enum, 2, "google.protobuf.User.UserType"
14
14
  repeated :posts, :message, 3, "google.protobuf.Post"
15
+ map :map_field, :string, :string, 4
15
16
  end
16
17
  add_enum "google.protobuf.User.UserType" do
17
18
  value :UNSPECIFIED, 0
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -36,6 +36,10 @@ describe Google::Gax do
36
36
  USER_NAME = 'Ernest'.freeze
37
37
  USER_TYPE = :ADMINISTRATOR
38
38
  POST_TEXT = 'This is a test post.'.freeze
39
+ MAP = {
40
+ 'key1' => 'val1',
41
+ 'key2' => 'val2'
42
+ }.freeze
39
43
 
40
44
  it 'creates a protobuf message from a simple hash' do
41
45
  hash = { name: USER_NAME, type: USER_TYPE }
@@ -91,6 +95,20 @@ describe Google::Gax do
91
95
  end
92
96
  end
93
97
 
98
+ it 'handles maps' do
99
+ request_hash = {
100
+ name: USER_NAME,
101
+ map_field: MAP
102
+ }
103
+ user = Google::Gax.to_proto(request_hash, Google::Protobuf::User)
104
+ expect(user).to be_an_instance_of(Google::Protobuf::User)
105
+ expect(user.name).to eq(USER_NAME)
106
+ expect(user.map_field).to be_an_instance_of(Google::Protobuf::Map)
107
+ user.map_field.each do |k, v|
108
+ expect(MAP[k]).to eq v
109
+ end
110
+ end
111
+
94
112
  it 'fails if a key does not exist in the target message type' do
95
113
  user_hash = {
96
114
  name: USER_NAME,
data/spec/spec_helper.rb CHANGED
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-gax
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.5
4
+ version: 0.8.7
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: 2017-07-21 00:00:00.000000000 Z
11
+ date: 2017-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: googleauth