From 1ed677b796afad92b5f232165912ab269c0b4b8a Mon Sep 17 00:00:00 2001 From: remi Taylor Date: Mon, 6 Apr 2015 21:38:42 -0700 Subject: [PATCH] Use discovered 'rootUrl' as base URI for services --- lib/google/api_client/discovery/api.rb | 14 +++++++++-- spec/google/api_client/discovery_spec.rb | 30 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/lib/google/api_client/discovery/api.rb b/lib/google/api_client/discovery/api.rb index be83f6279..3bbc90da3 100644 --- a/lib/google/api_client/discovery/api.rb +++ b/lib/google/api_client/discovery/api.rb @@ -30,7 +30,7 @@ module Google # Creates a description of a particular version of a service. # # @param [String] document_base - # Base URI for the service + # Base URI for the discovery document. # @param [Hash] discovery_document # The section of the discovery document that applies to this service # version. @@ -126,6 +126,16 @@ module Google return @discovery_document['features'] || [] end + ## + # Returns the root URI for this service. + # + # @return [Addressable::URI] The root URI. + def root_uri + return @root_uri ||= ( + Addressable::URI.parse(self.discovery_document['rootUrl']) + ) + end + ## # Returns true if this API uses a data wrapper. # @@ -148,7 +158,7 @@ module Google def method_base if @discovery_document['basePath'] return @method_base ||= ( - self.document_base.join(Addressable::URI.parse(@discovery_document['basePath'])) + self.root_uri.join(Addressable::URI.parse(@discovery_document['basePath'])) ).normalize else return nil diff --git a/spec/google/api_client/discovery_spec.rb b/spec/google/api_client/discovery_spec.rb index a2eb4e5f8..d9acf3374 100644 --- a/spec/google/api_client/discovery_spec.rb +++ b/spec/google/api_client/discovery_spec.rb @@ -473,6 +473,10 @@ RSpec.describe Google::APIClient do ).to_env(CLIENT.connection) end).to raise_error(ArgumentError) end + + it 'should correctly determine the service root_uri' do + expect(@plus.root_uri.to_s).to eq('https://www.googleapis.com/') + end end describe 'with the adsense API' do @@ -659,4 +663,30 @@ RSpec.describe Google::APIClient do expect(@drive.files.insert.media_upload.max_size).not_to eq(nil) end end + + describe 'with the Pub/Sub API' do + before do + CLIENT.authorization = nil + @pubsub = CLIENT.discovered_api('pubsub', 'v1beta2') + end + + it 'should generate requests against the correct URIs' do + conn = stub_connection do |stub| + stub.get('/v1beta2/projects/12345/topics') do |env| + expect(env[:url].host).to eq('pubsub.googleapis.com') + [200, {}, '{}'] + end + end + request = CLIENT.execute( + :api_method => @pubsub.projects.topics.list, + :parameters => {'project' => 'projects/12345'}, + :connection => conn + ) + conn.verify + end + + it 'should correctly determine the service root_uri' do + expect(@pubsub.root_uri.to_s).to eq('https://pubsub.googleapis.com/') + end + end end