Use discovered 'rootUrl' as base URI for services
authorremi Taylor <remily@google.com>
Tue, 7 Apr 2015 04:38:42 +0000 (21:38 -0700)
committerremi Taylor <remily@google.com>
Tue, 7 Apr 2015 04:38:42 +0000 (21:38 -0700)
lib/google/api_client/discovery/api.rb
spec/google/api_client/discovery_spec.rb

index be83f6279fdb2765f774bff8e9f2a39c60d762d4..3bbc90da3f3ca31d866dbb98c3c402c840802260 100644 (file)
@@ -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
index a2eb4e5f85966666ac55b9d2a88c0257d4076f9c..d9acf3374eab2ee28f9b45eada0d4f7de5a80ed6 100644 (file)
@@ -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