#160 - Add option to set encoding of response body
This commit is contained in:
1 parent
91ae01aa83
commit
fda7288859
5 files changed
+650
No files matched your search
@@ -31,6 +31,7 @@ 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/charset'
|
||||
require 'google/api_client/client_secrets'
|
||||
require 'google/api_client/railtie' if defined?(Rails)
|
||||
|
||||
@@ -75,6 +76,9 @@ module Google
|
||||
# @option options [String] :ca_file
|
||||
# Optional set of root certificates to use when validating SSL connections.
|
||||
# By default, a bundled set of trusted roots will be used.
|
||||
# @options options[Hash] :force_encoding
|
||||
# Experimental option. True if response body should be force encoded into the charset
|
||||
# specified in the Content-Type header. Mostly intended for compressed content.
|
||||
# @options options[Hash] :faraday_options
|
||||
# Pass through of options to set on the Faraday connection
|
||||
def initialize(options={})
|
||||
@@ -119,6 +123,7 @@ module Google
|
||||
@discovered_apis = {}
|
||||
ca_file = options[:ca_file] || File.expand_path('../../cacerts.pem', __FILE__)
|
||||
self.connection = Faraday.new do |faraday|
|
||||
faraday.response :charset if options[:force_encoding]
|
||||
faraday.response :gzip
|
||||
faraday.options.params_encoder = Faraday::FlatParamsEncoder
|
||||
faraday.ssl.ca_file = ca_file
|
||||
@@ -265,10 +270,12 @@ module Google
|
||||
# @param [String, Symbol] api The API name.
|
||||
# @param [String] version The desired version of the API.
|
||||
# @param [Addressable::URI] uri The URI of the discovery document.
|
||||
# @return [Google::APIClient::API] The service object.
|
||||
def register_discovery_uri(api, version, uri)
|
||||
api = api.to_s
|
||||
version = version || 'v1'
|
||||
@discovery_uris["#{api}:#{version}"] = uri
|
||||
discovered_api(api, version)
|
||||
end
|
||||
|
||||
##
|
||||
@@ -297,6 +304,7 @@ module Google
|
||||
# @param [String] version The desired version of the API.
|
||||
# @param [String, StringIO] discovery_document
|
||||
# The contents of the discovery document.
|
||||
# @return [Google::APIClient::API] The service object.
|
||||
def register_discovery_document(api, version, discovery_document)
|
||||
api = api.to_s
|
||||
version = version || 'v1'
|
||||
@@ -311,6 +319,7 @@ module Google
|
||||
end
|
||||
@discovery_documents["#{api}:#{version}"] =
|
||||
MultiJson.load(discovery_document)
|
||||
discovered_api(api, version)
|
||||
end
|
||||
|
||||
##
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
require 'faraday'
|
||||
require 'zlib'
|
||||
|
||||
module Google
|
||||
class APIClient
|
||||
class Charset < Faraday::Response::Middleware
|
||||
include Google::APIClient::Logging
|
||||
|
||||
def charset_for_content_type(type)
|
||||
if type
|
||||
m = type.match(/(?:charset|encoding)="?([a-z0-9-]+)"?/i)
|
||||
if m
|
||||
return Encoding.find(m[1])
|
||||
end
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
def adjust_encoding(env)
|
||||
charset = charset_for_content_type(env[:response_headers]['content-type'])
|
||||
if charset && env[:body].encoding != charset
|
||||
env[:body].force_encoding(charset)
|
||||
end
|
||||
end
|
||||
|
||||
def on_complete(env)
|
||||
adjust_encoding(env)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Faraday::Response.register_middleware :charset => Google::APIClient::Charset
|
||||
Reference in new issue
Block a user