NetHttp uses with_net_http_connection (#446)

This commit is contained in:
Ben Maraney 2015-01-04 10:19:31 +02:00
parent 13e12528c5
commit c2f325a329
3 changed files with 25 additions and 19 deletions

View File

@ -28,7 +28,7 @@ We will accept adapters that:
2. if they have features not present in included adapters.
We are pushing towards a 1.0 release, when we will have to follow [Semantic
Versioning][semver]. If your patch includes changes to break compatiblitity,
Versioning][semver]. If your patch includes changes to break compatibility,
note that so we can add it to the [Changelog][].
[semver]: http://semver.org/

View File

@ -29,26 +29,27 @@ module Faraday
def call(env)
super
http = net_http_connection(env)
configure_ssl(http, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
with_net_http_connection(env) do |http|
configure_ssl(http, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
req = env[:request]
http.read_timeout = http.open_timeout = req[:timeout] if req[:timeout]
http.open_timeout = req[:open_timeout] if req[:open_timeout]
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
http_response = perform_request(http, env)
rescue *NET_HTTP_EXCEPTIONS => err
if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
raise Faraday::SSLError, err
else
raise Error::ConnectionFailed, err
begin
http_response = perform_request(http, env)
rescue *NET_HTTP_EXCEPTIONS => err
if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
raise Faraday::SSLError, err
else
raise Error::ConnectionFailed, err
end
end
end
save_response(env, http_response.code.to_i, http_response.body || '') do |response_headers|
http_response.each_header do |key, value|
response_headers[key] = value
save_response(env, http_response.code.to_i, http_response.body || '') do |response_headers|
http_response.each_header do |key, value|
response_headers[key] = value
end
end
end
@ -82,6 +83,10 @@ module Faraday
end
end
def with_net_http_connection(env)
yield net_http_connection(env)
end
def net_http_connection(env)
if proxy = env[:request][:proxy]
Net::HTTP::Proxy(proxy[:uri].host, proxy[:uri].port, proxy[:user], proxy[:password])

View File

@ -8,7 +8,7 @@ module Faraday
class NetHttpPersistent < NetHttp
dependency 'net/http/persistent'
def net_http_connection(env)
def with_net_http_connection(env)
if proxy = env[:request][:proxy]
proxy_uri = ::URI::HTTP === proxy[:uri] ? proxy[:uri].dup : ::URI.parse(proxy[:uri].to_s)
proxy_uri.user = proxy_uri.password = nil
@ -18,7 +18,8 @@ module Faraday
define_method(:password) { proxy[:password] }
end if proxy[:user]
end
Net::HTTP::Persistent.new 'Faraday', proxy_uri
yield Net::HTTP::Persistent.new 'Faraday', proxy_uri
end
def perform_request(http, env)