mirror of
https://github.com/lostisland/faraday.git
synced 2025-10-17 00:05:28 -04:00
Support adapter customization (#651)
* add initializer on Faraday::Adapter that accept and store a block with custom configuration use the `@ config_block ` block on `net_http` and `net_http_persistent` adapters added tests * fixes warning in tests patron was supporting custom block already! Small refactoring to conform to other adapters * added implementation for httpclient excon and EM adapters now all support `connection_options` in the initializer * additional test for httpclient adapter
This commit is contained in:
parent
f58f251051
commit
f994b054f9
@ -30,6 +30,12 @@ module Faraday
|
|||||||
extend Parallelism
|
extend Parallelism
|
||||||
self.supports_parallel = false
|
self.supports_parallel = false
|
||||||
|
|
||||||
|
def initialize(app = nil, opts = {}, &block)
|
||||||
|
super(app)
|
||||||
|
@connection_options = opts
|
||||||
|
@config_block = block
|
||||||
|
end
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
env.clear_body if env.needs_body?
|
env.clear_body if env.needs_body?
|
||||||
end
|
end
|
||||||
|
@ -138,7 +138,7 @@ module Faraday
|
|||||||
|
|
||||||
# TODO: reuse the connection to support pipelining
|
# TODO: reuse the connection to support pipelining
|
||||||
def perform_single_request(env)
|
def perform_single_request(env)
|
||||||
req = EventMachine::HttpRequest.new(env[:url], connection_config(env))
|
req = create_request(env)
|
||||||
req.setup_request(env[:method], request_config(env)).callback { |client|
|
req.setup_request(env[:method], request_config(env)).callback { |client|
|
||||||
status = client.response_header.status
|
status = client.response_header.status
|
||||||
reason = client.response_header.http_reason
|
reason = client.response_header.http_reason
|
||||||
@ -150,6 +150,10 @@ module Faraday
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create_request(env)
|
||||||
|
EventMachine::HttpRequest.new(env[:url], connection_config(env).merge(@connection_options))
|
||||||
|
end
|
||||||
|
|
||||||
def error_message(client)
|
def error_message(client)
|
||||||
client.error or "request failed"
|
client.error or "request failed"
|
||||||
end
|
end
|
||||||
|
@ -19,7 +19,7 @@ module Faraday
|
|||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
super
|
super
|
||||||
request = EventMachine::HttpRequest.new(Utils::URI(env[:url].to_s), connection_config(env))
|
request = create_request(env)
|
||||||
|
|
||||||
http_method = env[:method].to_s.downcase.to_sym
|
http_method = env[:method].to_s.downcase.to_sym
|
||||||
|
|
||||||
@ -87,6 +87,10 @@ module Faraday
|
|||||||
raise
|
raise
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create_request(env)
|
||||||
|
EventMachine::HttpRequest.new(Utils::URI(env[:url].to_s), connection_config(env).merge(@connection_options))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -3,11 +3,6 @@ module Faraday
|
|||||||
class Excon < Faraday::Adapter
|
class Excon < Faraday::Adapter
|
||||||
dependency 'excon'
|
dependency 'excon'
|
||||||
|
|
||||||
def initialize(app, connection_options = {})
|
|
||||||
@connection_options = connection_options
|
|
||||||
super(app)
|
|
||||||
end
|
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
super
|
super
|
||||||
|
|
||||||
@ -50,7 +45,7 @@ module Faraday
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
conn = ::Excon.new(env[:url].to_s, opts.merge(@connection_options))
|
conn = create_connection(env, opts)
|
||||||
|
|
||||||
resp = conn.request \
|
resp = conn.request \
|
||||||
:method => env[:method].to_s.upcase,
|
:method => env[:method].to_s.upcase,
|
||||||
@ -72,6 +67,10 @@ module Faraday
|
|||||||
raise Error::TimeoutError, err
|
raise Error::TimeoutError, err
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create_connection(env, opts)
|
||||||
|
::Excon.new(env[:url].to_s, opts.merge(@connection_options))
|
||||||
|
end
|
||||||
|
|
||||||
# TODO: support streaming requests
|
# TODO: support streaming requests
|
||||||
def read_body(env)
|
def read_body(env)
|
||||||
env[:body].respond_to?(:read) ? env[:body].read : env[:body]
|
env[:body].respond_to?(:read) ? env[:body].read : env[:body]
|
||||||
|
@ -29,6 +29,8 @@ module Faraday
|
|||||||
configure_ssl ssl
|
configure_ssl ssl
|
||||||
end
|
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
|
||||||
@ -95,6 +97,10 @@ module Faraday
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def configure_client
|
||||||
|
@config_block.call(client) if @config_block
|
||||||
|
end
|
||||||
|
|
||||||
def ssl_cert_store(ssl)
|
def ssl_cert_store(ssl)
|
||||||
return ssl[:cert_store] if ssl[:cert_store]
|
return ssl[:cert_store] if ssl[:cert_store]
|
||||||
# Memoize the cert store so that the same one is passed to
|
# Memoize the cert store so that the same one is passed to
|
||||||
|
@ -32,10 +32,7 @@ module Faraday
|
|||||||
super
|
super
|
||||||
with_net_http_connection(env) do |http|
|
with_net_http_connection(env) do |http|
|
||||||
configure_ssl(http, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
|
configure_ssl(http, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
|
||||||
|
configure_request(http, env[:request])
|
||||||
req = env[:request]
|
|
||||||
http.read_timeout = http.open_timeout = req[:timeout] if req[:timeout]
|
|
||||||
http.open_timeout = req[:open_timeout] if req[:open_timeout]
|
|
||||||
|
|
||||||
begin
|
begin
|
||||||
http_response = perform_request(http, env)
|
http_response = perform_request(http, env)
|
||||||
@ -109,6 +106,13 @@ module Faraday
|
|||||||
http.ssl_version = ssl[:version] if ssl[:version]
|
http.ssl_version = ssl[:version] if ssl[:version]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def configure_request(http, req)
|
||||||
|
http.read_timeout = http.open_timeout = req[:timeout] if req[:timeout]
|
||||||
|
http.open_timeout = req[:open_timeout] if req[:open_timeout]
|
||||||
|
|
||||||
|
@config_block.call(http) if @config_block
|
||||||
|
end
|
||||||
|
|
||||||
def ssl_cert_store(ssl)
|
def ssl_cert_store(ssl)
|
||||||
return ssl[:cert_store] if ssl[:cert_store]
|
return ssl[:cert_store] if ssl[:cert_store]
|
||||||
# Use the default cert store by default, i.e. system ca certs
|
# Use the default cert store by default, i.e. system ca certs
|
||||||
|
@ -7,8 +7,8 @@ module Faraday
|
|||||||
class NetHttpPersistent < NetHttp
|
class NetHttpPersistent < NetHttp
|
||||||
dependency 'net/http/persistent'
|
dependency 'net/http/persistent'
|
||||||
|
|
||||||
def with_net_http_connection(env)
|
def net_http_connection(env)
|
||||||
if proxy = env[:request][:proxy]
|
if (proxy = env[:request][:proxy])
|
||||||
proxy_uri = ::URI::HTTP === proxy[:uri] ? proxy[:uri].dup : ::URI.parse(proxy[:uri].to_s)
|
proxy_uri = ::URI::HTTP === proxy[:uri] ? proxy[:uri].dup : ::URI.parse(proxy[:uri].to_s)
|
||||||
proxy_uri.user = proxy_uri.password = nil
|
proxy_uri.user = proxy_uri.password = nil
|
||||||
# awful patch for net-http-persistent 2.8 not unescaping user/password
|
# awful patch for net-http-persistent 2.8 not unescaping user/password
|
||||||
@ -16,9 +16,10 @@ module Faraday
|
|||||||
define_method(:user) { proxy[:user] }
|
define_method(:user) { proxy[:user] }
|
||||||
define_method(:password) { proxy[:password] }
|
define_method(:password) { proxy[:password] }
|
||||||
end if proxy[:user]
|
end if proxy[:user]
|
||||||
|
return Net::HTTP::Persistent.new 'Faraday', proxy_uri
|
||||||
end
|
end
|
||||||
|
|
||||||
yield Net::HTTP::Persistent.new 'Faraday', proxy_uri
|
Net::HTTP::Persistent.new 'Faraday'
|
||||||
end
|
end
|
||||||
|
|
||||||
def perform_request(http, env)
|
def perform_request(http, env)
|
||||||
|
@ -3,11 +3,6 @@ module Faraday
|
|||||||
class Patron < Faraday::Adapter
|
class Patron < Faraday::Adapter
|
||||||
dependency 'patron'
|
dependency 'patron'
|
||||||
|
|
||||||
def initialize(app, &block)
|
|
||||||
super(app)
|
|
||||||
@block = block
|
|
||||||
end
|
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
super
|
super
|
||||||
|
|
||||||
@ -73,7 +68,7 @@ module Faraday
|
|||||||
def create_session
|
def create_session
|
||||||
session = ::Patron::Session.new
|
session = ::Patron::Session.new
|
||||||
session.insecure = true
|
session.insecure = true
|
||||||
@block.call(session) if @block
|
@config_block.call(session) if @config_block
|
||||||
session
|
session
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -16,5 +16,15 @@ module Adapters
|
|||||||
end
|
end
|
||||||
end unless jruby? and ssl_mode?
|
end unless jruby? and ssl_mode?
|
||||||
# https://github.com/eventmachine/eventmachine/issues/180
|
# https://github.com/eventmachine/eventmachine/issues/180
|
||||||
|
|
||||||
|
def test_custom_adapter_config
|
||||||
|
url = URI('https://example.com:1234')
|
||||||
|
|
||||||
|
adapter = Faraday::Adapter::EMHttp.new nil, inactivity_timeout: 20
|
||||||
|
|
||||||
|
req = adapter.create_request(url: url, request: {})
|
||||||
|
|
||||||
|
assert_equal 20, req.connopts.inactivity_timeout
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -18,5 +18,15 @@ module Adapters
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_custom_adapter_config
|
||||||
|
url = URI('https://example.com:1234')
|
||||||
|
|
||||||
|
adapter = Faraday::Adapter::EMSynchrony.new nil, inactivity_timeout: 20
|
||||||
|
|
||||||
|
req = adapter.create_request(url: url, request: {})
|
||||||
|
|
||||||
|
assert_equal 20, req.connopts.inactivity_timeout
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -16,5 +16,15 @@ module Adapters
|
|||||||
# https://github.com/geemus/excon/issues/358
|
# https://github.com/geemus/excon/issues/358
|
||||||
undef :test_connection_error if RUBY_VERSION >= '2.1.0'
|
undef :test_connection_error if RUBY_VERSION >= '2.1.0'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_custom_adapter_config
|
||||||
|
url = URI('https://example.com:1234')
|
||||||
|
|
||||||
|
adapter = Faraday::Adapter::Excon.new nil, debug_request: true
|
||||||
|
|
||||||
|
conn = adapter.create_connection({url: url}, {})
|
||||||
|
|
||||||
|
assert_equal true, conn.data[:debug_request]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -16,6 +16,19 @@ module Adapters
|
|||||||
conn = create_connection :request => { :bind => { :host => host } }
|
conn = create_connection :request => { :bind => { :host => host } }
|
||||||
assert_equal host, conn.options[:bind][:host]
|
assert_equal host, conn.options[:bind][:host]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_custom_adapter_config
|
||||||
|
adapter = Faraday::Adapter::HTTPClient.new do |client|
|
||||||
|
client.keep_alive_timeout = 20
|
||||||
|
client.ssl_config.timeout = 25
|
||||||
|
end
|
||||||
|
|
||||||
|
client = adapter.client
|
||||||
|
adapter.configure_client
|
||||||
|
|
||||||
|
assert_equal 20, client.keep_alive_timeout
|
||||||
|
assert_equal 25, client.ssl_config.timeout
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -16,5 +16,17 @@ module Adapters
|
|||||||
end if ssl_mode?
|
end if ssl_mode?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_custom_adapter_config
|
||||||
|
url = URI('https://example.com:1234')
|
||||||
|
|
||||||
|
adapter = Faraday::Adapter::NetHttpPersistent.new do |http|
|
||||||
|
http.idle_timeout = 123
|
||||||
|
end
|
||||||
|
|
||||||
|
http = adapter.net_http_connection(:url => url, :request => {})
|
||||||
|
adapter.configure_request(http, {})
|
||||||
|
|
||||||
|
assert_equal 123, http.idle_timeout
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -40,5 +40,17 @@ module Adapters
|
|||||||
assert_equal 1234, http.port
|
assert_equal 1234, http.port
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_custom_adapter_config
|
||||||
|
url = URI('https://example.com:1234')
|
||||||
|
|
||||||
|
adapter = Faraday::Adapter::NetHttp.new do |http|
|
||||||
|
http.continue_timeout = 123
|
||||||
|
end
|
||||||
|
|
||||||
|
http = adapter.net_http_connection(:url => url, :request => {})
|
||||||
|
adapter.configure_request(http, {})
|
||||||
|
|
||||||
|
assert_equal 123, http.continue_timeout
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -17,5 +17,15 @@ module Adapters
|
|||||||
undef :test_GET_ssl_fails_with_bad_cert if ssl_mode?
|
undef :test_GET_ssl_fails_with_bad_cert if ssl_mode?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_custom_adapter_config
|
||||||
|
adapter = Faraday::Adapter::Patron.new do |session|
|
||||||
|
session.max_redirects = 10
|
||||||
|
end
|
||||||
|
|
||||||
|
session = adapter.create_session
|
||||||
|
|
||||||
|
assert_equal 10, session.max_redirects
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user