Skip to content

Commit

Permalink
Resolving issues introduced by Faraday dependency upgrade.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Aman committed Jul 21, 2012
1 parent 5e75168 commit 3bc7d52
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 49 deletions.
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ gem 'addressable', '>= 2.2.3'
gem 'uuidtools', '>= 2.1.0'
gem 'autoparse', '>= 0.3.1'
gem 'faraday', '~> 0.8.1'
gem 'multi_json', '>= 1.3.0'
gem 'multi_json', '>= 1.0.0'
gem 'extlib', '>= 0.9.15'
gem 'jruby-openssl', :platforms => :jruby

group :development do
gem 'launchy'
gem 'launchy', '>= 2.0.0'
gem 'yard'
gem 'redcarpet'
end
Expand All @@ -23,6 +23,7 @@ end

group :test, :development do
gem 'rake', '>= 0.9.0'
gem 'rspec', '>= 2.11.0'
gem 'rcov', '>= 0.9.9', :platform => :mri_18
end

Expand Down
18 changes: 12 additions & 6 deletions lib/google/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -542,15 +542,15 @@ def verify_id_token!
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,
:key => self.key,
: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
Expand Down Expand Up @@ -655,8 +655,10 @@ class << headers
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
Expand Down Expand Up @@ -709,15 +711,18 @@ def execute(*params)
params.size == 1
batch = params.pop
options = batch.options
options[:connection] ||= Faraday.default_connection
http_request = batch.to_http_request
request = nil

if @authorization
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
Expand Down Expand Up @@ -755,6 +760,7 @@ def execute(*params)
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(
Expand Down
11 changes: 8 additions & 3 deletions lib/google/api_client/discovery/method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,12 @@ def generate_uri(parameters={})
# 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)
Expand All @@ -234,8 +237,10 @@ def generate_request(parameters={}, body='', headers=[])
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
Expand Down
35 changes: 18 additions & 17 deletions lib/google/api_client/reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
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
Expand All @@ -46,17 +46,17 @@ def initialize(options={})
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
Expand All @@ -68,7 +68,7 @@ def initialize(options={})
}
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"
Expand All @@ -77,13 +77,13 @@ def initialize(options={})
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]
Expand All @@ -101,30 +101,30 @@ def initialize(options={})
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
Expand Down Expand Up @@ -241,13 +241,14 @@ def uri=(new_uri)
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
Expand Down
4 changes: 2 additions & 2 deletions spec/google/api_client/batch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,14 @@
@call1 = {
:api_method => @calendar.events.insert,
:parameters => {'calendarId' => 'myemail@mydomain.tld'},
:body => JSON.dump(event1),
:body => MultiJson.dump(event1),
:headers => {'Content-Type' => 'application/json'}
}

@call2 = {
:api_method => @calendar.events.insert,
:parameters => {'calendarId' => 'myemail@mydomain.tld'},
:body => JSON.dump(event2),
:body => MultiJson.dump(event2),
:headers => {'Content-Type' => 'application/json'}
}
end
Expand Down
36 changes: 19 additions & 17 deletions spec/google/api_client/discovery_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
:uri => @client.discovery_uri('prediction', 'v1.2'),
: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' +
'?userIp=127.0.0.1'
)
Expand All @@ -103,7 +103,7 @@
:uri => @client.discovery_uri('prediction', 'v1.2'),
: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' +
'?key=qwerty'
)
Expand All @@ -117,7 +117,9 @@
:uri => @client.discovery_uri('prediction', 'v1.2'),
: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',
'userIp' => '127.0.0.1'
}
Expand Down Expand Up @@ -178,10 +180,10 @@
it 'should generate valid requests' do
request = @client.generate_request(
:api_method => @prediction.training.insert,
:parameters => {'data' => '12345', }
:parameters => {'data' => '12345'}
)
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'
request.headers.should be_empty
request.body.should == ''
Expand All @@ -190,10 +192,10 @@
it 'should generate valid requests when repeated parameters are passed' do
request = @client.generate_request(
:api_method => @prediction.training.insert,
:parameters => [['data', '1'],['data','2']]
:parameters => [['data', '1'], ['data','2']]
)
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'
end

Expand All @@ -202,7 +204,7 @@
:api_method => @prediction.training.insert,
: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'
end

Expand All @@ -211,7 +213,7 @@
:api_method => @prediction.training.insert,
: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'
end

Expand All @@ -223,7 +225,7 @@
:api_method => prediction.training.insert,
: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/' +
'prediction/v1.2/training?data=123'
)
Expand Down Expand Up @@ -352,7 +354,7 @@
},
: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/' +
'people/107807692475771887386/activities/public'
)
Expand Down Expand Up @@ -422,7 +424,7 @@
:api_method => 'latitude.currentLocation.get',
: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'
end

Expand All @@ -431,7 +433,7 @@
:api_method => @latitude.current_location.get,
: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'
end

Expand Down Expand Up @@ -485,7 +487,7 @@
:api_method => 'moderator.profiles.get',
: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'
end

Expand All @@ -494,7 +496,7 @@
:api_method => @moderator.profiles.get,
: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'
end

Expand Down Expand Up @@ -545,7 +547,7 @@
:api_method => 'adsense.adclients.list',
: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'
end

Expand All @@ -554,7 +556,7 @@
:api_method => @adsense.adclients.list,
: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'
end

Expand Down
Loading

0 comments on commit 3bc7d52

Please sign in to comment.