faraday 1.0.1 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +104 -0
- data/LICENSE.md +1 -1
- data/README.md +3 -5
- data/examples/client_spec.rb +1 -1
- data/lib/faraday.rb +55 -41
- data/lib/faraday/adapter.rb +1 -8
- data/lib/faraday/autoload.rb +1 -6
- data/lib/faraday/connection.rb +11 -4
- data/lib/faraday/encoders/flat_params_encoder.rb +9 -2
- data/lib/faraday/encoders/nested_params_encoder.rb +7 -2
- data/lib/faraday/error.rb +20 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +14 -4
- data/lib/faraday/options.rb +4 -8
- data/lib/faraday/options/proxy_options.rb +4 -0
- data/lib/faraday/rack_builder.rb +13 -12
- data/lib/faraday/request.rb +20 -10
- data/lib/faraday/request/multipart.rb +9 -2
- data/lib/faraday/request/retry.rb +2 -2
- data/lib/faraday/response.rb +0 -6
- data/lib/faraday/response/raise_error.rb +12 -1
- data/lib/faraday/utils.rb +2 -2
- data/lib/faraday/utils/headers.rb +2 -2
- data/lib/faraday/version.rb +5 -0
- data/spec/faraday/adapter/test_spec.rb +260 -0
- data/spec/faraday/connection_spec.rb +38 -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/params_encoders/flat_spec.rb +8 -0
- data/spec/faraday/params_encoders/nested_spec.rb +8 -0
- data/spec/faraday/rack_builder_spec.rb +149 -0
- data/spec/faraday/request/authorization_spec.rb +2 -2
- data/spec/faraday/request/multipart_spec.rb +41 -13
- data/spec/faraday/request/retry_spec.rb +1 -1
- data/spec/faraday/request_spec.rb +16 -5
- data/spec/faraday/response/raise_error_spec.rb +63 -0
- data/spec/support/shared_examples/adapter.rb +2 -1
- data/spec/support/shared_examples/request_method.rb +39 -11
- metadata +92 -13
- 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/net_http.rb +0 -219
- data/lib/faraday/adapter/net_http_persistent.rb +0 -91
- data/spec/faraday/adapter/net_http_persistent_spec.rb +0 -57
@@ -117,7 +117,7 @@ RSpec.describe Faraday::Request::Retry do
|
|
117
117
|
let(:options) { { max: 2, interval: 0.1, interval_randomness: 0.05 } }
|
118
118
|
let(:middleware) { Faraday::Request::Retry.new(nil, options) }
|
119
119
|
|
120
|
-
it { expect(middleware.send(:calculate_retry_interval, 2)).to be_between(0.1, 0.
|
120
|
+
it { expect(middleware.send(:calculate_retry_interval, 2)).to be_between(0.1, 0.105) }
|
121
121
|
end
|
122
122
|
end
|
123
123
|
|
@@ -6,20 +6,31 @@ RSpec.describe Faraday::Request do
|
|
6
6
|
headers: { 'Mime-Version' => '1.0' },
|
7
7
|
request: { oauth: { consumer_key: 'anonymous' } })
|
8
8
|
end
|
9
|
-
let(:
|
9
|
+
let(:http_method) { :get }
|
10
10
|
let(:block) { nil }
|
11
11
|
|
12
|
-
subject { conn.build_request(
|
12
|
+
subject { conn.build_request(http_method, &block) }
|
13
13
|
|
14
14
|
context 'when nothing particular is configured' do
|
15
|
-
it { expect(subject.
|
15
|
+
it { expect(subject.http_method).to eq(:get) }
|
16
16
|
it { expect(subject.to_env(conn).ssl.verify).to be_falsey }
|
17
17
|
end
|
18
18
|
|
19
|
-
context 'when method is post' do
|
20
|
-
let(:
|
19
|
+
context 'when HTTP method is post' do
|
20
|
+
let(:http_method) { :post }
|
21
|
+
|
22
|
+
it { expect(subject.http_method).to eq(:post) }
|
23
|
+
end
|
24
|
+
|
25
|
+
describe 'deprecate method for HTTP method' do
|
26
|
+
let(:http_method) { :post }
|
27
|
+
let(:expected_warning) do
|
28
|
+
%r{WARNING: `Faraday::Request#method` is deprecated; use `#http_method` instead. It will be removed in or after version 2.0.\n`Faraday::Request#method` called from .+/spec/faraday/request_spec.rb:\d+.}
|
29
|
+
end
|
21
30
|
|
22
31
|
it { expect(subject.method).to eq(:post) }
|
32
|
+
|
33
|
+
it { expect { subject.method }.to output(expected_warning).to_stderr }
|
23
34
|
end
|
24
35
|
|
25
36
|
context 'when setting the url on setup with a URI' do
|
@@ -29,6 +29,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
29
29
|
expect(ex.message).to eq('the server responded with status 400')
|
30
30
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
31
31
|
expect(ex.response[:status]).to eq(400)
|
32
|
+
expect(ex.response_status).to eq(400)
|
33
|
+
expect(ex.response_body).to eq('keep looking')
|
34
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
32
35
|
end
|
33
36
|
end
|
34
37
|
|
@@ -37,6 +40,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
37
40
|
expect(ex.message).to eq('the server responded with status 401')
|
38
41
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
39
42
|
expect(ex.response[:status]).to eq(401)
|
43
|
+
expect(ex.response_status).to eq(401)
|
44
|
+
expect(ex.response_body).to eq('keep looking')
|
45
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
40
46
|
end
|
41
47
|
end
|
42
48
|
|
@@ -45,6 +51,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
45
51
|
expect(ex.message).to eq('the server responded with status 403')
|
46
52
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
47
53
|
expect(ex.response[:status]).to eq(403)
|
54
|
+
expect(ex.response_status).to eq(403)
|
55
|
+
expect(ex.response_body).to eq('keep looking')
|
56
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
48
57
|
end
|
49
58
|
end
|
50
59
|
|
@@ -53,6 +62,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
53
62
|
expect(ex.message).to eq('the server responded with status 404')
|
54
63
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
55
64
|
expect(ex.response[:status]).to eq(404)
|
65
|
+
expect(ex.response_status).to eq(404)
|
66
|
+
expect(ex.response_body).to eq('keep looking')
|
67
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
56
68
|
end
|
57
69
|
end
|
58
70
|
|
@@ -61,6 +73,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
61
73
|
expect(ex.message).to eq('407 "Proxy Authentication Required"')
|
62
74
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
63
75
|
expect(ex.response[:status]).to eq(407)
|
76
|
+
expect(ex.response_status).to eq(407)
|
77
|
+
expect(ex.response_body).to eq('keep looking')
|
78
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
64
79
|
end
|
65
80
|
end
|
66
81
|
|
@@ -69,6 +84,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
69
84
|
expect(ex.message).to eq('the server responded with status 409')
|
70
85
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
71
86
|
expect(ex.response[:status]).to eq(409)
|
87
|
+
expect(ex.response_status).to eq(409)
|
88
|
+
expect(ex.response_body).to eq('keep looking')
|
89
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
72
90
|
end
|
73
91
|
end
|
74
92
|
|
@@ -77,6 +95,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
77
95
|
expect(ex.message).to eq('the server responded with status 422')
|
78
96
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
79
97
|
expect(ex.response[:status]).to eq(422)
|
98
|
+
expect(ex.response_status).to eq(422)
|
99
|
+
expect(ex.response_body).to eq('keep looking')
|
100
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
80
101
|
end
|
81
102
|
end
|
82
103
|
|
@@ -85,6 +106,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
85
106
|
expect(ex.message).to eq('http status could not be derived from the server response')
|
86
107
|
expect(ex.response[:headers]['X-Reason']).to eq('nil')
|
87
108
|
expect(ex.response[:status]).to be_nil
|
109
|
+
expect(ex.response_status).to be_nil
|
110
|
+
expect(ex.response_body).to eq('fail')
|
111
|
+
expect(ex.response_headers['X-Reason']).to eq('nil')
|
88
112
|
end
|
89
113
|
end
|
90
114
|
|
@@ -93,6 +117,9 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
93
117
|
expect(ex.message).to eq('the server responded with status 499')
|
94
118
|
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
95
119
|
expect(ex.response[:status]).to eq(499)
|
120
|
+
expect(ex.response_status).to eq(499)
|
121
|
+
expect(ex.response_body).to eq('keep looking')
|
122
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
96
123
|
end
|
97
124
|
end
|
98
125
|
|
@@ -101,6 +128,42 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
101
128
|
expect(ex.message).to eq('the server responded with status 500')
|
102
129
|
expect(ex.response[:headers]['X-Error']).to eq('bailout')
|
103
130
|
expect(ex.response[:status]).to eq(500)
|
131
|
+
expect(ex.response_status).to eq(500)
|
132
|
+
expect(ex.response_body).to eq('fail')
|
133
|
+
expect(ex.response_headers['X-Error']).to eq('bailout')
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
describe 'request info' do
|
138
|
+
let(:conn) do
|
139
|
+
Faraday.new do |b|
|
140
|
+
b.response :raise_error
|
141
|
+
b.adapter :test do |stub|
|
142
|
+
stub.post('request?full=true', request_body, request_headers) do
|
143
|
+
[400, { 'X-Reason' => 'because' }, 'keep looking']
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
let(:request_body) { JSON.generate({ 'item' => 'sth' }) }
|
149
|
+
let(:request_headers) { { 'Authorization' => 'Basic 123' } }
|
150
|
+
|
151
|
+
subject(:perform_request) do
|
152
|
+
conn.post 'request' do |req|
|
153
|
+
req.headers['Authorization'] = 'Basic 123'
|
154
|
+
req.params[:full] = true
|
155
|
+
req.body = request_body
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'returns the request info in the exception' do
|
160
|
+
expect { perform_request }.to raise_error(Faraday::BadRequestError) do |ex|
|
161
|
+
expect(ex.response[:request][:method]).to eq(:post)
|
162
|
+
expect(ex.response[:request][:url_path]).to eq('/request')
|
163
|
+
expect(ex.response[:request][:params]).to eq({ 'full' => 'true' })
|
164
|
+
expect(ex.response[:request][:headers]).to match(a_hash_including(request_headers))
|
165
|
+
expect(ex.response[:request][:body]).to eq(request_body)
|
166
|
+
end
|
104
167
|
end
|
105
168
|
end
|
106
169
|
end
|
@@ -33,6 +33,7 @@ shared_examples 'adapter examples' do |**options|
|
|
33
33
|
|
34
34
|
let(:protocol) { ssl_mode? ? 'https' : 'http' }
|
35
35
|
let(:remote) { "#{protocol}://example.com" }
|
36
|
+
let(:stub_remote) { remote }
|
36
37
|
|
37
38
|
let(:conn) do
|
38
39
|
conn_options[:ssl] ||= {}
|
@@ -46,7 +47,7 @@ shared_examples 'adapter examples' do |**options|
|
|
46
47
|
end
|
47
48
|
end
|
48
49
|
|
49
|
-
let!(:request_stub) { stub_request(http_method,
|
50
|
+
let!(:request_stub) { stub_request(http_method, stub_remote) }
|
50
51
|
|
51
52
|
after do
|
52
53
|
expect(request_stub).to have_been_requested unless request_stub.disabled?
|
@@ -1,5 +1,19 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
shared_examples 'proxy examples' do
|
4
|
+
it 'handles requests with proxy' do
|
5
|
+
res = conn.public_send(http_method, '/')
|
6
|
+
|
7
|
+
expect(res.status).to eq(200)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'handles proxy failures' do
|
11
|
+
request_stub.to_return(status: 407)
|
12
|
+
|
13
|
+
expect { conn.public_send(http_method, '/') }.to raise_error(Faraday::ProxyAuthError)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
3
17
|
shared_examples 'a request method' do |http_method|
|
4
18
|
let(:query_or_body) { method_with_body?(http_method) ? :body : :query }
|
5
19
|
let(:response) { conn.public_send(http_method, '/') }
|
@@ -13,8 +27,8 @@ shared_examples 'a request method' do |http_method|
|
|
13
27
|
end
|
14
28
|
|
15
29
|
it 'handles headers with multiple values' do
|
16
|
-
request_stub.to_return(headers: { 'Set-Cookie' => '
|
17
|
-
expect(response.headers['set-cookie']).to eq('
|
30
|
+
request_stub.to_return(headers: { 'Set-Cookie' => 'name=value' })
|
31
|
+
expect(response.headers['set-cookie']).to eq('name=value')
|
18
32
|
end
|
19
33
|
|
20
34
|
it 'retrieves the response headers' do
|
@@ -119,7 +133,7 @@ shared_examples 'a request method' do |http_method|
|
|
119
133
|
request_stub.with(headers: { 'Content-Type' => %r{\Amultipart/form-data} }) do |request|
|
120
134
|
# WebMock does not support matching body for multipart/form-data requests yet :(
|
121
135
|
# https://github.com/bblimke/webmock/issues/623
|
122
|
-
request.body
|
136
|
+
request.body.include?('RubyMultipartPost')
|
123
137
|
end
|
124
138
|
conn.public_send(http_method, '/', payload)
|
125
139
|
end
|
@@ -218,17 +232,31 @@ shared_examples 'a request method' do |http_method|
|
|
218
232
|
end
|
219
233
|
end
|
220
234
|
|
221
|
-
|
222
|
-
|
235
|
+
context 'when a proxy is provided as option' do
|
236
|
+
before do
|
237
|
+
conn_options[:proxy] = 'http://env-proxy.com:80'
|
238
|
+
end
|
223
239
|
|
224
|
-
|
225
|
-
expect(res.status).to eq(200)
|
240
|
+
include_examples 'proxy examples'
|
226
241
|
end
|
227
242
|
|
228
|
-
|
229
|
-
|
230
|
-
request_stub.to_return(status: 407)
|
243
|
+
context 'when http_proxy env variable is set' do
|
244
|
+
let(:proxy_url) { 'http://env-proxy.com:80' }
|
231
245
|
|
232
|
-
|
246
|
+
around do |example|
|
247
|
+
with_env 'http_proxy' => proxy_url do
|
248
|
+
example.run
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
include_examples 'proxy examples'
|
253
|
+
|
254
|
+
context 'when the env proxy is ignored' do
|
255
|
+
around do |example|
|
256
|
+
with_env_proxy_disabled(&example)
|
257
|
+
end
|
258
|
+
|
259
|
+
include_examples 'proxy examples'
|
260
|
+
end
|
233
261
|
end
|
234
262
|
end
|
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.
|
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
|
@@ -32,6 +102,20 @@ dependencies:
|
|
32
102
|
- - "<"
|
33
103
|
- !ruby/object:Gem::Version
|
34
104
|
version: '3'
|
105
|
+
- !ruby/object:Gem::Dependency
|
106
|
+
name: ruby2_keywords
|
107
|
+
requirement: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: 0.0.4
|
112
|
+
type: :runtime
|
113
|
+
prerelease: false
|
114
|
+
version_requirements: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: 0.0.4
|
35
119
|
description:
|
36
120
|
email: technoweenie@gmail.com
|
37
121
|
executables: []
|
@@ -46,14 +130,7 @@ files:
|
|
46
130
|
- examples/client_test.rb
|
47
131
|
- lib/faraday.rb
|
48
132
|
- lib/faraday/adapter.rb
|
49
|
-
- lib/faraday/adapter/em_http.rb
|
50
|
-
- lib/faraday/adapter/em_http_ssl_patch.rb
|
51
|
-
- lib/faraday/adapter/em_synchrony.rb
|
52
|
-
- lib/faraday/adapter/em_synchrony/parallel_manager.rb
|
53
|
-
- lib/faraday/adapter/excon.rb
|
54
133
|
- lib/faraday/adapter/httpclient.rb
|
55
|
-
- lib/faraday/adapter/net_http.rb
|
56
|
-
- lib/faraday/adapter/net_http_persistent.rb
|
57
134
|
- lib/faraday/adapter/patron.rb
|
58
135
|
- lib/faraday/adapter/rack.rb
|
59
136
|
- lib/faraday/adapter/test.rb
|
@@ -67,6 +144,7 @@ files:
|
|
67
144
|
- lib/faraday/error.rb
|
68
145
|
- lib/faraday/file_part.rb
|
69
146
|
- lib/faraday/logging/formatter.rb
|
147
|
+
- lib/faraday/methods.rb
|
70
148
|
- lib/faraday/middleware.rb
|
71
149
|
- lib/faraday/middleware_registry.rb
|
72
150
|
- lib/faraday/options.rb
|
@@ -92,15 +170,16 @@ files:
|
|
92
170
|
- lib/faraday/utils.rb
|
93
171
|
- lib/faraday/utils/headers.rb
|
94
172
|
- lib/faraday/utils/params_hash.rb
|
173
|
+
- lib/faraday/version.rb
|
95
174
|
- spec/external_adapters/faraday_specs_setup.rb
|
96
175
|
- spec/faraday/adapter/em_http_spec.rb
|
97
176
|
- spec/faraday/adapter/em_synchrony_spec.rb
|
98
177
|
- spec/faraday/adapter/excon_spec.rb
|
99
178
|
- spec/faraday/adapter/httpclient_spec.rb
|
100
|
-
- spec/faraday/adapter/net_http_persistent_spec.rb
|
101
179
|
- spec/faraday/adapter/net_http_spec.rb
|
102
180
|
- spec/faraday/adapter/patron_spec.rb
|
103
181
|
- spec/faraday/adapter/rack_spec.rb
|
182
|
+
- spec/faraday/adapter/test_spec.rb
|
104
183
|
- spec/faraday/adapter/typhoeus_spec.rb
|
105
184
|
- spec/faraday/adapter_registry_spec.rb
|
106
185
|
- spec/faraday/adapter_spec.rb
|
@@ -142,7 +221,7 @@ licenses:
|
|
142
221
|
- MIT
|
143
222
|
metadata:
|
144
223
|
homepage_uri: https://lostisland.github.io/faraday
|
145
|
-
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.
|
224
|
+
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.4.2
|
146
225
|
source_code_uri: https://github.com/lostisland/faraday
|
147
226
|
bug_tracker_uri: https://github.com/lostisland/faraday/issues
|
148
227
|
post_install_message:
|
@@ -154,14 +233,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
154
233
|
requirements:
|
155
234
|
- - ">="
|
156
235
|
- !ruby/object:Gem::Version
|
157
|
-
version: '2.
|
236
|
+
version: '2.4'
|
158
237
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
238
|
requirements:
|
160
239
|
- - ">="
|
161
240
|
- !ruby/object:Gem::Version
|
162
241
|
version: '0'
|
163
242
|
requirements: []
|
164
|
-
rubygems_version: 3.0.3
|
243
|
+
rubygems_version: 3.0.3.1
|
165
244
|
signing_key:
|
166
245
|
specification_version: 4
|
167
246
|
summary: HTTP/REST API client library.
|
@@ -1,286 +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 'em-http'
|
94
|
-
|
95
|
-
self.supports_parallel = true
|
96
|
-
|
97
|
-
# @return [Manager]
|
98
|
-
def self.setup_parallel_manager(_options = nil)
|
99
|
-
Manager.new
|
100
|
-
end
|
101
|
-
|
102
|
-
def call(env)
|
103
|
-
super
|
104
|
-
perform_request env
|
105
|
-
@app.call env
|
106
|
-
end
|
107
|
-
|
108
|
-
def perform_request(env)
|
109
|
-
if parallel?(env)
|
110
|
-
manager = env[:parallel_manager]
|
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
|
123
|
-
end
|
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
|
134
|
-
end
|
135
|
-
raise_error(error) if error
|
136
|
-
end
|
137
|
-
rescue EventMachine::Connectify::CONNECTError => e
|
138
|
-
if e.message.include?('Proxy Authentication Required')
|
139
|
-
raise Faraday::ConnectionFailed,
|
140
|
-
%(407 "Proxy Authentication Required ")
|
141
|
-
end
|
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
|
148
|
-
end
|
149
|
-
|
150
|
-
raise
|
151
|
-
end
|
152
|
-
|
153
|
-
# TODO: reuse the connection to support pipelining
|
154
|
-
def perform_single_request(env)
|
155
|
-
req = create_request(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
|
166
|
-
status = client.response_header.status
|
167
|
-
reason = client.response_header.http_reason
|
168
|
-
save_response(env, status, client.response, nil, reason) do |headers|
|
169
|
-
client.response_header.each do |name, value|
|
170
|
-
headers[name.to_sym] = value
|
171
|
-
end
|
172
|
-
end
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
def create_request(env)
|
177
|
-
EventMachine::HttpRequest.new(
|
178
|
-
env[:url], connection_config(env).merge(@connection_options)
|
179
|
-
)
|
180
|
-
end
|
181
|
-
|
182
|
-
def error_message(client)
|
183
|
-
client.error || 'request failed'
|
184
|
-
end
|
185
|
-
|
186
|
-
def raise_error(msg)
|
187
|
-
error_class = Faraday::ClientError
|
188
|
-
if timeout_message?(msg)
|
189
|
-
error_class = Faraday::TimeoutError
|
190
|
-
msg = 'request timed out'
|
191
|
-
elsif msg == Errno::ECONNREFUSED
|
192
|
-
error_class = Faraday::ConnectionFailed
|
193
|
-
msg = 'connection refused'
|
194
|
-
elsif msg == 'connection closed by server'
|
195
|
-
error_class = Faraday::ConnectionFailed
|
196
|
-
end
|
197
|
-
raise error_class, msg
|
198
|
-
end
|
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]
|
206
|
-
def parallel?(env)
|
207
|
-
!!env[:parallel_manager]
|
208
|
-
end
|
209
|
-
|
210
|
-
# This parallel manager is designed to start an EventMachine loop
|
211
|
-
# and block until all registered requests have been completed.
|
212
|
-
class Manager
|
213
|
-
# @see reset
|
214
|
-
def initialize
|
215
|
-
reset
|
216
|
-
end
|
217
|
-
|
218
|
-
# Re-initializes instance variables
|
219
|
-
def reset
|
220
|
-
@registered_procs = []
|
221
|
-
@num_registered = 0
|
222
|
-
@num_succeeded = 0
|
223
|
-
@errors = []
|
224
|
-
@running = false
|
225
|
-
end
|
226
|
-
|
227
|
-
# @return [Boolean]
|
228
|
-
def running?
|
229
|
-
@running
|
230
|
-
end
|
231
|
-
|
232
|
-
def add(&block)
|
233
|
-
if running?
|
234
|
-
perform_request { yield }
|
235
|
-
else
|
236
|
-
@registered_procs << block
|
237
|
-
end
|
238
|
-
@num_registered += 1
|
239
|
-
end
|
240
|
-
|
241
|
-
def run
|
242
|
-
if @num_registered.positive?
|
243
|
-
@running = true
|
244
|
-
EventMachine.run do
|
245
|
-
@registered_procs.each do |proc|
|
246
|
-
perform_request(&proc)
|
247
|
-
end
|
248
|
-
end
|
249
|
-
unless @errors.empty?
|
250
|
-
raise Faraday::ClientError, @errors.first || 'connection failed'
|
251
|
-
end
|
252
|
-
end
|
253
|
-
ensure
|
254
|
-
reset
|
255
|
-
end
|
256
|
-
|
257
|
-
def perform_request
|
258
|
-
client = yield
|
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
|
267
|
-
end
|
268
|
-
|
269
|
-
def check_finished
|
270
|
-
EventMachine.stop if @num_succeeded + @errors.size == @num_registered
|
271
|
-
end
|
272
|
-
end
|
273
|
-
end
|
274
|
-
end
|
275
|
-
end
|
276
|
-
|
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
|