faraday 0.17.3 → 2.7.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +350 -8
- data/LICENSE.md +1 -1
- data/README.md +27 -367
- data/Rakefile +1 -7
- data/examples/client_spec.rb +119 -0
- data/examples/client_test.rb +144 -0
- data/lib/faraday/adapter/test.rb +170 -72
- data/lib/faraday/adapter.rb +69 -23
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/connection.rb +309 -233
- data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +183 -0
- data/lib/faraday/error.rb +31 -42
- data/lib/faraday/logging/formatter.rb +122 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +21 -25
- data/lib/faraday/middleware_registry.rb +83 -0
- data/lib/faraday/options/connection_options.rb +22 -0
- data/lib/faraday/options/env.rb +205 -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 +69 -0
- data/lib/faraday/options.rb +38 -193
- data/lib/faraday/parameters.rb +4 -197
- data/lib/faraday/rack_builder.rb +91 -76
- data/lib/faraday/request/authorization.rb +42 -29
- data/lib/faraday/request/instrumentation.rb +47 -27
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request/url_encoded.rb +48 -24
- data/lib/faraday/request.rb +64 -42
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +24 -67
- data/lib/faraday/response/raise_error.rb +57 -18
- data/lib/faraday/response.rb +26 -32
- data/lib/faraday/utils/headers.rb +144 -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 +108 -198
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/test_spec.rb +442 -0
- data/spec/faraday/adapter_registry_spec.rb +28 -0
- data/spec/faraday/adapter_spec.rb +55 -0
- data/spec/faraday/connection_spec.rb +808 -0
- data/spec/faraday/error_spec.rb +12 -54
- data/spec/faraday/middleware_registry_spec.rb +31 -0
- data/spec/faraday/middleware_spec.rb +70 -0
- data/spec/faraday/options/env_spec.rb +76 -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 +150 -0
- data/spec/faraday/rack_builder_spec.rb +317 -0
- data/spec/faraday/request/authorization_spec.rb +118 -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 +93 -0
- data/spec/faraday/request_spec.rb +110 -0
- data/spec/faraday/response/json_spec.rb +117 -0
- data/spec/faraday/response/logger_spec.rb +248 -0
- data/spec/faraday/response/raise_error_spec.rb +81 -15
- data/spec/faraday/response_spec.rb +77 -0
- data/spec/faraday/utils/headers_spec.rb +100 -0
- data/spec/faraday/utils_spec.rb +118 -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 +96 -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 +263 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- metadata +76 -59
- 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 -152
- 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/adapter/typhoeus.rb +0 -12
- data/lib/faraday/autoload.rb +0 -84
- data/lib/faraday/deprecate.rb +0 -107
- data/lib/faraday/request/basic_authentication.rb +0 -13
- data/lib/faraday/request/multipart.rb +0 -68
- data/lib/faraday/request/retry.rb +0 -213
- data/lib/faraday/request/token_authentication.rb +0 -15
- data/lib/faraday/upload_io.rb +0 -67
- data/spec/faraday/deprecate_spec.rb +0 -69
- 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/parameters.rb
CHANGED
@@ -1,198 +1,5 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
extend Forwardable
|
7
|
-
def_delegators :'Faraday::Utils', :escape, :unescape
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.encode(params)
|
11
|
-
return nil if params == nil
|
12
|
-
|
13
|
-
if !params.is_a?(Array)
|
14
|
-
if !params.respond_to?(:to_hash)
|
15
|
-
raise TypeError,
|
16
|
-
"Can't convert #{params.class} into Hash."
|
17
|
-
end
|
18
|
-
params = params.to_hash
|
19
|
-
params = params.map do |key, value|
|
20
|
-
key = key.to_s if key.kind_of?(Symbol)
|
21
|
-
[key, value]
|
22
|
-
end
|
23
|
-
# Useful default for OAuth and caching.
|
24
|
-
# Only to be used for non-Array inputs. Arrays should preserve order.
|
25
|
-
params.sort!
|
26
|
-
end
|
27
|
-
|
28
|
-
# Helper lambda
|
29
|
-
to_query = lambda do |parent, value|
|
30
|
-
if value.is_a?(Hash)
|
31
|
-
value = value.map do |key, val|
|
32
|
-
key = escape(key)
|
33
|
-
[key, val]
|
34
|
-
end
|
35
|
-
value.sort!
|
36
|
-
buffer = ""
|
37
|
-
value.each do |key, val|
|
38
|
-
new_parent = "#{parent}%5B#{key}%5D"
|
39
|
-
buffer << "#{to_query.call(new_parent, val)}&"
|
40
|
-
end
|
41
|
-
return buffer.chop
|
42
|
-
elsif value.is_a?(Array)
|
43
|
-
new_parent = "#{parent}%5B%5D"
|
44
|
-
return new_parent if value.empty?
|
45
|
-
buffer = ""
|
46
|
-
value.each_with_index do |val, i|
|
47
|
-
buffer << "#{to_query.call(new_parent, val)}&"
|
48
|
-
end
|
49
|
-
return buffer.chop
|
50
|
-
elsif value.nil?
|
51
|
-
return parent
|
52
|
-
else
|
53
|
-
encoded_value = escape(value)
|
54
|
-
return "#{parent}=#{encoded_value}"
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
# The params have form [['key1', 'value1'], ['key2', 'value2']].
|
59
|
-
buffer = ''
|
60
|
-
params.each do |parent, value|
|
61
|
-
encoded_parent = escape(parent)
|
62
|
-
buffer << "#{to_query.call(encoded_parent, value)}&"
|
63
|
-
end
|
64
|
-
return buffer.chop
|
65
|
-
end
|
66
|
-
|
67
|
-
def self.decode(query)
|
68
|
-
return nil if query == nil
|
69
|
-
|
70
|
-
params = {}
|
71
|
-
query.split("&").each do |pair|
|
72
|
-
next if pair.empty?
|
73
|
-
key, value = pair.split("=", 2)
|
74
|
-
key = unescape(key)
|
75
|
-
value = unescape(value.gsub(/\+/, ' ')) if value
|
76
|
-
|
77
|
-
subkeys = key.scan(/[^\[\]]+(?:\]?\[\])?/)
|
78
|
-
context = params
|
79
|
-
subkeys.each_with_index do |subkey, i|
|
80
|
-
is_array = subkey =~ /[\[\]]+\Z/
|
81
|
-
subkey = $` if is_array
|
82
|
-
last_subkey = i == subkeys.length - 1
|
83
|
-
|
84
|
-
if !last_subkey || is_array
|
85
|
-
value_type = is_array ? Array : Hash
|
86
|
-
if context[subkey] && !context[subkey].is_a?(value_type)
|
87
|
-
raise TypeError, "expected %s (got %s) for param `%s'" % [
|
88
|
-
value_type.name,
|
89
|
-
context[subkey].class.name,
|
90
|
-
subkey
|
91
|
-
]
|
92
|
-
end
|
93
|
-
context = (context[subkey] ||= value_type.new)
|
94
|
-
end
|
95
|
-
|
96
|
-
if context.is_a?(Array) && !is_array
|
97
|
-
if !context.last.is_a?(Hash) || context.last.has_key?(subkey)
|
98
|
-
context << {}
|
99
|
-
end
|
100
|
-
context = context.last
|
101
|
-
end
|
102
|
-
|
103
|
-
if last_subkey
|
104
|
-
if is_array
|
105
|
-
context << value
|
106
|
-
else
|
107
|
-
context[subkey] = value
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
dehash(params, 0)
|
114
|
-
end
|
115
|
-
|
116
|
-
# Internal: convert a nested hash with purely numeric keys into an array.
|
117
|
-
# FIXME: this is not compatible with Rack::Utils.parse_nested_query
|
118
|
-
def self.dehash(hash, depth)
|
119
|
-
hash.each do |key, value|
|
120
|
-
hash[key] = dehash(value, depth + 1) if value.kind_of?(Hash)
|
121
|
-
end
|
122
|
-
|
123
|
-
if depth > 0 && !hash.empty? && hash.keys.all? { |k| k =~ /^\d+$/ }
|
124
|
-
hash.keys.sort.inject([]) { |all, key| all << hash[key] }
|
125
|
-
else
|
126
|
-
hash
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
module FlatParamsEncoder
|
132
|
-
class << self
|
133
|
-
extend Forwardable
|
134
|
-
def_delegators :'Faraday::Utils', :escape, :unescape
|
135
|
-
end
|
136
|
-
|
137
|
-
def self.encode(params)
|
138
|
-
return nil if params == nil
|
139
|
-
|
140
|
-
if !params.is_a?(Array)
|
141
|
-
if !params.respond_to?(:to_hash)
|
142
|
-
raise TypeError,
|
143
|
-
"Can't convert #{params.class} into Hash."
|
144
|
-
end
|
145
|
-
params = params.to_hash
|
146
|
-
params = params.map do |key, value|
|
147
|
-
key = key.to_s if key.kind_of?(Symbol)
|
148
|
-
[key, value]
|
149
|
-
end
|
150
|
-
# Useful default for OAuth and caching.
|
151
|
-
# Only to be used for non-Array inputs. Arrays should preserve order.
|
152
|
-
params.sort!
|
153
|
-
end
|
154
|
-
|
155
|
-
# The params have form [['key1', 'value1'], ['key2', 'value2']].
|
156
|
-
buffer = ''
|
157
|
-
params.each do |key, value|
|
158
|
-
encoded_key = escape(key)
|
159
|
-
value = value.to_s if value == true || value == false
|
160
|
-
if value == nil
|
161
|
-
buffer << "#{encoded_key}&"
|
162
|
-
elsif value.kind_of?(Array)
|
163
|
-
value.each do |sub_value|
|
164
|
-
encoded_value = escape(sub_value)
|
165
|
-
buffer << "#{encoded_key}=#{encoded_value}&"
|
166
|
-
end
|
167
|
-
else
|
168
|
-
encoded_value = escape(value)
|
169
|
-
buffer << "#{encoded_key}=#{encoded_value}&"
|
170
|
-
end
|
171
|
-
end
|
172
|
-
return buffer.chop
|
173
|
-
end
|
174
|
-
|
175
|
-
def self.decode(query)
|
176
|
-
empty_accumulator = {}
|
177
|
-
return nil if query == nil
|
178
|
-
split_query = (query.split('&').map do |pair|
|
179
|
-
pair.split('=', 2) if pair && !pair.empty?
|
180
|
-
end).compact
|
181
|
-
return split_query.inject(empty_accumulator.dup) do |accu, pair|
|
182
|
-
pair[0] = unescape(pair[0])
|
183
|
-
pair[1] = true if pair[1].nil?
|
184
|
-
if pair[1].respond_to?(:to_str)
|
185
|
-
pair[1] = unescape(pair[1].to_str.gsub(/\+/, " "))
|
186
|
-
end
|
187
|
-
if accu[pair[0]].kind_of?(Array)
|
188
|
-
accu[pair[0]] << pair[1]
|
189
|
-
elsif accu[pair[0]]
|
190
|
-
accu[pair[0]] = [accu[pair[0]], pair[1]]
|
191
|
-
else
|
192
|
-
accu[pair[0]] = pair[1]
|
193
|
-
end
|
194
|
-
accu
|
195
|
-
end
|
196
|
-
end
|
197
|
-
end
|
198
|
-
end
|
3
|
+
require 'forwardable'
|
4
|
+
require 'faraday/encoders/nested_params_encoder'
|
5
|
+
require 'faraday/encoders/flat_params_encoder'
|
data/lib/faraday/rack_builder.rb
CHANGED
@@ -1,12 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ruby2_keywords'
|
4
|
+
require 'faraday/adapter_registry'
|
5
|
+
|
1
6
|
module Faraday
|
2
7
|
# A Builder that processes requests into responses by passing through an inner
|
3
8
|
# middleware stack (heavily inspired by Rack).
|
4
9
|
#
|
5
|
-
#
|
10
|
+
# @example
|
11
|
+
# Faraday::Connection.new(url: 'http://httpbingo.org') do |builder|
|
6
12
|
# builder.request :url_encoded # Faraday::Request::UrlEncoded
|
7
13
|
# builder.adapter :net_http # Faraday::Adapter::NetHttp
|
8
14
|
# end
|
9
15
|
class RackBuilder
|
16
|
+
# Used to detect missing arguments
|
17
|
+
NO_ARGUMENT = Object.new
|
18
|
+
|
10
19
|
attr_accessor :handlers
|
11
20
|
|
12
21
|
# Error raised when trying to modify the stack after calling `lock!`
|
@@ -15,28 +24,28 @@ module Faraday
|
|
15
24
|
# borrowed from ActiveSupport::Dependencies::Reference &
|
16
25
|
# ActionDispatch::MiddlewareStack::Middleware
|
17
26
|
class Handler
|
18
|
-
|
19
|
-
@@constants = Hash.new { |h, k|
|
20
|
-
value = k.respond_to?(:constantize) ? k.constantize : Object.const_get(k)
|
21
|
-
@@constants_mutex.synchronize { h[k] = value }
|
22
|
-
}
|
27
|
+
REGISTRY = Faraday::AdapterRegistry.new
|
23
28
|
|
24
29
|
attr_reader :name
|
25
30
|
|
26
|
-
def initialize(klass, *args, &block)
|
31
|
+
ruby2_keywords def initialize(klass, *args, &block)
|
27
32
|
@name = klass.to_s
|
28
|
-
if klass.respond_to?(:name)
|
29
|
-
|
30
|
-
|
31
|
-
@args, @block = args, block
|
33
|
+
REGISTRY.set(klass) if klass.respond_to?(:name)
|
34
|
+
@args = args
|
35
|
+
@block = block
|
32
36
|
end
|
33
37
|
|
34
|
-
def klass
|
35
|
-
|
38
|
+
def klass
|
39
|
+
REGISTRY.get(@name)
|
40
|
+
end
|
41
|
+
|
42
|
+
def inspect
|
43
|
+
@name
|
44
|
+
end
|
36
45
|
|
37
46
|
def ==(other)
|
38
47
|
if other.is_a? Handler
|
39
|
-
|
48
|
+
name == other.name
|
40
49
|
elsif other.respond_to? :name
|
41
50
|
klass == other
|
42
51
|
else
|
@@ -44,33 +53,34 @@ module Faraday
|
|
44
53
|
end
|
45
54
|
end
|
46
55
|
|
47
|
-
def build(app)
|
56
|
+
def build(app = nil)
|
48
57
|
klass.new(app, *@args, &@block)
|
49
58
|
end
|
50
59
|
end
|
51
60
|
|
52
|
-
def initialize(
|
53
|
-
@
|
54
|
-
|
55
|
-
|
56
|
-
elsif @handlers.empty?
|
57
|
-
# default stack, if nothing else is configured
|
58
|
-
self.request :url_encoded
|
59
|
-
self.adapter Faraday.default_adapter
|
60
|
-
end
|
61
|
+
def initialize(&block)
|
62
|
+
@adapter = nil
|
63
|
+
@handlers = []
|
64
|
+
build(&block)
|
61
65
|
end
|
62
66
|
|
63
|
-
def
|
67
|
+
def initialize_dup(original)
|
68
|
+
super
|
69
|
+
@adapter = original.adapter
|
70
|
+
@handlers = original.handlers.dup
|
71
|
+
end
|
72
|
+
|
73
|
+
def build
|
64
74
|
raise_if_locked
|
65
|
-
|
66
|
-
|
75
|
+
block_given? ? yield(self) : request(:url_encoded)
|
76
|
+
adapter(Faraday.default_adapter, **Faraday.default_adapter_options) unless @adapter
|
67
77
|
end
|
68
78
|
|
69
79
|
def [](idx)
|
70
80
|
@handlers[idx]
|
71
81
|
end
|
72
82
|
|
73
|
-
# Locks the middleware stack to ensure no further modifications are
|
83
|
+
# Locks the middleware stack to ensure no further modifications are made.
|
74
84
|
def lock!
|
75
85
|
@handlers.freeze
|
76
86
|
end
|
@@ -79,46 +89,48 @@ module Faraday
|
|
79
89
|
@handlers.frozen?
|
80
90
|
end
|
81
91
|
|
82
|
-
def use(klass, *args, &block)
|
92
|
+
ruby2_keywords def use(klass, *args, &block)
|
83
93
|
if klass.is_a? Symbol
|
84
94
|
use_symbol(Faraday::Middleware, klass, *args, &block)
|
85
95
|
else
|
86
96
|
raise_if_locked
|
87
|
-
|
97
|
+
raise_if_adapter(klass)
|
88
98
|
@handlers << self.class::Handler.new(klass, *args, &block)
|
89
99
|
end
|
90
100
|
end
|
91
101
|
|
92
|
-
def request(key, *args, &block)
|
102
|
+
ruby2_keywords def request(key, *args, &block)
|
93
103
|
use_symbol(Faraday::Request, key, *args, &block)
|
94
104
|
end
|
95
105
|
|
96
|
-
def response(key, *args, &block)
|
106
|
+
ruby2_keywords def response(key, *args, &block)
|
97
107
|
use_symbol(Faraday::Response, key, *args, &block)
|
98
108
|
end
|
99
109
|
|
100
|
-
def adapter(
|
101
|
-
|
110
|
+
ruby2_keywords def adapter(klass = NO_ARGUMENT, *args, &block)
|
111
|
+
return @adapter if klass == NO_ARGUMENT || klass.nil?
|
112
|
+
|
113
|
+
klass = Faraday::Adapter.lookup_middleware(klass) if klass.is_a?(Symbol)
|
114
|
+
@adapter = self.class::Handler.new(klass, *args, &block)
|
102
115
|
end
|
103
116
|
|
104
117
|
## methods to push onto the various positions in the stack:
|
105
118
|
|
106
|
-
def insert(index, *args, &block)
|
119
|
+
ruby2_keywords def insert(index, *args, &block)
|
107
120
|
raise_if_locked
|
108
121
|
index = assert_index(index)
|
109
|
-
warn_middleware_after_adapter if inserting_after_adapter?(index)
|
110
122
|
handler = self.class::Handler.new(*args, &block)
|
111
123
|
@handlers.insert(index, handler)
|
112
124
|
end
|
113
125
|
|
114
|
-
|
126
|
+
alias insert_before insert
|
115
127
|
|
116
|
-
def insert_after(index, *args, &block)
|
128
|
+
ruby2_keywords def insert_after(index, *args, &block)
|
117
129
|
index = assert_index(index)
|
118
130
|
insert(index + 1, *args, &block)
|
119
131
|
end
|
120
132
|
|
121
|
-
def swap(index, *args, &block)
|
133
|
+
ruby2_keywords def swap(index, *args, &block)
|
122
134
|
raise_if_locked
|
123
135
|
index = assert_index(index)
|
124
136
|
@handlers.delete_at(index)
|
@@ -133,13 +145,11 @@ module Faraday
|
|
133
145
|
# Processes a Request into a Response by passing it through this Builder's
|
134
146
|
# middleware stack.
|
135
147
|
#
|
136
|
-
# connection
|
137
|
-
# request
|
148
|
+
# @param connection [Faraday::Connection]
|
149
|
+
# @param request [Faraday::Request]
|
138
150
|
#
|
139
|
-
#
|
151
|
+
# @return [Faraday::Response]
|
140
152
|
def build_response(connection, request)
|
141
|
-
warn 'WARNING: No adapter was configured for this request' unless adapter_set?
|
142
|
-
|
143
153
|
app.call(build_env(connection, request))
|
144
154
|
end
|
145
155
|
|
@@ -153,30 +163,27 @@ module Faraday
|
|
153
163
|
def app
|
154
164
|
@app ||= begin
|
155
165
|
lock!
|
156
|
-
|
157
|
-
|
158
|
-
env.response = response
|
159
|
-
response.finish(env) unless env.parallel?
|
160
|
-
response
|
161
|
-
})
|
166
|
+
ensure_adapter!
|
167
|
+
to_app
|
162
168
|
end
|
163
169
|
end
|
164
170
|
|
165
|
-
def to_app
|
171
|
+
def to_app
|
166
172
|
# last added handler is the deepest and thus closest to the inner app
|
167
|
-
|
173
|
+
# adapter is always the last one
|
174
|
+
@handlers.reverse.inject(@adapter.build) do |app, handler|
|
175
|
+
handler.build(app)
|
176
|
+
end
|
168
177
|
end
|
169
178
|
|
170
179
|
def ==(other)
|
171
|
-
other.is_a?(self.class) &&
|
172
|
-
|
173
|
-
|
174
|
-
def dup
|
175
|
-
self.class.new(@handlers.dup)
|
180
|
+
other.is_a?(self.class) &&
|
181
|
+
@handlers == other.handlers &&
|
182
|
+
@adapter == other.adapter
|
176
183
|
end
|
177
184
|
|
178
185
|
# ENV Keys
|
179
|
-
# :
|
186
|
+
# :http_method - a symbolized request HTTP method (:get, :post)
|
180
187
|
# :body - the request body that will eventually be converted to a string.
|
181
188
|
# :url - URI instance for the current request.
|
182
189
|
# :status - HTTP response status code
|
@@ -192,45 +199,53 @@ module Faraday
|
|
192
199
|
# :password - Proxy server password
|
193
200
|
# :ssl - Hash of options for configuring SSL requests.
|
194
201
|
def build_env(connection, request)
|
195
|
-
|
196
|
-
|
197
|
-
request.options
|
198
|
-
|
202
|
+
exclusive_url = connection.build_exclusive_url(
|
203
|
+
request.path, request.params,
|
204
|
+
request.options.params_encoder
|
205
|
+
)
|
206
|
+
|
207
|
+
Env.new(request.http_method, request.body, exclusive_url,
|
208
|
+
request.options, request.headers, connection.ssl,
|
209
|
+
connection.parallel_manager)
|
199
210
|
end
|
200
211
|
|
201
212
|
private
|
202
213
|
|
214
|
+
LOCK_ERR = "can't modify middleware stack after making a request"
|
215
|
+
MISSING_ADAPTER_ERROR = "An attempt to run a request with a Faraday::Connection without adapter has been made.\n" \
|
216
|
+
"Please set Faraday.default_adapter or provide one when initializing the connection.\n" \
|
217
|
+
'For more info, check https://lostisland.github.io/faraday/usage/.'
|
218
|
+
|
203
219
|
def raise_if_locked
|
204
|
-
raise StackLocked,
|
220
|
+
raise StackLocked, LOCK_ERR if locked?
|
205
221
|
end
|
206
222
|
|
207
|
-
def
|
208
|
-
|
209
|
-
"This won't be supported from Faraday 1.0."
|
210
|
-
end
|
223
|
+
def raise_if_adapter(klass)
|
224
|
+
return unless is_adapter?(klass)
|
211
225
|
|
212
|
-
|
213
|
-
@handlers.any? { |handler| is_adapter?(handler) }
|
226
|
+
raise 'Adapter should be set using the `adapter` method, not `use`'
|
214
227
|
end
|
215
228
|
|
216
|
-
def
|
217
|
-
|
218
|
-
|
229
|
+
def ensure_adapter!
|
230
|
+
raise MISSING_ADAPTER_ERROR unless @adapter
|
231
|
+
end
|
219
232
|
|
220
|
-
|
233
|
+
def adapter_set?
|
234
|
+
!@adapter.nil?
|
221
235
|
end
|
222
236
|
|
223
|
-
def is_adapter?(
|
224
|
-
|
237
|
+
def is_adapter?(klass) # rubocop:disable Naming/PredicateName
|
238
|
+
klass <= Faraday::Adapter
|
225
239
|
end
|
226
240
|
|
227
|
-
def use_symbol(mod, key, *args, &block)
|
241
|
+
ruby2_keywords def use_symbol(mod, key, *args, &block)
|
228
242
|
use(mod.lookup_middleware(key), *args, &block)
|
229
243
|
end
|
230
244
|
|
231
245
|
def assert_index(index)
|
232
246
|
idx = index.is_a?(Integer) ? index : @handlers.index(index)
|
233
247
|
raise "No such handler: #{index.inspect}" unless idx
|
248
|
+
|
234
249
|
idx
|
235
250
|
end
|
236
251
|
end
|
@@ -1,41 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Faraday
|
2
|
-
class Request
|
3
|
-
|
4
|
+
class Request
|
5
|
+
# Request middleware for the Authorization HTTP header
|
6
|
+
class Authorization < Faraday::Middleware
|
7
|
+
KEY = 'Authorization'
|
4
8
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
# @param app [#call]
|
10
|
+
# @param type [String, Symbol] Type of Authorization
|
11
|
+
# @param params [Array<String, Proc, #call>] parameters to build the Authorization header.
|
12
|
+
# If the type is `:basic`, then these can be a login and password pair.
|
13
|
+
# Otherwise, a single value is expected that will be appended after the type.
|
14
|
+
# This value can be a proc or an object responding to `.call`, in which case
|
15
|
+
# it will be invoked on each request.
|
16
|
+
def initialize(app, type, *params)
|
17
|
+
@type = type
|
18
|
+
@params = params
|
19
|
+
super(app)
|
14
20
|
end
|
15
|
-
end
|
16
21
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
values << "#{key}=#{value.to_s.inspect}"
|
22
|
+
# @param env [Faraday::Env]
|
23
|
+
def on_request(env)
|
24
|
+
return if env.request_headers[KEY]
|
25
|
+
|
26
|
+
env.request_headers[KEY] = header_from(@type, env, *@params)
|
23
27
|
end
|
24
|
-
"#{type} #{values * comma}"
|
25
|
-
end
|
26
28
|
|
27
|
-
|
28
|
-
@header_value = self.class.header(type, token)
|
29
|
-
super(app)
|
30
|
-
end
|
29
|
+
private
|
31
30
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
31
|
+
# @param type [String, Symbol]
|
32
|
+
# @param env [Faraday::Env]
|
33
|
+
# @param params [Array]
|
34
|
+
# @return [String] a header value
|
35
|
+
def header_from(type, env, *params)
|
36
|
+
if type.to_s.casecmp('basic').zero? && params.size == 2
|
37
|
+
Utils.basic_header_from(*params)
|
38
|
+
elsif params.size != 1
|
39
|
+
raise ArgumentError, "Unexpected params received (got #{params.size} instead of 1)"
|
40
|
+
else
|
41
|
+
value = params.first
|
42
|
+
if (value.is_a?(Proc) && value.arity == 1) || (value.respond_to?(:call) && value.method(:call).arity == 1)
|
43
|
+
value = value.call(env)
|
44
|
+
elsif value.is_a?(Proc) || value.respond_to?(:call)
|
45
|
+
value = value.call
|
46
|
+
end
|
47
|
+
"#{type} #{value}"
|
48
|
+
end
|
36
49
|
end
|
37
|
-
@app.call(env)
|
38
50
|
end
|
39
51
|
end
|
40
52
|
end
|
41
53
|
|
54
|
+
Faraday::Request.register_middleware(authorization: Faraday::Request::Authorization)
|
@@ -1,36 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Faraday
|
2
|
-
class Request
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
class Request
|
5
|
+
# Middleware for instrumenting Requests.
|
6
|
+
class Instrumentation < Faraday::Middleware
|
7
|
+
# Options class used in Request::Instrumentation class.
|
8
|
+
class Options < Faraday::Options.new(:name, :instrumenter)
|
9
|
+
# @return [String]
|
10
|
+
def name
|
11
|
+
self[:name] ||= 'request.faraday'
|
12
|
+
end
|
7
13
|
|
8
|
-
|
9
|
-
|
14
|
+
# @return [Class]
|
15
|
+
def instrumenter
|
16
|
+
self[:instrumenter] ||= ActiveSupport::Notifications
|
17
|
+
end
|
10
18
|
end
|
11
|
-
end
|
12
19
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
20
|
+
# Instruments requests using Active Support.
|
21
|
+
#
|
22
|
+
# Measures time spent only for synchronous requests.
|
23
|
+
#
|
24
|
+
# @example Using ActiveSupport::Notifications to measure time spent
|
25
|
+
# for Faraday requests.
|
26
|
+
# ActiveSupport::Notifications
|
27
|
+
# .subscribe('request.faraday') do |name, starts, ends, _, env|
|
28
|
+
# url = env[:url]
|
29
|
+
# http_method = env[:method].to_s.upcase
|
30
|
+
# duration = ends - starts
|
31
|
+
# $stderr.puts '[%s] %s %s (%.3f s)' %
|
32
|
+
# [url.host, http_method, url.request_uri, duration]
|
33
|
+
# end
|
34
|
+
# @param app [#call]
|
35
|
+
# @param options [nil, Hash] Options hash
|
36
|
+
# @option options [String] :name ('request.faraday')
|
37
|
+
# Name of the instrumenter
|
38
|
+
# @option options [Class] :instrumenter (ActiveSupport::Notifications)
|
39
|
+
# Active Support instrumenter class.
|
40
|
+
def initialize(app, options = nil)
|
41
|
+
super(app)
|
42
|
+
@name, @instrumenter = Options.from(options)
|
43
|
+
.values_at(:name, :instrumenter)
|
44
|
+
end
|
29
45
|
|
30
|
-
|
31
|
-
|
32
|
-
@
|
46
|
+
# @param env [Faraday::Env]
|
47
|
+
def call(env)
|
48
|
+
@instrumenter.instrument(@name, env) do
|
49
|
+
@app.call(env)
|
50
|
+
end
|
33
51
|
end
|
34
52
|
end
|
35
53
|
end
|
36
54
|
end
|
55
|
+
|
56
|
+
Faraday::Request.register_middleware(instrumentation: Faraday::Request::Instrumentation)
|