Upgrade to Faraday 0.9 (fixes params encoding problems)

This commit is contained in:
Steven Bazyl 2013-05-13 14:03:49 -07:00
parent 9cd5601134
commit 794c6a90f7
11 changed files with 53 additions and 49 deletions

View File

@ -2,11 +2,11 @@ source 'https://rubygems.org'
gemspec gemspec
gem 'signet', '>= 0.4.5' gem 'signet', '>= 0.5.0'
gem 'addressable', '>= 2.3.2' gem 'addressable', '>= 2.3.2'
gem 'uuidtools', '>= 2.1.0' gem 'uuidtools', '>= 2.1.0'
gem 'autoparse', '>= 0.3.3' gem 'autoparse', '>= 0.3.3'
gem 'faraday', '~> 0.8.4' gem 'faraday', '>= 0.9.0.rc1'
gem 'multi_json', '>= 1.0.0' gem 'multi_json', '>= 1.0.0'
gem 'extlib', '>= 0.9.15' gem 'extlib', '>= 0.9.15'
gem 'jwt', '~> 0.1.5' gem 'jwt', '~> 0.1.5'
@ -23,6 +23,7 @@ group :examples do
end end
group :test, :development do group :test, :development do
gem 'json', '~> 1.7.7'
gem 'rake', '>= 0.9.0' gem 'rake', '>= 0.9.0'
gem 'rspec', '>= 2.11.0' gem 'rspec', '>= 2.11.0'
gem 'rcov', '>= 0.9.9', :platform => :mri_18 gem 'rcov', '>= 0.9.9', :platform => :mri_18

View File

@ -2,16 +2,16 @@
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = "google-api-client" 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.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Bob Aman", "Steve Bazyl"] 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.description = "The Google API Ruby Client makes it trivial to discover and access supported\nAPIs.\n"
s.email = "sbazyl@google.com" s.email = "sbazyl@google.com"
s.executables = ["google-api"] s.executables = ["google-api"]
s.extra_rdoc_files = ["README.md"] 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.homepage = "http://code.google.com/p/google-api-ruby-client/"
s.rdoc_options = ["--main", "README.md"] s.rdoc_options = ["--main", "README.md"]
s.require_paths = ["lib"] s.require_paths = ["lib"]
@ -22,11 +22,11 @@ Gem::Specification.new do |s|
s.specification_version = 3 s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<signet>, [">= 0.4.4"]) s.add_runtime_dependency(%q<signet>, [">= 0.5.0"])
s.add_runtime_dependency(%q<addressable>, [">= 2.3.2"]) s.add_runtime_dependency(%q<addressable>, [">= 2.3.2"])
s.add_runtime_dependency(%q<uuidtools>, [">= 2.1.0"]) s.add_runtime_dependency(%q<uuidtools>, [">= 2.1.0"])
s.add_runtime_dependency(%q<autoparse>, [">= 0.3.3"]) s.add_runtime_dependency(%q<autoparse>, [">= 0.3.3"])
s.add_runtime_dependency(%q<faraday>, ["~> 0.8.4"]) s.add_runtime_dependency(%q<faraday>, [">= 0.9.0.rc1"])
s.add_runtime_dependency(%q<multi_json>, [">= 1.0.0"]) s.add_runtime_dependency(%q<multi_json>, [">= 1.0.0"])
s.add_runtime_dependency(%q<extlib>, [">= 0.9.15"]) s.add_runtime_dependency(%q<extlib>, [">= 0.9.15"])
s.add_runtime_dependency(%q<jwt>, [">= 0.1.5"]) s.add_runtime_dependency(%q<jwt>, [">= 0.1.5"])
@ -34,11 +34,11 @@ Gem::Specification.new do |s|
s.add_development_dependency(%q<rake>, [">= 0.9.0"]) s.add_development_dependency(%q<rake>, [">= 0.9.0"])
s.add_development_dependency(%q<rspec>, [">= 2.11.0"]) s.add_development_dependency(%q<rspec>, [">= 2.11.0"])
else else
s.add_dependency(%q<signet>, [">= 0.4.4"]) s.add_dependency(%q<signet>, [">= 0.5.0"])
s.add_dependency(%q<addressable>, [">= 2.3.2"]) s.add_dependency(%q<addressable>, [">= 2.3.2"])
s.add_dependency(%q<uuidtools>, [">= 2.1.0"]) s.add_dependency(%q<uuidtools>, [">= 2.1.0"])
s.add_dependency(%q<autoparse>, [">= 0.3.3"]) s.add_dependency(%q<autoparse>, [">= 0.3.3"])
s.add_dependency(%q<faraday>, ["~> 0.8.4"]) s.add_dependency(%q<faraday>, [">= 0.9.0.rc1"])
s.add_dependency(%q<multi_json>, [">= 1.0.0"]) s.add_dependency(%q<multi_json>, [">= 1.0.0"])
s.add_dependency(%q<extlib>, [">= 0.9.15"]) s.add_dependency(%q<extlib>, [">= 0.9.15"])
s.add_dependency(%q<jwt>, [">= 0.1.5"]) s.add_dependency(%q<jwt>, [">= 0.1.5"])
@ -47,11 +47,11 @@ Gem::Specification.new do |s|
s.add_dependency(%q<rspec>, [">= 2.11.0"]) s.add_dependency(%q<rspec>, [">= 2.11.0"])
end end
else else
s.add_dependency(%q<signet>, [">= 0.4.4"]) s.add_dependency(%q<signet>, [">= 0.5.0"])
s.add_dependency(%q<addressable>, [">= 2.3.2"]) s.add_dependency(%q<addressable>, [">= 2.3.2"])
s.add_dependency(%q<uuidtools>, [">= 2.1.0"]) s.add_dependency(%q<uuidtools>, [">= 2.1.0"])
s.add_dependency(%q<autoparse>, [">= 0.3.3"]) s.add_dependency(%q<autoparse>, [">= 0.3.3"])
s.add_dependency(%q<faraday>, ["~> 0.8.4"]) s.add_dependency(%q<faraday>, [">= 0.9.0.rc1"])
s.add_dependency(%q<multi_json>, [">= 1.0.0"]) s.add_dependency(%q<multi_json>, [">= 1.0.0"])
s.add_dependency(%q<extlib>, [">= 0.9.15"]) s.add_dependency(%q<extlib>, [">= 0.9.15"])
s.add_dependency(%q<jwt>, [">= 0.1.5"]) s.add_dependency(%q<jwt>, [">= 0.1.5"])

