Resolving issues introduced by Faraday dependency upgrade.

This commit is contained in:
Bob Aman 2012-07-21 13:50:48 +03:00
parent 5e751681dd
commit 3bc7d52764
7 changed files with 64 additions and 49 deletions

View File

@ -7,12 +7,12 @@ gem 'addressable', '>= 2.2.3'
gem 'uuidtools', '>= 2.1.0' gem 'uuidtools', '>= 2.1.0'
gem 'autoparse', '>= 0.3.1' gem 'autoparse', '>= 0.3.1'
gem 'faraday', '~> 0.8.1' gem 'faraday', '~> 0.8.1'
gem 'multi_json', '>= 1.3.0' gem 'multi_json', '>= 1.0.0'
gem 'extlib', '>= 0.9.15' gem 'extlib', '>= 0.9.15'
gem 'jruby-openssl', :platforms => :jruby gem 'jruby-openssl', :platforms => :jruby
group :development do group :development do
gem 'launchy' gem 'launchy', '>= 2.0.0'
gem 'yard' gem 'yard'
gem 'redcarpet' gem 'redcarpet'
end end
@ -23,6 +23,7 @@ end
group :test, :development do group :test, :development do
gem 'rake', '>= 0.9.0' gem 'rake', '>= 0.9.0'
gem 'rspec', '>= 2.11.0'
gem 'rcov', '>= 0.9.9', :platform => :mri_18 gem 'rcov', '>= 0.9.9', :platform => :mri_18
end end

View File

