Resolving issues introduced by Faraday dependency upgrade.

This commit is contained in:
Bob Aman committed 2012-07-21 13:51:23 +03:00
1 parent 5e751681dd
commit 3bc7d52764
7 files changed
+64 -49

No files matched your search

+12 -6
View File
@@ -542,7 +542,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,
@@ -550,7 +550,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
@@ -655,8 +655,10 @@ module Google
end
end
request = Faraday::Request.create(method.to_s.downcase.to_sym) do |req|
req.url(Addressable::URI.parse(uri))
request = options[:connection].build_request(
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.body = body
end
@@ -709,6 +711,7 @@ module Google
params.size == 1
batch = params.pop
options = batch.options
options[:connection] ||= Faraday.default_connection
http_request = batch.to_http_request
request = nil
@@ -716,8 +719,10 @@ module Google
method, uri, headers, body = http_request
method = method.to_s.downcase.to_sym
faraday_request = Faraday::Request.create(method) do |req|
req.url(uri.to_s)
faraday_request = options[:connection].build_request(
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.body = body
end
@@ -755,6 +760,7 @@ module Google
options[:body] = params.shift if params.size > 0
options[:headers] = params.shift if params.size > 0
options[:client] = self
options[:connection] ||= Faraday.default_connection
reference = Google::APIClient::Reference.new(options)
request = self.generate_request(reference)
response = self.transmit(
+8 -3
View File
@@ -218,9 +218,12 @@ module Google
# The parameters to send.
# @param [String, StringIO] body The body for the HTTP 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.
def generate_request(parameters={}, body='', headers=[])
def generate_request(parameters={}, body='', headers=[], options={})
options[:connection] ||= Faraday.default_connection
if body.respond_to?(:string)
body = body.string
elsif body.respond_to?(:to_str)
@@ -234,8 +237,10 @@ module Google
method = self.http_method
uri = self.generate_uri(parameters)
headers = headers.to_a if headers.kind_of?(Hash)
return Faraday::Request.create(method.to_s.downcase.to_sym) do |req|
req.url(Addressable::URI.parse(uri))
return options[:connection].build_request(
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.body = body
end
+18 -17
View File
@@ -26,8 +26,8 @@ require 'google/api_client/discovery'
module Google
class APIClient
class Reference
MULTIPART_BOUNDARY = "-----------RubyApiMultipartPost".freeze
def initialize(options={})
# 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
@@ -46,17 +46,17 @@ module Google
self.headers = options[:headers] || {}
if options[:media]
self.media = options[:media]
upload_type = parameters['uploadType'] || parameters['upload_type']
upload_type = parameters['uploadType'] || parameters['upload_type']
case upload_type
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"
end
self.headers['Content-Type'] ||= self.media.content_type
self.body = self.media
when "multipart"
unless options[:body_object]
raise ArgumentError, "Multipart requested but no body object"
unless options[:body_object]
raise ArgumentError, "Multipart requested but no body object"
end
# 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
@@ -68,7 +68,7 @@ module Google
}
multipart = Faraday::Request::Multipart.new
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]])
self.headers.update(env[:request_headers])
when "resumable"
@@ -77,13 +77,13 @@ module Google
self.headers['X-Upload-Content-Length'] = file_length.to_s
if options[:body_object]
self.headers['Content-Type'] ||= 'application/json'
self.body = serialize_body(options[:body_object])
self.body = serialize_body(options[:body_object])
else
self.body = ''
end
else
raise ArgumentError, "Invalid uploadType for media"
end
end
elsif options[:body]
self.body = options[:body]
elsif options[:body_object]
@@ -101,30 +101,30 @@ module Google
end
end
end
def serialize_body(body)
return body.to_json if body.respond_to?(:to_json)
return MultiJson.dump(options[:body_object].to_hash) if body.respond_to?(:to_hash)
raise TypeError, 'Could not convert body object to JSON.' +
'Must respond to :to_json or :to_hash.'
end
def media
return @media
end
def media=(media)
@media = (media)
end
def authorization
return @authorization
end
def authorization=(new_authorization)
@authorization = new_authorization
end
def connection
return @connection
end
@@ -241,13 +241,14 @@ module Google
def to_request
if self.api_method
return self.api_method.generate_request(
self.parameters, self.body, self.headers
self.parameters, self.body, self.headers,
:connection => self.connection
)
else
return Faraday::Request.create(
return self.connection.build_request(
self.http_method.to_s.downcase.to_sym
) 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.body = self.body
end