Enable gzip compression

This commit is contained in:
Steven Bazyl committed 2013-06-19 17:37:38 -07:00
1 parent e8e2350612
commit 8aa50442ab
3 files changed
+120 -3

No files matched your search

+6 -3
View File
@@ -30,6 +30,7 @@ require 'google/api_client/result'
require 'google/api_client/media'
require 'google/api_client/service_account'
require 'google/api_client/batch'
require 'google/api_client/gzip'
require 'google/api_client/railtie' if defined?(Rails::Railtie)
module Google
@@ -97,8 +98,7 @@ module Google
end
self.user_agent = options[:user_agent] || (
"#{application_string} " +
"google-api-ruby-client/#{Google::APIClient::VERSION::STRING} " +
ENV::OS_VERSION
"google-api-ruby-client/#{Google::APIClient::VERSION::STRING} #{ENV::OS_VERSION} (gzip)"
).strip
# The writer method understands a few Symbols and will generate useful
# default authentication mechanisms.
@@ -112,6 +112,7 @@ module Google
@discovered_apis = {}
ca_file = options[:ca_file] || File.expand_path('../../cacerts.pem', __FILE__)
self.connection = Faraday.new do |faraday|
faraday.response :gzip
faraday.options.params_encoder = Faraday::FlatParamsEncoder
faraday.ssl.ca_file = ca_file
faraday.ssl.verify = true
@@ -176,7 +177,6 @@ module Google
return @authorization
end
##
# Default Faraday/HTTP connection.
#
@@ -531,6 +531,8 @@ module Google
# - (TrueClass, FalseClass) :authenticated (default: true) -
# `true` if the request must be signed or somehow
# authenticated, `false` otherwise.
# - (TrueClass, FalseClass) :gzip (default: true) -
# `true` if gzip enabled, `false` otherwise.
#
# @return [Google::APIClient::Result] The result from the API, nil if batch.
#
@@ -569,6 +571,7 @@ module Google
end
request.headers['User-Agent'] ||= '' + self.user_agent unless self.user_agent.nil?
request.headers['Accept-Encoding'] ||= 'gzip' unless options[:gzip] == false
request.parameters['key'] ||= self.key unless self.key.nil?
request.parameters['userIp'] ||= self.user_ip unless self.user_ip.nil?
+28
View File
@@ -0,0 +1,28 @@
require 'faraday'
require 'zlib'
module Google
class APIClient
class Gzip < Faraday::Response::Middleware
include Google::APIClient::Logging
def on_complete(env)
encoding = env[:response_headers]['content-encoding'].to_s.downcase
case encoding
when 'gzip'
logger.debug { "Decompressing gzip encoded response (#{env[:body].length} bytes)" }
env[:body] = Zlib::GzipReader.new(StringIO.new(env[:body])).read
env[:response_headers].delete('content-encoding')
logger.debug { "Decompressed (#{env[:body].length} bytes)" }
when 'deflate'
logger.debug{ "Decompressing deflate encoded response (#{env[:body].length} bytes)" }
env[:body] = Zlib::Inflate.inflate(env[:body])
env[:response_headers].delete('content-encoding')
logger.debug { "Decompressed (#{env[:body].length} bytes)" }
end
end
end
end
end
Faraday::Response.register_middleware :gzip => Google::APIClient::Gzip