@ -542,7 +542,7 @@ module Google
def generate_request(options={}) def generate_request(options={})
# Note: The merge method on a Hash object will coerce an API Reference # Note: The merge method on a Hash object will coerce an API Reference
# object into a Hash and merge with the default options. # object into a Hash and merge with the default options.
options={ options={
:version => 'v1', :version => 'v1',
:authorization => self.authorization, :authorization => self.authorization,
@ -550,7 +550,7 @@ module Google
:user_ip => self.user_ip, :user_ip => self.user_ip,
:connection => Faraday.default_connection :connection => Faraday.default_connection
}.merge(options) }.merge(options)
# The Reference object is going to need this to do method ID lookups. # The Reference object is going to need this to do method ID lookups.
options[:client] = self options[:client] = self
# The default value for the :authenticated option depends on whether an # The default value for the :authenticated option depends on whether an
@ -655,8 +655,10 @@ module Google
end end
end end
request = Faraday::Request.create(method.to_s.downcase.to_sym) do |req| request = options[:connection].build_request(
req.url(Addressable::URI.parse(uri)) method.to_s.downcase.to_sym
) do |req|
req.url(Addressable::URI.parse(uri).normalize.to_s)
req.headers = Faraday::Utils::Headers.new(headers) req.headers = Faraday::Utils::Headers.new(headers)
req.body = body req.body = body
end end
@ -709,6 +711,7 @@ module Google
params.size == 1 params.size == 1
batch = params.pop batch = params.pop
options = batch.options options = batch.options
options[:connection] ||= Faraday.default_connection
http_request = batch.to_http_request http_request = batch.to_http_request
request = nil request = nil
@ -716,8 +719,10 @@ module Google
method, uri, headers, body = http_request method, uri, headers, body = http_request
method = method.to_s.downcase.to_sym method = method.to_s.downcase.to_sym
faraday_request = Faraday::Request.create(method) do |req| faraday_request = options[:connection].build_request(
req.url(uri.to_s) method.to_s.downcase.to_sym
) do |req|
req.url(Addressable::URI.parse(uri).normalize.to_s)
req.headers = Faraday::Utils::Headers.new(headers) req.headers = Faraday::Utils::Headers.new(headers)
req.body = body req.body = body
end end
@ -755,6 +760,7 @@ module Google
options[:body] = params.shift if params.size > 0 options[:body] = params.shift if params.size > 0
options[:headers] = params.shift if params.size > 0 options[:headers] = params.shift if params.size > 0
options[:client] = self options[:client] = self
options[:connection] ||= Faraday.default_connection
reference = Google::APIClient::Reference.new(options) reference = Google::APIClient::Reference.new(options)
request = self.generate_request(reference) request = self.generate_request(reference)
response = self.transmit( response = self.transmit(

View File

@ -218,9 +218,12 @@ module Google
# The parameters to send. # The parameters to send.
# @param [String, StringIO] body The body for the HTTP request. # @param [String, StringIO] body The body for the HTTP request.
# @param [Hash, Array] headers The HTTP headers for the request. # @param [Hash, Array] headers The HTTP headers for the request.
# @option options [Faraday::Connection] :connection
# The HTTP connection to use.
# #
# @return [Array] The generated HTTP request. # @return [Array] The generated HTTP request.
def generate_request(parameters={}, body='', headers=[]) def generate_request(parameters={}, body='', headers=[], options={})
options[:connection] ||= Faraday.default_connection
if body.respond_to?(:string) if body.respond_to?(:string)
body = body.string body = body.string
elsif body.respond_to?(:to_str) elsif body.respond_to?(:to_str)
@ -234,8 +237,10 @@ module Google
method = self.http_method method = self.http_method
uri = self.generate_uri(parameters) uri = self.generate_uri(parameters)
headers = headers.to_a if headers.kind_of?(Hash) headers = headers.to_a if headers.kind_of?(Hash)
return Faraday::Request.create(method.to_s.downcase.to_sym) do |req| return options[:connection].build_request(
req.url(Addressable::URI.parse(uri)) method.to_s.downcase.to_sym
) do |req|
req.url(Addressable::URI.parse(uri).normalize.to_s)
req.headers = Faraday::Utils::Headers.new(headers) req.headers = Faraday::Utils::Headers.new(headers)
req.body = body req.body = body
end end

View File

@ -26,8 +26,8 @@ require 'google/api_client/discovery'
module Google module Google
class APIClient class APIClient
class Reference class Reference
MULTIPART_BOUNDARY = "-----------RubyApiMultipartPost".freeze MULTIPART_BOUNDARY = "-----------RubyApiMultipartPost".freeze
def initialize(options={}) def initialize(options={})
# We only need this to do lookups on method ID String values # We only need this to do lookups on method ID String values
# It's optional, but method ID lookups will fail if the client is # It's optional, but method ID lookups will fail if the client is
@ -46,17 +46,17 @@ module Google
self.headers = options[:headers] || {} self.headers = options[:headers] || {}
if options[:media] if options[:media]
self.media = options[:media] self.media = options[:media]
upload_type = parameters['uploadType'] || parameters['upload_type'] upload_type = parameters['uploadType'] || parameters['upload_type']
case upload_type case upload_type
when "media" when "media"
if options[:body] || options[:body_object] if options[:body] || options[:body_object]
raise ArgumentError, "Can not specify body & body object for simple uploads" raise ArgumentError, "Can not specify body & body object for simple uploads"
end end
self.headers['Content-Type'] ||= self.media.content_type self.headers['Content-Type'] ||= self.media.content_type
self.body = self.media self.body = self.media
when "multipart" when "multipart"
unless options[:body_object] unless options[:body_object]
raise ArgumentError, "Multipart requested but no body object" raise ArgumentError, "Multipart requested but no body object"
end end
# This is all a bit of a hack due to signet requiring body to be a string # This is all a bit of a hack due to signet requiring body to be a string
# Ideally, update signet to delay serialization so we can just pass # Ideally, update signet to delay serialization so we can just pass
@ -68,7 +68,7 @@ module Google
} }
multipart = Faraday::Request::Multipart.new multipart = Faraday::Request::Multipart.new
self.body = multipart.create_multipart(env, [ self.body = multipart.create_multipart(env, [
[nil,Faraday::UploadIO.new(metadata, 'application/json', 'file.json')], [nil,Faraday::UploadIO.new(metadata, 'application/json', 'file.json')],
[nil, self.media]]) [nil, self.media]])
self.headers.update(env[:request_headers]) self.headers.update(env[:request_headers])
when "resumable" when "resumable"
@ -77,13 +77,13 @@ module Google
self.headers['X-Upload-Content-Length'] = file_length.to_s self.headers['X-Upload-Content-Length'] = file_length.to_s
if options[:body_object] if options[:body_object]
self.headers['Content-Type'] ||= 'application/json' self.headers['Content-Type'] ||= 'application/json'
self.body = serialize_body(options[:body_object]) self.body = serialize_body(options[:body_object])
else else
self.body = '' self.body = ''
end end
else else
raise ArgumentError, "Invalid uploadType for media" raise ArgumentError, "Invalid uploadType for media"
end end
elsif options[:body] elsif options[:body]
self.body = options[:body] self.body = options[:body]
elsif options[:body_object] elsif options[:body_object]
@ -101,30 +101,30 @@ module Google
end end
end end
end end
def serialize_body(body) def serialize_body(body)
return body.to_json if body.respond_to?(:to_json) return body.to_json if body.respond_to?(:to_json)
return MultiJson.dump(options[:body_object].to_hash) if body.respond_to?(:to_hash) return MultiJson.dump(options[:body_object].to_hash) if body.respond_to?(:to_hash)
raise TypeError, 'Could not convert body object to JSON.' + raise TypeError, 'Could not convert body object to JSON.' +
'Must respond to :to_json or :to_hash.' 'Must respond to :to_json or :to_hash.'
end end
def media def media
return @media return @media
end end
def media=(media) def media=(media)
@media = (media) @media = (media)
end end
def authorization def authorization
return @authorization return @authorization
end end
def authorization=(new_authorization) def authorization=(new_authorization)
@authorization = new_authorization @authorization = new_authorization
end end
def connection def connection
return @connection return @connection
end end
@ -241,13 +241,14 @@ module Google
def to_request def to_request
if self.api_method if self.api_method
return self.api_method.generate_request( return self.api_method.generate_request(
self.parameters, self.body, self.headers self.parameters, self.body, self.headers,
:connection => self.connection
) )
else else
return Faraday::Request.create( return self.connection.build_request(
self.http_method.to_s.downcase.to_sym self.http_method.to_s.downcase.to_sym
) do |req| ) do |req|
req.url(Addressable::URI.parse(self.uri)) req.url(Addressable::URI.parse(self.uri).normalize.to_s)
req.headers = Faraday::Utils::Headers.new(self.headers) req.headers = Faraday::Utils::Headers.new(self.headers)
req.body = self.body req.body = self.body
end end

