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

- old
+ new

@@ -25,11 +25,11 @@ require "google/gax" require "google/gax/operation" require "google/longrunning/operations_client" require "google/cloud/dialogflow/v2/intent_pb" -require "google/cloud/dialogflow/credentials" +require "google/cloud/dialogflow/v2/credentials" module Google module Cloud module Dialogflow module V2 @@ -74,10 +74,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 PAGE_DESCRIPTORS = { "list_intents" => Google::Gax::PageDescriptor.new( "page_token", @@ -91,12 +94,14 @@ # this service. ALL_SCOPES = [ "https://www.googleapis.com/auth/cloud-platform" ].freeze + # @private class OperationsClient < Google::Longrunning::OperationsClient self::SERVICE_ADDRESS = IntentsClient::SERVICE_ADDRESS + self::GRPC_INTERCEPTORS = IntentsClient::GRPC_INTERCEPTORS end PROJECT_AGENT_PATH_TEMPLATE = Google::Gax::PathTemplate.new( "projects/{project}/agent" ) @@ -168,24 +173,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/dialogflow/v2/intent_services_pb" - credentials ||= Google::Cloud::Dialogflow::Credentials.default + credentials ||= Google::Cloud::Dialogflow::V2::Credentials.default @operations_client = OperationsClient.new( credentials: credentials, scopes: scopes, client_config: client_config, @@ -193,11 +205,11 @@ lib_name: lib_name, lib_version: lib_version, ) 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) @@ -217,10 +229,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( "intents_client_config.json" ) defaults = client_config_file.open do |f| Google::Gax.construct_settings( @@ -229,54 +242,63 @@ client_config, Google::Gax::Grpc::STATUS_CODE_NAMES, timeout, page_descriptors: PAGE_DESCRIPTORS, 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 @intents_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::Intents::Stub.method(:new) ) @list_intents = Google::Gax.create_api_call( @intents_stub.method(:list_intents), - defaults["list_intents"] + defaults["list_intents"], + exception_transformer: exception_transformer ) @get_intent = Google::Gax.create_api_call( @intents_stub.method(:get_intent), - defaults["get_intent"] + defaults["get_intent"], + exception_transformer: exception_transformer ) @create_intent = Google::Gax.create_api_call( @intents_stub.method(:create_intent), - defaults["create_intent"] + defaults["create_intent"], + exception_transformer: exception_transformer ) @update_intent = Google::Gax.create_api_call( @intents_stub.method(:update_intent), - defaults["update_intent"] + defaults["update_intent"], + exception_transformer: exception_transformer ) @delete_intent = Google::Gax.create_api_call( @intents_stub.method(:delete_intent), - defaults["delete_intent"] + defaults["delete_intent"], + exception_transformer: exception_transformer ) @batch_update_intents = Google::Gax.create_api_call( @intents_stub.method(:batch_update_intents), - defaults["batch_update_intents"] + defaults["batch_update_intents"], + exception_transformer: exception_transformer ) @batch_delete_intents = Google::Gax.create_api_call( @intents_stub.method(:batch_delete_intents), - defaults["batch_delete_intents"] + defaults["batch_delete_intents"], + exception_transformer: exception_transformer ) end # Service calls @@ -300,20 +322,23 @@ # performed per-page, this determines the maximum number of # resources in a page. # @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::Gax::PagedEnumerable<Google::Cloud::Dialogflow::V2::Intent>] + # @yieldparam operation [GRPC::ActiveCall::Operation] # @return [Google::Gax::PagedEnumerable<Google::Cloud::Dialogflow::V2::Intent>] # An enumerable of Google::Cloud::Dialogflow::V2::Intent instances. # See Google::Gax::PagedEnumerable documentation for other # operations such as per-page iteration or access to the response # object. # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example - # require "google/cloud/dialogflow/v2" + # require "google/cloud/dialogflow" # - # intents_client = Google::Cloud::Dialogflow::V2::Intents.new + # intents_client = Google::Cloud::Dialogflow::Intents.new(version: :v2) # formatted_parent = Google::Cloud::Dialogflow::V2::IntentsClient.project_agent_path("[PROJECT]") # # # Iterate over all results. # intents_client.list_intents(formatted_parent).each do |element| # # Process element. @@ -330,19 +355,20 @@ def list_intents \ parent, language_code: nil, intent_view: nil, page_size: nil, - options: nil + options: nil, + &block req = { parent: parent, language_code: language_code, intent_view: intent_view, page_size: page_size }.delete_if { |_, v| v.nil? } req = Google::Gax::to_proto(req, Google::Cloud::Dialogflow::V2::ListIntentsRequest) - @list_intents.call(req, options) + @list_intents.call(req, options, &block) end # Retrieves the specified intent. # # @param name [String] @@ -357,31 +383,35 @@ # @param intent_view [Google::Cloud::Dialogflow::V2::IntentView] # Optional. The resource view to apply to the returned intent. # @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::Intent] + # @yieldparam operation [GRPC::ActiveCall::Operation] # @return [Google::Cloud::Dialogflow::V2::Intent] # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example - # require "google/cloud/dialogflow/v2" + # require "google/cloud/dialogflow" # - # intents_client = Google::Cloud::Dialogflow::V2::Intents.new + # intents_client = Google::Cloud::Dialogflow::Intents.new(version: :v2) # formatted_name = Google::Cloud::Dialogflow::V2::IntentsClient.intent_path("[PROJECT]", "[INTENT]") # response = intents_client.get_intent(formatted_name) def get_intent \ name, language_code: nil, intent_view: nil, - options: nil + options: nil, + &block req = { name: name, language_code: language_code, intent_view: intent_view }.delete_if { |_, v| v.nil? } req = Google::Gax::to_proto(req, Google::Cloud::Dialogflow::V2::GetIntentRequest) - @get_intent.call(req, options) + @get_intent.call(req, options, &block) end # Creates an intent in the specified agent. # # @param parent [String] @@ -400,16 +430,19 @@ # @param intent_view [Google::Cloud::Dialogflow::V2::IntentView] # Optional. The resource view to apply to the returned intent. # @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::Intent] + # @yieldparam operation [GRPC::ActiveCall::Operation] # @return [Google::Cloud::Dialogflow::V2::Intent] # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example - # require "google/cloud/dialogflow/v2" + # require "google/cloud/dialogflow" # - # intents_client = Google::Cloud::Dialogflow::V2::Intents.new + # intents_client = Google::Cloud::Dialogflow::Intents.new(version: :v2) # formatted_parent = Google::Cloud::Dialogflow::V2::IntentsClient.project_agent_path("[PROJECT]") # # # TODO: Initialize +intent+: # intent = {} # response = intents_client.create_intent(formatted_parent, intent) @@ -417,19 +450,20 @@ def create_intent \ parent, intent, language_code: nil, intent_view: nil, - options: nil + options: nil, + &block req = { parent: parent, intent: intent, language_code: language_code, intent_view: intent_view }.delete_if { |_, v| v.nil? } req = Google::Gax::to_proto(req, Google::Cloud::Dialogflow::V2::CreateIntentRequest) - @create_intent.call(req, options) + @create_intent.call(req, options, &block) end # Updates the specified intent. # # @param intent [Google::Cloud::Dialogflow::V2::Intent | Hash] @@ -450,16 +484,19 @@ # @param intent_view [Google::Cloud::Dialogflow::V2::IntentView] # Optional. The resource view to apply to the returned intent. # @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::Intent] + # @yieldparam operation [GRPC::ActiveCall::Operation] # @return [Google::Cloud::Dialogflow::V2::Intent] # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example - # require "google/cloud/dialogflow/v2" + # require "google/cloud/dialogflow" # - # intents_client = Google::Cloud::Dialogflow::V2::Intents.new + # intents_client = Google::Cloud::Dialogflow::Intents.new(version: :v2) # # # TODO: Initialize +intent+: # intent = {} # # # TODO: Initialize +language_code+: @@ -469,45 +506,50 @@ def update_intent \ intent, language_code, update_mask: nil, intent_view: nil, - options: nil + options: nil, + &block req = { intent: intent, language_code: language_code, update_mask: update_mask, intent_view: intent_view }.delete_if { |_, v| v.nil? } req = Google::Gax::to_proto(req, Google::Cloud::Dialogflow::V2::UpdateIntentRequest) - @update_intent.call(req, options) + @update_intent.call(req, options, &block) end # Deletes the specified intent. # # @param name [String] # Required. The name of the intent to delete. # Format: +projects/<Project ID>/agent/intents/<Intent ID>+. # @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 [] + # @yieldparam operation [GRPC::ActiveCall::Operation] # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example - # require "google/cloud/dialogflow/v2" + # require "google/cloud/dialogflow" # - # intents_client = Google::Cloud::Dialogflow::V2::Intents.new + # intents_client = Google::Cloud::Dialogflow::Intents.new(version: :v2) # formatted_name = Google::Cloud::Dialogflow::V2::IntentsClient.intent_path("[PROJECT]", "[INTENT]") # intents_client.delete_intent(formatted_name) def delete_intent \ name, - options: nil + options: nil, + &block req = { name: name }.delete_if { |_, v| v.nil? } req = Google::Gax::to_proto(req, Google::Cloud::Dialogflow::V2::DeleteIntentRequest) - @delete_intent.call(req, options) + @delete_intent.call(req, options, &block) nil end # Updates/Creates multiple intents in the specified agent. # @@ -540,13 +582,13 @@ # Overrides the default settings for this call, e.g, timeout, # retries, etc. # @return [Google::Gax::Operation] # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example - # require "google/cloud/dialogflow/v2" + # require "google/cloud/dialogflow" # - # intents_client = Google::Cloud::Dialogflow::V2::Intents.new + # intents_client = Google::Cloud::Dialogflow::Intents.new(version: :v2) # formatted_parent = Google::Cloud::Dialogflow::V2::IntentsClient.project_agent_path("[PROJECT]") # # # TODO: Initialize +language_code+: # language_code = '' # @@ -621,12 +663,12 @@ # Overrides the default settings for this call, e.g, timeout, # retries, etc. # @return [Google::Gax::Operation] # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example - # require "google/cloud/dialogflow/v2" + # require "google/cloud/dialogflow" # - # intents_client = Google::Cloud::Dialogflow::V2::Intents.new + # intents_client = Google::Cloud::Dialogflow::Intents.new(version: :v2) # formatted_parent = Google::Cloud::Dialogflow::V2::IntentsClient.project_agent_path("[PROJECT]") # # # TODO: Initialize +intents+: # intents = [] #