Skip to content

Commit

Permalink
Add GAX client_config to service factory
Browse files Browse the repository at this point in the history
Replace retries value with the GAX client configuration.
This leaks the GAX implementation to the google-cloud public API.
If Gax changes its implementation this may result in a change to
the google-cloud public API.
  • Loading branch information
blowmage committed Sep 29, 2016
1 parent 2abf2a0 commit d448dbf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion google-cloud-language/acceptance/language_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
require "google/cloud/language"

# Create shared language object so we don't create new for each test
$language = Google::Cloud.language retries: 10
$language = Google::Cloud.language

require "google/cloud/storage"

Expand Down
20 changes: 11 additions & 9 deletions google-cloud-language/lib/google-cloud-language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ module Cloud
# The default scope is:
#
# * `"https://www.googleapis.com/auth/cloud-platform"`
# @param [Integer] retries This option is not currently supported.
# @param [Integer] timeout Default timeout to use in requests. Optional.
# @param [Hash] client_config A hash of values to override the default
# behavior of the API client. See Google::Gax::CallSettings. Optional.
#
# @return [Google::Cloud::Language::Project]
#
Expand All @@ -60,10 +61,10 @@ module Cloud
# platform_scope = "https://www.googleapis.com/auth/cloud-platform"
# language = gcloud.language scope: platform_scope
#
def language scope: nil, retries: nil, timeout: nil
Google::Cloud.language @project, @keyfile, scope: scope,
retries: (retries || @retries),
timeout: (timeout || @timeout)
def language scope: nil, timeout: nil, client_config: nil
Google::Cloud.language @project, @keyfile,
scope: scope, timeout: (timeout || @timeout),
client_config: client_config
end

##
Expand All @@ -85,8 +86,9 @@ def language scope: nil, retries: nil, timeout: nil
# The default scope is:
#
# * `"https://www.googleapis.com/auth/cloud-platform"`
# @param [Integer] retries This option is not currently supported.
# @param [Integer] timeout Default timeout to use in requests. Optional.
# @param [Hash] client_config A hash of values to override the default
# behavior of the API client. See Google::Gax::CallSettings. Optional.
#
# @return [Google::Cloud::Language::Project]
#
Expand All @@ -99,8 +101,8 @@ def language scope: nil, retries: nil, timeout: nil
# document = language.document content
# annotation = document.annotate
#
def self.language project = nil, keyfile = nil, scope: nil, retries: nil,
timeout: nil
def self.language project = nil, keyfile = nil, scope: nil, timeout: nil,
client_config: nil
require "google/cloud/language"
project ||= Google::Cloud::Language::Project.default_project
if keyfile.nil?
Expand All @@ -111,7 +113,7 @@ def self.language project = nil, keyfile = nil, scope: nil, retries: nil,
end
Google::Cloud::Language::Project.new(
Google::Cloud::Language::Service.new(
project, credentials, retries: retries, timeout: timeout))
project, credentials, timeout: timeout, client_config: client_config))
end
end
end
20 changes: 10 additions & 10 deletions google-cloud-language/lib/google/cloud/language/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ module Language
# @private Represents the gRPC Language service, including all the API
# methods.
class Service
attr_accessor :project, :credentials, :host, :retries, :timeout
attr_accessor :project, :credentials, :host, :client_config, :timeout

##
# Creates a new Service instance.
def initialize project, credentials, host: nil, retries: nil,
def initialize project, credentials, host: nil, client_config: nil,
timeout: nil
@project = project
@credentials = credentials
@host = host || V1beta1::LanguageServiceApi::SERVICE_ADDRESS
@retries = retries
@client_config = client_config || {}
@timeout = timeout
end

Expand All @@ -51,13 +51,13 @@ def chan_creds

def service
return mocked_service if mocked_service
@service ||= \
V1beta1::LanguageServiceApi.new(
service_path: host,
channel: channel,
timeout: timeout,
app_name: "google-cloud-language",
app_version: Google::Cloud::Language::VERSION)
@service ||= V1beta1::LanguageServiceApi.new(
service_path: host,
channel: channel,
timeout: timeout,
client_config: client_config,
app_name: "google-cloud-language",
app_version: Google::Cloud::Language::VERSION)
end
attr_accessor :mocked_service

Expand Down
18 changes: 9 additions & 9 deletions google-cloud-language/test/google/cloud/language_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
describe "#language" do
it "calls out to Google::Cloud.language" do
gcloud = Google::Cloud.new
stubbed_language = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) {
stubbed_language = ->(project, keyfile, scope: nil, timeout: nil, client_config: nil) {
project.must_equal nil
keyfile.must_equal nil
scope.must_be :nil?
retries.must_be :nil?
timeout.must_be :nil?
client_config.must_be :nil?
"language-project-object-empty"
}
Google::Cloud.stub :language, stubbed_language do
Expand All @@ -34,12 +34,12 @@

it "passes project and keyfile to Google::Cloud.language" do
gcloud = Google::Cloud.new "project-id", "keyfile-path"
stubbed_language = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) {
stubbed_language = ->(project, keyfile, scope: nil, timeout: nil, client_config: nil) {
project.must_equal "project-id"
keyfile.must_equal "keyfile-path"
scope.must_be :nil?
retries.must_be :nil?
timeout.must_be :nil?
client_config.must_be :nil?
"language-project-object"
}
Google::Cloud.stub :language, stubbed_language do
Expand All @@ -50,16 +50,16 @@

it "passes project and keyfile and options to Google::Cloud.language" do
gcloud = Google::Cloud.new "project-id", "keyfile-path"
stubbed_language = ->(project, keyfile, scope: nil, retries: nil, timeout: nil) {
stubbed_language = ->(project, keyfile, scope: nil, timeout: nil, client_config: nil) {
project.must_equal "project-id"
keyfile.must_equal "keyfile-path"
scope.must_equal "http://example.com/scope"
retries.must_equal 5
timeout.must_equal 60
client_config.must_equal({ "gax" => "options" })
"language-project-object-scoped"
}
Google::Cloud.stub :language, stubbed_language do
project = gcloud.language scope: "http://example.com/scope", retries: 5, timeout: 60
project = gcloud.language scope: "http://example.com/scope", timeout: 60, client_config: { "gax" => "options" }
project.must_equal "language-project-object-scoped"
end
end
Expand Down Expand Up @@ -90,11 +90,11 @@
scope.must_equal nil
"language-credentials"
}
stubbed_service = ->(project, credentials, retries: nil, timeout: nil) {
stubbed_service = ->(project, credentials, timeout: nil, client_config: nil) {
project.must_equal "project-id"
credentials.must_equal "language-credentials"
retries.must_equal nil
timeout.must_equal nil
client_config.must_equal nil
OpenStruct.new project: project
}

Expand Down

0 comments on commit d448dbf

Please sign in to comment.