forked from googleapis/gapic-generator-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.rb
515 lines (437 loc) · 22.9 KB
/
client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# frozen_string_literal: true
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require "google/gax"
require "google/gax/config"
require "google/gax/config/method"
require "google/showcase/version"
require "google/showcase/v1alpha3/echo_pb"
require "google/showcase/v1alpha3/echo/credentials"
require "google/showcase/v1alpha3/echo/operations"
module Google
module Showcase
module V1alpha3
module Echo
# Service that implements Echo API.
class Client
# @private
attr_reader :echo_stub
##
# Configuration for the Echo Client API.
#
# @yield [config] Configure the Client client.
# @yieldparam config [Client::Configuration]
#
# @return [Client::Configuration]
#
def self.configure
@configure ||= Client::Configuration.new
yield @configure if block_given?
@configure
end
##
# Configure the Echo Client instance.
#
# The configuration is set to the derived mode, meaning that values can be changed,
# but structural changes (adding new fields, etc.) are not allowed. Structural changes
# should be made on {Client.configure}.
#
# @yield [config] Configure the Client client.
# @yieldparam config [Client::Configuration]
#
# @return [Client::Configuration]
#
def configure
yield @config if block_given?
@config
end
##
# Create a new Client client object.
#
# @yield [config] Configure the Client client.
# @yieldparam config [Client::Configuration]
#
def initialize
# These require statements are intentionally placed here to initialize
# the gRPC module only when it's required.
# See https://github.com/googleapis/toolkit/issues/446
require "google/gax/grpc"
require "google/showcase/v1alpha3/echo_services_pb"
# Create the configuration object
@config = Configuration.new Client.configure
# Yield the configuration if needed
yield @config if block_given?
# Create credentials
credentials = @config.credentials
credentials ||= Credentials.default scope: @config.scope
if credentials.is_a?(String) || credentials.is_a?(Hash)
credentials = Credentials.new credentials, scope: @config.scope
end
@operations_client = Operations.new do |config|
config.credentials = credentials
end
@echo_stub = Google::Gax::Grpc::Stub.new(
Google::Showcase::V1alpha3::Echo::Stub,
credentials: credentials,
host: @config.host,
port: @config.port,
channel_args: @config.channel_args,
interceptors: @config.interceptors
)
end
# Service calls
##
# This method simply echos the request. This method is showcases unary rpcs.
#
# @param request [Google::Showcase::V1alpha3::EchoRequest | Hash]
# This method simply echos the request. This method is showcases unary rpcs.
# @param options [Google::Gax::ApiCall::Options, Hash]
# Overrides the default settings for this call, e.g, timeout, retries, etc.
#
# @yield [response, operation] Access the result along with the RPC operation
# @yieldparam response [Google::Showcase::V1alpha3::EchoResponse]
# @yieldparam operation [GRPC::ActiveCall::Operation]
#
# @return [Google::Showcase::V1alpha3::EchoResponse]
#
# @raise [Google::Gax::GaxError] if the RPC is aborted.
#
# @example
# TODO
#
def echo request, options = nil, &block
raise ArgumentError, "request must be provided" if request.nil?
request = Google::Gax::Protobuf.coerce request, to: Google::Showcase::V1alpha3::EchoRequest
# Converts hash and nil to an options object
options = Google::Gax::ApiCall::Options.new options.to_h if options.respond_to? :to_h
# Customize the options with defaults
metadata = @config.rpcs.echo.metadata.to_h
x_goog_api_client_header = ["gl-ruby/#{RUBY_VERSION}"]
x_goog_api_client_header << "#{@config.lib_name}/#{@config.lib_version}" if @config.lib_name
x_goog_api_client_header << "gapic/#{Google::Showcase::VERSION}"
x_goog_api_client_header << "gax/#{Google::Gax::VERSION}"
x_goog_api_client_header << "grpc/#{GRPC::VERSION}"
metadata[:"x-goog-api-client"] ||= x_goog_api_client_header.join " "
options.apply_defaults timeout: @config.rpcs.echo.timeout,
metadata: metadata,
retry_policy: @config.rpcs.echo.retry_policy
options.apply_defaults timeout: @config.timeout,
metadata: @config.metadata,
retry_policy: @config.retry_policy
@echo ||= Google::Gax::ApiCall.new @echo_stub.method :echo
@echo.call request, options: options, operation_callback: block
end
##
# This method split the given content into words and will pass each word back
# through the stream. This method showcases server-side streaming rpcs.
#
# @param request [Google::Showcase::V1alpha3::ExpandRequest | Hash]
# This method split the given content into words and will pass each word back
# through the stream. This method showcases server-side streaming rpcs.
# @param options [Google::Gax::ApiCall::Options, Hash]
# Overrides the default settings for this call, e.g, timeout, retries, etc.
#
# @yield [response, operation] Access the result along with the RPC operation
# @yieldparam response [Enumerable<Google::Showcase::V1alpha3::EchoResponse>]
# @yieldparam operation [GRPC::ActiveCall::Operation]
#
# @return [Enumerable<Google::Showcase::V1alpha3::EchoResponse>]
#
# @raise [Google::Gax::GaxError] if the RPC is aborted.
#
# @example
# TODO
#
def expand request, options = nil, &block
raise ArgumentError, "request must be provided" if request.nil?
request = Google::Gax::Protobuf.coerce request, to: Google::Showcase::V1alpha3::ExpandRequest
# Converts hash and nil to an options object
options = Google::Gax::ApiCall::Options.new options.to_h if options.respond_to? :to_h
# Customize the options with defaults
metadata = @config.rpcs.expand.metadata.to_h
x_goog_api_client_header = ["gl-ruby/#{RUBY_VERSION}"]
x_goog_api_client_header << "#{@config.lib_name}/#{@config.lib_version}" if @config.lib_name
x_goog_api_client_header << "gapic/#{Google::Showcase::VERSION}"
x_goog_api_client_header << "gax/#{Google::Gax::VERSION}"
x_goog_api_client_header << "grpc/#{GRPC::VERSION}"
metadata[:"x-goog-api-client"] ||= x_goog_api_client_header.join " "
options.apply_defaults timeout: @config.rpcs.expand.timeout,
metadata: metadata,
retry_policy: @config.rpcs.expand.retry_policy
options.apply_defaults timeout: @config.timeout,
metadata: @config.metadata,
retry_policy: @config.retry_policy
@expand ||= Google::Gax::ApiCall.new @echo_stub.method :expand
@expand.call request, options: options, operation_callback: block
end
##
# This method will collect the words given to it. When the stream is closed
# by the client, this method will return the a concatenation of the strings
# passed to it. This method showcases client-side streaming rpcs.
#
# @param request [Google::Gax::StreamInput, Enumerable<Google::Showcase::V1alpha3::EchoRequest | Hash>]
# An enumerable of {Google::Showcase::V1alpha3::EchoRequest} instances.
# @param options [Google::Gax::ApiCall::Options, Hash]
# Overrides the default settings for this call, e.g, timeout, retries, etc.
#
# @yield [response, operation] Access the result along with the RPC operation
# @yieldparam response [Google::Showcase::V1alpha3::EchoResponse]
# @yieldparam operation [GRPC::ActiveCall::Operation]
#
# @return [Google::Showcase::V1alpha3::EchoResponse]
#
# @raise [Google::Gax::GaxError] if the RPC is aborted.
#
# @example
# TODO
#
def collect request, options = nil, &block
unless request.is_a? Enumerable
if request.respond_to? :to_enum
request = request.to_enum
else
raise ArgumentError, "request must be an Enumerable"
end
end
request = request.lazy.map do |req|
Google::Gax::Protobuf.coerce req, to: Google::Showcase::V1alpha3::EchoRequest
end
# Converts hash and nil to an options object
options = Google::Gax::ApiCall::Options.new options.to_h if options.respond_to? :to_h
# Customize the options with defaults
metadata = @config.rpcs.collect.metadata.to_h
x_goog_api_client_header = ["gl-ruby/#{RUBY_VERSION}"]
x_goog_api_client_header << "#{@config.lib_name}/#{@config.lib_version}" if @config.lib_name
x_goog_api_client_header << "gapic/#{Google::Showcase::VERSION}"
x_goog_api_client_header << "gax/#{Google::Gax::VERSION}"
x_goog_api_client_header << "grpc/#{GRPC::VERSION}"
metadata[:"x-goog-api-client"] ||= x_goog_api_client_header.join " "
options.apply_defaults timeout: @config.rpcs.collect.timeout,
metadata: metadata,
retry_policy: @config.rpcs.collect.retry_policy
options.apply_defaults timeout: @config.timeout,
metadata: @config.metadata,
retry_policy: @config.retry_policy
@collect ||= Google::Gax::ApiCall.new @echo_stub.method :collect
@collect.call request, options: options, operation_callback: block
end
##
# This method, upon receiving a request on the stream, the same content will
# be passed back on the stream. This method showcases bidirectional
# streaming rpcs.
#
# @param request [Google::Gax::StreamInput, Enumerable<Google::Showcase::V1alpha3::EchoRequest | Hash>]
# An enumerable of {Google::Showcase::V1alpha3::EchoRequest} instances.
# @param options [Google::Gax::ApiCall::Options, Hash]
# Overrides the default settings for this call, e.g, timeout, retries, etc.
#
# @yield [response, operation] Access the result along with the RPC operation
# @yieldparam response [Enumerable<Google::Showcase::V1alpha3::EchoResponse>]
# @yieldparam operation [GRPC::ActiveCall::Operation]
#
# @return [Enumerable<Google::Showcase::V1alpha3::EchoResponse>]
#
# @raise [Google::Gax::GaxError] if the RPC is aborted.
#
# @example
# TODO
#
def chat request, options = nil, &block
unless request.is_a? Enumerable
if request.respond_to? :to_enum
request = request.to_enum
else
raise ArgumentError, "request must be an Enumerable"
end
end
request = request.lazy.map do |req|
Google::Gax::Protobuf.coerce req, to: Google::Showcase::V1alpha3::EchoRequest
end
# Converts hash and nil to an options object
options = Google::Gax::ApiCall::Options.new options.to_h if options.respond_to? :to_h
# Customize the options with defaults
metadata = @config.rpcs.chat.metadata.to_h
x_goog_api_client_header = ["gl-ruby/#{RUBY_VERSION}"]
x_goog_api_client_header << "#{@config.lib_name}/#{@config.lib_version}" if @config.lib_name
x_goog_api_client_header << "gapic/#{Google::Showcase::VERSION}"
x_goog_api_client_header << "gax/#{Google::Gax::VERSION}"
x_goog_api_client_header << "grpc/#{GRPC::VERSION}"
metadata[:"x-goog-api-client"] ||= x_goog_api_client_header.join " "
options.apply_defaults timeout: @config.rpcs.chat.timeout,
metadata: metadata,
retry_policy: @config.rpcs.chat.retry_policy
options.apply_defaults timeout: @config.timeout,
metadata: @config.metadata,
retry_policy: @config.retry_policy
@chat ||= Google::Gax::ApiCall.new @echo_stub.method :chat
@chat.call request, options: options, operation_callback: block
end
##
# This is similar to the Expand method but instead of returning a stream of
# expanded words, this method returns a paged list of expanded words.
#
# @param request [Google::Showcase::V1alpha3::PagedExpandRequest | Hash]
# This is similar to the Expand method but instead of returning a stream of
# expanded words, this method returns a paged list of expanded words.
# @param options [Google::Gax::ApiCall::Options, Hash]
# Overrides the default settings for this call, e.g, timeout, retries, etc.
#
# @yield [response, operation] Access the result along with the RPC operation
# @yieldparam response [Google::Gax::PagedEnumerable<Google::Showcase::V1alpha3::EchoResponse>]
# @yieldparam operation [GRPC::ActiveCall::Operation]
#
# @return [Google::Gax::PagedEnumerable<Google::Showcase::V1alpha3::EchoResponse>]
#
# @raise [Google::Gax::GaxError] if the RPC is aborted.
#
# @example
# TODO
#
def paged_expand request, options = nil, &block
raise ArgumentError, "request must be provided" if request.nil?
request = Google::Gax::Protobuf.coerce request, to: Google::Showcase::V1alpha3::PagedExpandRequest
# Converts hash and nil to an options object
options = Google::Gax::ApiCall::Options.new options.to_h if options.respond_to? :to_h
# Customize the options with defaults
metadata = @config.rpcs.paged_expand.metadata.to_h
x_goog_api_client_header = ["gl-ruby/#{RUBY_VERSION}"]
x_goog_api_client_header << "#{@config.lib_name}/#{@config.lib_version}" if @config.lib_name
x_goog_api_client_header << "gapic/#{Google::Showcase::VERSION}"
x_goog_api_client_header << "gax/#{Google::Gax::VERSION}"
x_goog_api_client_header << "grpc/#{GRPC::VERSION}"
metadata[:"x-goog-api-client"] ||= x_goog_api_client_header.join " "
options.apply_defaults timeout: @config.rpcs.paged_expand.timeout,
metadata: metadata,
retry_policy: @config.rpcs.paged_expand.retry_policy
options.apply_defaults timeout: @config.timeout,
metadata: @config.metadata,
retry_policy: @config.retry_policy
@paged_expand ||= Google::Gax::ApiCall.new @echo_stub.method :paged_expand
wrap_paged_enum = ->(response) { Google::Gax::PagedEnumerable.new @paged_expand, request, response, options }
@paged_expand.call request, options: options, operation_callback: block, format_response: wrap_paged_enum
end
##
# This method will wait the requested amount of and then return.
# This method showcases how a client handles a request timing out.
#
# @param request [Google::Showcase::V1alpha3::WaitRequest | Hash]
# This method will wait the requested amount of and then return.
# This method showcases how a client handles a request timing out.
# @param options [Google::Gax::ApiCall::Options, Hash]
# Overrides the default settings for this call, e.g, timeout, retries, etc.
#
# @yield [response, operation] Access the result along with the RPC operation
# @yieldparam response [Google::Gax::Operation]
# @yieldparam operation [GRPC::ActiveCall::Operation]
#
# @return [Google::Gax::Operation]
#
# @raise [Google::Gax::GaxError] if the RPC is aborted.
#
# @example
# TODO
#
def wait request, options = nil, &block
raise ArgumentError, "request must be provided" if request.nil?
request = Google::Gax::Protobuf.coerce request, to: Google::Showcase::V1alpha3::WaitRequest
# Converts hash and nil to an options object
options = Google::Gax::ApiCall::Options.new options.to_h if options.respond_to? :to_h
# Customize the options with defaults
metadata = @config.rpcs.wait.metadata.to_h
x_goog_api_client_header = ["gl-ruby/#{RUBY_VERSION}"]
x_goog_api_client_header << "#{@config.lib_name}/#{@config.lib_version}" if @config.lib_name
x_goog_api_client_header << "gapic/#{Google::Showcase::VERSION}"
x_goog_api_client_header << "gax/#{Google::Gax::VERSION}"
x_goog_api_client_header << "grpc/#{GRPC::VERSION}"
metadata[:"x-goog-api-client"] ||= x_goog_api_client_header.join " "
options.apply_defaults timeout: @config.rpcs.wait.timeout,
metadata: metadata,
retry_policy: @config.rpcs.wait.retry_policy
options.apply_defaults timeout: @config.timeout,
metadata: @config.metadata,
retry_policy: @config.retry_policy
@wait ||= Google::Gax::ApiCall.new @echo_stub.method :wait
wrap_gax_operation = ->(response) { Google::Gax::Operation.new response, @operations_client }
@wait.call request, options: options, operation_callback: block, format_response: wrap_gax_operation
end
class Configuration
extend Google::Gax::Config
config_attr :host, "localhost", String
config_attr :port, 7469, Integer
config_attr :credentials, nil do |value|
allowed = [::String, ::Hash, ::Proc, ::Google::Auth::Credentials, ::Signet::OAuth2::Client, nil]
allowed += [::GRPC::Core::Channel, ::GRPC::Core::ChannelCredentials] if defined? ::GRPC
allowed.any? { |klass| klass === value }
end
config_attr :scope, nil, String, Array, nil
config_attr :lib_name, nil, String, nil
config_attr :lib_version, nil, String, nil
config_attr :channel_args, nil, Hash, nil
config_attr :interceptors, nil, Array, nil
config_attr :timeout, nil, Numeric, nil
config_attr :metadata, nil, Hash, nil
config_attr :retry_policy, nil, Hash, Proc, nil
def initialize parent_config = nil
@parent_config = parent_config unless parent_config.nil?
yield self if block_given?
end
def rpcs
@rpcs ||= begin
parent_rpcs = nil
parent_rpcs = @parent_config.rpcs if @parent_config&.respond_to? :rpcs
Rpcs.new parent_rpcs
end
end
class Rpcs
attr_reader :echo
attr_reader :expand
attr_reader :collect
attr_reader :chat
attr_reader :paged_expand
attr_reader :wait
def initialize parent_rpcs = nil
echo_config = nil
echo_config = parent_rpcs&.echo if parent_rpcs&.respond_to? :echo
@echo = Google::Gax::Config::Method.new echo_config
expand_config = nil
expand_config = parent_rpcs&.expand if parent_rpcs&.respond_to? :expand
@expand = Google::Gax::Config::Method.new expand_config
collect_config = nil
collect_config = parent_rpcs&.collect if parent_rpcs&.respond_to? :collect
@collect = Google::Gax::Config::Method.new collect_config
chat_config = nil
chat_config = parent_rpcs&.chat if parent_rpcs&.respond_to? :chat
@chat = Google::Gax::Config::Method.new chat_config
paged_expand_config = nil
paged_expand_config = parent_rpcs&.paged_expand if parent_rpcs&.respond_to? :paged_expand
@paged_expand = Google::Gax::Config::Method.new paged_expand_config
wait_config = nil
wait_config = parent_rpcs&.wait if parent_rpcs&.respond_to? :wait
@wait = Google::Gax::Config::Method.new wait_config
yield self if block_given?
end
end
end
end
end
end
end
end
# rubocop:disable Lint/HandleExceptions
# Once client is loaded, load helpers.rb if it exists.
begin
require "google/showcase/v1alpha3/echo/helpers"
rescue LoadError
end
# rubocop:enable Lint/HandleExceptions