faraday 1.1.0 → 1.10.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +104 -0
  3. data/README.md +1 -2
  4. data/examples/client_spec.rb +34 -2
  5. data/examples/client_test.rb +41 -2
  6. data/lib/faraday/adapter/test.rb +59 -43
  7. data/lib/faraday/adapter.rb +1 -12
  8. data/lib/faraday/autoload.rb +1 -9
  9. data/lib/faraday/connection.rb +34 -6
  10. data/lib/faraday/deprecate.rb +110 -0
  11. data/lib/faraday/error.rb +12 -6
  12. data/lib/faraday/methods.rb +6 -0
  13. data/lib/faraday/middleware.rb +14 -4
  14. data/lib/faraday/options/proxy_options.rb +4 -0
  15. data/lib/faraday/request/authorization.rb +14 -7
  16. data/lib/faraday/request/json.rb +55 -0
  17. data/lib/faraday/request.rb +10 -12
  18. data/lib/faraday/response/json.rb +54 -0
  19. data/lib/faraday/response/logger.rb +2 -4
  20. data/lib/faraday/response.rb +3 -7
  21. data/lib/faraday/version.rb +5 -0
  22. data/lib/faraday.rb +67 -40
  23. data/spec/faraday/adapter/em_http_spec.rb +39 -37
  24. data/spec/faraday/adapter/em_synchrony_spec.rb +11 -9
  25. data/spec/faraday/adapter/test_spec.rb +117 -0
  26. data/spec/faraday/connection_spec.rb +45 -0
  27. data/spec/faraday/deprecate_spec.rb +147 -0
  28. data/spec/faraday/error_spec.rb +15 -0
  29. data/spec/faraday/middleware_spec.rb +32 -6
  30. data/spec/faraday/options/proxy_options_spec.rb +7 -0
  31. data/spec/faraday/request/authorization_spec.rb +8 -0
  32. data/spec/faraday/request/json_spec.rb +111 -0
  33. data/spec/faraday/request_spec.rb +1 -1
  34. data/spec/faraday/response/json_spec.rb +119 -0
  35. data/spec/faraday/response/raise_error_spec.rb +30 -0
  36. data/spec/spec_helper.rb +2 -0
  37. data/spec/support/shared_examples/adapter.rb +2 -1
  38. data/spec/support/shared_examples/request_method.rb +36 -8
  39. metadata +143 -32
  40. data/lib/faraday/adapter/em_http.rb +0 -286
  41. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -62
  42. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -69
  43. data/lib/faraday/adapter/em_synchrony.rb +0 -150
  44. data/lib/faraday/adapter/excon.rb +0 -124
  45. data/lib/faraday/adapter/httpclient.rb +0 -152
  46. data/lib/faraday/adapter/net_http.rb +0 -219
  47. data/lib/faraday/adapter/net_http_persistent.rb +0 -91
  48. data/lib/faraday/adapter/patron.rb +0 -132
  49. data/lib/faraday/adapter/rack.rb +0 -75
  50. data/lib/faraday/file_part.rb +0 -128
  51. data/lib/faraday/param_part.rb +0 -53
  52. data/lib/faraday/request/multipart.rb +0 -106
  53. data/lib/faraday/request/retry.rb +0 -239
  54. data/spec/faraday/adapter/net_http_persistent_spec.rb +0 -57
  55. data/spec/faraday/request/multipart_spec.rb +0 -302
  56. data/spec/faraday/request/retry_spec.rb +0 -242
