From 794c6a90f766a992c88e8dc8a0055a18d897069e Mon Sep 17 00:00:00 2001 From: Steven Bazyl Date: Mon, 13 May 2013 14:03:49 -0700 Subject: [PATCH] Upgrade to Faraday 0.9 (fixes params encoding problems) --- Gemfile | 5 ++-- google-api-client.gemspec | 18 ++++++------ lib/google/api_client.rb | 16 +++++++++-- lib/google/api_client/discovery/method.rb | 1 + lib/google/api_client/request.rb | 12 ++++---- lib/google/api_client/version.rb | 4 +-- spec/google/api_client/batch_spec.rb | 2 +- spec/google/api_client/discovery_spec.rb | 34 +++++++---------------- spec/google/api_client/result_spec.rb | 3 +- spec/spec_helper.rb | 3 ++ tasks/gem.rake | 4 +-- 11 files changed, 53 insertions(+), 49 deletions(-) diff --git a/Gemfile b/Gemfile index fe9680f27..33d9b8e3f 100644 --- a/Gemfile +++ b/Gemfile @@ -2,11 +2,11 @@ source 'https://rubygems.org' gemspec -gem 'signet', '>= 0.4.5' +gem 'signet', '>= 0.5.0' gem 'addressable', '>= 2.3.2' gem 'uuidtools', '>= 2.1.0' gem 'autoparse', '>= 0.3.3' -gem 'faraday', '~> 0.8.4' +gem 'faraday', '>= 0.9.0.rc1' gem 'multi_json', '>= 1.0.0' gem 'extlib', '>= 0.9.15' gem 'jwt', '~> 0.1.5' @@ -23,6 +23,7 @@ group :examples do end group :test, :development do + gem 'json', '~> 1.7.7' gem 'rake', '>= 0.9.0' gem 'rspec', '>= 2.11.0' gem 'rcov', '>= 0.9.9', :platform => :mri_18 diff --git a/google-api-client.gemspec b/google-api-client.gemspec index 601b3e7fe..021f4db5c 100644 --- a/google-api-client.gemspec +++ b/google-api-client.gemspec @@ -2,16 +2,16 @@ Gem::Specification.new do |s| s.name = "google-api-client" - s.version = "0.6.3" + s.version = "0.7.0" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Bob Aman", "Steve Bazyl"] - s.date = "2013-03-26" + s.date = "2013-05-11" s.description = "The Google API Ruby Client makes it trivial to discover and access supported\nAPIs.\n" s.email = "sbazyl@google.com" s.executables = ["google-api"] s.extra_rdoc_files = ["README.md"] - s.files = ["lib/compat", "lib/compat/multi_json.rb", "lib/google", "lib/google/api_client", "lib/google/api_client/auth", "lib/google/api_client/auth/installed_app.rb", "lib/google/api_client/auth/jwt_asserter.rb", "lib/google/api_client/auth/key_utils.rb", "lib/google/api_client/auth/pkcs12.rb", "lib/google/api_client/batch.rb", "lib/google/api_client/client_secrets.rb", "lib/google/api_client/discovery", "lib/google/api_client/discovery/api.rb", "lib/google/api_client/discovery/media.rb", "lib/google/api_client/discovery/method.rb", "lib/google/api_client/discovery/resource.rb", "lib/google/api_client/discovery/schema.rb", "lib/google/api_client/discovery.rb", "lib/google/api_client/environment.rb", "lib/google/api_client/errors.rb", "lib/google/api_client/logging.rb", "lib/google/api_client/media.rb", "lib/google/api_client/push", "lib/google/api_client/push/message.rb", "lib/google/api_client/push/subscription.rb", "lib/google/api_client/push/webhook_channel.rb", "lib/google/api_client/push.rb", "lib/google/api_client/railtie.rb", "lib/google/api_client/reference.rb", "lib/google/api_client/request.rb", "lib/google/api_client/result.rb", "lib/google/api_client/service_account.rb", "lib/google/api_client/version.rb", "lib/google/api_client.rb", "lib/google/inflection.rb", "spec/fixtures", "spec/fixtures/files", "spec/fixtures/files/privatekey.p12", "spec/fixtures/files/sample.txt", "spec/fixtures/files/secret.pem", "spec/google", "spec/google/api_client", "spec/google/api_client/batch_spec.rb", "spec/google/api_client/discovery_spec.rb", "spec/google/api_client/media_spec.rb", "spec/google/api_client/push_spec.rb", "spec/google/api_client/result_spec.rb", "spec/google/api_client/service_account_spec.rb", "spec/google/api_client_spec.rb", "spec/spec_helper.rb", "tasks/gem.rake", "tasks/git.rake", "tasks/metrics.rake", "tasks/spec.rake", "tasks/wiki.rake", "tasks/yard.rake", "CHANGELOG.md", "CONTRIB.md", "Gemfile", "LICENSE", "Rakefile", "README.md", "bin/google-api"] + s.files = ["lib/compat", "lib/compat/multi_json.rb", "lib/google", "lib/google/api_client", "lib/google/api_client.rb", "lib/google/api_client/auth", "lib/google/api_client/auth/installed_app.rb", "lib/google/api_client/auth/jwt_asserter.rb", "lib/google/api_client/auth/key_utils.rb", "lib/google/api_client/auth/pkcs12.rb", "lib/google/api_client/batch.rb", "lib/google/api_client/client_secrets.rb", "lib/google/api_client/discovery", "lib/google/api_client/discovery.rb", "lib/google/api_client/discovery/api.rb", "lib/google/api_client/discovery/media.rb", "lib/google/api_client/discovery/method.rb", "lib/google/api_client/discovery/resource.rb", "lib/google/api_client/discovery/schema.rb", "lib/google/api_client/environment.rb", "lib/google/api_client/errors.rb", "lib/google/api_client/logging.rb", "lib/google/api_client/media.rb", "lib/google/api_client/railtie.rb", "lib/google/api_client/reference.rb", "lib/google/api_client/request.rb", "lib/google/api_client/result.rb", "lib/google/api_client/service_account.rb", "lib/google/api_client/version.rb", "lib/google/inflection.rb", "spec/fixtures", "spec/fixtures/files", "spec/fixtures/files/privatekey.p12", "spec/fixtures/files/sample.txt", "spec/fixtures/files/secret.pem", "spec/google", "spec/google/api_client", "spec/google/api_client/batch_spec.rb", "spec/google/api_client/discovery_spec.rb", "spec/google/api_client/media_spec.rb", "spec/google/api_client/result_spec.rb", "spec/google/api_client/service_account_spec.rb", "spec/google/api_client_spec.rb", "spec/spec_helper.rb", "tasks/gem.rake", "tasks/git.rake", "tasks/metrics.rake", "tasks/spec.rake", "tasks/wiki.rake", "tasks/yard.rake", "CHANGELOG.md", "CONTRIB.md", "Gemfile", "LICENSE", "README.md", "Rakefile", "bin/google-api"] s.homepage = "http://code.google.com/p/google-api-ruby-client/" s.rdoc_options = ["--main", "README.md"] s.require_paths = ["lib"] @@ -22,11 +22,11 @@ Gem::Specification.new do |s| s.specification_version = 3 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then - s.add_runtime_dependency(%q, [">= 0.4.4"]) + s.add_runtime_dependency(%q, [">= 0.5.0"]) s.add_runtime_dependency(%q, [">= 2.3.2"]) s.add_runtime_dependency(%q, [">= 2.1.0"]) s.add_runtime_dependency(%q, [">= 0.3.3"]) - s.add_runtime_dependency(%q, ["~> 0.8.4"]) + s.add_runtime_dependency(%q, [">= 0.9.0.rc1"]) s.add_runtime_dependency(%q, [">= 1.0.0"]) s.add_runtime_dependency(%q, [">= 0.9.15"]) s.add_runtime_dependency(%q, [">= 0.1.5"]) @@ -34,11 +34,11 @@ Gem::Specification.new do |s| s.add_development_dependency(%q, [">= 0.9.0"]) s.add_development_dependency(%q, [">= 2.11.0"]) else - s.add_dependency(%q, [">= 0.4.4"]) + s.add_dependency(%q, [">= 0.5.0"]) s.add_dependency(%q, [">= 2.3.2"]) s.add_dependency(%q, [">= 2.1.0"]) s.add_dependency(%q, [">= 0.3.3"]) - s.add_dependency(%q, ["~> 0.8.4"]) + s.add_dependency(%q, [">= 0.9.0.rc1"]) s.add_dependency(%q, [">= 1.0.0"]) s.add_dependency(%q, [">= 0.9.15"]) s.add_dependency(%q, [">= 0.1.5"]) @@ -47,11 +47,11 @@ Gem::Specification.new do |s| s.add_dependency(%q, [">= 2.11.0"]) end else - s.add_dependency(%q, [">= 0.4.4"]) + s.add_dependency(%q, [">= 0.5.0"]) s.add_dependency(%q, [">= 2.3.2"]) s.add_dependency(%q, [">= 2.1.0"]) s.add_dependency(%q, [">= 0.3.3"]) - s.add_dependency(%q, ["~> 0.8.4"]) + s.add_dependency(%q, [">= 0.9.0.rc1"]) s.add_dependency(%q, [">= 1.0.0"]) s.add_dependency(%q, [">= 0.9.15"]) s.add_dependency(%q, [">= 0.1.5"]) diff --git a/lib/google/api_client.rb b/lib/google/api_client.rb index 814c416bb..4fa598345 100644 --- a/lib/google/api_client.rb +++ b/lib/google/api_client.rb @@ -107,7 +107,12 @@ module Google @discovery_uris = {} @discovery_documents = {} @discovered_apis = {} - + + self.connection = Faraday.new do |faraday| + faraday.adapter Faraday.default_adapter + faraday.options.params_encoder = Faraday::FlatParamsEncoder + end + return self end @@ -167,6 +172,13 @@ module Google return @authorization end + + ## + # Default Faraday/HTTP connection. + # + # @return [Faraday::Connection] + attr_accessor :connection + ## # The setting that controls whether or not the api client attempts to # refresh authorization when a 401 is hit in #execute. @@ -557,7 +569,7 @@ module Google request.parameters['key'] ||= self.key unless self.key.nil? request.parameters['userIp'] ||= self.user_ip unless self.user_ip.nil? - connection = options[:connection] || Faraday.default_connection + connection = options[:connection] || self.connection request.authorization = options[:authorization] || self.authorization unless options[:authenticated] == false result = request.send(connection) diff --git a/lib/google/api_client/discovery/method.rb b/lib/google/api_client/discovery/method.rb index 76bb741d7..79d1468ff 100644 --- a/lib/google/api_client/discovery/method.rb +++ b/lib/google/api_client/discovery/method.rb @@ -187,6 +187,7 @@ module Google # @return [Addressable::URI] The URI after expansion. def generate_uri(parameters={}) parameters = self.normalize_parameters(parameters) + self.validate_parameters(parameters) template_variables = self.uri_template.variables upload_type = parameters.assoc('uploadType') || parameters.assoc('upload_type') diff --git a/lib/google/api_client/request.rb b/lib/google/api_client/request.rb index 9a08c2a58..b2ff85438 100644 --- a/lib/google/api_client/request.rb +++ b/lib/google/api_client/request.rb @@ -14,6 +14,8 @@ require 'faraday' require 'faraday/utils' +require 'faraday/options' +require 'faraday/request/multipart' require 'multi_json' require 'compat/multi_json' require 'addressable/uri' @@ -71,7 +73,7 @@ module Google # @option options [String, Symbol] :http_method # HTTP method when requesting a URI def initialize(options={}) - @parameters = Hash[options[:parameters] || {}] + @parameters = Faraday::Utils::ParamsHash[options[:parameters] || {}] @headers = Faraday::Utils::Headers.new self.headers.merge!(options[:headers]) unless options[:headers].nil? self.api_method = options[:api_method] @@ -314,10 +316,10 @@ module Google # @param [String] boundary # Boundary for separating each part of the message def build_multipart(parts, mime_type = 'multipart/related', boundary = MULTIPART_BOUNDARY) - env = { - :request_headers => {'Content-Type' => "#{mime_type};boundary=#{boundary}"}, - :request => { :boundary => boundary } - } + env = Faraday::Env.new + env.request = Faraday::RequestOptions.new + env.request.boundary = boundary + env.request_headers = {'Content-Type' => "#{mime_type};boundary=#{boundary}"} multipart = Faraday::Request::Multipart.new self.body = multipart.create_multipart(env, parts.map {|part| [nil, part]}) self.headers.update(env[:request_headers]) diff --git a/lib/google/api_client/version.rb b/lib/google/api_client/version.rb index 1f94b1da2..65ff5b671 100644 --- a/lib/google/api_client/version.rb +++ b/lib/google/api_client/version.rb @@ -21,8 +21,8 @@ if !defined?(::Google::APIClient::VERSION) class APIClient module VERSION MAJOR = 0 - MINOR = 6 - TINY = 3 + MINOR = 7 + TINY = 0 STRING = [MAJOR, MINOR, TINY].join('.') end end diff --git a/spec/google/api_client/batch_spec.rb b/spec/google/api_client/batch_spec.rb index 9573eea8e..5c0c7087e 100644 --- a/spec/google/api_client/batch_spec.rb +++ b/spec/google/api_client/batch_spec.rb @@ -234,7 +234,7 @@ describe Google::APIClient::BatchRequest do it 'should convert to a correct HTTP request' do batch = Google::APIClient::BatchRequest.new { |result| } batch.add(@call1, '1').add(@call2, '2') - request = batch.to_env(Faraday.default_connection) + request = batch.to_env(CLIENT.connection) boundary = Google::APIClient::BatchRequest::BATCH_BOUNDARY request[:method].to_s.downcase.should == 'post' request[:url].to_s.should == 'https://www.googleapis.com/batch' diff --git a/spec/google/api_client/discovery_spec.rb b/spec/google/api_client/discovery_spec.rb index b707daf1b..520bafc8e 100644 --- a/spec/google/api_client/discovery_spec.rb +++ b/spec/google/api_client/discovery_spec.rb @@ -25,22 +25,6 @@ require 'signet/oauth_1/client' require 'google/api_client' require 'google/api_client/version' -def TestHandler - def initialize(&block) - @block = block - end - - def call(env) - @block.call(env) - end -end - -def mock_connection(&block) - connection = Faraday.new do |builder| - use TestHandler block - end -end - describe Google::APIClient do include ConnectionHelpers CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT) @@ -231,16 +215,18 @@ describe Google::APIClient do conn.verify end - it 'should generate valid requests when repeated parameters are passed' do - pending("This is caused by Faraday's encoding of query parameters.") + it 'should generate valid requests when multivalued parameters are passed' do conn = stub_connection do |stub| stub.post('/prediction/v1.2/training?data=1&data=2') do |env| - env[:params]['data'].should include('1', '2') + # Test is now passing with Faraday 0.9.0.rc1, but small bug in test adapter + #params = env[:params] + params = Faraday::FlatParamsEncoder.decode(env.url.query) + params['data'].should include('1', '2') end end request = CLIENT.execute( :api_method => @prediction.training.insert, - :parameters => [['data', '1'], ['data','2']], + :parameters => {'data' => ['1', '2']}, :connection => conn ) conn.verify @@ -465,7 +451,7 @@ describe Google::APIClient do 'userId' => '107807692475771887386', 'collection' => 'bogus' }, :authenticated => false - ).to_env(Faraday.default_connection) + ).to_env(CLIENT.connection) end).should raise_error(ArgumentError) end end @@ -511,7 +497,7 @@ describe Google::APIClient do :api_method => @latitude.current_location.get, :authenticated => false ) - request.to_env(Faraday.default_connection)[:url].to_s.should === + request.to_env(CLIENT.connection)[:url].to_s.should === 'https://www.googleapis.com/latitude/v1/currentLocation' end @@ -520,7 +506,7 @@ describe Google::APIClient do :api_method => @latitude.current_location.get, :authenticated => false ) - request.to_env(Faraday.default_connection)[:url].to_s.should === + request.to_env(CLIENT.connection)[:url].to_s.should === 'https://www.googleapis.com/latitude/v1/currentLocation' end @@ -630,7 +616,7 @@ describe Google::APIClient do end it 'should succeed when validating repeated parameters in a correct call' do - pending("This is caused by Faraday's encoding of query parameters.") +# pending("This is caused by Faraday's encoding of query parameters.") conn = stub_connection do |stub| stub.get('/adsense/v1/reports?dimension=DATE&dimension=PRODUCT_CODE'+ '&endDate=2010-01-01&metric=CLICKS&metric=PAGE_VIEWS&'+ diff --git a/spec/google/api_client/result_spec.rb b/spec/google/api_client/result_spec.rb index 55a01da32..69dd5f0b8 100644 --- a/spec/google/api_client/result_spec.rb +++ b/spec/google/api_client/result_spec.rb @@ -78,11 +78,10 @@ describe Google::APIClient::Result do end it 'should escape the next page token when calling next_page' do - pending("This is caused by Faraday's encoding of query parameters.") reference = @result.next_page Hash[reference.parameters].should include('pageToken') Hash[reference.parameters]['pageToken'].should == 'NEXT+PAGE+TOKEN' - url = reference.to_env(Faraday.default_connection)[:url] + url = reference.to_env(CLIENT.connection)[:url] url.to_s.should include('pageToken=NEXT%2BPAGE%2BTOKEN') end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 133fed609..f19b250e6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,6 +4,8 @@ $LOAD_PATH.uniq! require 'rspec' require 'faraday' +Faraday::Adapter.load_middleware(:test) + module Faraday class Connection def verify @@ -22,6 +24,7 @@ module ConnectionHelpers block.call(stub) end connection = Faraday.new do |builder| + builder.options.params_encoder = Faraday::FlatParamsEncoder builder.adapter(:test, stubs) end end diff --git a/tasks/gem.rake b/tasks/gem.rake index 26ca2b645..da1d773e8 100644 --- a/tasks/gem.rake +++ b/tasks/gem.rake @@ -24,11 +24,11 @@ namespace :gem do s.rdoc_options.concat ['--main', 'README.md'] # Dependencies used in the main library - s.add_runtime_dependency('signet', '>= 0.4.4') + s.add_runtime_dependency('signet', '>= 0.5.0') s.add_runtime_dependency('addressable', '>= 2.3.2') s.add_runtime_dependency('uuidtools', '>= 2.1.0') s.add_runtime_dependency('autoparse', '>= 0.3.3') - s.add_runtime_dependency('faraday', '~> 0.8.4') + s.add_runtime_dependency('faraday', '>= 0.9.0.rc1') s.add_runtime_dependency('multi_json', '>= 1.0.0') s.add_runtime_dependency('extlib', '>= 0.9.15') s.add_runtime_dependency('jwt', '>= 0.1.5')