View File

@ -108,6 +108,11 @@ module Google
@discovery_documents = {} @discovery_documents = {}
@discovered_apis = {} @discovered_apis = {}
self.connection = Faraday.new do |faraday|
faraday.adapter Faraday.default_adapter
faraday.options.params_encoder = Faraday::FlatParamsEncoder
end
return self return self
end end
@ -167,6 +172,13 @@ module Google
return @authorization return @authorization
end end
##
# Default Faraday/HTTP connection.
#
# @return [Faraday::Connection]
attr_accessor :connection
## ##
# The setting that controls whether or not the api client attempts to # The setting that controls whether or not the api client attempts to
# refresh authorization when a 401 is hit in #execute. # 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['key'] ||= self.key unless self.key.nil?
request.parameters['userIp'] ||= self.user_ip unless self.user_ip.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 request.authorization = options[:authorization] || self.authorization unless options[:authenticated] == false
result = request.send(connection) result = request.send(connection)

View File

@ -187,6 +187,7 @@ module Google
# @return [Addressable::URI] The URI after expansion. # @return [Addressable::URI] The URI after expansion.
def generate_uri(parameters={}) def generate_uri(parameters={})
parameters = self.normalize_parameters(parameters) parameters = self.normalize_parameters(parameters)
self.validate_parameters(parameters) self.validate_parameters(parameters)
template_variables = self.uri_template.variables template_variables = self.uri_template.variables
upload_type = parameters.assoc('uploadType') || parameters.assoc('upload_type') upload_type = parameters.assoc('uploadType') || parameters.assoc('upload_type')

View File

@ -14,6 +14,8 @@
require 'faraday' require 'faraday'
require 'faraday/utils' require 'faraday/utils'
require 'faraday/options'
require 'faraday/request/multipart'
require 'multi_json' require 'multi_json'
require 'compat/multi_json' require 'compat/multi_json'
require 'addressable/uri' require 'addressable/uri'
@ -71,7 +73,7 @@ module Google
# @option options [String, Symbol] :http_method # @option options [String, Symbol] :http_method
# HTTP method when requesting a URI # HTTP method when requesting a URI
def initialize(options={}) def initialize(options={})
@parameters = Hash[options[:parameters] || {}] @parameters = Faraday::Utils::ParamsHash[options[:parameters] || {}]
@headers = Faraday::Utils::Headers.new @headers = Faraday::Utils::Headers.new
self.headers.merge!(options[:headers]) unless options[:headers].nil? self.headers.merge!(options[:headers]) unless options[:headers].nil?
self.api_method = options[:api_method] self.api_method = options[:api_method]
@ -314,10 +316,10 @@ module Google
# @param [String] boundary # @param [String] boundary
# Boundary for separating each part of the message # Boundary for separating each part of the message
def build_multipart(parts, mime_type = 'multipart/related', boundary = MULTIPART_BOUNDARY) def build_multipart(parts, mime_type = 'multipart/related', boundary = MULTIPART_BOUNDARY)
env = { env = Faraday::Env.new
:request_headers => {'Content-Type' => "#{mime_type};boundary=#{boundary}"}, env.request = Faraday::RequestOptions.new
:request => { :boundary => boundary } env.request.boundary = boundary
} env.request_headers = {'Content-Type' => "#{mime_type};boundary=#{boundary}"}
multipart = Faraday::Request::Multipart.new multipart = Faraday::Request::Multipart.new
self.body = multipart.create_multipart(env, parts.map {|part| [nil, part]}) self.body = multipart.create_multipart(env, parts.map {|part| [nil, part]})
self.headers.update(env[:request_headers]) self.headers.update(env[:request_headers])

View File

