From a7b40becc607563b42f608a6a6531133b2b3d5ea Mon Sep 17 00:00:00 2001 From: Steven Bazyl Date: Wed, 6 Jun 2012 17:27:20 -0700 Subject: [PATCH] Allow credentials on per-request basis --- lib/google/api_client.rb | 6 +++++- lib/google/api_client/reference.rb | 12 +++++++++++- spec/google/api_client_spec.rb | 30 ++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/google/api_client.rb b/lib/google/api_client.rb index 84aac1d6d..9504f647d 100644 --- a/lib/google/api_client.rb +++ b/lib/google/api_client.rb @@ -26,6 +26,7 @@ require 'google/api_client/discovery' require 'google/api_client/reference' require 'google/api_client/result' require 'google/api_client/media' +require 'google/api_client/service_account' module Google # TODO(bobaman): Document all this stuff. @@ -540,6 +541,7 @@ module Google def generate_request(options={}) # Note: The merge method on a Hash object will coerce an API Reference # object into a Hash and merge with the default options. + options={ :version => 'v1', :authorization => self.authorization, @@ -547,6 +549,7 @@ module Google :user_ip => self.user_ip, :connection => Faraday.default_connection }.merge(options) + # The Reference object is going to need this to do method ID lookups. options[:client] = self # The default value for the :authenticated option depends on whether an @@ -559,7 +562,7 @@ module Google reference = Google::APIClient::Reference.new(options) request = reference.to_request if options[:authenticated] - request = self.generate_authenticated_request( + request = options[:authorization].generate_authenticated_request( :request => request, :connection => options[:connection] ) @@ -573,6 +576,7 @@ module Google # @param [Hash] options a customizable set of options # # @return [Faraday::Request] The signed or otherwise authenticated request. + # @deprecated No longer used internally def generate_authenticated_request(options={}) return authorization.generate_authenticated_request(options) end diff --git a/lib/google/api_client/reference.rb b/lib/google/api_client/reference.rb index 5fe0caa85..42dee0445 100644 --- a/lib/google/api_client/reference.rb +++ b/lib/google/api_client/reference.rb @@ -21,6 +21,7 @@ require 'addressable/uri' require 'stringio' require 'google/api_client/discovery' +# TODO - needs some serious cleanup module Google class APIClient @@ -35,6 +36,7 @@ module Google @version = options[:version] || 'v1' self.connection = options[:connection] || Faraday.default_connection + self.authorization = options[:authorization] self.api_method = options[:api_method] self.parameters = options[:parameters] || {} # These parameters are handled differently because they're not @@ -42,7 +44,6 @@ module Google self.parameters['key'] ||= options[:key] if options[:key] self.parameters['userIp'] ||= options[:user_ip] if options[:user_ip] self.headers = options[:headers] || {} - if options[:media] self.media = options[:media] upload_type = parameters['uploadType'] || parameters['upload_type'] @@ -116,6 +117,14 @@ module Google @media = (media) end + def authorization + return @authorization + end + + def authorization=(new_authorization) + @authorization = new_authorization + end + def connection return @connection end @@ -257,6 +266,7 @@ module Google options[:headers] = self.headers options[:body] = self.body options[:connection] = self.connection + options[:authorization] = self.authorization unless self.authorization.nil? return options end end diff --git a/spec/google/api_client_spec.rb b/spec/google/api_client_spec.rb index 9ac14fe36..dbd772c9d 100644 --- a/spec/google/api_client_spec.rb +++ b/spec/google/api_client_spec.rb @@ -108,4 +108,34 @@ describe Google::APIClient do # TODO it_should_behave_like 'configurable user agent' end + + describe 'when executing requests' do + before do + @client.authorization = :oauth_2 + @connection = Faraday.new(:url => 'https://www.googleapis.com') do |builder| + stubs = Faraday::Adapter::Test::Stubs.new do |stub| + stub.get('/test') do |env| + env[:request_headers]['Authorization'].should == 'Bearer 12345' + end + end + builder.adapter(:test, stubs) + end + end + + it 'should use default authorization' do + @client.authorization.access_token = "12345" + @client.execute(:http_method => :get, + :uri => 'https://www.googleapis.com/test', + :connection => @connection) + end + + it 'should use request scoped authorization when provided' do + @client.authorization.access_token = "abcdef" + new_auth = Signet::OAuth2::Client.new(:access_token => '12345') + @client.execute(:http_method => :get, + :uri => 'https://www.googleapis.com/test', + :connection => @connection, + :authorization => new_auth) + end + end end