Refactor httpclient adapter to use #connection

This commit is contained in:
rick olson 2019-09-19 20:44:18 -06:00
parent 86d3cf2e8b
commit bfdb66ab71
2 changed files with 45 additions and 44 deletions

View File

@ -6,51 +6,54 @@ module Faraday
class HTTPClient < Faraday::Adapter class HTTPClient < Faraday::Adapter
dependency 'httpclient' dependency 'httpclient'
# @return [HTTPClient] def build_connection(env)
def client @client ||= ::HTTPClient.new.tap do |cli|
@client ||= ::HTTPClient.new # enable compression
cli.transparent_gzip_decompression = true
end
if (req = env[:request])
if (proxy = req[:proxy])
configure_proxy @client, proxy
end
if (bind = req[:bind])
configure_socket @client, bind
end
configure_timeouts @client, req
end
if env[:url].scheme == 'https' && (ssl = env[:ssl])
configure_ssl @client, ssl
end
configure_client @client
@client
end end
def call(env) def call(env)
super super
# enable compression
client.transparent_gzip_decompression = true
if (req = env[:request])
if (proxy = req[:proxy])
configure_proxy proxy
end
if (bind = req[:bind])
configure_socket bind
end
configure_timeouts req
end
if env[:url].scheme == 'https' && (ssl = env[:ssl])
configure_ssl ssl
end
configure_client
# TODO: Don't stream yet. # TODO: Don't stream yet.
# https://github.com/nahi/httpclient/pull/90 # https://github.com/nahi/httpclient/pull/90
env[:body] = env[:body].read if env[:body].respond_to? :read env[:body] = env[:body].read if env[:body].respond_to? :read
resp = client.request env[:method], env[:url], connection(env) do |http|
resp = http.request env[:method], env[:url],
body: env[:body], body: env[:body],
header: env[:request_headers] header: env[:request_headers]
if (req = env[:request]).stream_response? if (req = env[:request]).stream_response?
warn "Streaming downloads for #{self.class.name} " \ warn "Streaming downloads for #{self.class.name} " \
'are not yet implemented.' 'are not yet implemented.'
req.on_data.call(resp.body, resp.body.bytesize) req.on_data.call(resp.body, resp.body.bytesize)
end end
save_response env, resp.status, resp.body, resp.headers, resp.reason save_response env, resp.status, resp.body, resp.headers, resp.reason
@app.call env @app.call env
end
rescue ::HTTPClient::TimeoutError, Errno::ETIMEDOUT rescue ::HTTPClient::TimeoutError, Errno::ETIMEDOUT
raise Faraday::TimeoutError, $ERROR_INFO raise Faraday::TimeoutError, $ERROR_INFO
rescue ::HTTPClient::BadResponseError => e rescue ::HTTPClient::BadResponseError => e
@ -71,7 +74,7 @@ module Faraday
end end
# @param bind [Hash] # @param bind [Hash]
def configure_socket(bind) def configure_socket(client, bind)
client.socket_local.host = bind[:host] client.socket_local.host = bind[:host]
client.socket_local.port = bind[:port] client.socket_local.port = bind[:port]
end end
@ -79,7 +82,7 @@ module Faraday
# Configure proxy URI and any user credentials. # Configure proxy URI and any user credentials.
# #
# @param proxy [Hash] # @param proxy [Hash]
def configure_proxy(proxy) def configure_proxy(client, proxy)
client.proxy = proxy[:uri] client.proxy = proxy[:uri]
return unless proxy[:user] && proxy[:password] return unless proxy[:user] && proxy[:password]
@ -87,7 +90,7 @@ module Faraday
end end
# @param ssl [Hash] # @param ssl [Hash]
def configure_ssl(ssl) def configure_ssl(client, ssl)
ssl_config = client.ssl_config ssl_config = client.ssl_config
ssl_config.verify_mode = ssl_verify_mode(ssl) ssl_config.verify_mode = ssl_verify_mode(ssl)
ssl_config.cert_store = ssl_cert_store(ssl) ssl_config.cert_store = ssl_cert_store(ssl)
@ -100,23 +103,23 @@ module Faraday
end end
# @param req [Hash] # @param req [Hash]
def configure_timeouts(req) def configure_timeouts(client, req)
configure_timeout(req) if req[:timeout] configure_timeout(client, req) if req[:timeout]
configure_open_timeout(req) if req[:open_timeout] configure_open_timeout(client, req) if req[:open_timeout]
end end
def configure_timeout(req) def configure_timeout(client, req)
client.connect_timeout = req[:timeout] client.connect_timeout = req[:timeout]
client.receive_timeout = req[:timeout] client.receive_timeout = req[:timeout]
client.send_timeout = req[:timeout] client.send_timeout = req[:timeout]
end end
def configure_open_timeout(req) def configure_open_timeout(client, req)
client.connect_timeout = req[:open_timeout] client.connect_timeout = req[:open_timeout]
client.send_timeout = req[:open_timeout] client.send_timeout = req[:open_timeout]
end end
def configure_client def configure_client(client)
@config_block&.call(client) @config_block&.call(client)
end end

View File

@ -12,9 +12,7 @@ RSpec.describe Faraday::Adapter::HTTPClient do
client.ssl_config.timeout = 25 client.ssl_config.timeout = 25
end end
client = adapter.client client = adapter.build_connection(url: URI.parse('https://example.com'))
adapter.configure_client
expect(client.keep_alive_timeout).to eq(20) expect(client.keep_alive_timeout).to eq(20)
expect(client.ssl_config.timeout).to eq(25) expect(client.ssl_config.timeout).to eq(25)
end end