faraday 0.13.0 → 2.0.0
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 +5 -5
- data/CHANGELOG.md +496 -0
- data/LICENSE.md +1 -1
- data/README.md +28 -328
- data/Rakefile +7 -0
- data/examples/client_spec.rb +97 -0
- data/examples/client_test.rb +118 -0
- data/lib/faraday/adapter/test.rb +127 -68
- data/lib/faraday/adapter.rb +71 -22
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/connection.rb +314 -226
- 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 +121 -37
- data/lib/faraday/logging/formatter.rb +106 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +18 -25
- data/lib/faraday/middleware_registry.rb +65 -0
- data/lib/faraday/options/connection_options.rb +22 -0
- data/lib/faraday/options/env.rb +181 -0
- data/lib/faraday/options/proxy_options.rb +32 -0
- data/lib/faraday/options/request_options.rb +22 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/options.rb +41 -195
- data/lib/faraday/parameters.rb +4 -196
- data/lib/faraday/rack_builder.rb +91 -74
- data/lib/faraday/request/authorization.rb +37 -29
- data/lib/faraday/request/instrumentation.rb +47 -27
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request/url_encoded.rb +45 -23
- data/lib/faraday/request.rb +74 -32
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +57 -14
- data/lib/faraday/response.rb +26 -33
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +47 -251
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +104 -197
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/test_spec.rb +377 -0
- data/spec/faraday/adapter_registry_spec.rb +28 -0
- data/spec/faraday/adapter_spec.rb +55 -0
- data/spec/faraday/connection_spec.rb +787 -0
- data/spec/faraday/error_spec.rb +60 -0
- data/spec/faraday/middleware_spec.rb +52 -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 +44 -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 +302 -0
- data/spec/faraday/request/authorization_spec.rb +83 -0
- data/spec/faraday/request/instrumentation_spec.rb +74 -0
- data/spec/faraday/request/json_spec.rb +111 -0
- data/spec/faraday/request/url_encoded_spec.rb +82 -0
- data/spec/faraday/request_spec.rb +109 -0
- data/spec/faraday/response/json_spec.rb +117 -0
- data/spec/faraday/response/logger_spec.rb +220 -0
- data/spec/faraday/response/raise_error_spec.rb +172 -0
- data/spec/faraday/response_spec.rb +75 -0
- data/spec/faraday/utils/headers_spec.rb +82 -0
- data/spec/faraday/utils_spec.rb +117 -0
- data/spec/faraday_spec.rb +37 -0
- data/spec/spec_helper.rb +132 -0
- data/spec/support/disabling_stub.rb +14 -0
- data/spec/support/fake_safe_buffer.rb +15 -0
- data/spec/support/helper_methods.rb +96 -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 +249 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- metadata +71 -34
- data/lib/faraday/adapter/em_http.rb +0 -243
- data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
- data/lib/faraday/adapter/em_synchrony.rb +0 -106
- data/lib/faraday/adapter/excon.rb +0 -80
- data/lib/faraday/adapter/httpclient.rb +0 -128
- data/lib/faraday/adapter/net_http.rb +0 -135
- data/lib/faraday/adapter/net_http_persistent.rb +0 -54
- data/lib/faraday/adapter/patron.rb +0 -83
- data/lib/faraday/adapter/rack.rb +0 -58
- data/lib/faraday/adapter/typhoeus.rb +0 -123
- data/lib/faraday/autoload.rb +0 -84
- data/lib/faraday/request/basic_authentication.rb +0 -13
- data/lib/faraday/request/multipart.rb +0 -68
- data/lib/faraday/request/retry.rb +0 -164
- data/lib/faraday/request/token_authentication.rb +0 -15
- data/lib/faraday/upload_io.rb +0 -67
@@ -1,164 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
# Catches exceptions and retries each request a limited number of times.
|
3
|
-
#
|
4
|
-
# By default, it retries 2 times and handles only timeout exceptions. It can
|
5
|
-
# be configured with an arbitrary number of retries, a list of exceptions to
|
6
|
-
# handle, a retry interval, a percentage of randomness to add to the retry
|
7
|
-
# interval, and a backoff factor.
|
8
|
-
#
|
9
|
-
# Examples
|
10
|
-
#
|
11
|
-
# Faraday.new do |conn|
|
12
|
-
# conn.request :retry, max: 2, interval: 0.05,
|
13
|
-
# interval_randomness: 0.5, backoff_factor: 2,
|
14
|
-
# exceptions: [CustomException, 'Timeout::Error']
|
15
|
-
# conn.adapter ...
|
16
|
-
# end
|
17
|
-
#
|
18
|
-
# This example will result in a first interval that is random between 0.05 and 0.075 and a second
|
19
|
-
# interval that is random between 0.1 and 0.15
|
20
|
-
#
|
21
|
-
class Request::Retry < Faraday::Middleware
|
22
|
-
|
23
|
-
IDEMPOTENT_METHODS = [:delete, :get, :head, :options, :put]
|
24
|
-
|
25
|
-
class Options < Faraday::Options.new(:max, :interval, :max_interval, :interval_randomness,
|
26
|
-
:backoff_factor, :exceptions, :methods, :retry_if)
|
27
|
-
DEFAULT_CHECK = lambda { |env,exception| false }
|
28
|
-
|
29
|
-
def self.from(value)
|
30
|
-
if Integer === value
|
31
|
-
new(value)
|
32
|
-
else
|
33
|
-
super(value)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def max
|
38
|
-
(self[:max] ||= 2).to_i
|
39
|
-
end
|
40
|
-
|
41
|
-
def interval
|
42
|
-
(self[:interval] ||= 0).to_f
|
43
|
-
end
|
44
|
-
|
45
|
-
def max_interval
|
46
|
-
(self[:max_interval] ||= Float::MAX).to_f
|
47
|
-
end
|
48
|
-
|
49
|
-
def interval_randomness
|
50
|
-
(self[:interval_randomness] ||= 0).to_f
|
51
|
-
end
|
52
|
-
|
53
|
-
def backoff_factor
|
54
|
-
(self[:backoff_factor] ||= 1).to_f
|
55
|
-
end
|
56
|
-
|
57
|
-
def exceptions
|
58
|
-
Array(self[:exceptions] ||= [Errno::ETIMEDOUT, 'Timeout::Error',
|
59
|
-
Error::TimeoutError])
|
60
|
-
end
|
61
|
-
|
62
|
-
def methods
|
63
|
-
Array(self[:methods] ||= IDEMPOTENT_METHODS)
|
64
|
-
end
|
65
|
-
|
66
|
-
def retry_if
|
67
|
-
self[:retry_if] ||= DEFAULT_CHECK
|
68
|
-
end
|
69
|
-
|
70
|
-
end
|
71
|
-
|
72
|
-
# Public: Initialize middleware
|
73
|
-
#
|
74
|
-
# Options:
|
75
|
-
# max - Maximum number of retries (default: 2)
|
76
|
-
# interval - Pause in seconds between retries (default: 0)
|
77
|
-
# interval_randomness - The maximum random interval amount expressed
|
78
|
-
# as a float between 0 and 1 to use in addition to the
|
79
|
-
# interval. (default: 0)
|
80
|
-
# max_interval - An upper limit for the interval (default: Float::MAX)
|
81
|
-
# backoff_factor - The amount to multiple each successive retry's
|
82
|
-
# interval amount by in order to provide backoff
|
83
|
-
# (default: 1)
|
84
|
-
# exceptions - The list of exceptions to handle. Exceptions can be
|
85
|
-
# given as Class, Module, or String. (default:
|
86
|
-
# [Errno::ETIMEDOUT, Timeout::Error,
|
87
|
-
# Error::TimeoutError])
|
88
|
-
# methods - A list of HTTP methods to retry without calling retry_if. Pass
|
89
|
-
# an empty Array to call retry_if for all exceptions.
|
90
|
-
# (defaults to the idempotent HTTP methods in IDEMPOTENT_METHODS)
|
91
|
-
# retry_if - block that will receive the env object and the exception raised
|
92
|
-
# and should decide if the code should retry still the action or
|
93
|
-
# not independent of the retry count. This would be useful
|
94
|
-
# if the exception produced is non-recoverable or if the
|
95
|
-
# the HTTP method called is not idempotent.
|
96
|
-
# (defaults to return false)
|
97
|
-
def initialize(app, options = nil)
|
98
|
-
super(app)
|
99
|
-
@options = Options.from(options)
|
100
|
-
@errmatch = build_exception_matcher(@options.exceptions)
|
101
|
-
end
|
102
|
-
|
103
|
-
def sleep_amount(retries)
|
104
|
-
retry_index = @options.max - retries
|
105
|
-
current_interval = @options.interval * (@options.backoff_factor ** retry_index)
|
106
|
-
current_interval = [current_interval, @options.max_interval].min
|
107
|
-
random_interval = rand * @options.interval_randomness.to_f * @options.interval
|
108
|
-
current_interval + random_interval
|
109
|
-
end
|
110
|
-
|
111
|
-
def call(env)
|
112
|
-
retries = @options.max
|
113
|
-
request_body = env[:body]
|
114
|
-
begin
|
115
|
-
env[:body] = request_body # after failure env[:body] is set to the response body
|
116
|
-
@app.call(env)
|
117
|
-
rescue @errmatch => exception
|
118
|
-
if retries > 0 && retry_request?(env, exception)
|
119
|
-
retries -= 1
|
120
|
-
rewind_files(request_body)
|
121
|
-
sleep sleep_amount(retries + 1)
|
122
|
-
retry
|
123
|
-
end
|
124
|
-
raise
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
# Private: construct an exception matcher object.
|
129
|
-
#
|
130
|
-
# An exception matcher for the rescue clause can usually be any object that
|
131
|
-
# responds to `===`, but for Ruby 1.8 it has to be a Class or Module.
|
132
|
-
def build_exception_matcher(exceptions)
|
133
|
-
matcher = Module.new
|
134
|
-
(class << matcher; self; end).class_eval do
|
135
|
-
define_method(:===) do |error|
|
136
|
-
exceptions.any? do |ex|
|
137
|
-
if ex.is_a? Module
|
138
|
-
error.is_a? ex
|
139
|
-
else
|
140
|
-
error.class.to_s == ex.to_s
|
141
|
-
end
|
142
|
-
end
|
143
|
-
end
|
144
|
-
end
|
145
|
-
matcher
|
146
|
-
end
|
147
|
-
|
148
|
-
private
|
149
|
-
|
150
|
-
def retry_request?(env, exception)
|
151
|
-
@options.methods.include?(env[:method]) || @options.retry_if.call(env, exception)
|
152
|
-
end
|
153
|
-
|
154
|
-
def rewind_files(body)
|
155
|
-
return unless body.is_a?(Hash)
|
156
|
-
body.each do |_, value|
|
157
|
-
if value.is_a? UploadIO
|
158
|
-
value.rewind
|
159
|
-
end
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
end
|
164
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
class Request::TokenAuthentication < Request.load_middleware(:authorization)
|
3
|
-
# Public
|
4
|
-
def self.header(token, options = nil)
|
5
|
-
options ||= {}
|
6
|
-
options[:token] = token
|
7
|
-
super(:Token, options)
|
8
|
-
end
|
9
|
-
|
10
|
-
def initialize(app, token, options = nil)
|
11
|
-
super(app, token, options)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
data/lib/faraday/upload_io.rb
DELETED
@@ -1,67 +0,0 @@
|
|
1
|
-
begin
|
2
|
-
require 'composite_io'
|
3
|
-
require 'parts'
|
4
|
-
require 'stringio'
|
5
|
-
rescue LoadError
|
6
|
-
$stderr.puts "Install the multipart-post gem."
|
7
|
-
raise
|
8
|
-
end
|
9
|
-
|
10
|
-
module Faraday
|
11
|
-
# Similar but not compatible with ::CompositeReadIO provided by multipart-post.
|
12
|
-
class CompositeReadIO
|
13
|
-
def initialize(*parts)
|
14
|
-
@parts = parts.flatten
|
15
|
-
@ios = @parts.map { |part| part.to_io }
|
16
|
-
@index = 0
|
17
|
-
end
|
18
|
-
|
19
|
-
def length
|
20
|
-
@parts.inject(0) { |sum, part| sum + part.length }
|
21
|
-
end
|
22
|
-
|
23
|
-
def rewind
|
24
|
-
@ios.each { |io| io.rewind }
|
25
|
-
@index = 0
|
26
|
-
end
|
27
|
-
|
28
|
-
# Read from IOs in order until `length` bytes have been received.
|
29
|
-
def read(length = nil, outbuf = nil)
|
30
|
-
got_result = false
|
31
|
-
outbuf = outbuf ? outbuf.replace("") : ""
|
32
|
-
|
33
|
-
while io = current_io
|
34
|
-
if result = io.read(length)
|
35
|
-
got_result ||= !result.nil?
|
36
|
-
result.force_encoding("BINARY") if result.respond_to?(:force_encoding)
|
37
|
-
outbuf << result
|
38
|
-
length -= result.length if length
|
39
|
-
break if length == 0
|
40
|
-
end
|
41
|
-
advance_io
|
42
|
-
end
|
43
|
-
(!got_result && length) ? nil : outbuf
|
44
|
-
end
|
45
|
-
|
46
|
-
def close
|
47
|
-
@ios.each { |io| io.close }
|
48
|
-
end
|
49
|
-
|
50
|
-
def ensure_open_and_readable
|
51
|
-
# Rubinius compatibility
|
52
|
-
end
|
53
|
-
|
54
|
-
private
|
55
|
-
|
56
|
-
def current_io
|
57
|
-
@ios[@index]
|
58
|
-
end
|
59
|
-
|
60
|
-
def advance_io
|
61
|
-
@index += 1
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
UploadIO = ::UploadIO
|
66
|
-
Parts = ::Parts
|
67
|
-
end
|