Skip to content

Commit fda7288

Browse files
committed
googleapis#160 - Add option to set encoding of response body
1 parent 91ae01a commit fda7288

File tree

5 files changed

+650
-0
lines changed

5 files changed

+650
-0
lines changed

lib/google/api_client.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
require 'google/api_client/service_account'
3232
require 'google/api_client/batch'
3333
require 'google/api_client/gzip'
34+
require 'google/api_client/charset'
3435
require 'google/api_client/client_secrets'
3536
require 'google/api_client/railtie' if defined?(Rails)
3637

@@ -75,6 +76,9 @@ class APIClient
7576
# @option options [String] :ca_file
7677
# Optional set of root certificates to use when validating SSL connections.
7778
# By default, a bundled set of trusted roots will be used.
79+
# @options options[Hash] :force_encoding
80+
# Experimental option. True if response body should be force encoded into the charset
81+
# specified in the Content-Type header. Mostly intended for compressed content.
7882
# @options options[Hash] :faraday_options
7983
# Pass through of options to set on the Faraday connection
8084
def initialize(options={})
@@ -119,6 +123,7 @@ def initialize(options={})
119123
@discovered_apis = {}
120124
ca_file = options[:ca_file] || File.expand_path('../../cacerts.pem', __FILE__)
121125
self.connection = Faraday.new do |faraday|
126+
faraday.response :charset if options[:force_encoding]
122127
faraday.response :gzip
123128
faraday.options.params_encoder = Faraday::FlatParamsEncoder
124129
faraday.ssl.ca_file = ca_file
@@ -265,10 +270,12 @@ def directory_uri
265270
# @param [String, Symbol] api The API name.
266271
# @param [String] version The desired version of the API.
267272
# @param [Addressable::URI] uri The URI of the discovery document.
273+
# @return [Google::APIClient::API] The service object.
268274
def register_discovery_uri(api, version, uri)
269275
api = api.to_s
270276
version = version || 'v1'
271277
@discovery_uris["#{api}:#{version}"] = uri
278+
discovered_api(api, version)
272279
end
273280

274281
##
@@ -297,6 +304,7 @@ def discovery_uri(api, version=nil)
297304
# @param [String] version The desired version of the API.
298305
# @param [String, StringIO] discovery_document
299306
# The contents of the discovery document.
307+
# @return [Google::APIClient::API] The service object.
300308
def register_discovery_document(api, version, discovery_document)
301309
api = api.to_s
302310
version = version || 'v1'
@@ -311,6 +319,7 @@ def register_discovery_document(api, version, discovery_document)
311319
end
312320
@discovery_documents["#{api}:#{version}"] =
313321
MultiJson.load(discovery_document)
322+
discovered_api(api, version)
314323
end
315324

316325
##

lib/google/api_client/charset.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'faraday'
2+
require 'zlib'
3+
4+
module Google
5+
class APIClient
6+
class Charset < Faraday::Response::Middleware
7+
include Google::APIClient::Logging
8+
9+
def charset_for_content_type(type)
10+
if type
11+
m = type.match(/(?:charset|encoding)="?([a-z0-9-]+)"?/i)
12+
if m
13+
return Encoding.find(m[1])
14+
end
15+
end
16+
nil
17+
end
18+
19+
def adjust_encoding(env)
20+
charset = charset_for_content_type(env[:response_headers]['content-type'])
21+
if charset && env[:body].encoding != charset
22+
env[:body].force_encoding(charset)
23+
end
24+
end
25+
26+
def on_complete(env)
27+
adjust_encoding(env)
28+
end
29+
end
30+
end
31+
end
32+
33+
Faraday::Response.register_middleware :charset => Google::APIClient::Charset

0 commit comments

Comments
 (0)