google-cloud-speech-v1 0.10.1 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,448 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2023 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ # Auto-generated by gapic-generator-ruby. DO NOT EDIT!
18
+
19
+ require "google/cloud/errors"
20
+ require "google/cloud/speech/v1/cloud_speech_pb"
21
+ require "google/cloud/speech/v1/speech/rest/service_stub"
22
+
23
+ module Google
24
+ module Cloud
25
+ module Speech
26
+ module V1
27
+ module Speech
28
+ module Rest
29
+ ##
30
+ # REST client for the Speech service.
31
+ #
32
+ # Service that implements Google Cloud Speech API.
33
+ #
34
+ class Client
35
+ include Paths
36
+
37
+ # @private
38
+ attr_reader :speech_stub
39
+
40
+ ##
41
+ # Configure the Speech Client class.
42
+ #
43
+ # See {::Google::Cloud::Speech::V1::Speech::Rest::Client::Configuration}
44
+ # for a description of the configuration fields.
45
+ #
46
+ # @example
47
+ #
48
+ # # Modify the configuration for all Speech clients
49
+ # ::Google::Cloud::Speech::V1::Speech::Rest::Client.configure do |config|
50
+ # config.timeout = 10.0
51
+ # end
52
+ #
53
+ # @yield [config] Configure the Client client.
54
+ # @yieldparam config [Client::Configuration]
55
+ #
56
+ # @return [Client::Configuration]
57
+ #
58
+ def self.configure
59
+ @configure ||= begin
60
+ namespace = ["Google", "Cloud", "Speech", "V1"]
61
+ parent_config = while namespace.any?
62
+ parent_name = namespace.join "::"
63
+ parent_const = const_get parent_name
64
+ break parent_const.configure if parent_const.respond_to? :configure
65
+ namespace.pop
66
+ end
67
+ default_config = Client::Configuration.new parent_config
68
+
69
+ default_config.rpcs.recognize.timeout = 5000.0
70
+ default_config.rpcs.recognize.retry_policy = {
71
+ initial_delay: 0.1, max_delay: 60.0, multiplier: 1.3, retry_codes: [14, 4]
72
+ }
73
+
74
+ default_config.rpcs.long_running_recognize.timeout = 5000.0
75
+
76
+ default_config
77
+ end
78
+ yield @configure if block_given?
79
+ @configure
80
+ end
81
+
82
+ ##
83
+ # Configure the Speech Client instance.
84
+ #
85
+ # The configuration is set to the derived mode, meaning that values can be changed,
86
+ # but structural changes (adding new fields, etc.) are not allowed. Structural changes
87
+ # should be made on {Client.configure}.
88
+ #
89
+ # See {::Google::Cloud::Speech::V1::Speech::Rest::Client::Configuration}
90
+ # for a description of the configuration fields.
91
+ #
92
+ # @yield [config] Configure the Client client.
93
+ # @yieldparam config [Client::Configuration]
94
+ #
95
+ # @return [Client::Configuration]
96
+ #
97
+ def configure
98
+ yield @config if block_given?
99
+ @config
100
+ end
101
+
102
+ ##
103
+ # Create a new Speech REST client object.
104
+ #
105
+ # @example
106
+ #
107
+ # # Create a client using the default configuration
108
+ # client = ::Google::Cloud::Speech::V1::Speech::Rest::Client.new
109
+ #
110
+ # # Create a client using a custom configuration
111
+ # client = ::Google::Cloud::Speech::V1::Speech::Rest::Client.new do |config|
112
+ # config.timeout = 10.0
113
+ # end
114
+ #
115
+ # @yield [config] Configure the Speech client.
116
+ # @yieldparam config [Client::Configuration]
117
+ #
118
+ def initialize
119
+ # Create the configuration object
120
+ @config = Configuration.new Client.configure
121
+
122
+ # Yield the configuration if needed
123
+ yield @config if block_given?
124
+
125
+ # Create credentials
126
+ credentials = @config.credentials
127
+ # Use self-signed JWT if the endpoint is unchanged from default,
128
+ # but only if the default endpoint does not have a region prefix.
129
+ enable_self_signed_jwt = @config.endpoint == Client.configure.endpoint &&
130
+ !@config.endpoint.split(".").first.include?("-")
131
+ credentials ||= Credentials.default scope: @config.scope,
132
+ enable_self_signed_jwt: enable_self_signed_jwt
133
+ if credentials.is_a?(::String) || credentials.is_a?(::Hash)
134
+ credentials = Credentials.new credentials, scope: @config.scope
135
+ end
136
+
137
+ @quota_project_id = @config.quota_project
138
+ @quota_project_id ||= credentials.quota_project_id if credentials.respond_to? :quota_project_id
139
+
140
+ @operations_client = ::Google::Cloud::Speech::V1::Speech::Rest::Operations.new do |config|
141
+ config.credentials = credentials
142
+ config.quota_project = @quota_project_id
143
+ config.endpoint = @config.endpoint
144
+ end
145
+
146
+ @speech_stub = ::Google::Cloud::Speech::V1::Speech::Rest::ServiceStub.new endpoint: @config.endpoint, credentials: credentials
147
+ end
148
+
149
+ ##
150
+ # Get the associated client for long-running operations.
151
+ #
152
+ # @return [::Google::Cloud::Speech::V1::Speech::Rest::Operations]
153
+ #
154
+ attr_reader :operations_client
155
+
156
+ # Service calls
157
+
158
+ ##
159
+ # Performs synchronous speech recognition: receive results after all audio
160
+ # has been sent and processed.
161
+ #
162
+ # @overload recognize(request, options = nil)
163
+ # Pass arguments to `recognize` via a request object, either of type
164
+ # {::Google::Cloud::Speech::V1::RecognizeRequest} or an equivalent Hash.
165
+ #
166
+ # @param request [::Google::Cloud::Speech::V1::RecognizeRequest, ::Hash]
167
+ # A request object representing the call parameters. Required. To specify no
168
+ # parameters, or to keep all the default parameter values, pass an empty Hash.
169
+ # @param options [::Gapic::CallOptions, ::Hash]
170
+ # Overrides the default settings for this call, e.g, timeout, retries etc. Optional.
171
+ #
172
+ # @overload recognize(config: nil, audio: nil)
173
+ # Pass arguments to `recognize` via keyword arguments. Note that at
174
+ # least one keyword argument is required. To specify no parameters, or to keep all
175
+ # the default parameter values, pass an empty Hash as a request object (see above).
176
+ #
177
+ # @param config [::Google::Cloud::Speech::V1::RecognitionConfig, ::Hash]
178
+ # Required. Provides information to the recognizer that specifies how to
179
+ # process the request.
180
+ # @param audio [::Google::Cloud::Speech::V1::RecognitionAudio, ::Hash]
181
+ # Required. The audio data to be recognized.
182
+ # @yield [result, operation] Access the result along with the TransportOperation object
183
+ # @yieldparam result [::Google::Cloud::Speech::V1::RecognizeResponse]
184
+ # @yieldparam operation [::Gapic::Rest::TransportOperation]
185
+ #
186
+ # @return [::Google::Cloud::Speech::V1::RecognizeResponse]
187
+ #
188
+ # @raise [::Google::Cloud::Error] if the REST call is aborted.
189
+ def recognize request, options = nil
190
+ raise ::ArgumentError, "request must be provided" if request.nil?
191
+
192
+ request = ::Gapic::Protobuf.coerce request, to: ::Google::Cloud::Speech::V1::RecognizeRequest
193
+
194
+ # Converts hash and nil to an options object
195
+ options = ::Gapic::CallOptions.new(**options.to_h) if options.respond_to? :to_h
196
+
197
+ # Customize the options with defaults
198
+ call_metadata = @config.rpcs.recognize.metadata.to_h
199
+
200
+ # Set x-goog-api-client and x-goog-user-project headers
201
+ call_metadata[:"x-goog-api-client"] ||= ::Gapic::Headers.x_goog_api_client \
202
+ lib_name: @config.lib_name, lib_version: @config.lib_version,
203
+ gapic_version: ::Google::Cloud::Speech::V1::VERSION,
204
+ transports_version_send: [:rest]
205
+
206
+ call_metadata[:"x-goog-user-project"] = @quota_project_id if @quota_project_id
207
+
208
+ options.apply_defaults timeout: @config.rpcs.recognize.timeout,
209
+ metadata: call_metadata,
210
+ retry_policy: @config.rpcs.recognize.retry_policy
211
+
212
+ options.apply_defaults timeout: @config.timeout,
213
+ metadata: @config.metadata,
214
+ retry_policy: @config.retry_policy
215
+
216
+ @speech_stub.recognize request, options do |result, operation|
217
+ yield result, operation if block_given?
218
+ return result
219
+ end
220
+ rescue ::Gapic::Rest::Error => e
221
+ raise ::Google::Cloud::Error.from_error(e)
222
+ end
223
+
224
+ ##
225
+ # Performs asynchronous speech recognition: receive results via the
226
+ # google.longrunning.Operations interface. Returns either an
227
+ # `Operation.error` or an `Operation.response` which contains
228
+ # a `LongRunningRecognizeResponse` message.
229
+ # For more information on asynchronous speech recognition, see the
230
+ # [how-to](https://cloud.google.com/speech-to-text/docs/async-recognize).
231
+ #
232
+ # @overload long_running_recognize(request, options = nil)
233
+ # Pass arguments to `long_running_recognize` via a request object, either of type
234
+ # {::Google::Cloud::Speech::V1::LongRunningRecognizeRequest} or an equivalent Hash.
235
+ #
236
+ # @param request [::Google::Cloud::Speech::V1::LongRunningRecognizeRequest, ::Hash]
237
+ # A request object representing the call parameters. Required. To specify no
238
+ # parameters, or to keep all the default parameter values, pass an empty Hash.
239
+ # @param options [::Gapic::CallOptions, ::Hash]
240
+ # Overrides the default settings for this call, e.g, timeout, retries etc. Optional.
241
+ #
242
+ # @overload long_running_recognize(config: nil, audio: nil, output_config: nil)
243
+ # Pass arguments to `long_running_recognize` via keyword arguments. Note that at
244
+ # least one keyword argument is required. To specify no parameters, or to keep all
245
+ # the default parameter values, pass an empty Hash as a request object (see above).
246
+ #
247
+ # @param config [::Google::Cloud::Speech::V1::RecognitionConfig, ::Hash]
248
+ # Required. Provides information to the recognizer that specifies how to
249
+ # process the request.
250
+ # @param audio [::Google::Cloud::Speech::V1::RecognitionAudio, ::Hash]
251
+ # Required. The audio data to be recognized.
252
+ # @param output_config [::Google::Cloud::Speech::V1::TranscriptOutputConfig, ::Hash]
253
+ # Optional. Specifies an optional destination for the recognition results.
254
+ # @yield [result, operation] Access the result along with the TransportOperation object
255
+ # @yieldparam result [::Gapic::Operation]
256
+ # @yieldparam operation [::Gapic::Rest::TransportOperation]
257
+ #
258
+ # @return [::Gapic::Operation]
259
+ #
260
+ # @raise [::Google::Cloud::Error] if the REST call is aborted.
261
+ def long_running_recognize request, options = nil
262
+ raise ::ArgumentError, "request must be provided" if request.nil?
263
+
264
+ request = ::Gapic::Protobuf.coerce request, to: ::Google::Cloud::Speech::V1::LongRunningRecognizeRequest
265
+
266
+ # Converts hash and nil to an options object
267
+ options = ::Gapic::CallOptions.new(**options.to_h) if options.respond_to? :to_h
268
+
269
+ # Customize the options with defaults
270
+ call_metadata = @config.rpcs.long_running_recognize.metadata.to_h
271
+
272
+ # Set x-goog-api-client and x-goog-user-project headers
273
+ call_metadata[:"x-goog-api-client"] ||= ::Gapic::Headers.x_goog_api_client \
274
+ lib_name: @config.lib_name, lib_version: @config.lib_version,
275
+ gapic_version: ::Google::Cloud::Speech::V1::VERSION,
276
+ transports_version_send: [:rest]
277
+
278
+ call_metadata[:"x-goog-user-project"] = @quota_project_id if @quota_project_id
279
+
280
+ options.apply_defaults timeout: @config.rpcs.long_running_recognize.timeout,
281
+ metadata: call_metadata,
282
+ retry_policy: @config.rpcs.long_running_recognize.retry_policy
283
+
284
+ options.apply_defaults timeout: @config.timeout,
285
+ metadata: @config.metadata,
286
+ retry_policy: @config.retry_policy
287
+
288
+ @speech_stub.long_running_recognize request, options do |result, operation|
289
+ result = ::Gapic::Operation.new result, @operations_client, options: options
290
+ yield result, operation if block_given?
291
+ return result
292
+ end
293
+ rescue ::Gapic::Rest::Error => e
294
+ raise ::Google::Cloud::Error.from_error(e)
295
+ end
296
+
297
+ ##
298
+ # Configuration class for the Speech REST API.
299
+ #
300
+ # This class represents the configuration for Speech REST,
301
+ # providing control over timeouts, retry behavior, logging, transport
302
+ # parameters, and other low-level controls. Certain parameters can also be
303
+ # applied individually to specific RPCs. See
304
+ # {::Google::Cloud::Speech::V1::Speech::Rest::Client::Configuration::Rpcs}
305
+ # for a list of RPCs that can be configured independently.
306
+ #
307
+ # Configuration can be applied globally to all clients, or to a single client
308
+ # on construction.
309
+ #
310
+ # @example
311
+ #
312
+ # # Modify the global config, setting the timeout for
313
+ # # recognize to 20 seconds,
314
+ # # and all remaining timeouts to 10 seconds.
315
+ # ::Google::Cloud::Speech::V1::Speech::Rest::Client.configure do |config|
316
+ # config.timeout = 10.0
317
+ # config.rpcs.recognize.timeout = 20.0
318
+ # end
319
+ #
320
+ # # Apply the above configuration only to a new client.
321
+ # client = ::Google::Cloud::Speech::V1::Speech::Rest::Client.new do |config|
322
+ # config.timeout = 10.0
323
+ # config.rpcs.recognize.timeout = 20.0
324
+ # end
325
+ #
326
+ # @!attribute [rw] endpoint
327
+ # The hostname or hostname:port of the service endpoint.
328
+ # Defaults to `"speech.googleapis.com"`.
329
+ # @return [::String]
330
+ # @!attribute [rw] credentials
331
+ # Credentials to send with calls. You may provide any of the following types:
332
+ # * (`String`) The path to a service account key file in JSON format
333
+ # * (`Hash`) A service account key as a Hash
334
+ # * (`Google::Auth::Credentials`) A googleauth credentials object
335
+ # (see the [googleauth docs](https://googleapis.dev/ruby/googleauth/latest/index.html))
336
+ # * (`Signet::OAuth2::Client`) A signet oauth2 client object
337
+ # (see the [signet docs](https://googleapis.dev/ruby/signet/latest/Signet/OAuth2/Client.html))
338
+ # * (`nil`) indicating no credentials
339
+ # @return [::Object]
340
+ # @!attribute [rw] scope
341
+ # The OAuth scopes
342
+ # @return [::Array<::String>]
343
+ # @!attribute [rw] lib_name
344
+ # The library name as recorded in instrumentation and logging
345
+ # @return [::String]
346
+ # @!attribute [rw] lib_version
347
+ # The library version as recorded in instrumentation and logging
348
+ # @return [::String]
349
+ # @!attribute [rw] timeout
350
+ # The call timeout in seconds.
351
+ # @return [::Numeric]
352
+ # @!attribute [rw] metadata
353
+ # Additional headers to be sent with the call.
354
+ # @return [::Hash{::Symbol=>::String}]
355
+ # @!attribute [rw] retry_policy
356
+ # The retry policy. The value is a hash with the following keys:
357
+ # * `:initial_delay` (*type:* `Numeric`) - The initial delay in seconds.
358
+ # * `:max_delay` (*type:* `Numeric`) - The max delay in seconds.
359
+ # * `:multiplier` (*type:* `Numeric`) - The incremental backoff multiplier.
360
+ # * `:retry_codes` (*type:* `Array<String>`) - The error codes that should
361
+ # trigger a retry.
362
+ # @return [::Hash]
363
+ # @!attribute [rw] quota_project
364
+ # A separate project against which to charge quota.
365
+ # @return [::String]
366
+ #
367
+ class Configuration
368
+ extend ::Gapic::Config
369
+
370
+ config_attr :endpoint, "speech.googleapis.com", ::String
371
+ config_attr :credentials, nil do |value|
372
+ allowed = [::String, ::Hash, ::Proc, ::Symbol, ::Google::Auth::Credentials, ::Signet::OAuth2::Client, nil]
373
+ allowed.any? { |klass| klass === value }
374
+ end
375
+ config_attr :scope, nil, ::String, ::Array, nil
376
+ config_attr :lib_name, nil, ::String, nil
377
+ config_attr :lib_version, nil, ::String, nil
378
+ config_attr :timeout, nil, ::Numeric, nil
379
+ config_attr :metadata, nil, ::Hash, nil
380
+ config_attr :retry_policy, nil, ::Hash, ::Proc, nil
381
+ config_attr :quota_project, nil, ::String, nil
382
+
383
+ # @private
384
+ def initialize parent_config = nil
385
+ @parent_config = parent_config unless parent_config.nil?
386
+
387
+ yield self if block_given?
388
+ end
389
+
390
+ ##
391
+ # Configurations for individual RPCs
392
+ # @return [Rpcs]
393
+ #
394
+ def rpcs
395
+ @rpcs ||= begin
396
+ parent_rpcs = nil
397
+ parent_rpcs = @parent_config.rpcs if defined?(@parent_config) && @parent_config.respond_to?(:rpcs)
398
+ Rpcs.new parent_rpcs
399
+ end
400
+ end
401
+
402
+ ##
403
+ # Configuration RPC class for the Speech API.
404
+ #
405
+ # Includes fields providing the configuration for each RPC in this service.
406
+ # Each configuration object is of type `Gapic::Config::Method` and includes
407
+ # the following configuration fields:
408
+ #
409
+ # * `timeout` (*type:* `Numeric`) - The call timeout in seconds
410
+ # * `metadata` (*type:* `Hash{Symbol=>String}`) - Additional headers
411
+ # * `retry_policy (*type:* `Hash`) - The retry policy. The policy fields
412
+ # include the following keys:
413
+ # * `:initial_delay` (*type:* `Numeric`) - The initial delay in seconds.
414
+ # * `:max_delay` (*type:* `Numeric`) - The max delay in seconds.
415
+ # * `:multiplier` (*type:* `Numeric`) - The incremental backoff multiplier.
416
+ # * `:retry_codes` (*type:* `Array<String>`) - The error codes that should
417
+ # trigger a retry.
418
+ #
419
+ class Rpcs
420
+ ##
421
+ # RPC-specific configuration for `recognize`
422
+ # @return [::Gapic::Config::Method]
423
+ #
424
+ attr_reader :recognize
425
+ ##
426
+ # RPC-specific configuration for `long_running_recognize`
427
+ # @return [::Gapic::Config::Method]
428
+ #
429
+ attr_reader :long_running_recognize
430
+
431
+ # @private
432
+ def initialize parent_rpcs = nil
433
+ recognize_config = parent_rpcs.recognize if parent_rpcs.respond_to? :recognize
434
+ @recognize = ::Gapic::Config::Method.new recognize_config
435
+ long_running_recognize_config = parent_rpcs.long_running_recognize if parent_rpcs.respond_to? :long_running_recognize
436
+ @long_running_recognize = ::Gapic::Config::Method.new long_running_recognize_config
437
+
438
+ yield self if block_given?
439
+ end
440
+ end
441
+ end
442
+ end
443
+ end
444
+ end
445
+ end
446
+ end
447
+ end
448
+ end