faraday 1.2.0 → 1.4.2
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 +7 -1
- data/lib/faraday/adapter.rb +0 -8
- data/lib/faraday/autoload.rb +0 -5
- data/lib/faraday/connection.rb +8 -1
- data/lib/faraday/error.rb +12 -0
- data/lib/faraday/options/proxy_options.rb +4 -0
- data/lib/faraday/request/retry.rb +1 -1
- data/lib/faraday/version.rb +1 -1
- data/spec/faraday/connection_spec.rb +38 -0
- data/spec/faraday/error_spec.rb +15 -0
- data/spec/faraday/options/proxy_options_spec.rb +7 -0
- data/spec/faraday/response/raise_error_spec.rb +30 -0
- metadata +76 -14
- data/lib/faraday/adapter/em_http.rb +0 -289
- data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -62
- data/lib/faraday/adapter/em_synchrony.rb +0 -153
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -69
- data/lib/faraday/adapter/excon.rb +0 -124
- data/lib/faraday/adapter/net_http.rb +0 -220
- data/lib/faraday/adapter/net_http_persistent.rb +0 -91
- data/spec/faraday/adapter/net_http_persistent_spec.rb +0 -57
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2
|
4
|
+
version: 1.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "@technoweenie"
|
@@ -10,8 +10,78 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2021-05-22 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: faraday-em_http
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - "~>"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - "~>"
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '1.0'
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: faraday-em_synchrony
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - "~>"
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '1.0'
|
36
|
+
type: :runtime
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - "~>"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '1.0'
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: faraday-excon
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '1.1'
|
50
|
+
type: :runtime
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - "~>"
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '1.1'
|
57
|
+
- !ruby/object:Gem::Dependency
|
58
|
+
name: faraday-net_http
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - "~>"
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '1.0'
|
64
|
+
type: :runtime
|
65
|
+
prerelease: false
|
66
|
+
version_requirements: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - "~>"
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '1.0'
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: faraday-net_http_persistent
|
73
|
+
requirement: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - "~>"
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '1.1'
|
78
|
+
type: :runtime
|
79
|
+
prerelease: false
|
80
|
+
version_requirements: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - "~>"
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '1.1'
|
15
85
|
- !ruby/object:Gem::Dependency
|
16
86
|
name: multipart-post
|
17
87
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,14 +108,14 @@ dependencies:
|
|
38
108
|
requirements:
|
39
109
|
- - ">="
|
40
110
|
- !ruby/object:Gem::Version
|
41
|
-
version:
|
111
|
+
version: 0.0.4
|
42
112
|
type: :runtime
|
43
113
|
prerelease: false
|
44
114
|
version_requirements: !ruby/object:Gem::Requirement
|
45
115
|
requirements:
|
46
116
|
- - ">="
|
47
117
|
- !ruby/object:Gem::Version
|
48
|
-
version:
|
118
|
+
version: 0.0.4
|
49
119
|
description:
|
50
120
|
email: technoweenie@gmail.com
|
51
121
|
executables: []
|
@@ -60,14 +130,7 @@ files:
|
|
60
130
|
- examples/client_test.rb
|
61
131
|
- lib/faraday.rb
|
62
132
|
- lib/faraday/adapter.rb
|
63
|
-
- lib/faraday/adapter/em_http.rb
|
64
|
-
- lib/faraday/adapter/em_http_ssl_patch.rb
|
65
|
-
- lib/faraday/adapter/em_synchrony.rb
|
66
|
-
- lib/faraday/adapter/em_synchrony/parallel_manager.rb
|
67
|
-
- lib/faraday/adapter/excon.rb
|
68
133
|
- lib/faraday/adapter/httpclient.rb
|
69
|
-
- lib/faraday/adapter/net_http.rb
|
70
|
-
- lib/faraday/adapter/net_http_persistent.rb
|
71
134
|
- lib/faraday/adapter/patron.rb
|
72
135
|
- lib/faraday/adapter/rack.rb
|
73
136
|
- lib/faraday/adapter/test.rb
|
@@ -113,7 +176,6 @@ files:
|
|
113
176
|
- spec/faraday/adapter/em_synchrony_spec.rb
|
114
177
|
- spec/faraday/adapter/excon_spec.rb
|
115
178
|
- spec/faraday/adapter/httpclient_spec.rb
|
116
|
-
- spec/faraday/adapter/net_http_persistent_spec.rb
|
117
179
|
- spec/faraday/adapter/net_http_spec.rb
|
118
180
|
- spec/faraday/adapter/patron_spec.rb
|
119
181
|
- spec/faraday/adapter/rack_spec.rb
|
@@ -159,7 +221,7 @@ licenses:
|
|
159
221
|
- MIT
|
160
222
|
metadata:
|
161
223
|
homepage_uri: https://lostisland.github.io/faraday
|
162
|
-
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.2
|
224
|
+
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.4.2
|
163
225
|
source_code_uri: https://github.com/lostisland/faraday
|
164
226
|
bug_tracker_uri: https://github.com/lostisland/faraday/issues
|
165
227
|
post_install_message:
|
@@ -178,7 +240,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
178
240
|
- !ruby/object:Gem::Version
|
179
241
|
version: '0'
|
180
242
|
requirements: []
|
181
|
-
rubygems_version: 3.0.3
|
243
|
+
rubygems_version: 3.0.3.1
|
182
244
|
signing_key:
|
183
245
|
specification_version: 4
|
184
246
|
summary: HTTP/REST API client library.
|
@@ -1,289 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Faraday
|
4
|
-
class Adapter
|
5
|
-
# EventMachine adapter. This adapter is useful for either asynchronous
|
6
|
-
# requests when in an EM reactor loop, or for making parallel requests in
|
7
|
-
# synchronous code.
|
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.
|
11
|
-
module Options
|
12
|
-
# @return [Hash]
|
13
|
-
def connection_config(env)
|
14
|
-
options = {}
|
15
|
-
configure_proxy(options, env)
|
16
|
-
configure_timeout(options, env)
|
17
|
-
configure_socket(options, env)
|
18
|
-
configure_ssl(options, env)
|
19
|
-
options
|
20
|
-
end
|
21
|
-
|
22
|
-
def request_config(env)
|
23
|
-
options = {
|
24
|
-
body: read_body(env),
|
25
|
-
head: env[:request_headers]
|
26
|
-
# keepalive: true,
|
27
|
-
# file: 'path/to/file', # stream data off disk
|
28
|
-
}
|
29
|
-
configure_compression(options, env)
|
30
|
-
options
|
31
|
-
end
|
32
|
-
|
33
|
-
def read_body(env)
|
34
|
-
body = env[:body]
|
35
|
-
body.respond_to?(:read) ? body.read : body
|
36
|
-
end
|
37
|
-
|
38
|
-
# Reads out proxy settings from env into options
|
39
|
-
def configure_proxy(options, env)
|
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
|
-
}
|
48
|
-
end
|
49
|
-
|
50
|
-
# Reads out host and port settings from env into options
|
51
|
-
def configure_socket(options, env)
|
52
|
-
bind = request_options(env)[:bind]
|
53
|
-
return unless bind
|
54
|
-
|
55
|
-
options[:bind] = {
|
56
|
-
host: bind[:host],
|
57
|
-
port: bind[:port]
|
58
|
-
}
|
59
|
-
end
|
60
|
-
|
61
|
-
# Reads out SSL certificate settings from env into options
|
62
|
-
def configure_ssl(options, env)
|
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
|
-
}
|
69
|
-
end
|
70
|
-
|
71
|
-
# Reads out timeout settings from env into options
|
72
|
-
def configure_timeout(options, env)
|
73
|
-
req = request_options(env)
|
74
|
-
options[:inactivity_timeout] = request_timeout(:read, req)
|
75
|
-
options[:connect_timeout] = request_timeout(:open, req)
|
76
|
-
end
|
77
|
-
|
78
|
-
# Reads out compression header settings from env into options
|
79
|
-
def configure_compression(options, env)
|
80
|
-
return unless (env[:method] == :get) &&
|
81
|
-
!options[:head].key?('accept-encoding')
|
82
|
-
|
83
|
-
options[:head]['accept-encoding'] = 'gzip, compressed'
|
84
|
-
end
|
85
|
-
|
86
|
-
def request_options(env)
|
87
|
-
env[:request]
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
include Options
|
92
|
-
|
93
|
-
dependency do
|
94
|
-
require 'em-http'
|
95
|
-
|
96
|
-
begin
|
97
|
-
require 'openssl'
|
98
|
-
rescue LoadError
|
99
|
-
warn 'Warning: no such file to load -- openssl. ' \
|
100
|
-
'Make sure it is installed if you want HTTPS support'
|
101
|
-
else
|
102
|
-
require 'em-http/version'
|
103
|
-
if EventMachine::HttpRequest::VERSION < '1.1.6'
|
104
|
-
require 'faraday/adapter/em_http_ssl_patch'
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
self.supports_parallel = true
|
110
|
-
|
111
|
-
# @return [Manager]
|
112
|
-
def self.setup_parallel_manager(_options = nil)
|
113
|
-
Manager.new
|
114
|
-
end
|
115
|
-
|
116
|
-
def call(env)
|
117
|
-
super
|
118
|
-
perform_request env
|
119
|
-
@app.call env
|
120
|
-
end
|
121
|
-
|
122
|
-
def perform_request(env)
|
123
|
-
if parallel?(env)
|
124
|
-
manager = env[:parallel_manager]
|
125
|
-
manager.add do
|
126
|
-
perform_single_request(env)
|
127
|
-
.callback { env[:response].finish(env) }
|
128
|
-
end
|
129
|
-
elsif EventMachine.reactor_running?
|
130
|
-
# EM is running: instruct upstream that this is an async request
|
131
|
-
env[:parallel_manager] = true
|
132
|
-
perform_single_request(env)
|
133
|
-
.callback { env[:response].finish(env) }
|
134
|
-
.errback do
|
135
|
-
# TODO: no way to communicate the error in async mode
|
136
|
-
raise NotImplementedError
|
137
|
-
end
|
138
|
-
else
|
139
|
-
error = nil
|
140
|
-
# start EM, block until request is completed
|
141
|
-
EventMachine.run do
|
142
|
-
perform_single_request(env)
|
143
|
-
.callback { EventMachine.stop }
|
144
|
-
.errback do |client|
|
145
|
-
error = error_message(client)
|
146
|
-
EventMachine.stop
|
147
|
-
end
|
148
|
-
end
|
149
|
-
raise_error(error) if error
|
150
|
-
end
|
151
|
-
rescue EventMachine::Connectify::CONNECTError => e
|
152
|
-
if e.message.include?('Proxy Authentication Required')
|
153
|
-
raise Faraday::ConnectionFailed,
|
154
|
-
%(407 "Proxy Authentication Required ")
|
155
|
-
end
|
156
|
-
|
157
|
-
raise Faraday::ConnectionFailed, e
|
158
|
-
rescue StandardError => e
|
159
|
-
if defined?(::OpenSSL::SSL::SSLError) && \
|
160
|
-
e.is_a?(::OpenSSL::SSL::SSLError)
|
161
|
-
raise Faraday::SSLError, e
|
162
|
-
end
|
163
|
-
|
164
|
-
raise
|
165
|
-
end
|
166
|
-
|
167
|
-
# TODO: reuse the connection to support pipelining
|
168
|
-
def perform_single_request(env)
|
169
|
-
req = create_request(env)
|
170
|
-
req = req.setup_request(env[:method], request_config(env))
|
171
|
-
req.callback do |client|
|
172
|
-
if env[:request].stream_response?
|
173
|
-
warn "Streaming downloads for #{self.class.name} " \
|
174
|
-
'are not yet implemented.'
|
175
|
-
env[:request].on_data.call(
|
176
|
-
client.response,
|
177
|
-
client.response.bytesize
|
178
|
-
)
|
179
|
-
end
|
180
|
-
status = client.response_header.status
|
181
|
-
reason = client.response_header.http_reason
|
182
|
-
save_response(env, status, client.response, nil, reason) do |headers|
|
183
|
-
client.response_header.each do |name, value|
|
184
|
-
headers[name.to_sym] = value
|
185
|
-
end
|
186
|
-
end
|
187
|
-
end
|
188
|
-
end
|
189
|
-
|
190
|
-
def create_request(env)
|
191
|
-
EventMachine::HttpRequest.new(
|
192
|
-
env[:url], connection_config(env).merge(@connection_options)
|
193
|
-
)
|
194
|
-
end
|
195
|
-
|
196
|
-
def error_message(client)
|
197
|
-
client.error || 'request failed'
|
198
|
-
end
|
199
|
-
|
200
|
-
def raise_error(msg)
|
201
|
-
error_class = Faraday::ClientError
|
202
|
-
if timeout_message?(msg)
|
203
|
-
error_class = Faraday::TimeoutError
|
204
|
-
msg = 'request timed out'
|
205
|
-
elsif msg == Errno::ECONNREFUSED
|
206
|
-
error_class = Faraday::ConnectionFailed
|
207
|
-
msg = 'connection refused'
|
208
|
-
elsif msg == 'connection closed by server'
|
209
|
-
error_class = Faraday::ConnectionFailed
|
210
|
-
end
|
211
|
-
raise error_class, msg
|
212
|
-
end
|
213
|
-
|
214
|
-
def timeout_message?(msg)
|
215
|
-
msg == Errno::ETIMEDOUT ||
|
216
|
-
(msg.is_a?(String) && msg.include?('timeout error'))
|
217
|
-
end
|
218
|
-
|
219
|
-
# @return [Boolean]
|
220
|
-
def parallel?(env)
|
221
|
-
!!env[:parallel_manager]
|
222
|
-
end
|
223
|
-
|
224
|
-
# This parallel manager is designed to start an EventMachine loop
|
225
|
-
# and block until all registered requests have been completed.
|
226
|
-
class Manager
|
227
|
-
# @see reset
|
228
|
-
def initialize
|
229
|
-
reset
|
230
|
-
end
|
231
|
-
|
232
|
-
# Re-initializes instance variables
|
233
|
-
def reset
|
234
|
-
@registered_procs = []
|
235
|
-
@num_registered = 0
|
236
|
-
@num_succeeded = 0
|
237
|
-
@errors = []
|
238
|
-
@running = false
|
239
|
-
end
|
240
|
-
|
241
|
-
# @return [Boolean]
|
242
|
-
def running?
|
243
|
-
@running
|
244
|
-
end
|
245
|
-
|
246
|
-
def add(&block)
|
247
|
-
if running?
|
248
|
-
perform_request(&block)
|
249
|
-
else
|
250
|
-
@registered_procs << block
|
251
|
-
end
|
252
|
-
@num_registered += 1
|
253
|
-
end
|
254
|
-
|
255
|
-
def run
|
256
|
-
if @num_registered.positive?
|
257
|
-
@running = true
|
258
|
-
EventMachine.run do
|
259
|
-
@registered_procs.each do |proc|
|
260
|
-
perform_request(&proc)
|
261
|
-
end
|
262
|
-
end
|
263
|
-
unless @errors.empty?
|
264
|
-
raise Faraday::ClientError, @errors.first || 'connection failed'
|
265
|
-
end
|
266
|
-
end
|
267
|
-
ensure
|
268
|
-
reset
|
269
|
-
end
|
270
|
-
|
271
|
-
def perform_request
|
272
|
-
client = yield
|
273
|
-
client.callback do
|
274
|
-
@num_succeeded += 1
|
275
|
-
check_finished
|
276
|
-
end
|
277
|
-
client.errback do
|
278
|
-
@errors << client.error
|
279
|
-
check_finished
|
280
|
-
end
|
281
|
-
end
|
282
|
-
|
283
|
-
def check_finished
|
284
|
-
EventMachine.stop if @num_succeeded + @errors.size == @num_registered
|
285
|
-
end
|
286
|
-
end
|
287
|
-
end
|
288
|
-
end
|
289
|
-
end
|
@@ -1,62 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'openssl'
|
4
|
-
require 'em-http'
|
5
|
-
|
6
|
-
# EventMachine patch to make SSL work.
|
7
|
-
module EmHttpSslPatch
|
8
|
-
def ssl_verify_peer(cert_string)
|
9
|
-
begin
|
10
|
-
@last_seen_cert = OpenSSL::X509::Certificate.new(cert_string)
|
11
|
-
rescue OpenSSL::X509::CertificateError
|
12
|
-
return false
|
13
|
-
end
|
14
|
-
|
15
|
-
unless certificate_store.verify(@last_seen_cert)
|
16
|
-
raise OpenSSL::SSL::SSLError,
|
17
|
-
%(unable to verify the server certificate for "#{host}")
|
18
|
-
end
|
19
|
-
|
20
|
-
begin
|
21
|
-
certificate_store.add_cert(@last_seen_cert)
|
22
|
-
rescue OpenSSL::X509::StoreError => e
|
23
|
-
raise e unless e.message == 'cert already in hash table'
|
24
|
-
end
|
25
|
-
true
|
26
|
-
end
|
27
|
-
|
28
|
-
def ssl_handshake_completed
|
29
|
-
return true unless verify_peer?
|
30
|
-
|
31
|
-
unless verified_cert_identity?
|
32
|
-
raise OpenSSL::SSL::SSLError,
|
33
|
-
%(host "#{host}" does not match the server certificate)
|
34
|
-
end
|
35
|
-
|
36
|
-
true
|
37
|
-
end
|
38
|
-
|
39
|
-
def verify_peer?
|
40
|
-
parent.connopts.tls[:verify_peer]
|
41
|
-
end
|
42
|
-
|
43
|
-
def verified_cert_identity?
|
44
|
-
OpenSSL::SSL.verify_certificate_identity(@last_seen_cert, host)
|
45
|
-
end
|
46
|
-
|
47
|
-
def host
|
48
|
-
parent.uri.host
|
49
|
-
end
|
50
|
-
|
51
|
-
def certificate_store
|
52
|
-
@certificate_store ||= begin
|
53
|
-
store = OpenSSL::X509::Store.new
|
54
|
-
store.set_default_paths
|
55
|
-
ca_file = parent.connopts.tls[:cert_chain_file]
|
56
|
-
store.add_file(ca_file) if ca_file
|
57
|
-
store
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
EventMachine::HttpStubConnection.include(EmHttpSslPatch)
|