@@ -1,239 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Faraday
4
- class Request
5
- # Catches exceptions and retries each request a limited number of times.
6
- #
7
- # By default, it retries 2 times and handles only timeout exceptions. It can
8
- # be configured with an arbitrary number of retries, a list of exceptions to
9
- # handle, a retry interval, a percentage of randomness to add to the retry
10
- # interval, and a backoff factor.
11
- #
12
- # @example Configure Retry middleware using intervals
13
- # Faraday.new do |conn|
14
- # conn.request(:retry, max: 2,
15
- # interval: 0.05,
16
- # interval_randomness: 0.5,
17
- # backoff_factor: 2,
18
- # exceptions: [CustomException, 'Timeout::Error'])
19
- #
20
- # conn.adapter(:net_http) # NB: Last middleware must be the adapter
21
- # end
22
- #
23
- # This example will result in a first interval that is random between 0.05
24
- # and 0.075 and a second interval that is random between 0.1 and 0.15.
25
- class Retry < Faraday::Middleware
26
- DEFAULT_EXCEPTIONS = [
27
- Errno::ETIMEDOUT, 'Timeout::Error',
28
- Faraday::TimeoutError, Faraday::RetriableResponse
29
- ].freeze
30
- IDEMPOTENT_METHODS = %i[delete get head options put].freeze
31
-
32
- # Options contains the configurable parameters for the Retry middleware.
33
- class Options < Faraday::Options.new(:max, :interval, :max_interval,
34
- :interval_randomness,
35
- :backoff_factor, :exceptions,
36
- :methods, :retry_if, :retry_block,
37
- :retry_statuses)
38
-
39
- DEFAULT_CHECK = ->(_env, _exception) { false }
40
-
41
- def self.from(value)
42
- if value.is_a?(Integer)
43
- new(value)
44
- else
45
- super(value)
46
- end
47
- end
48
-
49
- def max
50
- (self[:max] ||= 2).to_i
51
- end
52
-
53
- def interval
54
- (self[:interval] ||= 0).to_f
55
- end
56
-
57
- def max_interval
58
- (self[:max_interval] ||= Float::MAX).to_f
59
- end
60
-
61
- def interval_randomness
62
- (self[:interval_randomness] ||= 0).to_f
63
- end
64
-
65
- def backoff_factor
66
- (self[:backoff_factor] ||= 1).to_f
67
- end
68
-
69
- def exceptions
70
- Array(self[:exceptions] ||= DEFAULT_EXCEPTIONS)
71
- end
72
-
73
- def methods
74
- Array(self[:methods] ||= IDEMPOTENT_METHODS)
75
- end
76
-
77
- def retry_if
78
- self[:retry_if] ||= DEFAULT_CHECK
79
- end
80
-
81
- def retry_block
82
- self[:retry_block] ||= proc {}
83
- end
84
-
85
- def retry_statuses
86
- Array(self[:retry_statuses] ||= [])
87
- end
88
- end
89
-
90
- # @param app [#call]
91
- # @param options [Hash]
92
- # @option options [Integer] :max (2) Maximum number of retries
93
- # @option options [Integer] :interval (0) Pause in seconds between retries
94
- # @option options [Integer] :interval_randomness (0) The maximum random
95
- # interval amount expressed as a float between
96
- # 0 and 1 to use in addition to the interval.
97
- # @option options [Integer] :max_interval (Float::MAX) An upper limit
98
- # for the interval
99
- # @option options [Integer] :backoff_factor (1) The amount to multiply
100
- # each successive retry's interval amount by in order to provide backoff
101
- # @option options [Array] :exceptions ([ Errno::ETIMEDOUT,
102
- # 'Timeout::Error', Faraday::TimeoutError, Faraday::RetriableResponse])
103
- # The list of exceptions to handle. Exceptions can be given as
104
- # Class, Module, or String.
105
- # @option options [Array] :methods (the idempotent HTTP methods
106
- # in IDEMPOTENT_METHODS) A list of HTTP methods to retry without
107
- # calling retry_if. Pass an empty Array to call retry_if
108
- # for all exceptions.
109
- # @option options [Block] :retry_if (false) block that will receive
110
- # the env object and the exception raised
111
- # and should decide if the code should retry still the action or
112
- # not independent of the retry count. This would be useful
113
- # if the exception produced is non-recoverable or if the
114
- # the HTTP method called is not idempotent.
115
- # @option options [Block] :retry_block block that is executed after
116
- # every retry. Request environment, middleware options, current number
117
- # of retries and the exception is passed to the block as parameters.
118
- # @option options [Array] :retry_statuses Array of Integer HTTP status
119
- # codes or a single Integer value that determines whether to raise
120
- # a Faraday::RetriableResponse exception based on the HTTP status code
121
- # of an HTTP response.
122
- def initialize(app, options = nil)
123
- super(app)
124
- @options = Options.from(options)
125
- @errmatch = build_exception_matcher(@options.exceptions)
126
- end
127
-
128
- def calculate_sleep_amount(retries, env)
129
- retry_after = calculate_retry_after(env)
130
- retry_interval = calculate_retry_interval(retries)
131
-
132
- return if retry_after && retry_after > @options.max_interval
133
-
134
- if retry_after && retry_after >= retry_interval
135
- retry_after
136
- else
137
- retry_interval
138
- end
139
- end
140
-
141
- # @param env [Faraday::Env]
142
- def call(env)
143
- retries = @options.max
144
- request_body = env[:body]
145
- begin
146
- # after failure env[:body] is set to the response body
147
- env[:body] = request_body
148
- @app.call(env).tap do |resp|
149
- if @options.retry_statuses.include?(resp.status)
150
- raise Faraday::RetriableResponse.new(nil, resp)
151
- end
152
- end
153
- rescue @errmatch => e
154
- if retries.positive? && retry_request?(env, e)
155
- retries -= 1
156
- rewind_files(request_body)
157
- @options.retry_block.call(env, @options, retries, e)
158
- if (sleep_amount = calculate_sleep_amount(retries + 1, env))
159
- sleep sleep_amount
160
- retry
161
- end
162
- end
163
-
164
- raise unless e.is_a?(Faraday::RetriableResponse)
165
-
166
- e.response
167
- end
168
- end
169
-
170
- # An exception matcher for the rescue clause can usually be any object
171
- # that responds to `===`, but for Ruby 1.8 it has to be a Class or Module.
172
- #
173
- # @param exceptions [Array]
174
- # @api private
175
- # @return [Module] an exception matcher
176
- def build_exception_matcher(exceptions)
177
- matcher = Module.new
178
- (
179
- class << matcher
180
- self
181
- end).class_eval do
182
- define_method(:===) do |error|
183
- exceptions.any? do |ex|
184
- if ex.is_a? Module
185
- error.is_a? ex
186
- else
187
- error.class.to_s == ex.to_s
188
- end
189
- end
190
- end
191
- end
192
- matcher
193
- end
194
-
195
- private
196
-
197
- def retry_request?(env, exception)
198
- @options.methods.include?(env[:method]) ||
199
- @options.retry_if.call(env, exception)
200
- end
201
-
202
- def rewind_files(body)
203
- return unless body.is_a?(Hash)
204
-
205
- body.each do |_, value|
206
- value.rewind if value.is_a?(UploadIO)
207
- end
208
- end
209
-
210
- # MDN spec for Retry-After header:
211
- # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
212
- def calculate_retry_after(env)
213
- response_headers = env[:response_headers]
214
- return unless response_headers
215
-
216
- retry_after_value = env[:response_headers]['Retry-After']
217
-
218
- # Try to parse date from the header value
219
- begin
220
- datetime = DateTime.rfc2822(retry_after_value)
221
- datetime.to_time - Time.now.utc
222
- rescue ArgumentError
223
- retry_after_value.to_f
224
- end
225
- end
226
-
227
- def calculate_retry_interval(retries)
228
- retry_index = @options.max - retries
229
- current_interval = @options.interval *
230
- (@options.backoff_factor**retry_index)
231
- current_interval = [current_interval, @options.max_interval].min
232
- random_interval = rand * @options.interval_randomness.to_f *
233
- @options.interval
234
-
235
- current_interval + random_interval
236
- end
237
- end
238
- end
239
- 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
@@ -1,302 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Faraday::Request::Multipart do
4
- let(:options) { {} }
5
- let(:conn) do
6
- Faraday.new do |b|
7
- b.request :multipart, options
8
- b.request :url_encoded
9
- b.adapter :test do |stub|
10
- stub.post('/echo') do |env|
11
- posted_as = env[:request_headers]['Content-Type']
12
- expect(env[:body]).to be_a_kind_of(Faraday::CompositeReadIO)
13
- [200, { 'Content-Type' => posted_as }, env[:body].read]
14
- end
15
- end
16
- end
17
- end
18
-
19
- shared_examples 'a multipart request' do
20
- it 'generates a unique boundary for each request' do
21
- response1 = conn.post('/echo', payload)
22
- response2 = conn.post('/echo', payload)
23
-
24
- b1 = parse_multipart_boundary(response1.headers['Content-Type'])
25
- b2 = parse_multipart_boundary(response2.headers['Content-Type'])
26
- expect(b1).to_not eq(b2)
27
- end
28
- end
29
-
30
- context 'FilePart: when multipart objects in param' do
31
- let(:payload) do
32
- {
33
- a: 1,
34
- b: {
35
- c: Faraday::FilePart.new(__FILE__, 'text/x-ruby', nil,
36
- 'Content-Disposition' => 'form-data; foo=1'),
37
- d: 2
38
- }
39
- }
40
- end
41
- it_behaves_like 'a multipart request'
42
-
43
- it 'forms a multipart request' do
44
- response = conn.post('/echo', payload)
45
-
46
- boundary = parse_multipart_boundary(response.headers['Content-Type'])
47
- result = parse_multipart(boundary, response.body)
48
- expect(result[:errors]).to be_empty
49
-
50
- part_a, body_a = result.part('a')
51
- expect(part_a).to_not be_nil
52
- expect(part_a.filename).to be_nil
53
- expect(body_a).to eq('1')
54
-
55
- part_bc, body_bc = result.part('b[c]')
56
- expect(part_bc).to_not be_nil
57
- expect(part_bc.filename).to eq('multipart_spec.rb')
58
- expect(part_bc.headers['content-disposition'])
59
- .to eq(
60
- 'form-data; foo=1; name="b[c]"; filename="multipart_spec.rb"'
61
- )
62
- expect(part_bc.headers['content-type']).to eq('text/x-ruby')
63
- expect(part_bc.headers['content-transfer-encoding']).to eq('binary')
64
- expect(body_bc).to eq(File.read(__FILE__))
65
-
66
- part_bd, body_bd = result.part('b[d]')
67
- expect(part_bd).to_not be_nil
68
- expect(part_bd.filename).to be_nil
69
- expect(body_bd).to eq('2')
70
- end
71
- end
72
-
73
- context 'FilePart: when providing json and IO content in the same payload' do
74
- let(:io) { StringIO.new('io-content') }
75
- let(:json) do
76
- {
77
- b: 1,
78
- c: 2
79
- }.to_json
80
- end
81
-
82
- let(:payload) do
83
- {
84
- json: Faraday::ParamPart.new(json, 'application/json'),
85
- io: Faraday::FilePart.new(io, 'application/pdf')
86
- }
87
- end
88
-
89
- it_behaves_like 'a multipart request'
90
-
91
- it 'forms a multipart request' do
92
- response = conn.post('/echo', payload)
93
-
94
- boundary = parse_multipart_boundary(response.headers['Content-Type'])
95
- result = parse_multipart(boundary, response.body)
96
- expect(result[:errors]).to be_empty
97
-
98
- part_json, body_json = result.part('json')
99
- expect(part_json).to_not be_nil
100
- expect(part_json.mime).to eq('application/json')
101
- expect(part_json.filename).to be_nil
102
- expect(body_json).to eq(json)
103
-
104
- part_io, body_io = result.part('io')
105
- expect(part_io).to_not be_nil
106
- expect(part_io.mime).to eq('application/pdf')
107
- expect(part_io.filename).to eq('local.path')
108
- expect(body_io).to eq(io.string)
109
- end
110
- end
111
-
112
- context 'FilePart: when multipart objects in array param' do
113
- let(:payload) do
114
- {
115
- a: 1,
116
- b: [{
117
- c: Faraday::FilePart.new(__FILE__, 'text/x-ruby'),
118
- d: 2
119
- }]
120
- }
121
- end
122
-
123
- it_behaves_like 'a multipart request'
124
-
125
- it 'forms a multipart request' do
126
- response = conn.post('/echo', payload)
127
-
128
- boundary = parse_multipart_boundary(response.headers['Content-Type'])
129
- result = parse_multipart(boundary, response.body)
130
- expect(result[:errors]).to be_empty
131
-
132
- part_a, body_a = result.part('a')
133
- expect(part_a).to_not be_nil
134
- expect(part_a.filename).to be_nil
135
- expect(body_a).to eq('1')
136
-
137
- part_bc, body_bc = result.part('b[][c]')
138
- expect(part_bc).to_not be_nil
139
- expect(part_bc.filename).to eq('multipart_spec.rb')
140
- expect(part_bc.headers['content-disposition'])
141
- .to eq(
142
- 'form-data; name="b[][c]"; filename="multipart_spec.rb"'
143
- )
144
- expect(part_bc.headers['content-type']).to eq('text/x-ruby')
145
- expect(part_bc.headers['content-transfer-encoding']).to eq('binary')
146
- expect(body_bc).to eq(File.read(__FILE__))
147
-
148
- part_bd, body_bd = result.part('b[][d]')
149
- expect(part_bd).to_not be_nil
150
- expect(part_bd.filename).to be_nil
151
- expect(body_bd).to eq('2')
152
- end
153
- end
154
-
155
- context 'UploadIO: when multipart objects in param' do
156
- let(:payload) do
157
- {
158
- a: 1,
159
- b: {
160
- c: Faraday::UploadIO.new(__FILE__, 'text/x-ruby', nil,
161
- 'Content-Disposition' => 'form-data; foo=1'),
162
- d: 2
163
- }
164
- }
165
- end
166
- it_behaves_like 'a multipart request'
167
-
168
- it 'forms a multipart request' do
169
- response = conn.post('/echo', payload)
170
-
171
- boundary = parse_multipart_boundary(response.headers['Content-Type'])
172
- result = parse_multipart(boundary, response.body)
173
- expect(result[:errors]).to be_empty
174
-
175
- part_a, body_a = result.part('a')
176
- expect(part_a).to_not be_nil
177
- expect(part_a.filename).to be_nil
178
- expect(body_a).to eq('1')
179
-
180
- part_bc, body_bc = result.part('b[c]')
181
- expect(part_bc).to_not be_nil
182
- expect(part_bc.filename).to eq('multipart_spec.rb')
183
- expect(part_bc.headers['content-disposition'])
184
- .to eq(
185
- 'form-data; foo=1; name="b[c]"; filename="multipart_spec.rb"'
186
- )
187
- expect(part_bc.headers['content-type']).to eq('text/x-ruby')
188
- expect(part_bc.headers['content-transfer-encoding']).to eq('binary')
189
- expect(body_bc).to eq(File.read(__FILE__))
190
-
191
- part_bd, body_bd = result.part('b[d]')
192
- expect(part_bd).to_not be_nil
193
- expect(part_bd.filename).to be_nil
194
- expect(body_bd).to eq('2')
195
- end
196
- end
197
-
198
- context 'UploadIO: when providing json and IO content in the same payload' do
199
- let(:io) { StringIO.new('io-content') }
200
- let(:json) do
201
- {
202
- b: 1,
203
- c: 2
204
- }.to_json
205
- end
206
-
207
- let(:payload) do
208
- {
209
- json: Faraday::ParamPart.new(json, 'application/json'),
210
- io: Faraday::UploadIO.new(io, 'application/pdf')
211
- }
212
- end
213
-
214
- it_behaves_like 'a multipart request'
215
-
216
- it 'forms a multipart request' do
217
- response = conn.post('/echo', payload)
218
-
219
- boundary = parse_multipart_boundary(response.headers['Content-Type'])
220
- result = parse_multipart(boundary, response.body)
221
- expect(result[:errors]).to be_empty
222
-
223
- part_json, body_json = result.part('json')
224
- expect(part_json).to_not be_nil
225
- expect(part_json.mime).to eq('application/json')
226
- expect(part_json.filename).to be_nil
227
- expect(body_json).to eq(json)
228
-
229
- part_io, body_io = result.part('io')
230
- expect(part_io).to_not be_nil
231
- expect(part_io.mime).to eq('application/pdf')
232
- expect(part_io.filename).to eq('local.path')
233
- expect(body_io).to eq(io.string)
234
- end
235
- end
236
-
237
- context 'UploadIO: when multipart objects in array param' do
238
- let(:payload) do
239
- {
240
- a: 1,
241
- b: [{
242
- c: Faraday::UploadIO.new(__FILE__, 'text/x-ruby'),
243
- d: 2
244
- }]
245
- }
246
- end
247
-
248
- it_behaves_like 'a multipart request'
249
-
250
- it 'forms a multipart request' do
251
- response = conn.post('/echo', payload)
252
-
253
- boundary = parse_multipart_boundary(response.headers['Content-Type'])
254
- result = parse_multipart(boundary, response.body)
255
- expect(result[:errors]).to be_empty
256
-
257
- part_a, body_a = result.part('a')
258
- expect(part_a).to_not be_nil
259
- expect(part_a.filename).to be_nil
260
- expect(body_a).to eq('1')
261
-
262
- part_bc, body_bc = result.part('b[][c]')
263
- expect(part_bc).to_not be_nil
264
- expect(part_bc.filename).to eq('multipart_spec.rb')
265
- expect(part_bc.headers['content-disposition'])
266
- .to eq(
267
- 'form-data; name="b[][c]"; filename="multipart_spec.rb"'
268
- )
269
- expect(part_bc.headers['content-type']).to eq('text/x-ruby')
270
- expect(part_bc.headers['content-transfer-encoding']).to eq('binary')
271
- expect(body_bc).to eq(File.read(__FILE__))
272
-
273
- part_bd, body_bd = result.part('b[][d]')
274
- expect(part_bd).to_not be_nil
275
- expect(part_bd.filename).to be_nil
276
- expect(body_bd).to eq('2')
277
- end
278
- end
279
-
280
- context 'when passing flat_encode=true option' do
281
- let(:options) { { flat_encode: true } }
282
- let(:io) { StringIO.new('io-content') }
283
- let(:payload) do
284
- {
285
- a: 1,
286
- b: [
287
- Faraday::UploadIO.new(io, 'application/pdf'),
288
- Faraday::UploadIO.new(io, 'application/pdf')
289
- ]
290
- }
291
- end
292
-
293
- it_behaves_like 'a multipart request'
294
-
295
- it 'encode params using flat encoder' do
296
- response = conn.post('/echo', payload)
297
-
298
- expect(response.body).to include('name="b"')
299
- expect(response.body).not_to include('name="b[]"')
300
- end
301
- end
302
- end