faraday 0.17.6 → 1.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +156 -8
- data/LICENSE.md +1 -1
- data/README.md +16 -358
- data/Rakefile +1 -7
- data/examples/client_spec.rb +97 -0
- data/examples/client_test.rb +118 -0
- data/lib/faraday/adapter/test.rb +118 -69
- data/lib/faraday/adapter/typhoeus.rb +4 -1
- data/lib/faraday/adapter.rb +72 -22
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/autoload.rb +39 -36
- data/lib/faraday/connection.rb +343 -185
- data/lib/faraday/dependency_loader.rb +37 -0
- 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 +28 -40
- data/lib/faraday/logging/formatter.rb +105 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +19 -25
- data/lib/faraday/middleware_registry.rb +129 -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 +36 -191
- data/lib/faraday/parameters.rb +4 -197
- data/lib/faraday/rack_builder.rb +76 -64
- data/lib/faraday/request/authorization.rb +51 -30
- data/lib/faraday/request/basic_authentication.rb +14 -7
- data/lib/faraday/request/instrumentation.rb +45 -27
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request/token_authentication.rb +15 -10
- data/lib/faraday/request/url_encoded.rb +43 -23
- data/lib/faraday/request.rb +83 -44
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +20 -69
- data/lib/faraday/response/raise_error.rb +49 -18
- data/lib/faraday/response.rb +26 -20
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +38 -247
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +134 -188
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/em_http_spec.rb +49 -0
- data/spec/faraday/adapter/em_synchrony_spec.rb +18 -0
- data/spec/faraday/adapter/excon_spec.rb +49 -0
- data/spec/faraday/adapter/httpclient_spec.rb +73 -0
- data/spec/faraday/adapter/net_http_spec.rb +64 -0
- data/spec/faraday/adapter/patron_spec.rb +18 -0
- data/spec/faraday/adapter/rack_spec.rb +8 -0
- data/spec/faraday/adapter/test_spec.rb +377 -0
- data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
- data/spec/faraday/adapter_registry_spec.rb +28 -0
- data/spec/faraday/adapter_spec.rb +55 -0
- data/spec/faraday/composite_read_io_spec.rb +80 -0
- data/spec/faraday/connection_spec.rb +736 -0
- data/spec/faraday/error_spec.rb +12 -54
- 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 +345 -0
- data/spec/faraday/request/authorization_spec.rb +96 -0
- data/spec/faraday/request/instrumentation_spec.rb +76 -0
- data/spec/faraday/request/json_spec.rb +111 -0
- data/spec/faraday/request/url_encoded_spec.rb +83 -0
- data/spec/faraday/request_spec.rb +120 -0
- data/spec/faraday/response/json_spec.rb +119 -0
- data/spec/faraday/response/logger_spec.rb +220 -0
- data/spec/faraday/response/middleware_spec.rb +68 -0
- data/spec/faraday/response/raise_error_spec.rb +78 -15
- data/spec/faraday/response_spec.rb +75 -0
- data/spec/faraday/utils/headers_spec.rb +82 -0
- data/spec/faraday/utils_spec.rb +56 -0
- data/spec/faraday_spec.rb +37 -0
- data/spec/spec_helper.rb +63 -36
- data/spec/support/disabling_stub.rb +14 -0
- data/spec/support/fake_safe_buffer.rb +15 -0
- data/spec/support/helper_methods.rb +133 -0
- data/spec/support/shared_examples/adapter.rb +105 -0
- data/spec/support/shared_examples/params_encoder.rb +18 -0
- data/spec/support/shared_examples/request_method.rb +262 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- data/spec/support/webmock_rack_app.rb +68 -0
- metadata +210 -58
- 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 -82
- data/lib/faraday/adapter/httpclient.rb +0 -128
- data/lib/faraday/adapter/net_http.rb +0 -153
- data/lib/faraday/adapter/net_http_persistent.rb +0 -68
- data/lib/faraday/adapter/patron.rb +0 -95
- data/lib/faraday/adapter/rack.rb +0 -58
- data/lib/faraday/deprecate.rb +0 -109
- data/lib/faraday/request/multipart.rb +0 -68
- data/lib/faraday/request/retry.rb +0 -213
- data/lib/faraday/upload_io.rb +0 -77
- data/spec/faraday/deprecate_spec.rb +0 -147
- data/test/adapters/default_test.rb +0 -14
- data/test/adapters/em_http_test.rb +0 -30
- data/test/adapters/em_synchrony_test.rb +0 -32
- data/test/adapters/excon_test.rb +0 -30
- data/test/adapters/httpclient_test.rb +0 -34
- data/test/adapters/integration.rb +0 -263
- data/test/adapters/logger_test.rb +0 -136
- data/test/adapters/net_http_persistent_test.rb +0 -114
- data/test/adapters/net_http_test.rb +0 -79
- data/test/adapters/patron_test.rb +0 -40
- data/test/adapters/rack_test.rb +0 -38
- data/test/adapters/test_middleware_test.rb +0 -157
- data/test/adapters/typhoeus_test.rb +0 -38
- data/test/authentication_middleware_test.rb +0 -65
- data/test/composite_read_io_test.rb +0 -109
- data/test/connection_test.rb +0 -738
- data/test/env_test.rb +0 -268
- data/test/helper.rb +0 -75
- data/test/live_server.rb +0 -67
- data/test/middleware/instrumentation_test.rb +0 -88
- data/test/middleware/retry_test.rb +0 -282
- data/test/middleware_stack_test.rb +0 -260
- data/test/multibyte.txt +0 -1
- data/test/options_test.rb +0 -333
- data/test/parameters_test.rb +0 -157
- data/test/request_middleware_test.rb +0 -126
- data/test/response_middleware_test.rb +0 -72
- data/test/strawberry.rb +0 -2
- data/test/utils_test.rb +0 -98
data/lib/faraday/adapter/rack.rb
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
module Faraday
|
2
|
-
class Adapter
|
3
|
-
# Sends requests to a Rack app.
|
4
|
-
#
|
5
|
-
# Examples
|
6
|
-
#
|
7
|
-
# class MyRackApp
|
8
|
-
# def call(env)
|
9
|
-
# [200, {'Content-Type' => 'text/html'}, ["hello world"]]
|
10
|
-
# end
|
11
|
-
# end
|
12
|
-
#
|
13
|
-
# Faraday.new do |conn|
|
14
|
-
# conn.adapter :rack, MyRackApp.new
|
15
|
-
# end
|
16
|
-
class Rack < Faraday::Adapter
|
17
|
-
dependency 'rack/test'
|
18
|
-
|
19
|
-
# not prefixed with "HTTP_"
|
20
|
-
SPECIAL_HEADERS = %w[ CONTENT_LENGTH CONTENT_TYPE ]
|
21
|
-
|
22
|
-
def initialize(faraday_app, rack_app)
|
23
|
-
super(faraday_app)
|
24
|
-
mock_session = ::Rack::MockSession.new(rack_app)
|
25
|
-
@session = ::Rack::Test::Session.new(mock_session)
|
26
|
-
end
|
27
|
-
|
28
|
-
def call(env)
|
29
|
-
super
|
30
|
-
rack_env = {
|
31
|
-
:method => env[:method],
|
32
|
-
:input => env[:body].respond_to?(:read) ? env[:body].read : env[:body],
|
33
|
-
'rack.url_scheme' => env[:url].scheme
|
34
|
-
}
|
35
|
-
|
36
|
-
env[:request_headers].each do |name, value|
|
37
|
-
name = name.upcase.tr('-', '_')
|
38
|
-
name = "HTTP_#{name}" unless SPECIAL_HEADERS.include? name
|
39
|
-
rack_env[name] = value
|
40
|
-
end if env[:request_headers]
|
41
|
-
|
42
|
-
timeout = env[:request][:timeout] || env[:request][:open_timeout]
|
43
|
-
response = if timeout
|
44
|
-
Timer.timeout(timeout, Faraday::TimeoutError) { execute_request(env, rack_env) }
|
45
|
-
else
|
46
|
-
execute_request(env, rack_env)
|
47
|
-
end
|
48
|
-
|
49
|
-
save_response(env, response.status, response.body, response.headers)
|
50
|
-
@app.call env
|
51
|
-
end
|
52
|
-
|
53
|
-
def execute_request(env, rack_env)
|
54
|
-
@session.request(env[:url].to_s, rack_env)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
data/lib/faraday/deprecate.rb
DELETED
@@ -1,109 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Faraday
|
4
|
-
# @param new_klass [Class] new Klass to use
|
5
|
-
#
|
6
|
-
# @return [Class] A modified version of new_klass that warns on
|
7
|
-
# usage about deprecation.
|
8
|
-
# @see Faraday::Deprecate
|
9
|
-
module DeprecatedClass
|
10
|
-
def self.proxy_class(origclass, ver = '1.0')
|
11
|
-
proxy = Class.new(origclass) do
|
12
|
-
const_set("ORIG_CLASS", origclass)
|
13
|
-
|
14
|
-
class << self
|
15
|
-
extend Faraday::Deprecate
|
16
|
-
|
17
|
-
def ===(other)
|
18
|
-
(superclass == const_get("ORIG_CLASS") && other.is_a?(superclass)) || super
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
proxy.singleton_class.send(:deprecate, :new, "#{origclass}.new", ver)
|
23
|
-
proxy.singleton_class.send(:deprecate, :inherited, origclass.name, ver)
|
24
|
-
proxy
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
# Deprecation using semver instead of date, based on Gem::Deprecate
|
29
|
-
# Provides a single method +deprecate+ to be used to declare when
|
30
|
-
# something is going away.
|
31
|
-
#
|
32
|
-
# class Legacy
|
33
|
-
# def self.klass_method
|
34
|
-
# # ...
|
35
|
-
# end
|
36
|
-
#
|
37
|
-
# def instance_method
|
38
|
-
# # ...
|
39
|
-
# end
|
40
|
-
#
|
41
|
-
# extend Faraday::Deprecate
|
42
|
-
# deprecate :instance_method, "X.z", '1.0'
|
43
|
-
#
|
44
|
-
# class << self
|
45
|
-
# extend Faraday::Deprecate
|
46
|
-
# deprecate :klass_method, :none, '1.0'
|
47
|
-
# end
|
48
|
-
# end
|
49
|
-
module Deprecate
|
50
|
-
def self.skip # :nodoc:
|
51
|
-
@skip ||= begin
|
52
|
-
case ENV['FARADAY_DEPRECATE'].to_s.downcase
|
53
|
-
when '1', 'warn' then :warn
|
54
|
-
else :skip
|
55
|
-
end
|
56
|
-
end
|
57
|
-
@skip == :skip
|
58
|
-
end
|
59
|
-
|
60
|
-
def self.skip=(value) # :nodoc:
|
61
|
-
@skip = value ? :skip : :warn
|
62
|
-
end
|
63
|
-
|
64
|
-
# Temporarily turn off warnings. Intended for tests only.
|
65
|
-
def skip_during
|
66
|
-
original = Faraday::Deprecate.skip
|
67
|
-
Faraday::Deprecate.skip, = true
|
68
|
-
yield
|
69
|
-
ensure
|
70
|
-
Faraday::Deprecate.skip = original
|
71
|
-
end
|
72
|
-
|
73
|
-
# Simple deprecation method that deprecates +name+ by wrapping it up
|
74
|
-
# in a dummy method. It warns on each call to the dummy method
|
75
|
-
# telling the user of +repl+ (unless +repl+ is :none) and the
|
76
|
-
# semver that it is planned to go away.
|
77
|
-
# @param name [Symbol] the method symbol to deprecate
|
78
|
-
# @param repl [#to_s, :none] the replacement to use, when `:none` it will
|
79
|
-
# alert the user that no replacemtent is present.
|
80
|
-
# @param ver [String] the semver the method will be removed.
|
81
|
-
def deprecate(name, repl, ver)
|
82
|
-
class_eval do
|
83
|
-
gem_ver = Gem::Version.new(ver)
|
84
|
-
old = "_deprecated_#{name}"
|
85
|
-
alias_method old, name
|
86
|
-
define_method name do |*args, &block|
|
87
|
-
mod = is_a? Module
|
88
|
-
target = mod ? "#{self}." : "#{self.class}#"
|
89
|
-
target_message = if name == :inherited
|
90
|
-
"Inheriting #{self}"
|
91
|
-
else
|
92
|
-
"#{target}#{name}"
|
93
|
-
end
|
94
|
-
|
95
|
-
msg = [
|
96
|
-
"NOTE: #{target_message} is deprecated",
|
97
|
-
repl == :none ? ' with no replacement' : "; use #{repl} instead. ",
|
98
|
-
"It will be removed in or after version #{gem_ver}",
|
99
|
-
"\n#{target}#{name} called from #{Gem.location_of_caller.join(':')}"
|
100
|
-
]
|
101
|
-
warn "#{msg.join}." unless Faraday::Deprecate.skip
|
102
|
-
send old, *args, &block
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
module_function :deprecate, :skip_during
|
108
|
-
end
|
109
|
-
end
|
@@ -1,68 +0,0 @@
|
|
1
|
-
require File.expand_path("../url_encoded", __FILE__)
|
2
|
-
require 'securerandom'
|
3
|
-
|
4
|
-
module Faraday
|
5
|
-
class Request::Multipart < Request::UrlEncoded
|
6
|
-
self.mime_type = 'multipart/form-data'.freeze
|
7
|
-
DEFAULT_BOUNDARY_PREFIX = "-----------RubyMultipartPost".freeze unless defined? DEFAULT_BOUNDARY_PREFIX
|
8
|
-
|
9
|
-
def call(env)
|
10
|
-
match_content_type(env) do |params|
|
11
|
-
env.request.boundary ||= unique_boundary
|
12
|
-
env.request_headers[CONTENT_TYPE] += "; boundary=#{env.request.boundary}"
|
13
|
-
env.body = create_multipart(env, params)
|
14
|
-
end
|
15
|
-
@app.call env
|
16
|
-
end
|
17
|
-
|
18
|
-
def process_request?(env)
|
19
|
-
type = request_type(env)
|
20
|
-
env.body.respond_to?(:each_key) and !env.body.empty? and (
|
21
|
-
(type.empty? and has_multipart?(env.body)) or
|
22
|
-
type == self.class.mime_type
|
23
|
-
)
|
24
|
-
end
|
25
|
-
|
26
|
-
def has_multipart?(obj)
|
27
|
-
# string is an enum in 1.8, returning list of itself
|
28
|
-
if obj.respond_to?(:each) && !obj.is_a?(String)
|
29
|
-
(obj.respond_to?(:values) ? obj.values : obj).each do |val|
|
30
|
-
return true if (val.respond_to?(:content_type) || has_multipart?(val))
|
31
|
-
end
|
32
|
-
end
|
33
|
-
false
|
34
|
-
end
|
35
|
-
|
36
|
-
def create_multipart(env, params)
|
37
|
-
boundary = env.request.boundary
|
38
|
-
parts = process_params(params) do |key, value|
|
39
|
-
Faraday::Parts::Part.new(boundary, key, value)
|
40
|
-
end
|
41
|
-
parts << Faraday::Parts::EpiloguePart.new(boundary)
|
42
|
-
|
43
|
-
body = Faraday::CompositeReadIO.new(parts)
|
44
|
-
env.request_headers[Faraday::Env::ContentLength] = body.length.to_s
|
45
|
-
return body
|
46
|
-
end
|
47
|
-
|
48
|
-
def unique_boundary
|
49
|
-
"#{DEFAULT_BOUNDARY_PREFIX}-#{SecureRandom.hex}"
|
50
|
-
end
|
51
|
-
|
52
|
-
def process_params(params, prefix = nil, pieces = nil, &block)
|
53
|
-
params.inject(pieces || []) do |all, (key, value)|
|
54
|
-
key = "#{prefix}[#{key}]" if prefix
|
55
|
-
|
56
|
-
case value
|
57
|
-
when Array
|
58
|
-
values = value.inject([]) { |a,v| a << [nil, v] }
|
59
|
-
process_params(values, key, all, &block)
|
60
|
-
when Hash
|
61
|
-
process_params(value, key, all, &block)
|
62
|
-
else
|
63
|
-
all << block.call(key, value)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
@@ -1,213 +0,0 @@
|
|
1
|
-
require 'date'
|
2
|
-
|
3
|
-
module Faraday
|
4
|
-
# Catches exceptions and retries each request a limited number of times.
|
5
|
-
#
|
6
|
-
# By default, it retries 2 times and handles only timeout exceptions. It can
|
7
|
-
# be configured with an arbitrary number of retries, a list of exceptions to
|
8
|
-
# handle, a retry interval, a percentage of randomness to add to the retry
|
9
|
-
# interval, and a backoff factor.
|
10
|
-
#
|
11
|
-
# Examples
|
12
|
-
#
|
13
|
-
# Faraday.new do |conn|
|
14
|
-
# conn.request :retry, max: 2, interval: 0.05,
|
15
|
-
# interval_randomness: 0.5, backoff_factor: 2,
|
16
|
-
# exceptions: [CustomException, 'Timeout::Error']
|
17
|
-
# conn.adapter ...
|
18
|
-
# end
|
19
|
-
#
|
20
|
-
# This example will result in a first interval that is random between 0.05 and 0.075 and a second
|
21
|
-
# interval that is random between 0.1 and 0.15
|
22
|
-
#
|
23
|
-
class Request::Retry < Faraday::Middleware
|
24
|
-
DEFAULT_EXCEPTIONS = [Errno::ETIMEDOUT, 'Timeout::Error',
|
25
|
-
Faraday::TimeoutError, Faraday::RetriableResponse
|
26
|
-
].freeze
|
27
|
-
IDEMPOTENT_METHODS = [:delete, :get, :head, :options, :put]
|
28
|
-
|
29
|
-
class Options < Faraday::Options.new(:max, :interval, :max_interval, :interval_randomness,
|
30
|
-
:backoff_factor, :exceptions, :methods, :retry_if, :retry_block,
|
31
|
-
:retry_statuses)
|
32
|
-
|
33
|
-
DEFAULT_CHECK = lambda { |env,exception| false }
|
34
|
-
|
35
|
-
def self.from(value)
|
36
|
-
if Integer === value
|
37
|
-
new(value)
|
38
|
-
else
|
39
|
-
super(value)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def max
|
44
|
-
(self[:max] ||= 2).to_i
|
45
|
-
end
|
46
|
-
|
47
|
-
def interval
|
48
|
-
(self[:interval] ||= 0).to_f
|
49
|
-
end
|
50
|
-
|
51
|
-
def max_interval
|
52
|
-
(self[:max_interval] ||= Float::MAX).to_f
|
53
|
-
end
|
54
|
-
|
55
|
-
def interval_randomness
|
56
|
-
(self[:interval_randomness] ||= 0).to_f
|
57
|
-
end
|
58
|
-
|
59
|
-
def backoff_factor
|
60
|
-
(self[:backoff_factor] ||= 1).to_f
|
61
|
-
end
|
62
|
-
|
63
|
-
def exceptions
|
64
|
-
Array(self[:exceptions] ||= DEFAULT_EXCEPTIONS)
|
65
|
-
end
|
66
|
-
|
67
|
-
def methods
|
68
|
-
Array(self[:methods] ||= IDEMPOTENT_METHODS)
|
69
|
-
end
|
70
|
-
|
71
|
-
def retry_if
|
72
|
-
self[:retry_if] ||= DEFAULT_CHECK
|
73
|
-
end
|
74
|
-
|
75
|
-
def retry_block
|
76
|
-
self[:retry_block] ||= Proc.new {}
|
77
|
-
end
|
78
|
-
|
79
|
-
def retry_statuses
|
80
|
-
Array(self[:retry_statuses] ||= [])
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
# Public: Initialize middleware
|
85
|
-
#
|
86
|
-
# Options:
|
87
|
-
# max - Maximum number of retries (default: 2)
|
88
|
-
# interval - Pause in seconds between retries (default: 0)
|
89
|
-
# interval_randomness - The maximum random interval amount expressed
|
90
|
-
# as a float between 0 and 1 to use in addition to the
|
91
|
-
# interval. (default: 0)
|
92
|
-
# max_interval - An upper limit for the interval (default: Float::MAX)
|
93
|
-
# backoff_factor - The amount to multiple each successive retry's
|
94
|
-
# interval amount by in order to provide backoff
|
95
|
-
# (default: 1)
|
96
|
-
# exceptions - The list of exceptions to handle. Exceptions can be
|
97
|
-
# given as Class, Module, or String. (default:
|
98
|
-
# [Errno::ETIMEDOUT, 'Timeout::Error',
|
99
|
-
# Faraday::TimeoutError, Faraday::RetriableResponse])
|
100
|
-
# methods - A list of HTTP methods to retry without calling retry_if. Pass
|
101
|
-
# an empty Array to call retry_if for all exceptions.
|
102
|
-
# (defaults to the idempotent HTTP methods in IDEMPOTENT_METHODS)
|
103
|
-
# retry_if - block that will receive the env object and the exception raised
|
104
|
-
# and should decide if the code should retry still the action or
|
105
|
-
# not independent of the retry count. This would be useful
|
106
|
-
# if the exception produced is non-recoverable or if the
|
107
|
-
# the HTTP method called is not idempotent.
|
108
|
-
# (defaults to return false)
|
109
|
-
# retry_block - block that is executed after every retry. Request environment, middleware options,
|
110
|
-
# current number of retries and the exception is passed to the block as parameters.
|
111
|
-
def initialize(app, options = nil)
|
112
|
-
super(app)
|
113
|
-
@options = Options.from(options)
|
114
|
-
@errmatch = build_exception_matcher(@options.exceptions)
|
115
|
-
end
|
116
|
-
|
117
|
-
def calculate_sleep_amount(retries, env)
|
118
|
-
retry_after = calculate_retry_after(env)
|
119
|
-
retry_interval = calculate_retry_interval(retries)
|
120
|
-
|
121
|
-
return if retry_after && retry_after > @options.max_interval
|
122
|
-
|
123
|
-
retry_after && retry_after >= retry_interval ? retry_after : retry_interval
|
124
|
-
end
|
125
|
-
|
126
|
-
def call(env)
|
127
|
-
retries = @options.max
|
128
|
-
request_body = env[:body]
|
129
|
-
begin
|
130
|
-
env[:body] = request_body # after failure env[:body] is set to the response body
|
131
|
-
@app.call(env).tap do |resp|
|
132
|
-
raise Faraday::RetriableResponse.new(nil, resp) if @options.retry_statuses.include?(resp.status)
|
133
|
-
end
|
134
|
-
rescue @errmatch => exception
|
135
|
-
if retries > 0 && retry_request?(env, exception)
|
136
|
-
retries -= 1
|
137
|
-
rewind_files(request_body)
|
138
|
-
@options.retry_block.call(env, @options, retries, exception)
|
139
|
-
if (sleep_amount = calculate_sleep_amount(retries + 1, env))
|
140
|
-
sleep sleep_amount
|
141
|
-
retry
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
if exception.is_a?(Faraday::RetriableResponse)
|
146
|
-
exception.response
|
147
|
-
else
|
148
|
-
raise
|
149
|
-
end
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
# Private: construct an exception matcher object.
|
154
|
-
#
|
155
|
-
# An exception matcher for the rescue clause can usually be any object that
|
156
|
-
# responds to `===`, but for Ruby 1.8 it has to be a Class or Module.
|
157
|
-
def build_exception_matcher(exceptions)
|
158
|
-
matcher = Module.new
|
159
|
-
(class << matcher; self; end).class_eval do
|
160
|
-
define_method(:===) do |error|
|
161
|
-
exceptions.any? do |ex|
|
162
|
-
if ex.is_a? Module
|
163
|
-
error.is_a? ex
|
164
|
-
else
|
165
|
-
error.class.to_s == ex.to_s
|
166
|
-
end
|
167
|
-
end
|
168
|
-
end
|
169
|
-
end
|
170
|
-
matcher
|
171
|
-
end
|
172
|
-
|
173
|
-
private
|
174
|
-
|
175
|
-
def retry_request?(env, exception)
|
176
|
-
@options.methods.include?(env[:method]) || @options.retry_if.call(env, exception)
|
177
|
-
end
|
178
|
-
|
179
|
-
def rewind_files(body)
|
180
|
-
return unless body.is_a?(Hash)
|
181
|
-
body.each do |_, value|
|
182
|
-
if value.is_a? UploadIO
|
183
|
-
value.rewind
|
184
|
-
end
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
|
-
# MDN spec for Retry-After header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
|
189
|
-
def calculate_retry_after(env)
|
190
|
-
response_headers = env[:response_headers]
|
191
|
-
return unless response_headers
|
192
|
-
|
193
|
-
retry_after_value = env[:response_headers]["Retry-After"]
|
194
|
-
|
195
|
-
# Try to parse date from the header value
|
196
|
-
begin
|
197
|
-
datetime = DateTime.rfc2822(retry_after_value)
|
198
|
-
datetime.to_time - Time.now.utc
|
199
|
-
rescue ArgumentError
|
200
|
-
retry_after_value.to_f
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
def calculate_retry_interval(retries)
|
205
|
-
retry_index = @options.max - retries
|
206
|
-
current_interval = @options.interval * (@options.backoff_factor ** retry_index)
|
207
|
-
current_interval = [current_interval, @options.max_interval].min
|
208
|
-
random_interval = rand * @options.interval_randomness.to_f * @options.interval
|
209
|
-
|
210
|
-
current_interval + random_interval
|
211
|
-
end
|
212
|
-
end
|
213
|
-
end
|
data/lib/faraday/upload_io.rb
DELETED
@@ -1,77 +0,0 @@
|
|
1
|
-
begin
|
2
|
-
require 'multipart/post'
|
3
|
-
require 'stringio'
|
4
|
-
rescue LoadError
|
5
|
-
begin
|
6
|
-
require 'composite_io'
|
7
|
-
require 'parts'
|
8
|
-
require 'stringio'
|
9
|
-
rescue LoadError
|
10
|
-
$stderr.puts "Install the multipart-post gem."
|
11
|
-
raise
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
module Faraday
|
16
|
-
# Similar but not compatible with ::CompositeReadIO provided by multipart-post.
|
17
|
-
class CompositeReadIO
|
18
|
-
def initialize(*parts)
|
19
|
-
@parts = parts.flatten
|
20
|
-
@ios = @parts.map { |part| part.to_io }
|
21
|
-
@index = 0
|
22
|
-
end
|
23
|
-
|
24
|
-
def length
|
25
|
-
@parts.inject(0) { |sum, part| sum + part.length }
|
26
|
-
end
|
27
|
-
|
28
|
-
def rewind
|
29
|
-
@ios.each { |io| io.rewind }
|
30
|
-
@index = 0
|
31
|
-
end
|
32
|
-
|
33
|
-
# Read from IOs in order until `length` bytes have been received.
|
34
|
-
def read(length = nil, outbuf = nil)
|
35
|
-
got_result = false
|
36
|
-
outbuf = outbuf ? outbuf.replace("") : ""
|
37
|
-
|
38
|
-
while io = current_io
|
39
|
-
if result = io.read(length)
|
40
|
-
got_result ||= !result.nil?
|
41
|
-
result.force_encoding("BINARY") if result.respond_to?(:force_encoding)
|
42
|
-
outbuf << result
|
43
|
-
length -= result.length if length
|
44
|
-
break if length == 0
|
45
|
-
end
|
46
|
-
advance_io
|
47
|
-
end
|
48
|
-
(!got_result && length) ? nil : outbuf
|
49
|
-
end
|
50
|
-
|
51
|
-
def close
|
52
|
-
@ios.each { |io| io.close }
|
53
|
-
end
|
54
|
-
|
55
|
-
def ensure_open_and_readable
|
56
|
-
# Rubinius compatibility
|
57
|
-
end
|
58
|
-
|
59
|
-
private
|
60
|
-
|
61
|
-
def current_io
|
62
|
-
@ios[@index]
|
63
|
-
end
|
64
|
-
|
65
|
-
def advance_io
|
66
|
-
@index += 1
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
if defined?(::Multipart::Post::UploadIO)
|
71
|
-
UploadIO = ::Multipart::Post::UploadIO
|
72
|
-
Parts = ::Multipart::Post::Parts
|
73
|
-
else
|
74
|
-
UploadIO = ::UploadIO
|
75
|
-
Parts = ::Parts
|
76
|
-
end
|
77
|
-
end
|
@@ -1,147 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Faraday::DeprecatedClass do
|
4
|
-
class SampleClass < StandardError
|
5
|
-
attr_accessor :foo
|
6
|
-
|
7
|
-
def initialize(foo = nil)
|
8
|
-
@foo = foo || :foo
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
SampleDeprecatedClass = Faraday::DeprecatedClass.proxy_class(SampleClass)
|
13
|
-
|
14
|
-
it 'does not raise error for deprecated classes but prints an error message' do
|
15
|
-
error_message, foobar = with_warn_squelching { SampleDeprecatedClass.new(:foo_bar) }
|
16
|
-
expect(foobar).to be_a(SampleClass)
|
17
|
-
expect(foobar.foo).to eq(:foo_bar)
|
18
|
-
expect(error_message).to match(
|
19
|
-
Regexp.new(
|
20
|
-
'NOTE: SampleDeprecatedClass.new is deprecated; '\
|
21
|
-
'use SampleClass.new instead. It will be removed in or after version 1.0'
|
22
|
-
)
|
23
|
-
)
|
24
|
-
end
|
25
|
-
|
26
|
-
it 'does not raise an error for inherited error-namespaced classes but prints an error message' do
|
27
|
-
error_message, = with_warn_squelching { Class.new(SampleDeprecatedClass) }
|
28
|
-
|
29
|
-
expect(error_message).to match(
|
30
|
-
Regexp.new(
|
31
|
-
'NOTE: Inheriting SampleDeprecatedClass is deprecated; '\
|
32
|
-
'use SampleClass instead. It will be removed in or after version 1.0'
|
33
|
-
)
|
34
|
-
)
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'allows backward-compatible class to be subclassed' do
|
38
|
-
expect {
|
39
|
-
with_warn_squelching { Class.new(SampleDeprecatedClass) }
|
40
|
-
}.not_to raise_error
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'allows rescuing of a current error with a deprecated error' do
|
44
|
-
expect { raise SampleClass, nil }.to raise_error(SampleDeprecatedClass)
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'allows rescuing of a current error with a current error' do
|
48
|
-
expect { raise SampleClass, nil }.to raise_error(SampleClass)
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'allows rescuing of a deprecated error with a deprecated error' do
|
52
|
-
expect { raise SampleDeprecatedClass, nil }.to raise_error(SampleDeprecatedClass)
|
53
|
-
end
|
54
|
-
|
55
|
-
it 'allows rescuing of a deprecated error with a current error' do
|
56
|
-
expect { raise SampleDeprecatedClass, nil }.to raise_error(SampleClass)
|
57
|
-
end
|
58
|
-
|
59
|
-
describe 'match behavior' do
|
60
|
-
class SampleDeprecatedClassA < SampleDeprecatedClass; end
|
61
|
-
class SampleDeprecatedClassB < SampleDeprecatedClass; end
|
62
|
-
|
63
|
-
class SampleDeprecatedClassAX < SampleDeprecatedClassA; end
|
64
|
-
|
65
|
-
class SampleClassA < SampleClass; end
|
66
|
-
|
67
|
-
describe 'undeprecated class' do
|
68
|
-
it 'is === to instance of deprecated class' do
|
69
|
-
expect(SampleClass === SampleDeprecatedClass.new).to be true
|
70
|
-
end
|
71
|
-
|
72
|
-
it 'is === to instance of subclass of deprecated class' do
|
73
|
-
expect(SampleClass === SampleDeprecatedClassA.new).to be true
|
74
|
-
end
|
75
|
-
|
76
|
-
it 'is === to instance of subclass of subclass of deprecated class' do
|
77
|
-
expect(SampleClass === SampleDeprecatedClassAX.new).to be true
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
describe 'subclass of undeprecated class' do
|
82
|
-
it 'is not === to instance of undeprecated class' do
|
83
|
-
expect(SampleClassA === SampleClass.new).to be false
|
84
|
-
end
|
85
|
-
|
86
|
-
it 'is not === to instance of deprecated class' do
|
87
|
-
expect(SampleClassA === SampleDeprecatedClass.new).to be false
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
describe 'deprecated class' do
|
92
|
-
it 'is === to instance of undeprecated class' do
|
93
|
-
expect(SampleDeprecatedClass === SampleClass.new).to be true
|
94
|
-
end
|
95
|
-
|
96
|
-
it 'is === to instance of subclass of undeprecated class' do
|
97
|
-
expect(SampleDeprecatedClass === SampleClassA.new).to be true
|
98
|
-
end
|
99
|
-
|
100
|
-
it 'is === to instance of subclass of deprecated class' do
|
101
|
-
expect(SampleDeprecatedClass === SampleDeprecatedClassA.new).to be true
|
102
|
-
end
|
103
|
-
|
104
|
-
it 'is === to instance of subclass of subclass of deprecated class' do
|
105
|
-
expect(SampleDeprecatedClass === SampleDeprecatedClassAX.new).to be true
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
describe 'subclass of deprecated class' do
|
110
|
-
it 'is not === to instance of subclass of undeprecated class' do
|
111
|
-
expect(SampleDeprecatedClassA === SampleClass.new).to be false
|
112
|
-
end
|
113
|
-
|
114
|
-
it 'is not === to instance of another subclass of deprecated class' do
|
115
|
-
expect(SampleDeprecatedClassA === SampleDeprecatedClassB.new).to be false
|
116
|
-
end
|
117
|
-
|
118
|
-
it 'is === to instance of its subclass' do
|
119
|
-
expect(SampleDeprecatedClassA === SampleDeprecatedClassAX.new).to be true
|
120
|
-
end
|
121
|
-
|
122
|
-
it 'is === to instance of deprecated class' do
|
123
|
-
expect(SampleDeprecatedClass === SampleDeprecatedClassB.new).to be true
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
describe 'subclass of subclass of deprecated class' do
|
128
|
-
it 'is not === to instance of subclass of another subclass of deprecated class' do
|
129
|
-
expect(SampleDeprecatedClassAX === SampleDeprecatedClassB.new).to be false
|
130
|
-
end
|
131
|
-
|
132
|
-
it 'is not === to instance of its superclass' do
|
133
|
-
expect(SampleDeprecatedClassA === SampleDeprecatedClass.new).to be false
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
def with_warn_squelching
|
139
|
-
stderr_catcher = StringIO.new
|
140
|
-
original_stderr = $stderr
|
141
|
-
$stderr = stderr_catcher
|
142
|
-
result = yield if block_given?
|
143
|
-
[stderr_catcher.tap(&:rewind).string, result]
|
144
|
-
ensure
|
145
|
-
$stderr = original_stderr
|
146
|
-
end
|
147
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
require File.expand_path('../integration', __FILE__)
|
2
|
-
|
3
|
-
module Adapters
|
4
|
-
class DefaultTest < Faraday::TestCase
|
5
|
-
|
6
|
-
def adapter() :default end
|
7
|
-
|
8
|
-
Integration.apply(self, :NonParallel) do
|
9
|
-
# default stack is not configured with Multipart
|
10
|
-
undef :test_POST_sends_files
|
11
|
-
end
|
12
|
-
|
13
|
-
end
|
14
|
-
end
|