lib/google/cloud/translate.rb in google-cloud-translate-2.3.0 vs lib/google/cloud/translate.rb in google-cloud-translate-3.0.0

- old
+ new

@@ -1,6 +1,8 @@ -# Copyright 2019 Google LLC +# frozen_string_literal: true + +# Copyright 2020 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 # @@ -10,205 +12,106 @@ # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# Auto-generated by gapic-generator-ruby. DO NOT EDIT! -require "google-cloud-translate" +# Require this file early so that the version constant gets defined before +# requiring "google/cloud". This is because google-cloud-core will load the +# entrypoint (gem name) file, which in turn re-requires this file (hence +# causing a require cycle) unless the version constant is already defined. +require "google/cloud/translate/version" + +require "googleauth" +gem "google-cloud-core" +require "google/cloud" unless defined? ::Google::Cloud.new require "google/cloud/config" -require "google/gax" -require "pathname" +# Set the default configuration +::Google::Cloud.configure.add_config! :translate do |config| + config.add_field! :endpoint, "translate.googleapis.com", match: ::String + config.add_field! :credentials, nil, match: [::String, ::Hash, ::Google::Auth::Credentials] + config.add_field! :scope, nil, match: [::Array, ::String] + config.add_field! :lib_name, nil, match: ::String + config.add_field! :lib_version, nil, match: ::String + config.add_field! :interceptors, nil, match: ::Array + config.add_field! :timeout, nil, match: ::Numeric + config.add_field! :metadata, nil, match: ::Hash + config.add_field! :retry_policy, nil, match: [::Hash, ::Proc] + config.add_field! :quota_project, nil, match: ::String + config.add_field! :key, nil, match: ::String + config.add_field! :retries, nil, match: ::Integer +end + module Google module Cloud - ## - # # Google Cloud Translation API - # - # [Google Cloud Translation API](https://cloud.google.com/translation/) - # provides a simple, programmatic interface for translating an arbitrary - # string into any supported language. It is highly responsive, so websites - # and applications can integrate with Translation API for fast, dynamic - # translation of source text. Language detection is also available in cases - # where the source language is unknown. - # - # Translation API supports more than one hundred different languages, from - # Afrikaans to Zulu. Used in combination, this enables translation between - # thousands of language pairs. Also, you can send in HTML and receive HTML - # with translated text back. You don't need to extract your source text or - # reassemble the translated content. - # - # The google-cloud-translate 2.0 gem contains a generated v3 client and a legacy hand-written v2 client. - # To use the legacy v2 client, call {Google::Cloud::Translate.new} and specify `version: :v2`. - # See [Migrating to Translation v3](https://cloud.google.com/translate/docs/migrate-to-v3) for details regarding - # differences between v2 and v3. - # - # See {file:OVERVIEW.md Translation Overview}. - # module Translate - FILE_DIR = File.realdirpath Pathname.new(__FILE__).join("..").join("translate") - - AVAILABLE_VERSIONS = Dir["#{FILE_DIR}/*"] - .select { |file| File.directory? file } - .select { |dir| Google::Gax::VERSION_MATCHER.match File.basename(dir) } - .select { |dir| File.exist? dir + ".rb" } - .map { |dir| File.basename dir } - ## - # Provides natural language translation operations. + # Create a new client object for TranslationService. # - # @overload new(version:, credentials:, scopes:, client_config:, timeout:) - # @param version [Symbol, String] - # The major version of the service to be used. By default `:v3` is used. - # @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. - # A `Google::Auth::Credentials` uses a the properties of its represented keyfile for authenticating requests - # made by this client. - # A `String` will be treated as the path to the keyfile to be used for the construction of credentials for - # this client. - # A `Hash` will be treated as the contents of a keyfile to be used for the construction of credentials for - # this client. - # A `GRPC::Core::Channel` will be used to make calls through. - # A `GRPC::Core::ChannelCredentials` for the setting up the RPC client. The channel credentials should already - # be composed with a `GRPC::Core::CallCredentials` object. - # A `Proc` will be used as an updater_proc for the Grpc channel. The proc transforms the metadata for - # requests, generally, to give OAuth credentials. - # @param scopes [Array<String>] - # The OAuth scopes for this service. This parameter is ignored if an updater_proc is supplied. - # @param client_config [Hash] - # A Hash for call options for each method. See 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 service_address [String] - # Override for the service hostname, or `nil` to leave as the default. - # @param service_port [Integer] - # Override for the service port, or `nil` to leave as the default. - # @param exception_transformer [Proc] - # An optional proc that intercepts any exceptions raised during an API call to inject custom error handling. - # @overload new(version:, project_id:, credentials:, key:, scope:, retries:, timeout:, endpoint:) - # @param version [Symbol, String] - # The major version of the service to be used. Specifying `:v2` will return the legacy client. - # @param [String] project_id Project identifier for the Cloud Translation service you are connecting to. If not - # present, the default project for the credentials is used. - # @param [String, Hash, Google::Auth::Credentials] credentials The path to the keyfile as a String, the contents - # of the keyfile as a Hash, or a Google::Auth::Credentials object. (See - # {Google::Cloud::Translate::V2::Credentials}) - # @param [String] key a public API access key (not an OAuth 2.0 token) - # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the set of resources and operations that - # the connection can access. See [Using OAuth 2.0 to Access Google - # APIs](https://developers.google.com/identity/protocols/OAuth2). + # By default, this returns an instance of + # [Google::Cloud::Translate::V3::TranslationService::Client](https://googleapis.dev/ruby/google-cloud-translate-v3/latest/Google/Cloud/Translate/V3/TranslationService/Client.html) + # for version V3 of the API. + # However, you can specify specify a different API version by passing it in the + # `version` parameter. If the TranslationService service is + # supported by that API version, and the corresponding gem is available, the + # appropriate versioned client will be returned. # - # The default scope is: + # ## About TranslationService # - # * `https://www.googleapis.com/auth/cloud-platform` - # @param [Integer] retries Number of times to retry requests on server error. The default value is `3`. - # Optional. - # @param [Integer] timeout Default timeout to use in requests. Optional. - # @param [String] endpoint Override of the endpoint host name. Optional. If the param is nil, uses the default - # endpoint. + # Provides natural language translation operations. # - # @example Using the v3 client. - # require "google/cloud/translate" + # @param version [::String, ::Symbol] The API version to connect to. Optional. + # Defaults to `:v3`. + # @return [TranslationService::Client] A client object for the specified version. # - # client = Google::Cloud::Translate.new - # - # project_id = "my-project-id" - # location_id = "us-central1" - # model_id = "my-automl-model-id" - # - # # The `model` type requested for this translation. - # model = "projects/#{project_id}/locations/#{location_id}/models/#{model_id}" - # # The content to translate in string format - # contents = ["Hello, world!"] - # # Required. The BCP-47 language code to use for translation. - # target_language = "fr" - # # Optional. The BCP-47 language code of the input text. - # source_language = "en" - # # Optional. Can be "text/plain" or "text/html". - # mime_type = "text/plain" - # parent = client.class.location_path project_id, location_id - # - # response = client.translate_text contents, target_language, parent, - # source_language_code: source_language, model: model, mime_type: mime_type - # - # # Display the translation for each input text provided - # response.translations.each do |translation| - # puts "Translated text: #{translation.translated_text}" - # end - # - # @example Using the legacy v2 client. - # require "google/cloud/translate" - # - # translate = Google::Cloud::Translate.new( - # version: :v2, - # project_id: "my-todo-project", - # credentials: "/path/to/keyfile.json" - # ) - # - # translation = translate.translate "Hello world!", to: "la" - # translation.text #=> "Salve mundi!" - # - # @example Using the legacy v2 client with an API Key. - # require "google/cloud/translate" - # - # translate = Google::Cloud::Translate.new( - # version: :v2, - # key: "api-key-abc123XYZ789" - # ) - # - # translation = translate.translate "Hello world!", to: "la" - # translation.text #=> "Salve mundi!" - # - # @example Using API Key from the environment variable. - # require "google/cloud/translate" - # - # ENV["TRANSLATE_KEY"] = "api-key-abc123XYZ789" - # - # translate = Google::Cloud::Translate.new version: :v2 - # - # translation = translate.translate "Hello world!", to: "la" - # translation.text #=> "Salve mundi!" - # - def self.new *args, version: :v3, **kwargs - unless AVAILABLE_VERSIONS.include? version.to_s.downcase - raise "The version: #{version} is not available. The available versions " \ - "are: [#{AVAILABLE_VERSIONS.join ', '}]" - end + def self.translation_service version: :v3, &block + require "google/cloud/translate/#{version.to_s.downcase}" - require "#{FILE_DIR}/#{version.to_s.downcase}" - version_module = Google::Cloud::Translate - .constants - .select { |sym| sym.to_s.casecmp(version.to_s).zero? } - .first - Google::Cloud::Translate.const_get(version_module).new(*args, **kwargs) + package_name = Google::Cloud::Translate + .constants + .select { |sym| sym.to_s.downcase == version.to_s.downcase.tr("_", "") } + .first + package_module = Google::Cloud::Translate.const_get package_name + package_module.const_get(:TranslationService).const_get(:Client).new(&block) end ## - # Configure the Google Cloud Translate library. + # Configure the google-cloud-translate library. # - # The following Translate configuration parameters are supported: + # The following configuration parameters are supported: # - # * `project_id` - (String) Identifier for a Translate project. - # * `credentials` - (String, Hash, Google::Auth::Credentials) The path to the keyfile as a String, the contents of - # the keyfile as a Hash, or a Google::Auth::Credentials object. (See - # {Google::Cloud::Translate::V2::Credentials}) - # * `scope` - (String, Array<String>) The OAuth 2.0 scopes controlling the set of resources and operations that - # the connection can access. - # * `retries` - (Integer) Number of times to retry requests on server error. - # * `timeout` - (Integer) Default timeout to use in requests. - # * `endpoint` - (String) Override of the endpoint host name, or `nil` to use the default endpoint. + # * `credentials` (*type:* `String, Hash, Google::Auth::Credentials`) - + # The path to the keyfile as a String, the contents of the keyfile as a + # Hash, or a Google::Auth::Credentials object. + # * `lib_name` (*type:* `String`) - + # The library name as recorded in instrumentation and logging. + # * `lib_version` (*type:* `String`) - + # The library version as recorded in instrumentation and logging. + # * `interceptors` (*type:* `Array<GRPC::ClientInterceptor>`) - + # An array of interceptors that are run before calls are executed. + # * `timeout` (*type:* `Integer`) - + # Default timeout in milliseconds. + # * `metadata` (*type:* `Hash{Symbol=>String}`) - + # Additional gRPC headers to be sent with the call. + # * `retry_policy` (*type:* `Hash`) - + # The retry policy. The value is a hash with the following keys: + # * `:initial_delay` (*type:* `Numeric`) - The initial delay in seconds. + # * `:max_delay` (*type:* `Numeric`) - The max delay in seconds. + # * `:multiplier` (*type:* `Numeric`) - The incremental backoff multiplier. + # * `:retry_codes` (*type:* `Array<String>`) - + # The error codes that should trigger a retry. # - # @note These values are only used by the legacy v2 client. + # @return [::Google::Cloud::Config] The default configuration used by this library # - # @return [Google::Cloud::Config] The configuration object the Google::Cloud::Translate library uses. - # def self.configure - yield Google::Cloud.configure.translate if block_given? + yield ::Google::Cloud.configure.translate if block_given? - Google::Cloud.configure.translate + ::Google::Cloud.configure.translate end end end end + +helper_path = ::File.join __dir__, "translate", "helpers.rb" +require "google/cloud/translate/helpers" if ::File.file? helper_path