lib/google/cloud/speech/v1/speech_client.rb in google-cloud-speech-0.29.0 vs lib/google/cloud/speech/v1/speech_client.rb in google-cloud-speech-0.30.0
- old
+ new
@@ -1,6 +1,6 @@
-# Copyright 2017 Google LLC
+# 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
#
@@ -16,23 +16,20 @@
# This file was generated from the file
# https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1/cloud_speech.proto,
# and updates to that file get reflected here through a refresh process.
# For the short term, the refresh process will only be runnable by Google
# engineers.
-#
-# The only allowed edits are to method and file documentation. A 3-way
-# merge preserves those additions if the generated source changes.
require "json"
require "pathname"
require "google/gax"
require "google/gax/operation"
require "google/longrunning/operations_client"
require "google/cloud/speech/v1/cloud_speech_pb"
-require "google/cloud/speech/credentials"
+require "google/cloud/speech/v1/credentials"
module Google
module Cloud
module Speech
module V1
@@ -56,11 +53,11 @@
ALL_SCOPES = [
"https://www.googleapis.com/auth/cloud-platform"
].freeze
class OperationsClient < Google::Longrunning::OperationsClient
- SERVICE_ADDRESS = SERVICE_ADDRESS
+ self::SERVICE_ADDRESS = SpeechClient::SERVICE_ADDRESS
end
# @param credentials [Google::Auth::Credentials, String, Hash, GRPC::Core::Channel, GRPC::Core::ChannelCredentials, Proc]
# Provides the means for authenticating requests made by the client. This parameter can
# be many types.
@@ -83,24 +80,31 @@
# Google::Gax#construct_settings for the structure of
# this data. Falls back to the default config if not specified
# or the specified config is missing data points.
# @param timeout [Numeric]
# The default timeout, in seconds, for calls made through this client.
+ # @param metadata [Hash]
+ # Default metadata to be sent with each request. This can be overridden on a per call basis.
+ # @param exception_transformer [Proc]
+ # An optional proc that intercepts any exceptions raised during an API call to inject
+ # custom error handling.
def initialize \
credentials: nil,
scopes: ALL_SCOPES,
client_config: {},
timeout: DEFAULT_TIMEOUT,
+ metadata: nil,
+ exception_transformer: nil,
lib_name: nil,
lib_version: ""
# 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/cloud/speech/v1/cloud_speech_services_pb"
- credentials ||= Google::Cloud::Speech::Credentials.default
+ credentials ||= Google::Cloud::Speech::V1::Credentials.default
@operations_client = OperationsClient.new(
credentials: credentials,
scopes: scopes,
client_config: client_config,
@@ -108,11 +112,11 @@
lib_name: lib_name,
lib_version: lib_version,
)
if credentials.is_a?(String) || credentials.is_a?(Hash)
- updater_proc = Google::Cloud::Speech::Credentials.new(credentials).updater_proc
+ updater_proc = Google::Cloud::Speech::V1::Credentials.new(credentials).updater_proc
end
if credentials.is_a?(GRPC::Core::Channel)
channel = credentials
end
if credentials.is_a?(GRPC::Core::ChannelCredentials)
@@ -132,10 +136,11 @@
google_api_client << " gapic/#{package_version} gax/#{Google::Gax::VERSION}"
google_api_client << " grpc/#{GRPC::VERSION}"
google_api_client.freeze
headers = { :"x-goog-api-client" => google_api_client }
+ headers.merge!(metadata) unless metadata.nil?
client_config_file = Pathname.new(__dir__).join(
"speech_client_config.json"
)
defaults = client_config_file.open do |f|
Google::Gax.construct_settings(
@@ -143,11 +148,11 @@
JSON.parse(f.read),
client_config,
Google::Gax::Grpc::STATUS_CODE_NAMES,
timeout,
errors: Google::Gax::Grpc::API_ERRORS,
- kwargs: headers
+ metadata: headers
)
end
# Allow overriding the service path/port in subclasses.
service_path = self.class::SERVICE_ADDRESS
@@ -162,19 +167,22 @@
&Google::Cloud::Speech::V1::Speech::Stub.method(:new)
)
@recognize = Google::Gax.create_api_call(
@speech_stub.method(:recognize),
- defaults["recognize"]
+ defaults["recognize"],
+ exception_transformer: exception_transformer
)
@long_running_recognize = Google::Gax.create_api_call(
@speech_stub.method(:long_running_recognize),
- defaults["long_running_recognize"]
+ defaults["long_running_recognize"],
+ exception_transformer: exception_transformer
)
@streaming_recognize = Google::Gax.create_api_call(
@speech_stub.method(:streaming_recognize),
- defaults["streaming_recognize"]
+ defaults["streaming_recognize"],
+ exception_transformer: exception_transformer
)
end
# Service calls
@@ -191,10 +199,13 @@
# A hash of the same form as `Google::Cloud::Speech::V1::RecognitionAudio`
# can also be provided.
# @param options [Google::Gax::CallOptions]
# Overrides the default settings for this call, e.g, timeout,
# retries, etc.
+ # @yield [result, operation] Access the result along with the RPC operation
+ # @yieldparam result [Google::Cloud::Speech::V1::RecognizeResponse]
+ # @yieldparam operation [GRPC::ActiveCall::Operation]
# @return [Google::Cloud::Speech::V1::RecognizeResponse]
# @raise [Google::Gax::GaxError] if the RPC is aborted.
# @example
# require "google/cloud/speech/v1"
#
@@ -212,17 +223,18 @@
# response = speech_client.recognize(config, audio)
def recognize \
config,
audio,
- options: nil
+ options: nil,
+ &block
req = {
config: config,
audio: audio
}.delete_if { |_, v| v.nil? }
req = Google::Gax::to_proto(req, Google::Cloud::Speech::V1::RecognizeRequest)
- @recognize.call(req, options)
+ @recognize.call(req, options, &block)
end
# Performs asynchronous speech recognition: receive results via the
# google.longrunning.Operations interface. Returns either an
# +Operation.error+ or an +Operation.response+ which contains
@@ -300,44 +312,9 @@
Google::Cloud::Speech::V1::LongRunningRecognizeMetadata,
call_options: options
)
operation.on_done { |operation| yield(operation) } if block_given?
operation
- end
-
- # Performs bidirectional streaming speech recognition: receive results while
- # sending audio. This method is only available via the gRPC API (not REST).
- #
- # @param reqs [Enumerable<Google::Cloud::Speech::V1::StreamingRecognizeRequest>]
- # The input requests.
- # @param options [Google::Gax::CallOptions]
- # Overrides the default settings for this call, e.g, timeout,
- # retries, etc.
- # @return [Enumerable<Google::Cloud::Speech::V1::StreamingRecognizeResponse>]
- # An enumerable of Google::Cloud::Speech::V1::StreamingRecognizeResponse instances.
- #
- # @raise [Google::Gax::GaxError] if the RPC is aborted.
- #
- # @note
- # EXPERIMENTAL:
- # Streaming requests are still undergoing review.
- # This method interface might change in the future.
- #
- # @example
- # require "google/cloud/speech/v1"
- #
- # speech_client = Google::Cloud::Speech::V1.new
- # request = {}
- # requests = [request]
- # speech_client.streaming_recognize(requests).each do |element|
- # # Process element.
- # end
-
- def streaming_recognize reqs, options: nil
- request_protos = reqs.lazy.map do |req|
- Google::Gax::to_proto(req, Google::Cloud::Speech::V1::StreamingRecognizeRequest)
- end
- @streaming_recognize.call(request_protos, options)
end
end
end
end
end