View File

@ -207,14 +207,14 @@ describe Google::APIClient::BatchRequest do
@call1 = { @call1 = {
:api_method => @calendar.events.insert, :api_method => @calendar.events.insert,
:parameters => {'calendarId' => 'myemail@mydomain.tld'}, :parameters => {'calendarId' => 'myemail@mydomain.tld'},
:body => JSON.dump(event1), :body => MultiJson.dump(event1),
:headers => {'Content-Type' => 'application/json'} :headers => {'Content-Type' => 'application/json'}
} }
@call2 = { @call2 = {
:api_method => @calendar.events.insert, :api_method => @calendar.events.insert,
:parameters => {'calendarId' => 'myemail@mydomain.tld'}, :parameters => {'calendarId' => 'myemail@mydomain.tld'},
:body => JSON.dump(event2), :body => MultiJson.dump(event2),
:headers => {'Content-Type' => 'application/json'} :headers => {'Content-Type' => 'application/json'}
} }
end end

View File

@ -90,7 +90,7 @@ describe Google::APIClient do
:uri => @client.discovery_uri('prediction', 'v1.2'), :uri => @client.discovery_uri('prediction', 'v1.2'),
:authenticated => false :authenticated => false
) )
request.to_env(Faraday.default_connection)[:url].should === ( request.to_env(Faraday.default_connection)[:url].to_s.should === (
'https://www.googleapis.com/discovery/v1/apis/prediction/v1.2/rest' + 'https://www.googleapis.com/discovery/v1/apis/prediction/v1.2/rest' +
'?userIp=127.0.0.1' '?userIp=127.0.0.1'
) )
@ -103,7 +103,7 @@ describe Google::APIClient do
:uri => @client.discovery_uri('prediction', 'v1.2'), :uri => @client.discovery_uri('prediction', 'v1.2'),
:authenticated => false :authenticated => false
) )
request.to_env(Faraday.default_connection)[:url].should === ( request.to_env(Faraday.default_connection)[:url].to_s.should === (
'https://www.googleapis.com/discovery/v1/apis/prediction/v1.2/rest' + 'https://www.googleapis.com/discovery/v1/apis/prediction/v1.2/rest' +
'?key=qwerty' '?key=qwerty'
) )
@ -117,7 +117,9 @@ describe Google::APIClient do
:uri => @client.discovery_uri('prediction', 'v1.2'), :uri => @client.discovery_uri('prediction', 'v1.2'),
:authenticated => false :authenticated => false
) )
request.to_env(Faraday.default_connection)[:url].query_values.should == { Addressable::URI.parse(
request.to_env(Faraday.default_connection)[:url]
).query_values.should == {
'key' => 'qwerty', 'key' => 'qwerty',
'userIp' => '127.0.0.1' 'userIp' => '127.0.0.1'
} }
@ -178,10 +180,10 @@ describe Google::APIClient do
it 'should generate valid requests' do it 'should generate valid requests' do
request = @client.generate_request( request = @client.generate_request(
:api_method => @prediction.training.insert, :api_method => @prediction.training.insert,
:parameters => {'data' => '12345', } :parameters => {'data' => '12345'}
) )
request.method.should == :post request.method.should == :post
request.to_env(Faraday.default_connection)[:url].should === request.to_env(Faraday.default_connection)[:url].to_s.should ===
'https://www.googleapis.com/prediction/v1.2/training?data=12345' 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
request.headers.should be_empty request.headers.should be_empty
request.body.should == '' request.body.should == ''
@ -190,10 +192,10 @@ describe Google::APIClient do
it 'should generate valid requests when repeated parameters are passed' do it 'should generate valid requests when repeated parameters are passed' do
request = @client.generate_request( request = @client.generate_request(
:api_method => @prediction.training.insert, :api_method => @prediction.training.insert,
:parameters => [['data', '1'],['data','2']] :parameters => [['data', '1'], ['data','2']]
) )
request.method.should == :post request.method.should == :post
request.to_env(Faraday.default_connection)[:url].should === request.to_env(Faraday.default_connection)[:url].to_s.should ===
'https://www.googleapis.com/prediction/v1.2/training?data=1&data=2' 'https://www.googleapis.com/prediction/v1.2/training?data=1&data=2'
end end
@ -202,7 +204,7 @@ describe Google::APIClient do
:api_method => @prediction.training.insert, :api_method => @prediction.training.insert,
:parameters => {'data' => '12345'} :parameters => {'data' => '12345'}
) )
request.to_env(Faraday.default_connection)[:url].should === request.to_env(Faraday.default_connection)[:url].to_s.should ===
'https://www.googleapis.com/prediction/v1.2/training?data=12345' 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
end end
@ -211,7 +213,7 @@ describe Google::APIClient do
:api_method => @prediction.training.insert, :api_method => @prediction.training.insert,
:parameters => {'data' => '12345'} :parameters => {'data' => '12345'}
) )
request.to_env(Faraday.default_connection)[:url].should === request.to_env(Faraday.default_connection)[:url].to_s.should ===
'https://www.googleapis.com/prediction/v1.2/training?data=12345' 'https://www.googleapis.com/prediction/v1.2/training?data=12345'
end end
@ -223,7 +225,7 @@ describe Google::APIClient do
:api_method => prediction.training.insert, :api_method => prediction.training.insert,
:parameters => {'data' => '123'} :parameters => {'data' => '123'}
) )
request.to_env(Faraday.default_connection)[:url].should === ( request.to_env(Faraday.default_connection)[:url].to_s.should === (
'https://testing-domain.googleapis.com/' + 'https://testing-domain.googleapis.com/' +
'prediction/v1.2/training?data=123' 'prediction/v1.2/training?data=123'
) )
@ -352,7 +354,7 @@ describe Google::APIClient do
}, },
:authenticated => false :authenticated => false
) )
request.to_env(Faraday.default_connection)[:url].should === ( request.to_env(Faraday.default_connection)[:url].to_s.should === (
'https://www.googleapis.com/plus/v1/' + 'https://www.googleapis.com/plus/v1/' +
'people/107807692475771887386/activities/public' 'people/107807692475771887386/activities/public'
) )
@ -422,7 +424,7 @@ describe Google::APIClient do
:api_method => 'latitude.currentLocation.get', :api_method => 'latitude.currentLocation.get',
:authenticated => false :authenticated => false
) )
request.to_env(Faraday.default_connection)[:url].should === request.to_env(Faraday.default_connection)[:url].to_s.should ===
'https://www.googleapis.com/latitude/v1/currentLocation' 'https://www.googleapis.com/latitude/v1/currentLocation'
end end
@ -431,7 +433,7 @@ describe Google::APIClient do
:api_method => @latitude.current_location.get, :api_method => @latitude.current_location.get,
:authenticated => false :authenticated => false
) )
request.to_env(Faraday.default_connection)[:url].should === request.to_env(Faraday.default_connection)[:url].to_s.should ===
'https://www.googleapis.com/latitude/v1/currentLocation' 'https://www.googleapis.com/latitude/v1/currentLocation'
end end
@ -485,7 +487,7 @@ describe Google::APIClient do
:api_method => 'moderator.profiles.get', :api_method => 'moderator.profiles.get',
:authenticated => false :authenticated => false
) )
request.to_env(Faraday.default_connection)[:url].should === request.to_env(Faraday.default_connection)[:url].to_s.should ===
'https://www.googleapis.com/moderator/v1/profiles/@me' 'https://www.googleapis.com/moderator/v1/profiles/@me'
end end
@ -494,7 +496,7 @@ describe Google::APIClient do
:api_method => @moderator.profiles.get, :api_method => @moderator.profiles.get,
:authenticated => false :authenticated => false
) )
request.to_env(Faraday.default_connection)[:url].should === request.to_env(Faraday.default_connection)[:url].to_s.should ===
'https://www.googleapis.com/moderator/v1/profiles/@me' 'https://www.googleapis.com/moderator/v1/profiles/@me'
end end
@ -545,7 +547,7 @@ describe Google::APIClient do
:api_method => 'adsense.adclients.list', :api_method => 'adsense.adclients.list',
:authenticated => false :authenticated => false
) )
request.to_env(Faraday.default_connection)[:url].should === request.to_env(Faraday.default_connection)[:url].to_s.should ===
'https://www.googleapis.com/adsense/v1/adclients' 'https://www.googleapis.com/adsense/v1/adclients'
end end
@ -554,7 +556,7 @@ describe Google::APIClient do
:api_method => @adsense.adclients.list, :api_method => @adsense.adclients.list,
:authenticated => false :authenticated => false
) )
request.to_env(Faraday.default_connection)[:url].should === request.to_env(Faraday.default_connection)[:url].to_s.should ===
'https://www.googleapis.com/adsense/v1/adclients' 'https://www.googleapis.com/adsense/v1/adclients'
end end

