google-cloud-language-v2 0.a → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,751 @@
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/language/v2/language_service_pb"
21
+
22
+ module Google
23
+ module Cloud
24
+ module Language
25
+ module V2
26
+ module LanguageService
27
+ ##
28
+ # Client for the LanguageService service.
29
+ #
30
+ # Provides text analysis operations such as sentiment analysis and entity
31
+ # recognition.
32
+ #
33
+ class Client
34
+ # @private
35
+ attr_reader :language_service_stub
36
+
37
+ ##
38
+ # Configure the LanguageService Client class.
39
+ #
40
+ # See {::Google::Cloud::Language::V2::LanguageService::Client::Configuration}
41
+ # for a description of the configuration fields.
42
+ #
43
+ # @example
44
+ #
45
+ # # Modify the configuration for all LanguageService clients
46
+ # ::Google::Cloud::Language::V2::LanguageService::Client.configure do |config|
47
+ # config.timeout = 10.0
48
+ # end
49
+ #
50
+ # @yield [config] Configure the Client client.
51
+ # @yieldparam config [Client::Configuration]
52
+ #
53
+ # @return [Client::Configuration]
54
+ #
55
+ def self.configure
56
+ @configure ||= begin
57
+ namespace = ["Google", "Cloud", "Language", "V2"]
58
+ parent_config = while namespace.any?
59
+ parent_name = namespace.join "::"
60
+ parent_const = const_get parent_name
61
+ break parent_const.configure if parent_const.respond_to? :configure
62
+ namespace.pop
63
+ end
64
+ default_config = Client::Configuration.new parent_config
65
+
66
+ default_config.rpcs.analyze_sentiment.timeout = 600.0
67
+ default_config.rpcs.analyze_sentiment.retry_policy = {
68
+ initial_delay: 0.1, max_delay: 60.0, multiplier: 1.3, retry_codes: [4, 14]
69
+ }
70
+
71
+ default_config.rpcs.analyze_entities.timeout = 600.0
72
+ default_config.rpcs.analyze_entities.retry_policy = {
73
+ initial_delay: 0.1, max_delay: 60.0, multiplier: 1.3, retry_codes: [4, 14]
74
+ }
75
+
76
+ default_config.rpcs.classify_text.timeout = 600.0
77
+ default_config.rpcs.classify_text.retry_policy = {
78
+ initial_delay: 0.1, max_delay: 60.0, multiplier: 1.3, retry_codes: [4, 14]
79
+ }
80
+
81
+ default_config.rpcs.moderate_text.timeout = 600.0
82
+ default_config.rpcs.moderate_text.retry_policy = {
83
+ initial_delay: 0.1, max_delay: 60.0, multiplier: 1.3, retry_codes: [4, 14]
84
+ }
85
+
86
+ default_config.rpcs.annotate_text.timeout = 600.0
87
+ default_config.rpcs.annotate_text.retry_policy = {
88
+ initial_delay: 0.1, max_delay: 60.0, multiplier: 1.3, retry_codes: [4, 14]
89
+ }
90
+
91
+ default_config
92
+ end
93
+ yield @configure if block_given?
94
+ @configure
95
+ end
96
+
97
+ ##
98
+ # Configure the LanguageService Client instance.
99
+ #
100
+ # The configuration is set to the derived mode, meaning that values can be changed,
101
+ # but structural changes (adding new fields, etc.) are not allowed. Structural changes
102
+ # should be made on {Client.configure}.
103
+ #
104
+ # See {::Google::Cloud::Language::V2::LanguageService::Client::Configuration}
105
+ # for a description of the configuration fields.
106
+ #
107
+ # @yield [config] Configure the Client client.
108
+ # @yieldparam config [Client::Configuration]
109
+ #
110
+ # @return [Client::Configuration]
111
+ #
112
+ def configure
113
+ yield @config if block_given?
114
+ @config
115
+ end
116
+
117
+ ##
118
+ # Create a new LanguageService client object.
119
+ #
120
+ # @example
121
+ #
122
+ # # Create a client using the default configuration
123
+ # client = ::Google::Cloud::Language::V2::LanguageService::Client.new
124
+ #
125
+ # # Create a client using a custom configuration
126
+ # client = ::Google::Cloud::Language::V2::LanguageService::Client.new do |config|
127
+ # config.timeout = 10.0
128
+ # end
129
+ #
130
+ # @yield [config] Configure the LanguageService client.
131
+ # @yieldparam config [Client::Configuration]
132
+ #
133
+ def initialize
134
+ # These require statements are intentionally placed here to initialize
135
+ # the gRPC module only when it's required.
136
+ # See https://github.com/googleapis/toolkit/issues/446
137
+ require "gapic/grpc"
138
+ require "google/cloud/language/v2/language_service_services_pb"
139
+
140
+ # Create the configuration object
141
+ @config = Configuration.new Client.configure
142
+
143
+ # Yield the configuration if needed
144
+ yield @config if block_given?
145
+
146
+ # Create credentials
147
+ credentials = @config.credentials
148
+ # Use self-signed JWT if the endpoint is unchanged from default,
149
+ # but only if the default endpoint does not have a region prefix.
150
+ enable_self_signed_jwt = @config.endpoint == Configuration::DEFAULT_ENDPOINT &&
151
+ !@config.endpoint.split(".").first.include?("-")
152
+ credentials ||= Credentials.default scope: @config.scope,
153
+ enable_self_signed_jwt: enable_self_signed_jwt
154
+ if credentials.is_a?(::String) || credentials.is_a?(::Hash)
155
+ credentials = Credentials.new credentials, scope: @config.scope
156
+ end
157
+ @quota_project_id = @config.quota_project
158
+ @quota_project_id ||= credentials.quota_project_id if credentials.respond_to? :quota_project_id
159
+
160
+ @language_service_stub = ::Gapic::ServiceStub.new(
161
+ ::Google::Cloud::Language::V2::LanguageService::Stub,
162
+ credentials: credentials,
163
+ endpoint: @config.endpoint,
164
+ channel_args: @config.channel_args,
165
+ interceptors: @config.interceptors
166
+ )
167
+ end
168
+
169
+ # Service calls
170
+
171
+ ##
172
+ # Analyzes the sentiment of the provided text.
173
+ #
174
+ # @overload analyze_sentiment(request, options = nil)
175
+ # Pass arguments to `analyze_sentiment` via a request object, either of type
176
+ # {::Google::Cloud::Language::V2::AnalyzeSentimentRequest} or an equivalent Hash.
177
+ #
178
+ # @param request [::Google::Cloud::Language::V2::AnalyzeSentimentRequest, ::Hash]
179
+ # A request object representing the call parameters. Required. To specify no
180
+ # parameters, or to keep all the default parameter values, pass an empty Hash.
181
+ # @param options [::Gapic::CallOptions, ::Hash]
182
+ # Overrides the default settings for this call, e.g, timeout, retries, etc. Optional.
183
+ #
184
+ # @overload analyze_sentiment(document: nil, encoding_type: nil)
185
+ # Pass arguments to `analyze_sentiment` via keyword arguments. Note that at
186
+ # least one keyword argument is required. To specify no parameters, or to keep all
187
+ # the default parameter values, pass an empty Hash as a request object (see above).
188
+ #
189
+ # @param document [::Google::Cloud::Language::V2::Document, ::Hash]
190
+ # Required. Input document.
191
+ # @param encoding_type [::Google::Cloud::Language::V2::EncodingType]
192
+ # The encoding type used by the API to calculate sentence offsets.
193
+ #
194
+ # @yield [response, operation] Access the result along with the RPC operation
195
+ # @yieldparam response [::Google::Cloud::Language::V2::AnalyzeSentimentResponse]
196
+ # @yieldparam operation [::GRPC::ActiveCall::Operation]
197
+ #
198
+ # @return [::Google::Cloud::Language::V2::AnalyzeSentimentResponse]
199
+ #
200
+ # @raise [::Google::Cloud::Error] if the RPC is aborted.
201
+ #
202
+ # @example Basic example
203
+ # require "google/cloud/language/v2"
204
+ #
205
+ # # Create a client object. The client can be reused for multiple calls.
206
+ # client = Google::Cloud::Language::V2::LanguageService::Client.new
207
+ #
208
+ # # Create a request. To set request fields, pass in keyword arguments.
209
+ # request = Google::Cloud::Language::V2::AnalyzeSentimentRequest.new
210
+ #
211
+ # # Call the analyze_sentiment method.
212
+ # result = client.analyze_sentiment request
213
+ #
214
+ # # The returned object is of type Google::Cloud::Language::V2::AnalyzeSentimentResponse.
215
+ # p result
216
+ #
217
+ def analyze_sentiment request, options = nil
218
+ raise ::ArgumentError, "request must be provided" if request.nil?
219
+
220
+ request = ::Gapic::Protobuf.coerce request, to: ::Google::Cloud::Language::V2::AnalyzeSentimentRequest
221
+
222
+ # Converts hash and nil to an options object
223
+ options = ::Gapic::CallOptions.new(**options.to_h) if options.respond_to? :to_h
224
+
225
+ # Customize the options with defaults
226
+ metadata = @config.rpcs.analyze_sentiment.metadata.to_h
227
+
228
+ # Set x-goog-api-client and x-goog-user-project headers
229
+ metadata[:"x-goog-api-client"] ||= ::Gapic::Headers.x_goog_api_client \
230
+ lib_name: @config.lib_name, lib_version: @config.lib_version,
231
+ gapic_version: ::Google::Cloud::Language::V2::VERSION
232
+ metadata[:"x-goog-user-project"] = @quota_project_id if @quota_project_id
233
+
234
+ options.apply_defaults timeout: @config.rpcs.analyze_sentiment.timeout,
235
+ metadata: metadata,
236
+ retry_policy: @config.rpcs.analyze_sentiment.retry_policy
237
+
238
+ options.apply_defaults timeout: @config.timeout,
239
+ metadata: @config.metadata,
240
+ retry_policy: @config.retry_policy
241
+
242
+ @language_service_stub.call_rpc :analyze_sentiment, request, options: options do |response, operation|
243
+ yield response, operation if block_given?
244
+ return response
245
+ end
246
+ rescue ::GRPC::BadStatus => e
247
+ raise ::Google::Cloud::Error.from_error(e)
248
+ end
249
+
250
+ ##
251
+ # Finds named entities (currently proper names and common nouns) in the text
252
+ # along with entity types, probability, mentions for each entity, and
253
+ # other properties.
254
+ #
255
+ # @overload analyze_entities(request, options = nil)
256
+ # Pass arguments to `analyze_entities` via a request object, either of type
257
+ # {::Google::Cloud::Language::V2::AnalyzeEntitiesRequest} or an equivalent Hash.
258
+ #
259
+ # @param request [::Google::Cloud::Language::V2::AnalyzeEntitiesRequest, ::Hash]
260
+ # A request object representing the call parameters. Required. To specify no
261
+ # parameters, or to keep all the default parameter values, pass an empty Hash.
262
+ # @param options [::Gapic::CallOptions, ::Hash]
263
+ # Overrides the default settings for this call, e.g, timeout, retries, etc. Optional.
264
+ #
265
+ # @overload analyze_entities(document: nil, encoding_type: nil)
266
+ # Pass arguments to `analyze_entities` via keyword arguments. Note that at
267
+ # least one keyword argument is required. To specify no parameters, or to keep all
268
+ # the default parameter values, pass an empty Hash as a request object (see above).
269
+ #
270
+ # @param document [::Google::Cloud::Language::V2::Document, ::Hash]
271
+ # Required. Input document.
272
+ # @param encoding_type [::Google::Cloud::Language::V2::EncodingType]
273
+ # The encoding type used by the API to calculate offsets.
274
+ #
275
+ # @yield [response, operation] Access the result along with the RPC operation
276
+ # @yieldparam response [::Google::Cloud::Language::V2::AnalyzeEntitiesResponse]
277
+ # @yieldparam operation [::GRPC::ActiveCall::Operation]
278
+ #
279
+ # @return [::Google::Cloud::Language::V2::AnalyzeEntitiesResponse]
280
+ #
281
+ # @raise [::Google::Cloud::Error] if the RPC is aborted.
282
+ #
283
+ # @example Basic example
284
+ # require "google/cloud/language/v2"
285
+ #
286
+ # # Create a client object. The client can be reused for multiple calls.
287
+ # client = Google::Cloud::Language::V2::LanguageService::Client.new
288
+ #
289
+ # # Create a request. To set request fields, pass in keyword arguments.
290
+ # request = Google::Cloud::Language::V2::AnalyzeEntitiesRequest.new
291
+ #
292
+ # # Call the analyze_entities method.
293
+ # result = client.analyze_entities request
294
+ #
295
+ # # The returned object is of type Google::Cloud::Language::V2::AnalyzeEntitiesResponse.
296
+ # p result
297
+ #
298
+ def analyze_entities request, options = nil
299
+ raise ::ArgumentError, "request must be provided" if request.nil?
300
+
301
+ request = ::Gapic::Protobuf.coerce request, to: ::Google::Cloud::Language::V2::AnalyzeEntitiesRequest
302
+
303
+ # Converts hash and nil to an options object
304
+ options = ::Gapic::CallOptions.new(**options.to_h) if options.respond_to? :to_h
305
+
306
+ # Customize the options with defaults
307
+ metadata = @config.rpcs.analyze_entities.metadata.to_h
308
+
309
+ # Set x-goog-api-client and x-goog-user-project headers
310
+ metadata[:"x-goog-api-client"] ||= ::Gapic::Headers.x_goog_api_client \
311
+ lib_name: @config.lib_name, lib_version: @config.lib_version,
312
+ gapic_version: ::Google::Cloud::Language::V2::VERSION
313
+ metadata[:"x-goog-user-project"] = @quota_project_id if @quota_project_id
314
+
315
+ options.apply_defaults timeout: @config.rpcs.analyze_entities.timeout,
316
+ metadata: metadata,
317
+ retry_policy: @config.rpcs.analyze_entities.retry_policy
318
+
319
+ options.apply_defaults timeout: @config.timeout,
320
+ metadata: @config.metadata,
321
+ retry_policy: @config.retry_policy
322
+
323
+ @language_service_stub.call_rpc :analyze_entities, request, options: options do |response, operation|
324
+ yield response, operation if block_given?
325
+ return response
326
+ end
327
+ rescue ::GRPC::BadStatus => e
328
+ raise ::Google::Cloud::Error.from_error(e)
329
+ end
330
+
331
+ ##
332
+ # Classifies a document into categories.
333
+ #
334
+ # @overload classify_text(request, options = nil)
335
+ # Pass arguments to `classify_text` via a request object, either of type
336
+ # {::Google::Cloud::Language::V2::ClassifyTextRequest} or an equivalent Hash.
337
+ #
338
+ # @param request [::Google::Cloud::Language::V2::ClassifyTextRequest, ::Hash]
339
+ # A request object representing the call parameters. Required. To specify no
340
+ # parameters, or to keep all the default parameter values, pass an empty Hash.
341
+ # @param options [::Gapic::CallOptions, ::Hash]
342
+ # Overrides the default settings for this call, e.g, timeout, retries, etc. Optional.
343
+ #
344
+ # @overload classify_text(document: nil)
345
+ # Pass arguments to `classify_text` via keyword arguments. Note that at
346
+ # least one keyword argument is required. To specify no parameters, or to keep all
347
+ # the default parameter values, pass an empty Hash as a request object (see above).
348
+ #
349
+ # @param document [::Google::Cloud::Language::V2::Document, ::Hash]
350
+ # Required. Input document.
351
+ #
352
+ # @yield [response, operation] Access the result along with the RPC operation
353
+ # @yieldparam response [::Google::Cloud::Language::V2::ClassifyTextResponse]
354
+ # @yieldparam operation [::GRPC::ActiveCall::Operation]
355
+ #
356
+ # @return [::Google::Cloud::Language::V2::ClassifyTextResponse]
357
+ #
358
+ # @raise [::Google::Cloud::Error] if the RPC is aborted.
359
+ #
360
+ # @example Basic example
361
+ # require "google/cloud/language/v2"
362
+ #
363
+ # # Create a client object. The client can be reused for multiple calls.
364
+ # client = Google::Cloud::Language::V2::LanguageService::Client.new
365
+ #
366
+ # # Create a request. To set request fields, pass in keyword arguments.
367
+ # request = Google::Cloud::Language::V2::ClassifyTextRequest.new
368
+ #
369
+ # # Call the classify_text method.
370
+ # result = client.classify_text request
371
+ #
372
+ # # The returned object is of type Google::Cloud::Language::V2::ClassifyTextResponse.
373
+ # p result
374
+ #
375
+ def classify_text request, options = nil
376
+ raise ::ArgumentError, "request must be provided" if request.nil?
377
+
378
+ request = ::Gapic::Protobuf.coerce request, to: ::Google::Cloud::Language::V2::ClassifyTextRequest
379
+
380
+ # Converts hash and nil to an options object
381
+ options = ::Gapic::CallOptions.new(**options.to_h) if options.respond_to? :to_h
382
+
383
+ # Customize the options with defaults
384
+ metadata = @config.rpcs.classify_text.metadata.to_h
385
+
386
+ # Set x-goog-api-client and x-goog-user-project headers
387
+ metadata[:"x-goog-api-client"] ||= ::Gapic::Headers.x_goog_api_client \
388
+ lib_name: @config.lib_name, lib_version: @config.lib_version,
389
+ gapic_version: ::Google::Cloud::Language::V2::VERSION
390
+ metadata[:"x-goog-user-project"] = @quota_project_id if @quota_project_id
391
+
392
+ options.apply_defaults timeout: @config.rpcs.classify_text.timeout,
393
+ metadata: metadata,
394
+ retry_policy: @config.rpcs.classify_text.retry_policy
395
+
396
+ options.apply_defaults timeout: @config.timeout,
397
+ metadata: @config.metadata,
398
+ retry_policy: @config.retry_policy
399
+
400
+ @language_service_stub.call_rpc :classify_text, request, options: options do |response, operation|
401
+ yield response, operation if block_given?
402
+ return response
403
+ end
404
+ rescue ::GRPC::BadStatus => e
405
+ raise ::Google::Cloud::Error.from_error(e)
406
+ end
407
+
408
+ ##
409
+ # Moderates a document for harmful and sensitive categories.
410
+ #
411
+ # @overload moderate_text(request, options = nil)
412
+ # Pass arguments to `moderate_text` via a request object, either of type
413
+ # {::Google::Cloud::Language::V2::ModerateTextRequest} or an equivalent Hash.
414
+ #
415
+ # @param request [::Google::Cloud::Language::V2::ModerateTextRequest, ::Hash]
416
+ # A request object representing the call parameters. Required. To specify no
417
+ # parameters, or to keep all the default parameter values, pass an empty Hash.
418
+ # @param options [::Gapic::CallOptions, ::Hash]
419
+ # Overrides the default settings for this call, e.g, timeout, retries, etc. Optional.
420
+ #
421
+ # @overload moderate_text(document: nil)
422
+ # Pass arguments to `moderate_text` via keyword arguments. Note that at
423
+ # least one keyword argument is required. To specify no parameters, or to keep all
424
+ # the default parameter values, pass an empty Hash as a request object (see above).
425
+ #
426
+ # @param document [::Google::Cloud::Language::V2::Document, ::Hash]
427
+ # Required. Input document.
428
+ #
429
+ # @yield [response, operation] Access the result along with the RPC operation
430
+ # @yieldparam response [::Google::Cloud::Language::V2::ModerateTextResponse]
431
+ # @yieldparam operation [::GRPC::ActiveCall::Operation]
432
+ #
433
+ # @return [::Google::Cloud::Language::V2::ModerateTextResponse]
434
+ #
435
+ # @raise [::Google::Cloud::Error] if the RPC is aborted.
436
+ #
437
+ # @example Basic example
438
+ # require "google/cloud/language/v2"
439
+ #
440
+ # # Create a client object. The client can be reused for multiple calls.
441
+ # client = Google::Cloud::Language::V2::LanguageService::Client.new
442
+ #
443
+ # # Create a request. To set request fields, pass in keyword arguments.
444
+ # request = Google::Cloud::Language::V2::ModerateTextRequest.new
445
+ #
446
+ # # Call the moderate_text method.
447
+ # result = client.moderate_text request
448
+ #
449
+ # # The returned object is of type Google::Cloud::Language::V2::ModerateTextResponse.
450
+ # p result
451
+ #
452
+ def moderate_text request, options = nil
453
+ raise ::ArgumentError, "request must be provided" if request.nil?
454
+
455
+ request = ::Gapic::Protobuf.coerce request, to: ::Google::Cloud::Language::V2::ModerateTextRequest
456
+
457
+ # Converts hash and nil to an options object
458
+ options = ::Gapic::CallOptions.new(**options.to_h) if options.respond_to? :to_h
459
+
460
+ # Customize the options with defaults
461
+ metadata = @config.rpcs.moderate_text.metadata.to_h
462
+
463
+ # Set x-goog-api-client and x-goog-user-project headers
464
+ metadata[:"x-goog-api-client"] ||= ::Gapic::Headers.x_goog_api_client \
465
+ lib_name: @config.lib_name, lib_version: @config.lib_version,
466
+ gapic_version: ::Google::Cloud::Language::V2::VERSION
467
+ metadata[:"x-goog-user-project"] = @quota_project_id if @quota_project_id
468
+
469
+ options.apply_defaults timeout: @config.rpcs.moderate_text.timeout,
470
+ metadata: metadata,
471
+ retry_policy: @config.rpcs.moderate_text.retry_policy
472
+
473
+ options.apply_defaults timeout: @config.timeout,
474
+ metadata: @config.metadata,
475
+ retry_policy: @config.retry_policy
476
+
477
+ @language_service_stub.call_rpc :moderate_text, request, options: options do |response, operation|
478
+ yield response, operation if block_given?
479
+ return response
480
+ end
481
+ rescue ::GRPC::BadStatus => e
482
+ raise ::Google::Cloud::Error.from_error(e)
483
+ end
484
+
485
+ ##
486
+ # A convenience method that provides all features in one call.
487
+ #
488
+ # @overload annotate_text(request, options = nil)
489
+ # Pass arguments to `annotate_text` via a request object, either of type
490
+ # {::Google::Cloud::Language::V2::AnnotateTextRequest} or an equivalent Hash.
491
+ #
492
+ # @param request [::Google::Cloud::Language::V2::AnnotateTextRequest, ::Hash]
493
+ # A request object representing the call parameters. Required. To specify no
494
+ # parameters, or to keep all the default parameter values, pass an empty Hash.
495
+ # @param options [::Gapic::CallOptions, ::Hash]
496
+ # Overrides the default settings for this call, e.g, timeout, retries, etc. Optional.
497
+ #
498
+ # @overload annotate_text(document: nil, features: nil, encoding_type: nil)
499
+ # Pass arguments to `annotate_text` via keyword arguments. Note that at
500
+ # least one keyword argument is required. To specify no parameters, or to keep all
501
+ # the default parameter values, pass an empty Hash as a request object (see above).
502
+ #
503
+ # @param document [::Google::Cloud::Language::V2::Document, ::Hash]
504
+ # Required. Input document.
505
+ # @param features [::Google::Cloud::Language::V2::AnnotateTextRequest::Features, ::Hash]
506
+ # Required. The enabled features.
507
+ # @param encoding_type [::Google::Cloud::Language::V2::EncodingType]
508
+ # The encoding type used by the API to calculate offsets.
509
+ #
510
+ # @yield [response, operation] Access the result along with the RPC operation
511
+ # @yieldparam response [::Google::Cloud::Language::V2::AnnotateTextResponse]
512
+ # @yieldparam operation [::GRPC::ActiveCall::Operation]
513
+ #
514
+ # @return [::Google::Cloud::Language::V2::AnnotateTextResponse]
515
+ #
516
+ # @raise [::Google::Cloud::Error] if the RPC is aborted.
517
+ #
518
+ # @example Basic example
519
+ # require "google/cloud/language/v2"
520
+ #
521
+ # # Create a client object. The client can be reused for multiple calls.
522
+ # client = Google::Cloud::Language::V2::LanguageService::Client.new
523
+ #
524
+ # # Create a request. To set request fields, pass in keyword arguments.
525
+ # request = Google::Cloud::Language::V2::AnnotateTextRequest.new
526
+ #
527
+ # # Call the annotate_text method.
528
+ # result = client.annotate_text request
529
+ #
530
+ # # The returned object is of type Google::Cloud::Language::V2::AnnotateTextResponse.
531
+ # p result
532
+ #
533
+ def annotate_text request, options = nil
534
+ raise ::ArgumentError, "request must be provided" if request.nil?
535
+
536
+ request = ::Gapic::Protobuf.coerce request, to: ::Google::Cloud::Language::V2::AnnotateTextRequest
537
+
538
+ # Converts hash and nil to an options object
539
+ options = ::Gapic::CallOptions.new(**options.to_h) if options.respond_to? :to_h
540
+
541
+ # Customize the options with defaults
542
+ metadata = @config.rpcs.annotate_text.metadata.to_h
543
+
544
+ # Set x-goog-api-client and x-goog-user-project headers
545
+ metadata[:"x-goog-api-client"] ||= ::Gapic::Headers.x_goog_api_client \
546
+ lib_name: @config.lib_name, lib_version: @config.lib_version,
547
+ gapic_version: ::Google::Cloud::Language::V2::VERSION
548
+ metadata[:"x-goog-user-project"] = @quota_project_id if @quota_project_id
549
+
550
+ options.apply_defaults timeout: @config.rpcs.annotate_text.timeout,
551
+ metadata: metadata,
552
+ retry_policy: @config.rpcs.annotate_text.retry_policy
553
+
554
+ options.apply_defaults timeout: @config.timeout,
555
+ metadata: @config.metadata,
556
+ retry_policy: @config.retry_policy
557
+
558
+ @language_service_stub.call_rpc :annotate_text, request, options: options do |response, operation|
559
+ yield response, operation if block_given?
560
+ return response
561
+ end
562
+ rescue ::GRPC::BadStatus => e
563
+ raise ::Google::Cloud::Error.from_error(e)
564
+ end
565
+
566
+ ##
567
+ # Configuration class for the LanguageService API.
568
+ #
569
+ # This class represents the configuration for LanguageService,
570
+ # providing control over timeouts, retry behavior, logging, transport
571
+ # parameters, and other low-level controls. Certain parameters can also be
572
+ # applied individually to specific RPCs. See
573
+ # {::Google::Cloud::Language::V2::LanguageService::Client::Configuration::Rpcs}
574
+ # for a list of RPCs that can be configured independently.
575
+ #
576
+ # Configuration can be applied globally to all clients, or to a single client
577
+ # on construction.
578
+ #
579
+ # @example
580
+ #
581
+ # # Modify the global config, setting the timeout for
582
+ # # analyze_sentiment to 20 seconds,
583
+ # # and all remaining timeouts to 10 seconds.
584
+ # ::Google::Cloud::Language::V2::LanguageService::Client.configure do |config|
585
+ # config.timeout = 10.0
586
+ # config.rpcs.analyze_sentiment.timeout = 20.0
587
+ # end
588
+ #
589
+ # # Apply the above configuration only to a new client.
590
+ # client = ::Google::Cloud::Language::V2::LanguageService::Client.new do |config|
591
+ # config.timeout = 10.0
592
+ # config.rpcs.analyze_sentiment.timeout = 20.0
593
+ # end
594
+ #
595
+ # @!attribute [rw] endpoint
596
+ # The hostname or hostname:port of the service endpoint.
597
+ # Defaults to `"language.googleapis.com"`.
598
+ # @return [::String]
599
+ # @!attribute [rw] credentials
600
+ # Credentials to send with calls. You may provide any of the following types:
601
+ # * (`String`) The path to a service account key file in JSON format
602
+ # * (`Hash`) A service account key as a Hash
603
+ # * (`Google::Auth::Credentials`) A googleauth credentials object
604
+ # (see the [googleauth docs](https://rubydoc.info/gems/googleauth/Google/Auth/Credentials))
605
+ # * (`Signet::OAuth2::Client`) A signet oauth2 client object
606
+ # (see the [signet docs](https://rubydoc.info/gems/signet/Signet/OAuth2/Client))
607
+ # * (`GRPC::Core::Channel`) a gRPC channel with included credentials
608
+ # * (`GRPC::Core::ChannelCredentials`) a gRPC credentails object
609
+ # * (`nil`) indicating no credentials
610
+ # @return [::Object]
611
+ # @!attribute [rw] scope
612
+ # The OAuth scopes
613
+ # @return [::Array<::String>]
614
+ # @!attribute [rw] lib_name
615
+ # The library name as recorded in instrumentation and logging
616
+ # @return [::String]
617
+ # @!attribute [rw] lib_version
618
+ # The library version as recorded in instrumentation and logging
619
+ # @return [::String]
620
+ # @!attribute [rw] channel_args
621
+ # Extra parameters passed to the gRPC channel. Note: this is ignored if a
622
+ # `GRPC::Core::Channel` object is provided as the credential.
623
+ # @return [::Hash]
624
+ # @!attribute [rw] interceptors
625
+ # An array of interceptors that are run before calls are executed.
626
+ # @return [::Array<::GRPC::ClientInterceptor>]
627
+ # @!attribute [rw] timeout
628
+ # The call timeout in seconds.
629
+ # @return [::Numeric]
630
+ # @!attribute [rw] metadata
631
+ # Additional gRPC headers to be sent with the call.
632
+ # @return [::Hash{::Symbol=>::String}]
633
+ # @!attribute [rw] retry_policy
634
+ # The retry policy. The value is a hash with the following keys:
635
+ # * `:initial_delay` (*type:* `Numeric`) - The initial delay in seconds.
636
+ # * `:max_delay` (*type:* `Numeric`) - The max delay in seconds.
637
+ # * `:multiplier` (*type:* `Numeric`) - The incremental backoff multiplier.
638
+ # * `:retry_codes` (*type:* `Array<String>`) - The error codes that should
639
+ # trigger a retry.
640
+ # @return [::Hash]
641
+ # @!attribute [rw] quota_project
642
+ # A separate project against which to charge quota.
643
+ # @return [::String]
644
+ #
645
+ class Configuration
646
+ extend ::Gapic::Config
647
+
648
+ DEFAULT_ENDPOINT = "language.googleapis.com"
649
+
650
+ config_attr :endpoint, DEFAULT_ENDPOINT, ::String
651
+ config_attr :credentials, nil do |value|
652
+ allowed = [::String, ::Hash, ::Proc, ::Symbol, ::Google::Auth::Credentials, ::Signet::OAuth2::Client, nil]
653
+ allowed += [::GRPC::Core::Channel, ::GRPC::Core::ChannelCredentials] if defined? ::GRPC
654
+ allowed.any? { |klass| klass === value }
655
+ end
656
+ config_attr :scope, nil, ::String, ::Array, nil
657
+ config_attr :lib_name, nil, ::String, nil
658
+ config_attr :lib_version, nil, ::String, nil
659
+ config_attr(:channel_args, { "grpc.service_config_disable_resolution" => 1 }, ::Hash, nil)
660
+ config_attr :interceptors, nil, ::Array, nil
661
+ config_attr :timeout, nil, ::Numeric, nil
662
+ config_attr :metadata, nil, ::Hash, nil
663
+ config_attr :retry_policy, nil, ::Hash, ::Proc, nil
664
+ config_attr :quota_project, nil, ::String, nil
665
+
666
+ # @private
667
+ def initialize parent_config = nil
668
+ @parent_config = parent_config unless parent_config.nil?
669
+
670
+ yield self if block_given?
671
+ end
672
+
673
+ ##
674
+ # Configurations for individual RPCs
675
+ # @return [Rpcs]
676
+ #
677
+ def rpcs
678
+ @rpcs ||= begin
679
+ parent_rpcs = nil
680
+ parent_rpcs = @parent_config.rpcs if defined?(@parent_config) && @parent_config.respond_to?(:rpcs)
681
+ Rpcs.new parent_rpcs
682
+ end
683
+ end
684
+
685
+ ##
686
+ # Configuration RPC class for the LanguageService API.
687
+ #
688
+ # Includes fields providing the configuration for each RPC in this service.
689
+ # Each configuration object is of type `Gapic::Config::Method` and includes
690
+ # the following configuration fields:
691
+ #
692
+ # * `timeout` (*type:* `Numeric`) - The call timeout in seconds
693
+ # * `metadata` (*type:* `Hash{Symbol=>String}`) - Additional gRPC headers
694
+ # * `retry_policy (*type:* `Hash`) - The retry policy. The policy fields
695
+ # include the following keys:
696
+ # * `:initial_delay` (*type:* `Numeric`) - The initial delay in seconds.
697
+ # * `:max_delay` (*type:* `Numeric`) - The max delay in seconds.
698
+ # * `:multiplier` (*type:* `Numeric`) - The incremental backoff multiplier.
699
+ # * `:retry_codes` (*type:* `Array<String>`) - The error codes that should
700
+ # trigger a retry.
701
+ #
702
+ class Rpcs
703
+ ##
704
+ # RPC-specific configuration for `analyze_sentiment`
705
+ # @return [::Gapic::Config::Method]
706
+ #
707
+ attr_reader :analyze_sentiment
708
+ ##
709
+ # RPC-specific configuration for `analyze_entities`
710
+ # @return [::Gapic::Config::Method]
711
+ #
712
+ attr_reader :analyze_entities
713
+ ##
714
+ # RPC-specific configuration for `classify_text`
715
+ # @return [::Gapic::Config::Method]
716
+ #
717
+ attr_reader :classify_text
718
+ ##
719
+ # RPC-specific configuration for `moderate_text`
720
+ # @return [::Gapic::Config::Method]
721
+ #
722
+ attr_reader :moderate_text
723
+ ##
724
+ # RPC-specific configuration for `annotate_text`
725
+ # @return [::Gapic::Config::Method]
726
+ #
727
+ attr_reader :annotate_text
728
+
729
+ # @private
730
+ def initialize parent_rpcs = nil
731
+ analyze_sentiment_config = parent_rpcs.analyze_sentiment if parent_rpcs.respond_to? :analyze_sentiment
732
+ @analyze_sentiment = ::Gapic::Config::Method.new analyze_sentiment_config
733
+ analyze_entities_config = parent_rpcs.analyze_entities if parent_rpcs.respond_to? :analyze_entities
734
+ @analyze_entities = ::Gapic::Config::Method.new analyze_entities_config
735
+ classify_text_config = parent_rpcs.classify_text if parent_rpcs.respond_to? :classify_text
736
+ @classify_text = ::Gapic::Config::Method.new classify_text_config
737
+ moderate_text_config = parent_rpcs.moderate_text if parent_rpcs.respond_to? :moderate_text
738
+ @moderate_text = ::Gapic::Config::Method.new moderate_text_config
739
+ annotate_text_config = parent_rpcs.annotate_text if parent_rpcs.respond_to? :annotate_text
740
+ @annotate_text = ::Gapic::Config::Method.new annotate_text_config
741
+
742
+ yield self if block_given?
743
+ end
744
+ end
745
+ end
746
+ end
747
+ end
748
+ end
749
+ end
750
+ end
751
+ end