From 3bc7d52764c7dc6d7561e26974bdd515b9d7da98 Mon Sep 17 00:00:00 2001 From: Bob Aman Date: Sat, 21 Jul 2012 13:50:48 +0300 Subject: [PATCH] Resolving issues introduced by Faraday dependency upgrade. --- Gemfile | 5 ++-- lib/google/api_client.rb | 18 ++++++++---- lib/google/api_client/discovery/method.rb | 11 +++++-- lib/google/api_client/reference.rb | 35 +++++++++++----------- spec/google/api_client/batch_spec.rb | 4 +-- spec/google/api_client/discovery_spec.rb | 36 ++++++++++++----------- tasks/gem.rake | 4 +-- 7 files changed, 64 insertions(+), 49 deletions(-) diff --git a/Gemfile b/Gemfile index 3cb397121..11c210f86 100644 --- a/Gemfile +++ b/Gemfile @@ -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 @@ -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 diff --git a/lib/google/api_client.rb b/lib/google/api_client.rb index 399dfdaee..0db717e45 100644 --- a/lib/google/api_client.rb +++ b/lib/google/api_client.rb @@ -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( diff --git a/lib/google/api_client/discovery/method.rb b/lib/google/api_client/discovery/method.rb index 3390245c8..a774e2c87 100644 --- a/lib/google/api_client/discovery/method.rb +++ b/lib/google/api_client/discovery/method.rb @@ -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 diff --git a/lib/google/api_client/reference.rb b/lib/google/api_client/reference.rb index f95761fca..8bcc6d615 100644 --- a/lib/google/api_client/reference.rb +++ b/lib/google/api_client/reference.rb @@ -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 diff --git a/spec/google/api_client/batch_spec.rb b/spec/google/api_client/batch_spec.rb index 5d7d66e6d..b85977741 100644 --- a/spec/google/api_client/batch_spec.rb +++ b/spec/google/api_client/batch_spec.rb @@ -207,14 +207,14 @@ describe Google::APIClient::BatchRequest do @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 diff --git a/spec/google/api_client/discovery_spec.rb b/spec/google/api_client/discovery_spec.rb index c4c8bfedf..b719a56fc 100644 --- a/spec/google/api_client/discovery_spec.rb +++ b/spec/google/api_client/discovery_spec.rb @@ -90,7 +90,7 @@ describe Google::APIClient do :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' ) @@ -103,7 +103,7 @@ describe Google::APIClient do :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' ) @@ -117,7 +117,9 @@ describe Google::APIClient do :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' } @@ -178,10 +180,10 @@ describe Google::APIClient do 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 == '' @@ -190,10 +192,10 @@ describe Google::APIClient do 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 @@ -202,7 +204,7 @@ describe Google::APIClient do :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 @@ -211,7 +213,7 @@ describe Google::APIClient do :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 @@ -223,7 +225,7 @@ describe Google::APIClient do :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' ) @@ -352,7 +354,7 @@ describe Google::APIClient do }, :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' ) @@ -422,7 +424,7 @@ describe Google::APIClient do :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 @@ -431,7 +433,7 @@ describe Google::APIClient do :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 @@ -485,7 +487,7 @@ describe Google::APIClient do :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 @@ -494,7 +496,7 @@ describe Google::APIClient do :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 @@ -545,7 +547,7 @@ describe Google::APIClient do :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 @@ -554,7 +556,7 @@ describe Google::APIClient do :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 diff --git a/tasks/gem.rake b/tasks/gem.rake index 0192db8d9..af1065ef2 100644 --- a/tasks/gem.rake +++ b/tasks/gem.rake @@ -28,7 +28,7 @@ namespace :gem do s.add_runtime_dependency('addressable', '>= 2.2.3') s.add_runtime_dependency('autoparse', '>= 0.3.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('uuidtools', '>= 2.1.0') @@ -39,7 +39,7 @@ namespace :gem do s.add_development_dependency('sinatra', '>= 1.2.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'