Skip to content

Commit b8caede

Browse files
authored
feat: Introduce api_version to discovery clients (#18969)
1 parent 0f19a58 commit b8caede

File tree

6 files changed

+64
-0
lines changed

6 files changed

+64
-0
lines changed

google-apis-core/lib/google/apis/core/http_command.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,12 @@ def allow_form_encoding?
348348
[:post, :put].include?(method) && body.nil?
349349
end
350350

351+
# Set the API version header for the service if not empty.
352+
# @return [void]
353+
def set_api_version_header api_version
354+
self.header['X-Goog-Api-Version'] = api_version unless api_version.empty?
355+
end
356+
351357
private
352358

353359
UNSAFE_CLASS_NAMES = [

google-apis-core/spec/google/apis/core/http_command_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,16 @@ class SecretPayload
528528
command.execute(client)
529529
end
530530

531+
it 'should set X-Goog-Api-Version headers when requested' do
532+
command = Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals')
533+
command.set_api_version_header "v1_20240502"
534+
stub_request(:get, 'https://www.googleapis.com/zoo/animals').to_return(body: %(Api version))
535+
result = command.execute(client)
536+
expect(a_request(:get, 'https://www.googleapis.com/zoo/animals')
537+
.with { |req| req.headers['X-Goog-Api-Version'] == 'v1_20240502' }).to have_been_made
538+
expect(result).to eql "Api version"
539+
end
540+
531541
describe "#safe_pretty_representation" do
532542
let(:command) do
533543
Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals')

google-apis-generator/lib/google/apis/generator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
require 'google/apis/generator/template'
1919
require 'google/apis/generator/updater'
2020
require 'google/apis/generator/version'
21+
require 'google/apis/generator/patch'
2122
require 'active_support'
2223
require 'active_support/core_ext'
2324
require 'active_support/inflector'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require 'google/apis/discovery_v1'
2+
3+
# Extend the DiscoveryV1 API classes with additional fields for code generation,
4+
# supporting features which are not present in the schema itself.
5+
unless Google::Apis::DiscoveryV1::RestMethod.method_defined? :api_version
6+
module Google
7+
module Apis
8+
module DiscoveryV1
9+
class RestMethod
10+
# The `apiVersion` for this method, or empty if not present.
11+
# @return [String]
12+
attr_accessor :api_version
13+
14+
# @private
15+
# The original DiscoveryV1::RestMethod `update!` method to be called
16+
# after applying patches to this schema.
17+
alias_method :update_discovery!, :update!
18+
19+
# Update properties of this object.
20+
def update!(**args)
21+
@api_version = args.key?(:api_version) ? args[:api_version] : ""
22+
update_discovery!(**args)
23+
end
24+
25+
# @private
26+
class Representation
27+
# @private
28+
# The api_version based on the JSON key value.
29+
property :api_version, as: 'apiVersion'
30+
end
31+
end
32+
end
33+
end
34+
end
35+
end

google-apis-generator/lib/google/apis/generator/templates/_method.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ def <%= api_method.generated_name %>(<% for param in api_method.required_paramet
102102
<% end -%>
103103
<% for param in api.parameters.values.reject {|p| p.name == 'key'} -%>
104104
command.query['<%= param.name %>'] = <%= param.generated_name %> unless <%= param.generated_name %>.nil?
105+
<% end -%>
106+
<% unless api_method.api_version.empty? -%>
107+
command.set_api_version_header "<%= api_method.api_version %>"
105108
<% end -%>
106109
execute_or_queue_command(command, &block)
107110
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require 'spec_helper'
2+
require 'google/apis/generator'
3+
4+
RSpec.describe 'Google::Apis::Generator with DiscoveryV1 patch' do
5+
6+
it 'should modify RestMethod if `api_version` is not defined' do
7+
expect(Google::Apis::Generator::Discovery::RestMethod.method_defined? :update_discovery!).to eql(true)
8+
end
9+
end

0 commit comments

Comments
 (0)