faraday 0.17.1 → 1.1.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 +69 -9
- data/LICENSE.md +1 -1
- data/README.md +17 -347
- data/Rakefile +1 -7
- data/examples/client_spec.rb +65 -0
- data/examples/client_test.rb +79 -0
- data/lib/faraday.rb +94 -176
- data/lib/faraday/adapter.rb +83 -22
- data/lib/faraday/adapter/em_http.rb +143 -100
- data/lib/faraday/adapter/em_http_ssl_patch.rb +24 -18
- data/lib/faraday/adapter/em_synchrony.rb +104 -60
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
- data/lib/faraday/adapter/excon.rb +98 -56
- data/lib/faraday/adapter/httpclient.rb +83 -59
- data/lib/faraday/adapter/net_http.rb +130 -63
- data/lib/faraday/adapter/net_http_persistent.rb +51 -28
- data/lib/faraday/adapter/patron.rb +80 -43
- data/lib/faraday/adapter/rack.rb +30 -13
- data/lib/faraday/adapter/test.rb +86 -53
- data/lib/faraday/adapter/typhoeus.rb +4 -1
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/autoload.rb +47 -36
- data/lib/faraday/connection.rb +312 -182
- data/lib/faraday/dependency_loader.rb +39 -0
- data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
- data/lib/faraday/error.rb +46 -25
- data/lib/faraday/file_part.rb +128 -0
- data/lib/faraday/logging/formatter.rb +105 -0
- data/lib/faraday/middleware.rb +12 -28
- data/lib/faraday/middleware_registry.rb +129 -0
- data/lib/faraday/options.rb +38 -193
- data/lib/faraday/options/connection_options.rb +22 -0
- data/lib/faraday/options/env.rb +181 -0
- data/lib/faraday/options/proxy_options.rb +28 -0
- data/lib/faraday/options/request_options.rb +22 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/param_part.rb +53 -0
- data/lib/faraday/parameters.rb +4 -197
- data/lib/faraday/rack_builder.rb +77 -65
- data/lib/faraday/request.rb +86 -44
- data/lib/faraday/request/authorization.rb +44 -30
- data/lib/faraday/request/basic_authentication.rb +14 -7
- data/lib/faraday/request/instrumentation.rb +45 -27
- data/lib/faraday/request/multipart.rb +86 -48
- data/lib/faraday/request/retry.rb +197 -171
- data/lib/faraday/request/token_authentication.rb +15 -10
- data/lib/faraday/request/url_encoded.rb +43 -23
- data/lib/faraday/response.rb +24 -14
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +49 -18
- data/lib/faraday/utils.rb +38 -247
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/em_http_spec.rb +47 -0
- data/spec/faraday/adapter/em_synchrony_spec.rb +16 -0
- data/spec/faraday/adapter/excon_spec.rb +49 -0
- data/spec/faraday/adapter/httpclient_spec.rb +73 -0
- data/spec/faraday/adapter/net_http_persistent_spec.rb +57 -0
- data/spec/faraday/adapter/net_http_spec.rb +64 -0
- data/spec/faraday/adapter/patron_spec.rb +18 -0
- data/spec/faraday/adapter/rack_spec.rb +8 -0
- data/spec/faraday/adapter/test_spec.rb +260 -0
- data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
- data/spec/faraday/adapter_registry_spec.rb +28 -0
- data/spec/faraday/adapter_spec.rb +55 -0
- data/spec/faraday/composite_read_io_spec.rb +80 -0
- data/spec/faraday/connection_spec.rb +691 -0
- data/spec/faraday/error_spec.rb +0 -57
- data/spec/faraday/middleware_spec.rb +26 -0
- data/spec/faraday/options/env_spec.rb +70 -0
- data/spec/faraday/options/options_spec.rb +297 -0
- data/spec/faraday/options/proxy_options_spec.rb +37 -0
- data/spec/faraday/options/request_options_spec.rb +19 -0
- data/spec/faraday/params_encoders/flat_spec.rb +42 -0
- data/spec/faraday/params_encoders/nested_spec.rb +142 -0
- data/spec/faraday/rack_builder_spec.rb +345 -0
- data/spec/faraday/request/authorization_spec.rb +88 -0
- data/spec/faraday/request/instrumentation_spec.rb +76 -0
- data/spec/faraday/request/multipart_spec.rb +302 -0
- data/spec/faraday/request/retry_spec.rb +242 -0
- data/spec/faraday/request/url_encoded_spec.rb +83 -0
- data/spec/faraday/request_spec.rb +120 -0
- data/spec/faraday/response/logger_spec.rb +220 -0
- data/spec/faraday/response/middleware_spec.rb +68 -0
- data/spec/faraday/response/raise_error_spec.rb +60 -16
- data/spec/faraday/response_spec.rb +75 -0
- data/spec/faraday/utils/headers_spec.rb +82 -0
- data/spec/faraday/utils_spec.rb +56 -0
- data/spec/faraday_spec.rb +37 -0
- data/spec/spec_helper.rb +63 -35
- data/spec/support/disabling_stub.rb +14 -0
- data/spec/support/fake_safe_buffer.rb +15 -0
- data/spec/support/helper_methods.rb +133 -0
- data/spec/support/shared_examples/adapter.rb +104 -0
- data/spec/support/shared_examples/params_encoder.rb +18 -0
- data/spec/support/shared_examples/request_method.rb +234 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- data/spec/support/webmock_rack_app.rb +68 -0
- metadata +80 -37
- data/lib/faraday/deprecate.rb +0 -101
- data/lib/faraday/upload_io.rb +0 -67
- data/spec/faraday/deprecate_spec.rb +0 -69
- data/test/adapters/default_test.rb +0 -14
- data/test/adapters/em_http_test.rb +0 -30
- data/test/adapters/em_synchrony_test.rb +0 -32
- data/test/adapters/excon_test.rb +0 -30
- data/test/adapters/httpclient_test.rb +0 -34
- data/test/adapters/integration.rb +0 -263
- data/test/adapters/logger_test.rb +0 -136
- data/test/adapters/net_http_persistent_test.rb +0 -114
- data/test/adapters/net_http_test.rb +0 -79
- data/test/adapters/patron_test.rb +0 -40
- data/test/adapters/rack_test.rb +0 -38
- data/test/adapters/test_middleware_test.rb +0 -157
- data/test/adapters/typhoeus_test.rb +0 -38
- data/test/authentication_middleware_test.rb +0 -65
- data/test/composite_read_io_test.rb +0 -109
- data/test/connection_test.rb +0 -738
- data/test/env_test.rb +0 -268
- data/test/helper.rb +0 -75
- data/test/live_server.rb +0 -67
- data/test/middleware/instrumentation_test.rb +0 -88
- data/test/middleware/retry_test.rb +0 -282
- data/test/middleware_stack_test.rb +0 -260
- data/test/multibyte.txt +0 -1
- data/test/options_test.rb +0 -333
- data/test/parameters_test.rb +0 -157
- data/test/request_middleware_test.rb +0 -126
- data/test/response_middleware_test.rb +0 -72
- data/test/strawberry.rb +0 -2
- data/test/utils_test.rb +0 -98
data/lib/faraday/adapter.rb
CHANGED
@@ -1,43 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Faraday
|
2
|
-
#
|
4
|
+
# Base class for all Faraday adapters. Adapters are
|
3
5
|
# responsible for fulfilling a Faraday request.
|
4
|
-
class Adapter
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
6
|
+
class Adapter
|
7
|
+
extend MiddlewareRegistry
|
8
|
+
extend DependencyLoader
|
9
|
+
|
10
|
+
CONTENT_LENGTH = 'Content-Length'
|
11
|
+
|
12
|
+
register_middleware File.expand_path('adapter', __dir__),
|
13
|
+
test: [:Test, 'test'],
|
14
|
+
net_http: [:NetHttp, 'net_http'],
|
15
|
+
net_http_persistent: [
|
16
|
+
:NetHttpPersistent,
|
17
|
+
'net_http_persistent'
|
18
|
+
],
|
19
|
+
typhoeus: [:Typhoeus, 'typhoeus'],
|
20
|
+
patron: [:Patron, 'patron'],
|
21
|
+
em_synchrony: [:EMSynchrony, 'em_synchrony'],
|
22
|
+
em_http: [:EMHttp, 'em_http'],
|
23
|
+
excon: [:Excon, 'excon'],
|
24
|
+
rack: [:Rack, 'rack'],
|
25
|
+
httpclient: [:HTTPClient, 'httpclient']
|
26
|
+
|
27
|
+
# This module marks an Adapter as supporting parallel requests.
|
20
28
|
module Parallelism
|
21
29
|
attr_writer :supports_parallel
|
22
|
-
|
30
|
+
|
31
|
+
def supports_parallel?
|
32
|
+
@supports_parallel
|
33
|
+
end
|
23
34
|
|
24
35
|
def inherited(subclass)
|
25
36
|
super
|
26
|
-
subclass.supports_parallel =
|
37
|
+
subclass.supports_parallel = supports_parallel?
|
27
38
|
end
|
28
39
|
end
|
29
40
|
|
30
41
|
extend Parallelism
|
31
42
|
self.supports_parallel = false
|
32
43
|
|
33
|
-
def initialize(
|
34
|
-
|
44
|
+
def initialize(_app = nil, opts = {}, &block)
|
45
|
+
@app = ->(env) { env.response }
|
35
46
|
@connection_options = opts
|
36
47
|
@config_block = block
|
37
48
|
end
|
38
49
|
|
50
|
+
# Yields or returns an adapter's configured connection. Depends on
|
51
|
+
# #build_connection being defined on this adapter.
|
52
|
+
#
|
53
|
+
# @param env [Faraday::Env, Hash] The env object for a faraday request.
|
54
|
+
#
|
55
|
+
# @return The return value of the given block, or the HTTP connection object
|
56
|
+
# if no block is given.
|
57
|
+
def connection(env)
|
58
|
+
conn = build_connection(env)
|
59
|
+
return conn unless block_given?
|
60
|
+
|
61
|
+
yield conn
|
62
|
+
end
|
63
|
+
|
64
|
+
# Close any persistent connections. The adapter should still be usable
|
65
|
+
# after calling close.
|
66
|
+
def close
|
67
|
+
# Possible implementation:
|
68
|
+
# @app.close if @app.respond_to?(:close)
|
69
|
+
end
|
70
|
+
|
39
71
|
def call(env)
|
40
72
|
env.clear_body if env.needs_body?
|
73
|
+
env.response = Response.new
|
41
74
|
end
|
42
75
|
|
43
76
|
private
|
@@ -45,11 +78,39 @@ module Faraday
|
|
45
78
|
def save_response(env, status, body, headers = nil, reason_phrase = nil)
|
46
79
|
env.status = status
|
47
80
|
env.body = body
|
48
|
-
env.reason_phrase = reason_phrase
|
81
|
+
env.reason_phrase = reason_phrase&.to_s&.strip
|
49
82
|
env.response_headers = Utils::Headers.new.tap do |response_headers|
|
50
83
|
response_headers.update headers unless headers.nil?
|
51
84
|
yield(response_headers) if block_given?
|
52
85
|
end
|
86
|
+
|
87
|
+
env.response.finish(env) unless env.parallel?
|
88
|
+
env.response
|
89
|
+
end
|
90
|
+
|
91
|
+
# Fetches either a read, write, or open timeout setting. Defaults to the
|
92
|
+
# :timeout value if a more specific one is not given.
|
93
|
+
#
|
94
|
+
# @param type [Symbol] Describes which timeout setting to get: :read,
|
95
|
+
# :write, or :open.
|
96
|
+
# @param options [Hash] Hash containing Symbol keys like :timeout,
|
97
|
+
# :read_timeout, :write_timeout, :open_timeout, or
|
98
|
+
# :timeout
|
99
|
+
#
|
100
|
+
# @return [Integer, nil] Timeout duration in seconds, or nil if no timeout
|
101
|
+
# has been set.
|
102
|
+
def request_timeout(type, options)
|
103
|
+
key = TIMEOUT_KEYS.fetch(type) do
|
104
|
+
msg = "Expected :read, :write, :open. Got #{type.inspect} :("
|
105
|
+
raise ArgumentError, msg
|
106
|
+
end
|
107
|
+
options[key] || options[:timeout]
|
53
108
|
end
|
109
|
+
|
110
|
+
TIMEOUT_KEYS = {
|
111
|
+
read: :read_timeout,
|
112
|
+
open: :open_timeout,
|
113
|
+
write: :write_timeout
|
114
|
+
}.freeze
|
54
115
|
end
|
55
116
|
end
|
@@ -1,10 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Faraday
|
2
4
|
class Adapter
|
3
|
-
# EventMachine adapter is useful for either asynchronous
|
4
|
-
# when in EM reactor loop or for making parallel requests in
|
5
|
+
# EventMachine adapter. This adapter is useful for either asynchronous
|
6
|
+
# requests when in an EM reactor loop, or for making parallel requests in
|
5
7
|
# synchronous code.
|
6
8
|
class EMHttp < Faraday::Adapter
|
9
|
+
# Options is a module containing helpers to convert the Faraday env object
|
10
|
+
# into options hashes for EMHTTP method calls.
|
7
11
|
module Options
|
12
|
+
# @return [Hash]
|
8
13
|
def connection_config(env)
|
9
14
|
options = {}
|
10
15
|
configure_proxy(options, env)
|
@@ -16,10 +21,10 @@ module Faraday
|
|
16
21
|
|
17
22
|
def request_config(env)
|
18
23
|
options = {
|
19
|
-
:
|
20
|
-
:
|
21
|
-
# :
|
22
|
-
# :
|
24
|
+
body: read_body(env),
|
25
|
+
head: env[:request_headers]
|
26
|
+
# keepalive: true,
|
27
|
+
# file: 'path/to/file', # stream data off disk
|
23
28
|
}
|
24
29
|
configure_compression(options, env)
|
25
30
|
options
|
@@ -30,44 +35,52 @@ module Faraday
|
|
30
35
|
body.respond_to?(:read) ? body.read : body
|
31
36
|
end
|
32
37
|
|
38
|
+
# Reads out proxy settings from env into options
|
33
39
|
def configure_proxy(options, env)
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
40
|
+
proxy = request_options(env)[:proxy]
|
41
|
+
return unless proxy
|
42
|
+
|
43
|
+
options[:proxy] = {
|
44
|
+
host: proxy[:uri].host,
|
45
|
+
port: proxy[:uri].port,
|
46
|
+
authorization: [proxy[:user], proxy[:password]]
|
47
|
+
}
|
41
48
|
end
|
42
49
|
|
50
|
+
# Reads out host and port settings from env into options
|
43
51
|
def configure_socket(options, env)
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
52
|
+
bind = request_options(env)[:bind]
|
53
|
+
return unless bind
|
54
|
+
|
55
|
+
options[:bind] = {
|
56
|
+
host: bind[:host],
|
57
|
+
port: bind[:port]
|
58
|
+
}
|
50
59
|
end
|
51
60
|
|
61
|
+
# Reads out SSL certificate settings from env into options
|
52
62
|
def configure_ssl(options, env)
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
63
|
+
return unless env[:url].scheme == 'https' && env[:ssl]
|
64
|
+
|
65
|
+
options[:ssl] = {
|
66
|
+
cert_chain_file: env[:ssl][:ca_file],
|
67
|
+
verify_peer: env[:ssl].fetch(:verify, true)
|
68
|
+
}
|
59
69
|
end
|
60
70
|
|
71
|
+
# Reads out timeout settings from env into options
|
61
72
|
def configure_timeout(options, env)
|
62
|
-
|
63
|
-
options[:
|
64
|
-
options[:connect_timeout] =
|
73
|
+
req = request_options(env)
|
74
|
+
options[:inactivity_timeout] = request_timeout(:read, req)
|
75
|
+
options[:connect_timeout] = request_timeout(:open, req)
|
65
76
|
end
|
66
77
|
|
78
|
+
# Reads out compression header settings from env into options
|
67
79
|
def configure_compression(options, env)
|
68
|
-
|
69
|
-
|
70
|
-
|
80
|
+
return unless (env[:method] == :get) &&
|
81
|
+
!options[:head].key?('accept-encoding')
|
82
|
+
|
83
|
+
options[:head]['accept-encoding'] = 'gzip, compressed'
|
71
84
|
end
|
72
85
|
|
73
86
|
def request_options(env)
|
@@ -81,7 +94,8 @@ module Faraday
|
|
81
94
|
|
82
95
|
self.supports_parallel = true
|
83
96
|
|
84
|
-
|
97
|
+
# @return [Manager]
|
98
|
+
def self.setup_parallel_manager(_options = nil)
|
85
99
|
Manager.new
|
86
100
|
end
|
87
101
|
|
@@ -94,95 +108,114 @@ module Faraday
|
|
94
108
|
def perform_request(env)
|
95
109
|
if parallel?(env)
|
96
110
|
manager = env[:parallel_manager]
|
97
|
-
manager.add
|
98
|
-
perform_single_request(env)
|
99
|
-
callback { env[:response].finish(env) }
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
error = error_message(client)
|
110
|
-
EventMachine.stop
|
111
|
-
}
|
111
|
+
manager.add do
|
112
|
+
perform_single_request(env)
|
113
|
+
.callback { env[:response].finish(env) }
|
114
|
+
end
|
115
|
+
elsif EventMachine.reactor_running?
|
116
|
+
# EM is running: instruct upstream that this is an async request
|
117
|
+
env[:parallel_manager] = true
|
118
|
+
perform_single_request(env)
|
119
|
+
.callback { env[:response].finish(env) }
|
120
|
+
.errback do
|
121
|
+
# TODO: no way to communicate the error in async mode
|
122
|
+
raise NotImplementedError
|
112
123
|
end
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
perform_single_request(env)
|
118
|
-
callback {
|
119
|
-
errback
|
120
|
-
|
121
|
-
|
122
|
-
|
124
|
+
else
|
125
|
+
error = nil
|
126
|
+
# start EM, block until request is completed
|
127
|
+
EventMachine.run do
|
128
|
+
perform_single_request(env)
|
129
|
+
.callback { EventMachine.stop }
|
130
|
+
.errback do |client|
|
131
|
+
error = error_message(client)
|
132
|
+
EventMachine.stop
|
133
|
+
end
|
123
134
|
end
|
135
|
+
raise_error(error) if error
|
124
136
|
end
|
125
|
-
rescue EventMachine::Connectify::CONNECTError =>
|
126
|
-
if
|
127
|
-
raise Faraday::ConnectionFailed,
|
128
|
-
|
129
|
-
raise Faraday::ConnectionFailed, err
|
137
|
+
rescue EventMachine::Connectify::CONNECTError => e
|
138
|
+
if e.message.include?('Proxy Authentication Required')
|
139
|
+
raise Faraday::ConnectionFailed,
|
140
|
+
%(407 "Proxy Authentication Required ")
|
130
141
|
end
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
142
|
+
|
143
|
+
raise Faraday::ConnectionFailed, e
|
144
|
+
rescue StandardError => e
|
145
|
+
if defined?(::OpenSSL::SSL::SSLError) && \
|
146
|
+
e.is_a?(::OpenSSL::SSL::SSLError)
|
147
|
+
raise Faraday::SSLError, e
|
136
148
|
end
|
149
|
+
|
150
|
+
raise
|
137
151
|
end
|
138
152
|
|
139
153
|
# TODO: reuse the connection to support pipelining
|
140
154
|
def perform_single_request(env)
|
141
155
|
req = create_request(env)
|
142
|
-
req.setup_request(env[:method], request_config(env))
|
156
|
+
req = req.setup_request(env[:method], request_config(env))
|
157
|
+
req.callback do |client|
|
158
|
+
if env[:request].stream_response?
|
159
|
+
warn "Streaming downloads for #{self.class.name} " \
|
160
|
+
'are not yet implemented.'
|
161
|
+
env[:request].on_data.call(
|
162
|
+
client.response,
|
163
|
+
client.response.bytesize
|
164
|
+
)
|
165
|
+
end
|
143
166
|
status = client.response_header.status
|
144
167
|
reason = client.response_header.http_reason
|
145
|
-
save_response(env, status, client.response, nil, reason) do |
|
168
|
+
save_response(env, status, client.response, nil, reason) do |headers|
|
146
169
|
client.response_header.each do |name, value|
|
147
|
-
|
170
|
+
headers[name.to_sym] = value
|
148
171
|
end
|
149
172
|
end
|
150
|
-
|
173
|
+
end
|
151
174
|
end
|
152
175
|
|
153
176
|
def create_request(env)
|
154
|
-
EventMachine::HttpRequest.new(
|
177
|
+
EventMachine::HttpRequest.new(
|
178
|
+
env[:url], connection_config(env).merge(@connection_options)
|
179
|
+
)
|
155
180
|
end
|
156
181
|
|
157
182
|
def error_message(client)
|
158
|
-
client.error
|
183
|
+
client.error || 'request failed'
|
159
184
|
end
|
160
185
|
|
161
186
|
def raise_error(msg)
|
162
|
-
|
163
|
-
if msg
|
164
|
-
|
165
|
-
msg =
|
187
|
+
error_class = Faraday::ClientError
|
188
|
+
if timeout_message?(msg)
|
189
|
+
error_class = Faraday::TimeoutError
|
190
|
+
msg = 'request timed out'
|
166
191
|
elsif msg == Errno::ECONNREFUSED
|
167
|
-
|
168
|
-
msg =
|
169
|
-
elsif msg ==
|
170
|
-
|
192
|
+
error_class = Faraday::ConnectionFailed
|
193
|
+
msg = 'connection refused'
|
194
|
+
elsif msg == 'connection closed by server'
|
195
|
+
error_class = Faraday::ConnectionFailed
|
171
196
|
end
|
172
|
-
raise
|
197
|
+
raise error_class, msg
|
173
198
|
end
|
174
199
|
|
200
|
+
def timeout_message?(msg)
|
201
|
+
msg == Errno::ETIMEDOUT ||
|
202
|
+
(msg.is_a?(String) && msg.include?('timeout error'))
|
203
|
+
end
|
204
|
+
|
205
|
+
# @return [Boolean]
|
175
206
|
def parallel?(env)
|
176
207
|
!!env[:parallel_manager]
|
177
208
|
end
|
178
209
|
|
179
|
-
#
|
210
|
+
# This parallel manager is designed to start an EventMachine loop
|
180
211
|
# and block until all registered requests have been completed.
|
181
212
|
class Manager
|
213
|
+
# @see reset
|
182
214
|
def initialize
|
183
215
|
reset
|
184
216
|
end
|
185
217
|
|
218
|
+
# Re-initializes instance variables
|
186
219
|
def reset
|
187
220
|
@registered_procs = []
|
188
221
|
@num_registered = 0
|
@@ -191,11 +224,14 @@ module Faraday
|
|
191
224
|
@running = false
|
192
225
|
end
|
193
226
|
|
194
|
-
|
227
|
+
# @return [Boolean]
|
228
|
+
def running?
|
229
|
+
@running
|
230
|
+
end
|
195
231
|
|
196
232
|
def add(&block)
|
197
233
|
if running?
|
198
|
-
perform_request
|
234
|
+
perform_request(&block)
|
199
235
|
else
|
200
236
|
@registered_procs << block
|
201
237
|
end
|
@@ -203,15 +239,15 @@ module Faraday
|
|
203
239
|
end
|
204
240
|
|
205
241
|
def run
|
206
|
-
if @num_registered
|
242
|
+
if @num_registered.positive?
|
207
243
|
@running = true
|
208
244
|
EventMachine.run do
|
209
245
|
@registered_procs.each do |proc|
|
210
246
|
perform_request(&proc)
|
211
247
|
end
|
212
248
|
end
|
213
|
-
|
214
|
-
raise Faraday::ClientError, @errors.first ||
|
249
|
+
unless @errors.empty?
|
250
|
+
raise Faraday::ClientError, @errors.first || 'connection failed'
|
215
251
|
end
|
216
252
|
end
|
217
253
|
ensure
|
@@ -220,24 +256,31 @@ module Faraday
|
|
220
256
|
|
221
257
|
def perform_request
|
222
258
|
client = yield
|
223
|
-
client.callback
|
224
|
-
|
259
|
+
client.callback do
|
260
|
+
@num_succeeded += 1
|
261
|
+
check_finished
|
262
|
+
end
|
263
|
+
client.errback do
|
264
|
+
@errors << client.error
|
265
|
+
check_finished
|
266
|
+
end
|
225
267
|
end
|
226
268
|
|
227
269
|
def check_finished
|
228
|
-
if @num_succeeded + @errors.size == @num_registered
|
229
|
-
EventMachine.stop
|
230
|
-
end
|
270
|
+
EventMachine.stop if @num_succeeded + @errors.size == @num_registered
|
231
271
|
end
|
232
272
|
end
|
233
273
|
end
|
234
274
|
end
|
235
275
|
end
|
236
276
|
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
277
|
+
if Faraday::Adapter::EMHttp.loaded?
|
278
|
+
begin
|
279
|
+
require 'openssl'
|
280
|
+
rescue LoadError
|
281
|
+
warn 'Warning: no such file to load -- openssl. ' \
|
282
|
+
'Make sure it is installed if you want HTTPS support'
|
283
|
+
else
|
284
|
+
require 'faraday/adapter/em_http_ssl_patch'
|
285
|
+
end
|
286
|
+
end
|