Skip to content

Commit

Permalink
Merge branch 'main' into dev/virost/misc/auth_error_design
Browse files Browse the repository at this point in the history
  • Loading branch information
viacheslav-rostovtsev authored Sep 13, 2022
2 parents d0c2cf1 + fc0065a commit a76effa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
6 changes: 4 additions & 2 deletions gapic-common/lib/gapic/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,22 @@ module Headers
# `:rest` to send the REST library version (if defined)
# Defaults to `[:grpc]`
def self.x_goog_api_client ruby_version: nil, lib_name: nil, lib_version: nil, gax_version: nil,
gapic_version: nil, grpc_version: nil, rest_version: nil,
gapic_version: nil, grpc_version: nil, rest_version: nil, protobuf_version: nil,
transports_version_send: [:grpc]

ruby_version ||= ::RUBY_VERSION
gax_version ||= ::Gapic::Common::VERSION
grpc_version ||= ::GRPC::VERSION if defined? ::GRPC::VERSION
rest_version ||= ::Faraday::VERSION if defined? ::Faraday
rest_version ||= ::Faraday::VERSION if defined? ::Faraday::VERSION
protobuf_version ||= Gem.loaded_specs["google-protobuf"].version.to_s if Gem.loaded_specs.key? "google-protobuf"

x_goog_api_client_header = ["gl-ruby/#{ruby_version}"]
x_goog_api_client_header << "#{lib_name}/#{lib_version}" if lib_name
x_goog_api_client_header << "gax/#{gax_version}"
x_goog_api_client_header << "gapic/#{gapic_version}" if gapic_version
x_goog_api_client_header << "grpc/#{grpc_version}" if grpc_version && transports_version_send.include?(:grpc)
x_goog_api_client_header << "rest/#{rest_version}" if rest_version && transports_version_send.include?(:rest)
x_goog_api_client_header << "pb/#{protobuf_version}" if protobuf_version
x_goog_api_client_header.join " ".freeze
end
end
Expand Down
22 changes: 11 additions & 11 deletions gapic-common/test/gapic/headers/x_goog_api_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,46 @@
describe Gapic::Headers, :x_goog_api_client do
it "with no arguments" do
header = Gapic::Headers.x_goog_api_client
_(header).must_equal "gl-ruby/#{RUBY_VERSION} gax/#{Gapic::Common::VERSION} grpc/#{GRPC::VERSION}"
_(header).must_equal "gl-ruby/#{RUBY_VERSION} gax/#{Gapic::Common::VERSION} grpc/#{GRPC::VERSION} pb/#{Gem.loaded_specs["google-protobuf"].version.to_s}"
end

it "prints lib when provided" do
header = Gapic::Headers.x_goog_api_client lib_name: "foo", lib_version: "bar"
_(header).must_equal "gl-ruby/#{RUBY_VERSION} foo/bar gax/#{Gapic::Common::VERSION} grpc/#{GRPC::VERSION}"
_(header).must_equal "gl-ruby/#{RUBY_VERSION} foo/bar gax/#{Gapic::Common::VERSION} grpc/#{GRPC::VERSION} pb/#{Gem.loaded_specs["google-protobuf"].version.to_s}"
end

it "prints gax version when provided" do
header = Gapic::Headers.x_goog_api_client gax_version: "foobar"
_(header).must_equal "gl-ruby/#{RUBY_VERSION} gax/foobar grpc/#{GRPC::VERSION}"
_(header).must_equal "gl-ruby/#{RUBY_VERSION} gax/foobar grpc/#{GRPC::VERSION} pb/#{Gem.loaded_specs["google-protobuf"].version.to_s}"
end

it "prints all arguments provided" do
header = Gapic::Headers.x_goog_api_client ruby_version: "1.2.3", lib_name: "foo", lib_version: "bar",
gax_version: "4.5.6", gapic_version: "7.8.9", grpc_version: "10.11.12"
_(header).must_equal "gl-ruby/1.2.3 foo/bar gax/4.5.6 gapic/7.8.9 grpc/10.11.12"
gax_version: "4.5.6", gapic_version: "7.8.9", grpc_version: "10.11.12", protobuf_version: "16.17.18"
_(header).must_equal "gl-ruby/1.2.3 foo/bar gax/4.5.6 gapic/7.8.9 grpc/10.11.12 pb/16.17.18"
end

it "sets rest headers with no other arguments" do
header = Gapic::Headers.x_goog_api_client transports_version_send: [:rest]
_(header).must_equal "gl-ruby/#{RUBY_VERSION} gax/#{Gapic::Common::VERSION} rest/#{::Faraday::VERSION}"
_(header).must_equal "gl-ruby/#{RUBY_VERSION} gax/#{Gapic::Common::VERSION} rest/#{::Faraday::VERSION} pb/#{Gem.loaded_specs["google-protobuf"].version.to_s}"
end

it "overrides rest version" do
header = Gapic::Headers.x_goog_api_client rest_version: "13.14.15", transports_version_send: [:rest]
_(header).must_equal "gl-ruby/#{RUBY_VERSION} gax/#{Gapic::Common::VERSION} rest/13.14.15"
_(header).must_equal "gl-ruby/#{RUBY_VERSION} gax/#{Gapic::Common::VERSION} rest/13.14.15 pb/#{Gem.loaded_specs["google-protobuf"].version.to_s}"
end

it "sends rest version with other arguments provided" do
header = Gapic::Headers.x_goog_api_client ruby_version: "1.2.3", lib_name: "foo", lib_version: "bar",
gax_version: "4.5.6", gapic_version: "7.8.9",
rest_version: "13.14.15", transports_version_send: [:rest]
_(header).must_equal "gl-ruby/1.2.3 foo/bar gax/4.5.6 gapic/7.8.9 rest/13.14.15"
rest_version: "13.14.15", transports_version_send: [:rest], protobuf_version: "16.17.18"
_(header).must_equal "gl-ruby/1.2.3 foo/bar gax/4.5.6 gapic/7.8.9 rest/13.14.15 pb/16.17.18"
end

it "sends grpc and rest versions together" do
header = Gapic::Headers.x_goog_api_client ruby_version: "1.2.3", lib_name: "foo", lib_version: "bar",
gax_version: "4.5.6", gapic_version: "7.8.9", grpc_version: "10.11.12",
rest_version: "13.14.15", transports_version_send: [:rest, :grpc]
_(header).must_equal "gl-ruby/1.2.3 foo/bar gax/4.5.6 gapic/7.8.9 grpc/10.11.12 rest/13.14.15"
rest_version: "13.14.15", transports_version_send: [:rest, :grpc], protobuf_version: "16.17.18"
_(header).must_equal "gl-ruby/1.2.3 foo/bar gax/4.5.6 gapic/7.8.9 grpc/10.11.12 rest/13.14.15 pb/16.17.18"
end
end

0 comments on commit a76effa

Please sign in to comment.