Skip to content

Commit 4295b7c

Browse files
committed
BREAKING: remove compress option
This change removes the `compress` option from the client entirely. We use the default faraday adapter, which is `net/http` from stdlib. If you read [the documentation for that module][net-http-docs], you'll come across this: > `Net::HTTP` automatically adds Accept-Encoding for compression of > response bodies and automatically decompresses gzip and deflate > responses unless a Range header was sent. In other words, by default, ruby's net/http module will request gzipped content and automatically uncompress it if that's what the response contains. If instead one sets the `Accept-Encoding` header on a request that's made with `net/http`, that default behavior is ignored and we're left to deal with unpacking hte response ourselves as was done in the `FaradayMiddleware` class. In other words, whether the `compress` option is used or not, the behavior has been that we're asking for compressed responses and the option is effectively ignored. I went back to ruby 1.9.2 and the same behavior existed there. Given that, I'm confident this option has always been either ignored or redundant. [net-http-docs]: https://ruby-doc.org/stdlib-3.0.0/libdoc/net/http/rdoc/Net/HTTP.html#class-Net::HTTP-label-Compression
1 parent ea9712c commit 4295b7c

File tree

7 files changed

+3
-84
lines changed

7 files changed

+3
-84
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ v2.0.0
44
- Require minimum version of faraday 2.0
55
- Disabled request/response logging by default. Old behavior can be restored
66
by setting a new `verbose` configuration option to true
7+
- BREAKING: remove `compress` from configuration. gzip compression is now
8+
always enabled
79

810
v1.6.3
911
- Add support for sending end user ip address in request headers

