Skip to content

Commit

Permalink
feat: Introduce api_version to discovery clients (#18969)
Browse files Browse the repository at this point in the history
  • Loading branch information
aandreassa authored May 13, 2024
1 parent 0f19a58 commit b8caede
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
6 changes: 6 additions & 0 deletions google-apis-core/lib/google/apis/core/http_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,12 @@ def allow_form_encoding?
[:post, :put].include?(method) && body.nil?
end

# Set the API version header for the service if not empty.
# @return [void]
def set_api_version_header api_version
self.header['X-Goog-Api-Version'] = api_version unless api_version.empty?
end

private

UNSAFE_CLASS_NAMES = [
Expand Down
10 changes: 10 additions & 0 deletions google-apis-core/spec/google/apis/core/http_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,16 @@ class SecretPayload
command.execute(client)
end

it 'should set X-Goog-Api-Version headers when requested' do
command = Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals')
command.set_api_version_header "v1_20240502"
stub_request(:get, 'https://www.googleapis.com/zoo/animals').to_return(body: %(Api version))
result = command.execute(client)
expect(a_request(:get, 'https://www.googleapis.com/zoo/animals')
.with { |req| req.headers['X-Goog-Api-Version'] == 'v1_20240502' }).to have_been_made
expect(result).to eql "Api version"
end

describe "#safe_pretty_representation" do
let(:command) do
Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals')
Expand Down
1 change: 1 addition & 0 deletions google-apis-generator/lib/google/apis/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
require 'google/apis/generator/template'
require 'google/apis/generator/updater'
require 'google/apis/generator/version'
require 'google/apis/generator/patch'
require 'active_support'
require 'active_support/core_ext'
require 'active_support/inflector'
Expand Down
35 changes: 35 additions & 0 deletions google-apis-generator/lib/google/apis/generator/patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'google/apis/discovery_v1'

# Extend the DiscoveryV1 API classes with additional fields for code generation,
# supporting features which are not present in the schema itself.
unless Google::Apis::DiscoveryV1::RestMethod.method_defined? :api_version
module Google
module Apis
module DiscoveryV1
class RestMethod
# The `apiVersion` for this method, or empty if not present.
# @return [String]
attr_accessor :api_version

# @private
# The original DiscoveryV1::RestMethod `update!` method to be called
# after applying patches to this schema.
alias_method :update_discovery!, :update!

# Update properties of this object.
def update!(**args)
@api_version = args.key?(:api_version) ? args[:api_version] : ""
update_discovery!(**args)
end

# @private
class Representation
# @private
# The api_version based on the JSON key value.
property :api_version, as: 'apiVersion'
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ def <%= api_method.generated_name %>(<% for param in api_method.required_paramet
<% end -%>
<% for param in api.parameters.values.reject {|p| p.name == 'key'} -%>
command.query['<%= param.name %>'] = <%= param.generated_name %> unless <%= param.generated_name %>.nil?
<% end -%>
<% unless api_method.api_version.empty? -%>
command.set_api_version_header "<%= api_method.api_version %>"
<% end -%>
execute_or_queue_command(command, &block)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'spec_helper'
require 'google/apis/generator'

RSpec.describe 'Google::Apis::Generator with DiscoveryV1 patch' do

it 'should modify RestMethod if `api_version` is not defined' do
expect(Google::Apis::Generator::Discovery::RestMethod.method_defined? :update_discovery!).to eql(true)
end
end

0 comments on commit b8caede

Please sign in to comment.