faraday 1.1.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +104 -0
- data/README.md +1 -2
- data/lib/faraday.rb +57 -41
- data/lib/faraday/adapter.rb +1 -11
- data/lib/faraday/autoload.rb +1 -8
- data/lib/faraday/connection.rb +10 -3
- data/lib/faraday/dependency_loader.rb +1 -3
- data/lib/faraday/error.rb +12 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +14 -4
- data/lib/faraday/options/proxy_options.rb +4 -0
- data/lib/faraday/request/retry.rb +2 -2
- data/lib/faraday/response.rb +0 -6
- data/lib/faraday/version.rb +5 -0
- data/spec/faraday/connection_spec.rb +45 -0
- data/spec/faraday/error_spec.rb +15 -0
- data/spec/faraday/middleware_spec.rb +32 -6
- data/spec/faraday/options/proxy_options_spec.rb +7 -0
- data/spec/faraday/request/retry_spec.rb +1 -1
- data/spec/faraday/response/raise_error_spec.rb +30 -0
- data/spec/support/shared_examples/adapter.rb +2 -1
- data/spec/support/shared_examples/request_method.rb +36 -8
- metadata +106 -16
- data/lib/faraday/adapter/em_http.rb +0 -286
- data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -62
- data/lib/faraday/adapter/em_synchrony.rb +0 -150
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -69
- data/lib/faraday/adapter/excon.rb +0 -124
- data/lib/faraday/adapter/httpclient.rb +0 -152
- data/lib/faraday/adapter/net_http.rb +0 -219
- data/lib/faraday/adapter/net_http_persistent.rb +0 -91
- data/lib/faraday/adapter/patron.rb +0 -132
- data/spec/faraday/adapter/net_http_persistent_spec.rb +0 -57
@@ -1,91 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Faraday
|
4
|
-
class Adapter
|
5
|
-
# Net::HTTP::Persistent adapter.
|
6
|
-
class NetHttpPersistent < NetHttp
|
7
|
-
dependency 'net/http/persistent'
|
8
|
-
|
9
|
-
private
|
10
|
-
|
11
|
-
def net_http_connection(env)
|
12
|
-
@cached_connection ||=
|
13
|
-
if Net::HTTP::Persistent.instance_method(:initialize)
|
14
|
-
.parameters.first == %i[key name]
|
15
|
-
options = { name: 'Faraday' }
|
16
|
-
if @connection_options.key?(:pool_size)
|
17
|
-
options[:pool_size] = @connection_options[:pool_size]
|
18
|
-
end
|
19
|
-
Net::HTTP::Persistent.new(**options)
|
20
|
-
else
|
21
|
-
Net::HTTP::Persistent.new('Faraday')
|
22
|
-
end
|
23
|
-
|
24
|
-
proxy_uri = proxy_uri(env)
|
25
|
-
if @cached_connection.proxy_uri != proxy_uri
|
26
|
-
@cached_connection.proxy = proxy_uri
|
27
|
-
end
|
28
|
-
@cached_connection
|
29
|
-
end
|
30
|
-
|
31
|
-
def proxy_uri(env)
|
32
|
-
proxy_uri = nil
|
33
|
-
if (proxy = env[:request][:proxy])
|
34
|
-
proxy_uri = if proxy[:uri].is_a?(::URI::HTTP)
|
35
|
-
proxy[:uri].dup
|
36
|
-
else
|
37
|
-
::URI.parse(proxy[:uri].to_s)
|
38
|
-
end
|
39
|
-
proxy_uri.user = proxy_uri.password = nil
|
40
|
-
# awful patch for net-http-persistent 2.8
|
41
|
-
# not unescaping user/password
|
42
|
-
if proxy[:user]
|
43
|
-
(class << proxy_uri; self; end).class_eval do
|
44
|
-
define_method(:user) { proxy[:user] }
|
45
|
-
define_method(:password) { proxy[:password] }
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
proxy_uri
|
50
|
-
end
|
51
|
-
|
52
|
-
def perform_request(http, env)
|
53
|
-
http.request env[:url], create_request(env)
|
54
|
-
rescue Errno::ETIMEDOUT => e
|
55
|
-
raise Faraday::TimeoutError, e
|
56
|
-
rescue Net::HTTP::Persistent::Error => e
|
57
|
-
raise Faraday::TimeoutError, e if e.message.include? 'Timeout'
|
58
|
-
|
59
|
-
if e.message.include? 'connection refused'
|
60
|
-
raise Faraday::ConnectionFailed, e
|
61
|
-
end
|
62
|
-
|
63
|
-
raise
|
64
|
-
end
|
65
|
-
|
66
|
-
SSL_CONFIGURATIONS = {
|
67
|
-
certificate: :client_cert,
|
68
|
-
private_key: :client_key,
|
69
|
-
ca_file: :ca_file,
|
70
|
-
ssl_version: :version,
|
71
|
-
min_version: :min_version,
|
72
|
-
max_version: :max_version
|
73
|
-
}.freeze
|
74
|
-
|
75
|
-
def configure_ssl(http, ssl)
|
76
|
-
return unless ssl
|
77
|
-
|
78
|
-
http_set(http, :verify_mode, ssl_verify_mode(ssl))
|
79
|
-
http_set(http, :cert_store, ssl_cert_store(ssl))
|
80
|
-
|
81
|
-
SSL_CONFIGURATIONS
|
82
|
-
.select { |_, key| ssl[key] }
|
83
|
-
.each { |target, key| http_set(http, target, ssl[key]) }
|
84
|
-
end
|
85
|
-
|
86
|
-
def http_set(http, attr, value)
|
87
|
-
http.send("#{attr}=", value) if http.send(attr) != value
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
@@ -1,132 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Faraday
|
4
|
-
class Adapter
|
5
|
-
# Patron adapter.
|
6
|
-
class Patron < Faraday::Adapter
|
7
|
-
dependency 'patron'
|
8
|
-
|
9
|
-
def build_connection(env)
|
10
|
-
session = ::Patron::Session.new
|
11
|
-
@config_block&.call(session)
|
12
|
-
if (env[:url].scheme == 'https') && env[:ssl]
|
13
|
-
configure_ssl(session, env[:ssl])
|
14
|
-
end
|
15
|
-
|
16
|
-
if (req = env[:request])
|
17
|
-
configure_timeouts(session, req)
|
18
|
-
configure_proxy(session, req[:proxy])
|
19
|
-
end
|
20
|
-
|
21
|
-
session
|
22
|
-
end
|
23
|
-
|
24
|
-
def call(env)
|
25
|
-
super
|
26
|
-
# TODO: support streaming requests
|
27
|
-
env[:body] = env[:body].read if env[:body].respond_to? :read
|
28
|
-
|
29
|
-
response = connection(env) do |session|
|
30
|
-
begin
|
31
|
-
data = env[:body] ? env[:body].to_s : nil
|
32
|
-
session.request(env[:method], env[:url].to_s,
|
33
|
-
env[:request_headers], data: data)
|
34
|
-
rescue Errno::ECONNREFUSED, ::Patron::ConnectionFailed
|
35
|
-
raise Faraday::ConnectionFailed, $ERROR_INFO
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
if (req = env[:request]).stream_response?
|
40
|
-
warn "Streaming downloads for #{self.class.name} " \
|
41
|
-
'are not yet implemented.'
|
42
|
-
req.on_data.call(response.body, response.body.bytesize)
|
43
|
-
end
|
44
|
-
# Remove the "HTTP/1.1 200", leaving just the reason phrase
|
45
|
-
reason_phrase = response.status_line.gsub(/^.* \d{3} /, '')
|
46
|
-
|
47
|
-
save_response(env, response.status, response.body,
|
48
|
-
response.headers, reason_phrase)
|
49
|
-
|
50
|
-
@app.call env
|
51
|
-
rescue ::Patron::TimeoutError => e
|
52
|
-
if connection_timed_out_message?(e.message)
|
53
|
-
raise Faraday::ConnectionFailed, e
|
54
|
-
end
|
55
|
-
|
56
|
-
raise Faraday::TimeoutError, e
|
57
|
-
rescue ::Patron::Error => e
|
58
|
-
if e.message.include?('code 407')
|
59
|
-
raise Faraday::ConnectionFailed,
|
60
|
-
%(407 "Proxy Authentication Required ")
|
61
|
-
end
|
62
|
-
|
63
|
-
raise Faraday::ConnectionFailed, e
|
64
|
-
end
|
65
|
-
|
66
|
-
if loaded? && defined?(::Patron::Request::VALID_ACTIONS)
|
67
|
-
# HAX: helps but doesn't work completely
|
68
|
-
# https://github.com/toland/patron/issues/34
|
69
|
-
::Patron::Request::VALID_ACTIONS.tap do |actions|
|
70
|
-
if actions[0].is_a?(Symbol)
|
71
|
-
actions << :patch unless actions.include? :patch
|
72
|
-
actions << :options unless actions.include? :options
|
73
|
-
else
|
74
|
-
# Patron 0.4.20 and up
|
75
|
-
actions << 'PATCH' unless actions.include? 'PATCH'
|
76
|
-
actions << 'OPTIONS' unless actions.include? 'OPTIONS'
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
def configure_ssl(session, ssl)
|
82
|
-
if ssl.fetch(:verify, true)
|
83
|
-
session.cacert = ssl[:ca_file]
|
84
|
-
else
|
85
|
-
session.insecure = true
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
def configure_timeouts(session, req)
|
90
|
-
return unless req
|
91
|
-
|
92
|
-
if (sec = request_timeout(:read, req))
|
93
|
-
session.timeout = sec
|
94
|
-
end
|
95
|
-
|
96
|
-
return unless (sec = request_timeout(:open, req))
|
97
|
-
|
98
|
-
session.connect_timeout = sec
|
99
|
-
end
|
100
|
-
|
101
|
-
def configure_proxy(session, proxy)
|
102
|
-
return unless proxy
|
103
|
-
|
104
|
-
proxy_uri = proxy[:uri].dup
|
105
|
-
proxy_uri.user = proxy[:user] &&
|
106
|
-
Utils.escape(proxy[:user]).gsub('+', '%20')
|
107
|
-
proxy_uri.password = proxy[:password] &&
|
108
|
-
Utils.escape(proxy[:password]).gsub('+', '%20')
|
109
|
-
session.proxy = proxy_uri.to_s
|
110
|
-
end
|
111
|
-
|
112
|
-
private
|
113
|
-
|
114
|
-
CURL_TIMEOUT_MESSAGES = [
|
115
|
-
'Connection time-out',
|
116
|
-
'Connection timed out',
|
117
|
-
'Timed out before name resolve',
|
118
|
-
'server connect has timed out',
|
119
|
-
'Resolving timed out',
|
120
|
-
'name lookup timed out',
|
121
|
-
'timed out before SSL',
|
122
|
-
'connect() timed out'
|
123
|
-
].freeze
|
124
|
-
|
125
|
-
def connection_timed_out_message?(message)
|
126
|
-
CURL_TIMEOUT_MESSAGES.any? do |curl_message|
|
127
|
-
message.include?(curl_message)
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
@@ -1,57 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Faraday::Adapter::NetHttpPersistent do
|
4
|
-
features :request_body_on_query_methods, :reason_phrase_parse, :compression, :trace_method
|
5
|
-
|
6
|
-
it_behaves_like 'an adapter'
|
7
|
-
|
8
|
-
it 'allows to provide adapter specific configs' do
|
9
|
-
url = URI('https://example.com')
|
10
|
-
|
11
|
-
adapter = described_class.new do |http|
|
12
|
-
http.idle_timeout = 123
|
13
|
-
end
|
14
|
-
|
15
|
-
http = adapter.send(:connection, url: url, request: {})
|
16
|
-
adapter.send(:configure_request, http, {})
|
17
|
-
|
18
|
-
expect(http.idle_timeout).to eq(123)
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'sets max_retries to 0' do
|
22
|
-
url = URI('http://example.com')
|
23
|
-
|
24
|
-
adapter = described_class.new
|
25
|
-
|
26
|
-
http = adapter.send(:connection, url: url, request: {})
|
27
|
-
adapter.send(:configure_request, http, {})
|
28
|
-
|
29
|
-
# `max_retries=` is only present in Ruby 2.5
|
30
|
-
expect(http.max_retries).to eq(0) if http.respond_to?(:max_retries=)
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'allows to set pool_size on initialize' do
|
34
|
-
url = URI('https://example.com')
|
35
|
-
|
36
|
-
adapter = described_class.new(nil, pool_size: 5)
|
37
|
-
|
38
|
-
http = adapter.send(:connection, url: url, request: {})
|
39
|
-
|
40
|
-
# `pool` is only present in net_http_persistent >= 3.0
|
41
|
-
expect(http.pool.size).to eq(5) if http.respond_to?(:pool)
|
42
|
-
end
|
43
|
-
|
44
|
-
context 'min_version' do
|
45
|
-
it 'allows to set min_version in SSL settings' do
|
46
|
-
url = URI('https://example.com')
|
47
|
-
|
48
|
-
adapter = described_class.new(nil)
|
49
|
-
|
50
|
-
http = adapter.send(:connection, url: url, request: {})
|
51
|
-
adapter.send(:configure_ssl, http, min_version: :TLS1_2)
|
52
|
-
|
53
|
-
# `min_version` is only present in net_http_persistent >= 3.1 (UNRELEASED)
|
54
|
-
expect(http.min_version).to eq(:TLS1_2) if http.respond_to?(:min_version)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|