lib/spark_api/configuration.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Configuration
1111
# valid configuration options
1212
VALID_OPTION_KEYS = [:api_key, :api_secret, :api_user, :endpoint,
1313
:user_agent, :version, :ssl, :ssl_verify, :oauth2_provider, :authentication_mode,
14-
:auth_endpoint, :callback, :compress, :timeout, :middleware, :dictionary_version, :request_id_chain, :user_ip_address, :verbose].freeze
14+
:auth_endpoint, :callback, :timeout, :middleware, :dictionary_version, :request_id_chain, :user_ip_address, :verbose].freeze
1515
OAUTH2_KEYS = [:authorization_uri, :access_uri, :client_id, :client_secret,
1616
# Requirements for authorization_code grant type
1717
:redirect_uri,
@@ -41,7 +41,6 @@ module Configuration
4141
DEFAULT_SSL = true
4242
DEFAULT_SSL_VERIFY = true
4343
DEFAULT_OAUTH2 = nil
44-
DEFAULT_COMPRESS = false
4544
DEFAULT_TIMEOUT = 5 # seconds
4645
DEFAULT_MIDDLEWARE = 'spark_api'
4746
DEFAULT_DICTIONARY_VERSION = nil
@@ -80,7 +79,6 @@ def reset_configuration
8079
self.ssl = DEFAULT_SSL
8180
self.ssl_verify = DEFAULT_SSL_VERIFY
8281
self.version = DEFAULT_VERSION
83-
self.compress = DEFAULT_COMPRESS
8482
self.timeout = DEFAULT_TIMEOUT
8583
self.middleware = DEFAULT_MIDDLEWARE
8684
self.dictionary_version = DEFAULT_DICTIONARY_VERSION

lib/spark_api/connection.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ module Connection
1010
HTTP_SCHEME = 'http:'
1111
HTTPS_SCHEME = 'https:'
1212
ACCEPT_ENCODING = 'Accept-Encoding'
13-
COMPRESS_ACCEPT_ENCODING = 'gzip, deflate'
1413
X_REQUEST_ID_CHAIN = 'X-Request-Id-Chain'
1514
MIME_JSON = 'application/json'
1615
MIME_RESO = 'application/json, application/xml'
@@ -27,10 +26,6 @@ def connection(force_ssl = false)
2726
opts[:url] = @endpoint.sub REG_HTTPS, HTTP_SCHEME
2827
end
2928

30-
if self.compress
31-
opts[:headers][ACCEPT_ENCODING] = COMPRESS_ACCEPT_ENCODING
32-
end
33-
3429
if request_id_chain
3530
opts[:headers][X_REQUEST_ID_CHAIN] = request_id_chain
3631
end

lib/spark_api/faraday_middleware.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ def initialize(app)
1515
# Handles pretty much all the api response parsing and error handling. All responses that
1616
# indicate a failure will raise a SparkApi::ClientError exception
1717
def on_complete(env)
18-
env[:body] = decompress_body(env)
19-
2018
body = MultiJson.decode(env[:body])
2119
if SparkApi.verbose
2220
SparkApi.logger.debug{ "Response Body: #{body.inspect}" }
@@ -82,18 +80,6 @@ def on_complete(env)
8280
env[:body] = results
8381
end
8482

85-
def decompress_body(env)
86-
encoding = env[:response_headers]['content-encoding'].to_s.downcase
87-
88-
if encoding == 'gzip'
89-
env[:body] = Zlib::GzipReader.new(StringIO.new(env[:body])).read
90-
elsif encoding == 'deflate'
91-
env[:body] = Zlib::Inflate.inflate(env[:body])
92-
end
93-
94-
env[:body]
95-
end
96-
9783
private
9884

9985
def http_method_override_request?(env)

lib/spark_api/reso_faraday_middleware.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11

22
module SparkApi
3-
43
class ResoFaradayMiddleware < FaradayMiddleware
5-
64
def on_complete(env)
7-
8-
body = decompress_body(env)
9-
105
begin
116
body = MultiJson.decode(body)
127

@@ -21,9 +16,7 @@ def on_complete(env)
2116
# some minor format verification
2217
raise e if body.strip[/\A<\?xml/].nil?
2318
end
24-
2519
end
26-
2720
end
2821

2922
Faraday::Response.register_middleware :reso_api => ResoFaradayMiddleware

spec/unit/spark_api/configuration_spec.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,6 @@
228228
expect(c.connection.headers["Accept-Encoding"]).to be_nil
229229
end
230230

231-
it "should set gzip header if compress option is set" do
232-
c = SparkApi::Client.new(:endpoint => "https://api.sparkapi.com",
233-
:compress => true)
234-
expect(c.connection.headers["Accept-Encoding"]).to eq("gzip, deflate")
235-
end
236-
237231
it "should set default timeout of 5 seconds" do
238232
c = SparkApi::Client.new(:endpoint => "https://sparkapi.com")
239233
expect(c.connection.options[:timeout]).to eq(5)

spec/unit/spark_api/faraday_middleware_spec.rb

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -97,55 +97,6 @@
9797
expect(e.errors).to eq("Some errors and stuff.")
9898
}
9999
end
100-
101-
end
102-
103-
describe "#decompress_body" do
104-
let(:middleware) do
105-
SparkApi::FaradayMiddleware.new(SparkApi)
106-
end
107-
108-
it "should leave the body along if content-encoding not set" do
109-
env = {
110-
:body => "UNCOMPRESSED",
111-
:response_headers => {}
112-
}
113-
114-
expect(middleware.decompress_body(env)).to eq("UNCOMPRESSED")
115-
end
116-
117-
it "should unzip gzipped data" do
118-
bod = "OUTPUT BODY"
119-
120-
out = StringIO.new
121-
gz = Zlib::GzipWriter.new(out)
122-
gz.write bod
123-
gz.close
124-
125-
env = {
126-
:body => out.string,
127-
:response_headers => {
128-
'content-encoding' => 'gzip'
129-
}
130-
}
131-
132-
expect(middleware.decompress_body(env)).to eq(bod)
133-
end
134-
135-
it "should inflate deflated data" do
136-
bod = "INFLATED BODY"
137-
deflated_bod = Zlib::Deflate.deflate(bod)
138-
139-
env = {
140-
:body => deflated_bod,
141-
:response_headers => {
142-
'content-encoding' => 'deflate'
143-
}
144-
}
145-
146-
expect(middleware.decompress_body(env)).to eq(bod)
147-
end
148100
end
149-
150101
end
151102

0 commit comments

Comments
 (0)