Skip to content

Commit 0003e56

Browse files
committed
Merge pull request googleapis#213 from remi/respect-discovered-rootUrl
Use discovered 'rootUrl' as base URI for services
2 parents 066ca68 + 1ed677b commit 0003e56

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

lib/google/api_client/discovery/api.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class API
3030
# Creates a description of a particular version of a service.
3131
#
3232
# @param [String] document_base
33-
# Base URI for the service
33+
# Base URI for the discovery document.
3434
# @param [Hash] discovery_document
3535
# The section of the discovery document that applies to this service
3636
# version.
@@ -126,6 +126,16 @@ def features
126126
return @discovery_document['features'] || []
127127
end
128128

129+
##
130+
# Returns the root URI for this service.
131+
#
132+
# @return [Addressable::URI] The root URI.
133+
def root_uri
134+
return @root_uri ||= (
135+
Addressable::URI.parse(self.discovery_document['rootUrl'])
136+
)
137+
end
138+
129139
##
130140
# Returns true if this API uses a data wrapper.
131141
#
@@ -148,7 +158,7 @@ def data_wrapper?
148158
def method_base
149159
if @discovery_document['basePath']
150160
return @method_base ||= (
151-
self.document_base.join(Addressable::URI.parse(@discovery_document['basePath']))
161+
self.root_uri.join(Addressable::URI.parse(@discovery_document['basePath']))
152162
).normalize
153163
else
154164
return nil

spec/google/api_client/discovery_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,10 @@
473473
).to_env(CLIENT.connection)
474474
end).to raise_error(ArgumentError)
475475
end
476+
477+
it 'should correctly determine the service root_uri' do
478+
expect(@plus.root_uri.to_s).to eq('https://www.googleapis.com/')
479+
end
476480
end
477481

478482
describe 'with the adsense API' do
@@ -659,4 +663,30 @@
659663
expect(@drive.files.insert.media_upload.max_size).not_to eq(nil)
660664
end
661665
end
666+
667+
describe 'with the Pub/Sub API' do
668+
before do
669+
CLIENT.authorization = nil
670+
@pubsub = CLIENT.discovered_api('pubsub', 'v1beta2')
671+
end
672+
673+
it 'should generate requests against the correct URIs' do
674+
conn = stub_connection do |stub|
675+
stub.get('/v1beta2/projects/12345/topics') do |env|
676+
expect(env[:url].host).to eq('pubsub.googleapis.com')
677+
[200, {}, '{}']
678+
end
679+
end
680+
request = CLIENT.execute(
681+
:api_method => @pubsub.projects.topics.list,
682+
:parameters => {'project' => 'projects/12345'},
683+
:connection => conn
684+
)
685+
conn.verify
686+
end
687+
688+
it 'should correctly determine the service root_uri' do
689+
expect(@pubsub.root_uri.to_s).to eq('https://pubsub.googleapis.com/')
690+
end
691+
end
662692
end

0 commit comments

Comments
 (0)