lib/google/cloud/dialogflow/v2/sessions_client.rb in google-cloud-dialogflow-0.1.0 vs lib/google/cloud/dialogflow/v2/sessions_client.rb in google-cloud-dialogflow-0.2.0

- old
+ new

@@ -23,11 +23,11 @@ require "pathname" require "google/gax" require "google/cloud/dialogflow/v2/session_pb" -require "google/cloud/dialogflow/credentials" +require "google/cloud/dialogflow/v2/credentials" module Google module Cloud module Dialogflow module V2 @@ -45,10 +45,13 @@ SERVICE_ADDRESS = "dialogflow.googleapis.com".freeze # The default port of the service. DEFAULT_SERVICE_PORT = 443 + # The default set of gRPC interceptors. + GRPC_INTERCEPTORS = [] + DEFAULT_TIMEOUT = 30 # The scopes needed to make gRPC calls to all of the methods defined in # this service. ALL_SCOPES = [ @@ -95,27 +98,34 @@ # 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/dialogflow/v2/session_services_pb" - credentials ||= Google::Cloud::Dialogflow::Credentials.default + credentials ||= Google::Cloud::Dialogflow::V2::Credentials.default if credentials.is_a?(String) || credentials.is_a?(Hash) - updater_proc = Google::Cloud::Dialogflow::Credentials.new(credentials).updater_proc + updater_proc = Google::Cloud::Dialogflow::V2::Credentials.new(credentials).updater_proc end if credentials.is_a?(GRPC::Core::Channel) channel = credentials end if credentials.is_a?(GRPC::Core::ChannelCredentials) @@ -135,10 +145,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( "sessions_client_config.json" ) defaults = client_config_file.open do |f| Google::Gax.construct_settings( @@ -146,34 +157,38 @@ 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 port = self.class::DEFAULT_SERVICE_PORT + interceptors = self.class::GRPC_INTERCEPTORS @sessions_stub = Google::Gax::Grpc.create_stub( service_path, port, chan_creds: chan_creds, channel: channel, updater_proc: updater_proc, scopes: scopes, + interceptors: interceptors, &Google::Cloud::Dialogflow::V2::Sessions::Stub.method(:new) ) @detect_intent = Google::Gax.create_api_call( @sessions_stub.method(:detect_intent), - defaults["detect_intent"] + defaults["detect_intent"], + exception_transformer: exception_transformer ) @streaming_detect_intent = Google::Gax.create_api_call( @sessions_stub.method(:streaming_detect_intent), - defaults["streaming_detect_intent"] + defaults["streaming_detect_intent"], + exception_transformer: exception_transformer ) end # Service calls @@ -208,16 +223,19 @@ # should be populated iff +query_input+ is set to an input audio config. # A single request can contain up to 1 minute of speech audio data. # @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::Dialogflow::V2::DetectIntentResponse] + # @yieldparam operation [GRPC::ActiveCall::Operation] # @return [Google::Cloud::Dialogflow::V2::DetectIntentResponse] # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example - # require "google/cloud/dialogflow/v2" + # require "google/cloud/dialogflow" # - # sessions_client = Google::Cloud::Dialogflow::V2::Sessions.new + # sessions_client = Google::Cloud::Dialogflow::Sessions.new(version: :v2) # formatted_session = Google::Cloud::Dialogflow::V2::SessionsClient.session_path("[PROJECT]", "[SESSION]") # # # TODO: Initialize +query_input+: # query_input = {} # response = sessions_client.detect_intent(formatted_session, query_input) @@ -225,19 +243,20 @@ def detect_intent \ session, query_input, query_params: nil, input_audio: nil, - options: nil + options: nil, + &block req = { session: session, query_input: query_input, query_params: query_params, input_audio: input_audio }.delete_if { |_, v| v.nil? } req = Google::Gax::to_proto(req, Google::Cloud::Dialogflow::V2::DetectIntentRequest) - @detect_intent.call(req, options) + @detect_intent.call(req, options, &block) end # Processes a natural language query in audio format in a streaming fashion # and returns structured, actionable data as a result. This method is only # available via the gRPC API (not REST). @@ -256,12 +275,12 @@ # EXPERIMENTAL: # Streaming requests are still undergoing review. # This method interface might change in the future. # # @example - # require "google/cloud/dialogflow/v2" + # require "google/cloud/dialogflow" # - # sessions_client = Google::Cloud::Dialogflow::V2::Sessions.new + # sessions_client = Google::Cloud::Dialogflow::Sessions.new(version: :v2) # # # TODO: Initialize +session+: # session = '' # # # TODO: Initialize +query_input+: