Allow credentials on per-request basis

This commit is contained in:
Steven Bazyl 2012-06-06 17:27:20 -07:00
parent f7a4db959d
commit a7b40becc6
3 changed files with 46 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -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