View File

@ -28,7 +28,7 @@ namespace :gem do
s.add_runtime_dependency('addressable', '>= 2.2.3') s.add_runtime_dependency('addressable', '>= 2.2.3')
s.add_runtime_dependency('autoparse', '>= 0.3.1') s.add_runtime_dependency('autoparse', '>= 0.3.1')
s.add_runtime_dependency('faraday', '~> 0.8.1') s.add_runtime_dependency('faraday', '~> 0.8.1')
s.add_runtime_dependency('multi_json', '>= 1.3.0') s.add_runtime_dependency('multi_json', '>= 1.0.0')
s.add_runtime_dependency('extlib', '>= 0.9.15') s.add_runtime_dependency('extlib', '>= 0.9.15')
s.add_runtime_dependency('uuidtools', '>= 2.1.0') s.add_runtime_dependency('uuidtools', '>= 2.1.0')
@ -39,7 +39,7 @@ namespace :gem do
s.add_development_dependency('sinatra', '>= 1.2.0') s.add_development_dependency('sinatra', '>= 1.2.0')
s.add_development_dependency('rake', '>= 0.9.0') s.add_development_dependency('rake', '>= 0.9.0')
s.add_development_dependency('rspec', '~> 2.10.0') s.add_development_dependency('rspec', '>= 2.11.0')
s.require_path = 'lib' s.require_path = 'lib'