@ -21,8 +21,8 @@ if !defined?(::Google::APIClient::VERSION)
class APIClient class APIClient
module VERSION module VERSION
MAJOR = 0 MAJOR = 0
MINOR = 6 MINOR = 7
TINY = 3 TINY = 0
STRING = [MAJOR, MINOR, TINY].join('.') STRING = [MAJOR, MINOR, TINY].join('.')
end end
end end

View File

@ -234,7 +234,7 @@ describe Google::APIClient::BatchRequest do
it 'should convert to a correct HTTP request' do it 'should convert to a correct HTTP request' do
batch = Google::APIClient::BatchRequest.new { |result| } batch = Google::APIClient::BatchRequest.new { |result| }
batch.add(@call1, '1').add(@call2, '2') 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 boundary = Google::APIClient::BatchRequest::BATCH_BOUNDARY
request[:method].to_s.downcase.should == 'post' request[:method].to_s.downcase.should == 'post'
request[:url].to_s.should == 'https://www.googleapis.com/batch' request[:url].to_s.should == 'https://www.googleapis.com/batch'

View File

@ -25,22 +25,6 @@ require 'signet/oauth_1/client'
require 'google/api_client' require 'google/api_client'
require 'google/api_client/version' 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 describe Google::APIClient do
include ConnectionHelpers include ConnectionHelpers
CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT) CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
@ -231,16 +215,18 @@ describe Google::APIClient do
conn.verify conn.verify
end end
it 'should generate valid requests when repeated parameters are passed' do it 'should generate valid requests when multivalued parameters are passed' do
pending("This is caused by Faraday's encoding of query parameters.")
conn = stub_connection do |stub| conn = stub_connection do |stub|
stub.post('/prediction/v1.2/training?data=1&data=2') do |env| 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
end end
request = CLIENT.execute( request = CLIENT.execute(
:api_method => @prediction.training.insert, :api_method => @prediction.training.insert,
:parameters => [['data', '1'], ['data','2']], :parameters => {'data' => ['1', '2']},
:connection => conn :connection => conn
) )
conn.verify conn.verify
@ -465,7 +451,7 @@ describe Google::APIClient do
'userId' => '107807692475771887386', 'collection' => 'bogus' 'userId' => '107807692475771887386', 'collection' => 'bogus'
}, },
:authenticated => false :authenticated => false
).to_env(Faraday.default_connection) ).to_env(CLIENT.connection)
end).should raise_error(ArgumentError) end).should raise_error(ArgumentError)
end end
end end
@ -511,7 +497,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].to_s.should === request.to_env(CLIENT.connection)[:url].to_s.should ===
'https://www.googleapis.com/latitude/v1/currentLocation' 'https://www.googleapis.com/latitude/v1/currentLocation'
end end
@ -520,7 +506,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].to_s.should === request.to_env(CLIENT.connection)[:url].to_s.should ===
'https://www.googleapis.com/latitude/v1/currentLocation' 'https://www.googleapis.com/latitude/v1/currentLocation'
end end
@ -630,7 +616,7 @@ describe Google::APIClient do
end end
it 'should succeed when validating repeated parameters in a correct call' do 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| conn = stub_connection do |stub|
stub.get('/adsense/v1/reports?dimension=DATE&dimension=PRODUCT_CODE'+ stub.get('/adsense/v1/reports?dimension=DATE&dimension=PRODUCT_CODE'+
'&endDate=2010-01-01&metric=CLICKS&metric=PAGE_VIEWS&'+ '&endDate=2010-01-01&metric=CLICKS&metric=PAGE_VIEWS&'+

View File

@ -78,11 +78,10 @@ describe Google::APIClient::Result do
end end
it 'should escape the next page token when calling next_page' do 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 reference = @result.next_page
Hash[reference.parameters].should include('pageToken') Hash[reference.parameters].should include('pageToken')
Hash[reference.parameters]['pageToken'].should == 'NEXT+PAGE+TOKEN' 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') url.to_s.should include('pageToken=NEXT%2BPAGE%2BTOKEN')
end end

View File

@ -4,6 +4,8 @@ $LOAD_PATH.uniq!
require 'rspec' require 'rspec'
require 'faraday' require 'faraday'
Faraday::Adapter.load_middleware(:test)
module Faraday module Faraday
class Connection class Connection
def verify def verify
@ -22,6 +24,7 @@ module ConnectionHelpers
block.call(stub) block.call(stub)
end end
connection = Faraday.new do |builder| connection = Faraday.new do |builder|
builder.options.params_encoder = Faraday::FlatParamsEncoder
builder.adapter(:test, stubs) builder.adapter(:test, stubs)
end end
end end

View File

@ -24,11 +24,11 @@ namespace :gem do
s.rdoc_options.concat ['--main', 'README.md'] s.rdoc_options.concat ['--main', 'README.md']
# Dependencies used in the main library # 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('addressable', '>= 2.3.2')
s.add_runtime_dependency('uuidtools', '>= 2.1.0') s.add_runtime_dependency('uuidtools', '>= 2.1.0')
s.add_runtime_dependency('autoparse', '>= 0.3.3') 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('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('jwt', '>= 0.1.5') s.add_runtime_dependency('jwt', '>= 0.1.5')