faraday-conductivity 0.3.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +3 -5
- data/Appraisals +2 -6
- data/README.md +44 -35
- data/faraday-conductivity.gemspec +4 -5
- data/gemfiles/{faraday_0.8.gemfile → faraday_1.0.0.gemfile} +2 -2
- data/lib/faraday/conductivity.rb +0 -4
- data/lib/faraday/conductivity/selective_errors.rb +32 -17
- data/lib/faraday/conductivity/version.rb +1 -1
- data/spec/middleware/selective_errors_spec.rb +5 -5
- data/spec/spec_helper.rb +0 -9
- metadata +9 -32
- data/gemfiles/faraday_0.9.gemfile +0 -7
- data/lib/faraday/conductivity/mimetype.rb +0 -18
- data/lib/faraday/conductivity/repeater.rb +0 -61
- data/spec/fake_server.rb +0 -21
- data/spec/middleware/mimetype_spec.rb +0 -17
- data/spec/middleware/repeater_spec.rb +0 -61
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4b7daa7d2c2b8b16dc880bfb8ac956542d2db10f299a2821f1d1df8323c88e96
|
4
|
+
data.tar.gz: fa99a8e801b0cab12612be567d7558612e6da81a7c849a5cfd0a0a77358b4f42
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 26827ab33d953a9bd4ea2212cdaf9c2ffb3f7537f79c6143496976c38a5c4d55bb3a55e9315f6f169e802aba5165acd54c2a7ffee56a65bdb3c4254405749fae
|
7
|
+
data.tar.gz: f16d6d9307c8747ef3510fa447c07fdb5ed89040fad92fc7ff5bdbb87e0d8b199aa9d2979d7030cb54183bbae9cbe03fcdb9f70d82b9edb0494fc87c43010f30
|
data/.travis.yml
CHANGED
data/Appraisals
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
-
# Faraday::Conductivity [![Build Status](https://travis-ci.org/
|
1
|
+
# Faraday::Conductivity [![Build Status](https://travis-ci.org/enova/faraday-conductivity.svg?branch=master)](https://travis-ci.org/enova/faraday-conductivity)
|
2
|
+
|
3
|
+
Originally implemented by [yourkarma/faraday-conductivity](https://github.com/yourkarma/faraday-conductivity).
|
4
|
+
|
5
|
+
---
|
2
6
|
|
3
7
|
Extra Faraday Middleware! Geared towards a service oriented architecture.
|
4
8
|
|
@@ -144,35 +148,6 @@ The User-Agent will looks like this on my machine:
|
|
144
148
|
MarketingSite/1.1 (iain.local; iain; 30360) ruby/1.9.3 (327; x86_64-darwin12.2.0)
|
145
149
|
```
|
146
150
|
|
147
|
-
### Repeater
|
148
|
-
|
149
|
-
The Repeater will retry your requests until they succeed. This is handy for
|
150
|
-
reaching servers that are not too reliable.
|
151
|
-
|
152
|
-
``` ruby
|
153
|
-
connection = Faraday.new(url: "http://slow.website.com") do |faraday|
|
154
|
-
faraday.use :repeater, retries: 6, mode: :rapid
|
155
|
-
end
|
156
|
-
```
|
157
|
-
|
158
|
-
The `retries` parameter specifies how many times to retry before succeeding.
|
159
|
-
|
160
|
-
The `mode` parameter specifies how long to wait before retrying. `:rapid` will
|
161
|
-
retry instantly, `:one`, will wait one second between retries, `:linear` and
|
162
|
-
`:exponential` will retry longer and longer after every retry.
|
163
|
-
|
164
|
-
It's also possible to specify your own pattern by providing a lambda, that
|
165
|
-
returns the number of seconds to wait. For example:
|
166
|
-
|
167
|
-
``` ruby
|
168
|
-
connection = Faraday.new(url: "http://slow.website.com") do |faraday|
|
169
|
-
faraday.use :repeater, retries: 6, pattern: ->(n) { rand < 0.5 ? 10 : 2 }
|
170
|
-
end
|
171
|
-
```
|
172
|
-
|
173
|
-
You can use the repeater together with the `raise_error` middleware to also
|
174
|
-
retry after getting 404s and other succeeded requests, but failed statuses.
|
175
|
-
|
176
151
|
### Selective Errors
|
177
152
|
|
178
153
|
The default `:raise_error` middleware raises errors for every http status above
|
@@ -189,10 +164,7 @@ connection = Faraday.new(url: "http://widgets.yourapp.com") do |faraday|
|
|
189
164
|
end
|
190
165
|
```
|
191
166
|
|
192
|
-
The errors raised will be the same as Faraday
|
193
|
-
`Faraday::Error::ResourceNotFound` for 404 errors,
|
194
|
-
`Faraday::Error::ConnectionFailed` for 407 and `Faraday::Error::ClientError` for
|
195
|
-
the rest.
|
167
|
+
The errors raised will be the same [as Faraday](https://github.com/lostisland/faraday/blob/019e1a841707718adad2fd05c602eb1a869b42bc/lib/faraday/response/raise_error.rb).
|
196
168
|
|
197
169
|
If you don't specify the `:on` or `:except` options, it will behave exactly like
|
198
170
|
`:raise_error`. The errors are however "enhanced" with extra information about
|
@@ -201,7 +173,7 @@ the request that normally are lost:
|
|
201
173
|
``` ruby
|
202
174
|
begin
|
203
175
|
do_failing_request_here
|
204
|
-
rescue Faraday::
|
176
|
+
rescue Faraday::ClientError => error
|
205
177
|
puts error.request[:url]
|
206
178
|
puts error.request[:method]
|
207
179
|
puts error.request[:body]
|
@@ -227,6 +199,43 @@ connection = Faraday.new(url: "http://widgets.yourapp.com") do |faraday|
|
|
227
199
|
end
|
228
200
|
```
|
229
201
|
|
202
|
+
## faraday-conductivity ~> 0.3
|
203
|
+
|
204
|
+
The following middlewares have been removed entirely and are no longer
|
205
|
+
supported, but were available prior in faraday-conductivity versions <= 0.3.
|
206
|
+
|
207
|
+
### Repeater
|
208
|
+
|
209
|
+
Suggested to use [Faraday's Retry](https://github.com/lostisland/faraday/blob/master/docs/middleware/request/retry.md)
|
210
|
+
instead.
|
211
|
+
|
212
|
+
The Repeater will retry your requests until they succeed. This is handy for
|
213
|
+
reaching servers that are not too reliable.
|
214
|
+
|
215
|
+
``` ruby
|
216
|
+
connection = Faraday.new(url: "http://slow.website.com") do |faraday|
|
217
|
+
faraday.use :repeater, retries: 6, mode: :rapid
|
218
|
+
end
|
219
|
+
```
|
220
|
+
|
221
|
+
The `retries` parameter specifies how many times to retry before succeeding.
|
222
|
+
|
223
|
+
The `mode` parameter specifies how long to wait before retrying. `:rapid` will
|
224
|
+
retry instantly, `:one`, will wait one second between retries, `:linear` and
|
225
|
+
`:exponential` will retry longer and longer after every retry.
|
226
|
+
|
227
|
+
It's also possible to specify your own pattern by providing a lambda, that
|
228
|
+
returns the number of seconds to wait. For example:
|
229
|
+
|
230
|
+
``` ruby
|
231
|
+
connection = Faraday.new(url: "http://slow.website.com") do |faraday|
|
232
|
+
faraday.use :repeater, retries: 6, pattern: ->(n) { rand < 0.5 ? 10 : 2 }
|
233
|
+
end
|
234
|
+
```
|
235
|
+
|
236
|
+
You can use the repeater together with the `raise_error` middleware to also
|
237
|
+
retry after getting 404s and other succeeded requests, but failed statuses.
|
238
|
+
|
230
239
|
### Mimetype
|
231
240
|
|
232
241
|
This one is deprecated. Use Request Headers instead.
|
@@ -6,11 +6,11 @@ require 'faraday/conductivity/version'
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
7
|
gem.name = "faraday-conductivity"
|
8
8
|
gem.version = Faraday::Conductivity::VERSION
|
9
|
-
gem.authors = ["iain"]
|
10
|
-
gem.email = ["iain@iain.nl"]
|
9
|
+
gem.authors = ["iain", "sshao"]
|
10
|
+
gem.email = ["iain@iain.nl", "ssh.sshao@gmail.com"]
|
11
11
|
gem.description = %q{Extra Faraday middleware, geared towards a service oriented architecture.}
|
12
12
|
gem.summary = %q{Extra Faraday middleware, geared towards a service oriented architecture.}
|
13
|
-
gem.homepage = "https://github.com/
|
13
|
+
gem.homepage = "https://github.com/enova/faraday-conductivity"
|
14
14
|
gem.license = "MIT"
|
15
15
|
|
16
16
|
gem.files = `git ls-files`.split($/)
|
@@ -18,10 +18,9 @@ Gem::Specification.new do |gem|
|
|
18
18
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
19
|
gem.require_paths = ["lib"]
|
20
20
|
|
21
|
-
gem.add_dependency "faraday", "~> 0
|
21
|
+
gem.add_dependency "faraday", "~> 1.0"
|
22
22
|
gem.add_development_dependency "rake"
|
23
23
|
gem.add_development_dependency "rspec", "~> 3"
|
24
24
|
gem.add_development_dependency "pry"
|
25
|
-
gem.add_development_dependency "service_double"
|
26
25
|
gem.add_development_dependency "appraisal"
|
27
26
|
end
|
data/lib/faraday/conductivity.rb
CHANGED
@@ -3,11 +3,9 @@ require "faraday"
|
|
3
3
|
require "faraday/conductivity/version"
|
4
4
|
|
5
5
|
require "faraday/conductivity/extended_logging"
|
6
|
-
require "faraday/conductivity/mimetype"
|
7
6
|
require "faraday/conductivity/request_id"
|
8
7
|
require "faraday/conductivity/request_id_filter"
|
9
8
|
require "faraday/conductivity/user_agent"
|
10
|
-
require "faraday/conductivity/repeater"
|
11
9
|
require "faraday/conductivity/selective_errors"
|
12
10
|
require "faraday/conductivity/request_headers"
|
13
11
|
|
@@ -16,8 +14,6 @@ module Faraday
|
|
16
14
|
end
|
17
15
|
|
18
16
|
Faraday::Middleware.register_middleware :extended_logging => Faraday::Conductivity::ExtendedLogging
|
19
|
-
Faraday::Middleware.register_middleware :repeater => Faraday::Conductivity::Repeater
|
20
|
-
Faraday::Request.register_middleware :mimetype => Faraday::Conductivity::Mimetype
|
21
17
|
Faraday::Request.register_middleware :request_id => Faraday::Conductivity::RequestId
|
22
18
|
Faraday::Request.register_middleware :request_headers => Faraday::Conductivity::RequestHeaders
|
23
19
|
Faraday::Request.register_middleware :user_agent => Faraday::Conductivity::UserAgent
|
@@ -54,7 +54,8 @@ module Faraday
|
|
54
54
|
# end
|
55
55
|
class SelectiveErrors < Faraday::Middleware
|
56
56
|
|
57
|
-
ClientErrorStatuses = 400...
|
57
|
+
ClientErrorStatuses = (400...500).freeze
|
58
|
+
ServerErrorStatuses = (500...600).freeze
|
58
59
|
|
59
60
|
def initialize(app, options = {})
|
60
61
|
@app = app
|
@@ -74,35 +75,49 @@ module Faraday
|
|
74
75
|
start_time = Time.now
|
75
76
|
|
76
77
|
@app.call(env).on_complete do
|
77
|
-
|
78
78
|
status = env[:status]
|
79
79
|
|
80
80
|
if should_raise_error?(status)
|
81
|
-
response = {
|
82
|
-
:status => env[:status],
|
83
|
-
:body => env[:body],
|
84
|
-
:headers => env[:response_headers],
|
85
|
-
}
|
86
81
|
error = case status
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
82
|
+
when 400
|
83
|
+
Faraday::BadRequestError.new(response_values(env))
|
84
|
+
when 401
|
85
|
+
Faraday::UnauthorizedError.new(response_values(env))
|
86
|
+
when 403
|
87
|
+
Faraday::ForbiddenError.new(response_values(env))
|
88
|
+
when 404
|
89
|
+
Faraday::ResourceNotFound.new(response_values(env))
|
90
|
+
when 407
|
91
|
+
# mimic the behavior that we get with proxy requests with HTTPS
|
92
|
+
msg = %(407 "Proxy Authentication Required")
|
93
|
+
Faraday::ProxyAuthError.new(msg, response_values(env))
|
94
|
+
when 409
|
95
|
+
Faraday::ConflictError.new(response_values(env))
|
96
|
+
when 422
|
97
|
+
Faraday::UnprocessableEntityError.new(response_values(env))
|
98
|
+
when ClientErrorStatuses
|
99
|
+
Faraday::ClientError.new(response_values(env))
|
100
|
+
when ServerErrorStatuses
|
101
|
+
Faraday::ServerError.new(response_values(env))
|
102
|
+
when nil
|
103
|
+
Faraday::NilStatusError.new(response_values(env))
|
104
|
+
end
|
105
|
+
|
95
106
|
error.extend Error
|
96
|
-
error.response =
|
107
|
+
error.response = response_values(env)
|
97
108
|
error.request = request
|
98
109
|
error.response_time = Time.now - start_time
|
99
|
-
raise error
|
100
110
|
|
111
|
+
raise error
|
101
112
|
end
|
102
113
|
|
103
114
|
end
|
104
115
|
end
|
105
116
|
|
117
|
+
def response_values(env)
|
118
|
+
{ status: env.status, headers: env.response_headers, body: env.body }
|
119
|
+
end
|
120
|
+
|
106
121
|
def should_raise_error?(status)
|
107
122
|
@on.include?(status) && !@except.include?(status)
|
108
123
|
end
|
@@ -4,7 +4,7 @@ RSpec.describe Faraday::Conductivity::SelectiveErrors do
|
|
4
4
|
|
5
5
|
it "raises an exception if the error is inside the :on argument" do
|
6
6
|
apply_selective_errors on: 407..409
|
7
|
-
expect { response_with_status(408) }.to raise_error Faraday::
|
7
|
+
expect { response_with_status(408) }.to raise_error Faraday::ClientError
|
8
8
|
end
|
9
9
|
|
10
10
|
it "won't raise an exception when outside the range" do
|
@@ -19,17 +19,17 @@ RSpec.describe Faraday::Conductivity::SelectiveErrors do
|
|
19
19
|
|
20
20
|
it "raises an exception if included in :on and not included in :except" do
|
21
21
|
apply_selective_errors on: 403..422, except: [408]
|
22
|
-
expect { response_with_status(409) }.to raise_error Faraday::
|
22
|
+
expect { response_with_status(409) }.to raise_error Faraday::ClientError
|
23
23
|
end
|
24
24
|
|
25
25
|
it "raises a resource not found error when the actual status is 404" do
|
26
26
|
apply_selective_errors on: 403..422, except: [408]
|
27
|
-
expect { response_with_status(404) }.to raise_error Faraday::
|
27
|
+
expect { response_with_status(404) }.to raise_error Faraday::ResourceNotFound
|
28
28
|
end
|
29
29
|
|
30
|
-
it "raises
|
30
|
+
it "raises proxy auth required on 407" do
|
31
31
|
apply_selective_errors on: 403..422, except: [408]
|
32
|
-
expect { response_with_status(407) }.to raise_error Faraday::
|
32
|
+
expect { response_with_status(407) }.to raise_error Faraday::ProxyAuthError
|
33
33
|
end
|
34
34
|
|
35
35
|
it "stores more information about the request and response" do
|
data/spec/spec_helper.rb
CHANGED
@@ -31,12 +31,3 @@ RSpec.configure do |config|
|
|
31
31
|
config.disable_monkey_patching!
|
32
32
|
config.include SpecHelper
|
33
33
|
end
|
34
|
-
|
35
|
-
require 'service_double'
|
36
|
-
|
37
|
-
$service_double_url = "http://localhost:3434"
|
38
|
-
|
39
|
-
$service_double = ServiceDouble.hook_into(:rspec) do |config|
|
40
|
-
config.server = File.expand_path('../fake_server.rb', __FILE__)
|
41
|
-
config.url = $service_double_url
|
42
|
-
end
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday-conductivity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- iain
|
8
|
+
- sshao
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2020-05-26 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: faraday
|
@@ -16,14 +17,14 @@ dependencies:
|
|
16
17
|
requirements:
|
17
18
|
- - "~>"
|
18
19
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0
|
20
|
+
version: '1.0'
|
20
21
|
type: :runtime
|
21
22
|
prerelease: false
|
22
23
|
version_requirements: !ruby/object:Gem::Requirement
|
23
24
|
requirements:
|
24
25
|
- - "~>"
|
25
26
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0
|
27
|
+
version: '1.0'
|
27
28
|
- !ruby/object:Gem::Dependency
|
28
29
|
name: rake
|
29
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,20 +67,6 @@ dependencies:
|
|
66
67
|
- - ">="
|
67
68
|
- !ruby/object:Gem::Version
|
68
69
|
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: service_double
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
83
70
|
- !ruby/object:Gem::Dependency
|
84
71
|
name: appraisal
|
85
72
|
requirement: !ruby/object:Gem::Requirement
|
@@ -97,6 +84,7 @@ dependencies:
|
|
97
84
|
description: Extra Faraday middleware, geared towards a service oriented architecture.
|
98
85
|
email:
|
99
86
|
- iain@iain.nl
|
87
|
+
- ssh.sshao@gmail.com
|
100
88
|
executables: []
|
101
89
|
extensions: []
|
102
90
|
extra_rdoc_files: []
|
@@ -110,28 +98,22 @@ files:
|
|
110
98
|
- README.md
|
111
99
|
- Rakefile
|
112
100
|
- faraday-conductivity.gemspec
|
113
|
-
- gemfiles/
|
114
|
-
- gemfiles/faraday_0.9.gemfile
|
101
|
+
- gemfiles/faraday_1.0.0.gemfile
|
115
102
|
- lib/faraday/conductivity.rb
|
116
103
|
- lib/faraday/conductivity/extended_logging.rb
|
117
|
-
- lib/faraday/conductivity/mimetype.rb
|
118
|
-
- lib/faraday/conductivity/repeater.rb
|
119
104
|
- lib/faraday/conductivity/request_headers.rb
|
120
105
|
- lib/faraday/conductivity/request_id.rb
|
121
106
|
- lib/faraday/conductivity/request_id_filter.rb
|
122
107
|
- lib/faraday/conductivity/selective_errors.rb
|
123
108
|
- lib/faraday/conductivity/user_agent.rb
|
124
109
|
- lib/faraday/conductivity/version.rb
|
125
|
-
- spec/fake_server.rb
|
126
110
|
- spec/middleware/extended_logging_spec.rb
|
127
|
-
- spec/middleware/mimetype_spec.rb
|
128
|
-
- spec/middleware/repeater_spec.rb
|
129
111
|
- spec/middleware/request_headers_spec.rb
|
130
112
|
- spec/middleware/request_id_spec.rb
|
131
113
|
- spec/middleware/selective_errors_spec.rb
|
132
114
|
- spec/middleware/user_agent_spec.rb
|
133
115
|
- spec/spec_helper.rb
|
134
|
-
homepage: https://github.com/
|
116
|
+
homepage: https://github.com/enova/faraday-conductivity
|
135
117
|
licenses:
|
136
118
|
- MIT
|
137
119
|
metadata: {}
|
@@ -150,19 +132,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
150
132
|
- !ruby/object:Gem::Version
|
151
133
|
version: '0'
|
152
134
|
requirements: []
|
153
|
-
|
154
|
-
rubygems_version: 2.2.2
|
135
|
+
rubygems_version: 3.1.2
|
155
136
|
signing_key:
|
156
137
|
specification_version: 4
|
157
138
|
summary: Extra Faraday middleware, geared towards a service oriented architecture.
|
158
139
|
test_files:
|
159
|
-
- spec/fake_server.rb
|
160
140
|
- spec/middleware/extended_logging_spec.rb
|
161
|
-
- spec/middleware/mimetype_spec.rb
|
162
|
-
- spec/middleware/repeater_spec.rb
|
163
141
|
- spec/middleware/request_headers_spec.rb
|
164
142
|
- spec/middleware/request_id_spec.rb
|
165
143
|
- spec/middleware/selective_errors_spec.rb
|
166
144
|
- spec/middleware/user_agent_spec.rb
|
167
145
|
- spec/spec_helper.rb
|
168
|
-
has_rdoc:
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
module Conductivity
|
3
|
-
# Deprecated. Use RequestHeaders
|
4
|
-
class Mimetype < Faraday::Middleware
|
5
|
-
|
6
|
-
def initialize(app, options = {})
|
7
|
-
super(app)
|
8
|
-
@accept = options.fetch(:accept)
|
9
|
-
end
|
10
|
-
|
11
|
-
def call(env)
|
12
|
-
env[:request_headers]['Accept'] ||= @accept
|
13
|
-
@app.call(env)
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
@@ -1,61 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
module Conductivity
|
3
|
-
class Repeater < Faraday::Middleware
|
4
|
-
|
5
|
-
PATTERNS = {
|
6
|
-
:rapid => lambda { |n| 0 },
|
7
|
-
:one => lambda { |n| 1 },
|
8
|
-
:linear => lambda { |n| n },
|
9
|
-
:exponential => lambda { |n| n ** 2 },
|
10
|
-
}
|
11
|
-
|
12
|
-
def initialize(app, options = {})
|
13
|
-
@app = app
|
14
|
-
@retries = options[:retries] || 10
|
15
|
-
|
16
|
-
if mode = options[:mode]
|
17
|
-
@pattern = build_pattern(PATTERNS.fetch(mode))
|
18
|
-
elsif pattern = options[:pattern]
|
19
|
-
@pattern = build_pattern(pattern)
|
20
|
-
else
|
21
|
-
@pattern = build_pattern(PATTERNS.fetch(:exponential))
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def call(env)
|
26
|
-
tries = 0
|
27
|
-
begin
|
28
|
-
@app.call(env)
|
29
|
-
rescue Faraday::Error::ClientError, SystemCallError
|
30
|
-
if tries < @retries
|
31
|
-
tries += 1
|
32
|
-
@pattern.wait(tries)
|
33
|
-
retry
|
34
|
-
else
|
35
|
-
raise
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def build_pattern(pattern)
|
41
|
-
Pattern.new(pattern)
|
42
|
-
end
|
43
|
-
|
44
|
-
class Pattern
|
45
|
-
|
46
|
-
def initialize(pattern)
|
47
|
-
@pattern = pattern
|
48
|
-
end
|
49
|
-
|
50
|
-
def wait(num)
|
51
|
-
seconds = @pattern.call(num)
|
52
|
-
if seconds != 0
|
53
|
-
sleep num
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
data/spec/fake_server.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
RSpec.describe Faraday::Conductivity::Mimetype do
|
2
|
-
|
3
|
-
subject(:request_headers) { response.env[:request_headers] }
|
4
|
-
|
5
|
-
it "includes the mimetype specified" do
|
6
|
-
expect(request_headers["Accept"]).to eq mimetype
|
7
|
-
end
|
8
|
-
|
9
|
-
let(:mimetype) { "application/vnd.users-v2+json" }
|
10
|
-
|
11
|
-
def connection
|
12
|
-
create_connection do |faraday|
|
13
|
-
faraday.request :mimetype, :accept => mimetype
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
@@ -1,61 +0,0 @@
|
|
1
|
-
RSpec.describe Faraday::Conductivity::Repeater do
|
2
|
-
|
3
|
-
let(:connection) {
|
4
|
-
Faraday.new(url: $service_double_url) { |faraday|
|
5
|
-
faraday.use :repeater, mode: :rapid, retries: 6
|
6
|
-
faraday.response :raise_error
|
7
|
-
faraday.adapter Faraday.default_adapter
|
8
|
-
}
|
9
|
-
}
|
10
|
-
|
11
|
-
it "retries after timeouts" do
|
12
|
-
expect(get_with_max(4).body).to eq "fast"
|
13
|
-
end
|
14
|
-
|
15
|
-
it "gives up after a number of retries" do
|
16
|
-
expect { get_with_max(20) }.to raise_error(Faraday::Error::TimeoutError)
|
17
|
-
end
|
18
|
-
|
19
|
-
class MyPattern
|
20
|
-
|
21
|
-
def initialize
|
22
|
-
@waited = []
|
23
|
-
end
|
24
|
-
|
25
|
-
attr_reader :waited
|
26
|
-
|
27
|
-
def wait(x)
|
28
|
-
waited << x
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
it "waits according to a pattern" do
|
34
|
-
pattern = MyPattern.new
|
35
|
-
expect(Faraday::Conductivity::Repeater::Pattern).to receive(:new).and_return(pattern)
|
36
|
-
get_with_max(6)
|
37
|
-
expect(pattern.waited).to eq pattern.waited.sort
|
38
|
-
end
|
39
|
-
|
40
|
-
it "handles other errors too" do
|
41
|
-
connection = Faraday.new(url: "http://blabla.bla") { |faraday|
|
42
|
-
faraday.use :repeater, mode: :rapid, retries: 2
|
43
|
-
faraday.adapter Faraday.default_adapter
|
44
|
-
}
|
45
|
-
|
46
|
-
pattern = double :pattern
|
47
|
-
expect(Faraday::Conductivity::Repeater::Pattern).to receive(:new).and_return(pattern)
|
48
|
-
expect(pattern).to receive(:wait).with(1).ordered
|
49
|
-
expect(pattern).to receive(:wait).with(2).ordered
|
50
|
-
|
51
|
-
expect { connection.get("/") }.to raise_error(Faraday::Error::ConnectionFailed)
|
52
|
-
end
|
53
|
-
|
54
|
-
def get_with_max(num)
|
55
|
-
connection.get("/unreliable/#{num}") { |req|
|
56
|
-
req.options[:timeout] = 1
|
57
|
-
req.options[:open_timeout] = 1
|
58
|
-
}
|
59
|
-
end
|
60
|
-
|
61
|
-
end
|