mirror of
https://github.com/lostisland/faraday.git
synced 2025-10-05 00:05:35 -04:00
Refactor net_http and net_http_persistent adapters to use #connection
This commit is contained in:
parent
b863c167d6
commit
36ff4a5c3e
@ -46,6 +46,19 @@ module Faraday
|
|||||||
@config_block = block
|
@config_block = block
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Yields an adapter's configured connection. Depends on #build_connection
|
||||||
|
# being defined on this adapter.
|
||||||
|
#
|
||||||
|
# @param env [Faraday::Env, Hash] The env object for a faraday request.
|
||||||
|
#
|
||||||
|
# @return An HTTP client connection for this adapter.
|
||||||
|
def connection(env)
|
||||||
|
conn = build_connection(env)
|
||||||
|
return conn unless block_given?
|
||||||
|
|
||||||
|
yield conn
|
||||||
|
end
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
env.clear_body if env.needs_body?
|
env.clear_body if env.needs_body?
|
||||||
env.response = Response.new
|
env.response = Response.new
|
||||||
|
@ -40,16 +40,26 @@ module Faraday
|
|||||||
super(app, opts, &block)
|
super(app, opts, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def build_connection(env)
|
||||||
|
klass = if (proxy = env[:request][:proxy])
|
||||||
|
Net::HTTP::Proxy(proxy[:uri].hostname, proxy[:uri].port,
|
||||||
|
proxy[:user], proxy[:password])
|
||||||
|
else
|
||||||
|
Net::HTTP
|
||||||
|
end
|
||||||
|
port = env[:url].port || (env[:url].scheme == 'https' ? 443 : 80)
|
||||||
|
klass.new(env[:url].hostname, port).tap do |http|
|
||||||
|
configure_ssl(http, env[:ssl])
|
||||||
|
configure_request(http, env[:request])
|
||||||
|
http.use_ssl = env[:url].scheme == 'https'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
super
|
super
|
||||||
with_net_http_connection(env) do |http|
|
http_response = connection(env) do |http|
|
||||||
if (env[:url].scheme == 'https') && env[:ssl]
|
|
||||||
configure_ssl(http, env[:ssl])
|
|
||||||
end
|
|
||||||
configure_request(http, env[:request])
|
|
||||||
|
|
||||||
begin
|
begin
|
||||||
http_response = perform_request(http, env)
|
perform_request(http, env)
|
||||||
rescue *NET_HTTP_EXCEPTIONS => e
|
rescue *NET_HTTP_EXCEPTIONS => e
|
||||||
if defined?(OpenSSL) && e.is_a?(OpenSSL::SSL::SSLError)
|
if defined?(OpenSSL) && e.is_a?(OpenSSL::SSL::SSLError)
|
||||||
raise Faraday::SSLError, e
|
raise Faraday::SSLError, e
|
||||||
@ -57,13 +67,13 @@ module Faraday
|
|||||||
|
|
||||||
raise Faraday::ConnectionFailed, e
|
raise Faraday::ConnectionFailed, e
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
save_response(env, http_response.code.to_i,
|
save_response(env, http_response.code.to_i,
|
||||||
http_response.body || '', nil,
|
http_response.body || '', nil,
|
||||||
http_response.message) do |response_headers|
|
http_response.message) do |response_headers|
|
||||||
http_response.each_header do |key, value|
|
http_response.each_header do |key, value|
|
||||||
response_headers[key] = value
|
response_headers[key] = value
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -134,23 +144,9 @@ module Faraday
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def with_net_http_connection(env)
|
|
||||||
yield net_http_connection(env)
|
|
||||||
end
|
|
||||||
|
|
||||||
def net_http_connection(env)
|
|
||||||
klass = if (proxy = env[:request][:proxy])
|
|
||||||
Net::HTTP::Proxy(proxy[:uri].hostname, proxy[:uri].port,
|
|
||||||
proxy[:user], proxy[:password])
|
|
||||||
else
|
|
||||||
Net::HTTP
|
|
||||||
end
|
|
||||||
port = env[:url].port || (env[:url].scheme == 'https' ? 443 : 80)
|
|
||||||
klass.new(env[:url].hostname, port)
|
|
||||||
end
|
|
||||||
|
|
||||||
def configure_ssl(http, ssl)
|
def configure_ssl(http, ssl)
|
||||||
http.use_ssl = true
|
return unless ssl
|
||||||
|
|
||||||
http.verify_mode = ssl_verify_mode(ssl)
|
http.verify_mode = ssl_verify_mode(ssl)
|
||||||
http.cert_store = ssl_cert_store(ssl)
|
http.cert_store = ssl_cert_store(ssl)
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ module Faraday
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def net_http_connection(env)
|
def build_connection(env)
|
||||||
@cached_connection ||=
|
@cached_connection ||=
|
||||||
if Net::HTTP::Persistent.instance_method(:initialize)
|
if Net::HTTP::Persistent.instance_method(:initialize)
|
||||||
.parameters.first == %i[key name]
|
.parameters.first == %i[key name]
|
||||||
|
@ -12,7 +12,7 @@ RSpec.describe Faraday::Adapter::NetHttpPersistent do
|
|||||||
http.idle_timeout = 123
|
http.idle_timeout = 123
|
||||||
end
|
end
|
||||||
|
|
||||||
http = adapter.send(:net_http_connection, url: url, request: {})
|
http = adapter.send(:connection, url: url, request: {})
|
||||||
adapter.send(:configure_request, http, {})
|
adapter.send(:configure_request, http, {})
|
||||||
|
|
||||||
expect(http.idle_timeout).to eq(123)
|
expect(http.idle_timeout).to eq(123)
|
||||||
@ -23,7 +23,7 @@ RSpec.describe Faraday::Adapter::NetHttpPersistent do
|
|||||||
|
|
||||||
adapter = described_class.new
|
adapter = described_class.new
|
||||||
|
|
||||||
http = adapter.send(:net_http_connection, url: url, request: {})
|
http = adapter.send(:connection, url: url, request: {})
|
||||||
adapter.send(:configure_request, http, {})
|
adapter.send(:configure_request, http, {})
|
||||||
|
|
||||||
# `max_retries=` is only present in Ruby 2.5
|
# `max_retries=` is only present in Ruby 2.5
|
||||||
@ -35,7 +35,7 @@ RSpec.describe Faraday::Adapter::NetHttpPersistent do
|
|||||||
|
|
||||||
adapter = described_class.new(nil, pool_size: 5)
|
adapter = described_class.new(nil, pool_size: 5)
|
||||||
|
|
||||||
http = adapter.send(:net_http_connection, url: url, request: {})
|
http = adapter.send(:connection, url: url, request: {})
|
||||||
|
|
||||||
# `pool` is only present in net_http_persistent >= 3.0
|
# `pool` is only present in net_http_persistent >= 3.0
|
||||||
expect(http.pool.size).to eq(5) if http.respond_to?(:pool)
|
expect(http.pool.size).to eq(5) if http.respond_to?(:pool)
|
||||||
@ -47,7 +47,7 @@ RSpec.describe Faraday::Adapter::NetHttpPersistent do
|
|||||||
|
|
||||||
adapter = described_class.new(nil)
|
adapter = described_class.new(nil)
|
||||||
|
|
||||||
http = adapter.send(:net_http_connection, url: url, request: {})
|
http = adapter.send(:connection, url: url, request: {})
|
||||||
adapter.send(:configure_ssl, http, min_version: :TLS1_2)
|
adapter.send(:configure_ssl, http, min_version: :TLS1_2)
|
||||||
|
|
||||||
# `min_version` is only present in net_http_persistent >= 3.1 (UNRELEASED)
|
# `min_version` is only present in net_http_persistent >= 3.1 (UNRELEASED)
|
||||||
|
@ -8,14 +8,16 @@ RSpec.describe Faraday::Adapter::NetHttp do
|
|||||||
context 'checking http' do
|
context 'checking http' do
|
||||||
let(:url) { URI('http://example.com') }
|
let(:url) { URI('http://example.com') }
|
||||||
let(:adapter) { described_class.new }
|
let(:adapter) { described_class.new }
|
||||||
let(:http) { adapter.send(:net_http_connection, url: url, request: {}) }
|
let(:http) { adapter.send(:connection, url: url, request: {}) }
|
||||||
|
|
||||||
it { expect(http.port).to eq(80) }
|
it { expect(http.port).to eq(80) }
|
||||||
|
|
||||||
it 'sets max_retries to 0' do
|
it 'sets max_retries to 0' do
|
||||||
adapter.send(:configure_request, http, {})
|
adapter.send(:configure_request, http, {})
|
||||||
|
|
||||||
expect(http.max_retries).to eq(0) if http.respond_to?(:max_retries=)
|
expect(http.max_retries).to eq(0) if http.respond_to?(:max_retries=)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'supports write_timeout' do
|
it 'supports write_timeout' do
|
||||||
adapter.send(:configure_request, http, write_timeout: 10)
|
adapter.send(:configure_request, http, write_timeout: 10)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user