faraday 0.16.0 → 0.17.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.md +1 -1
- data/README.md +347 -18
- data/lib/faraday/adapter/em_http.rb +99 -142
- data/lib/faraday/adapter/em_http_ssl_patch.rb +17 -23
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +15 -18
- data/lib/faraday/adapter/em_synchrony.rb +60 -104
- data/lib/faraday/adapter/excon.rb +55 -100
- data/lib/faraday/adapter/httpclient.rb +39 -61
- data/lib/faraday/adapter/net_http.rb +51 -104
- data/lib/faraday/adapter/net_http_persistent.rb +27 -48
- data/lib/faraday/adapter/patron.rb +35 -54
- data/lib/faraday/adapter/rack.rb +12 -28
- data/lib/faraday/adapter/test.rb +53 -86
- data/lib/faraday/adapter/typhoeus.rb +1 -4
- data/lib/faraday/adapter.rb +22 -36
- data/lib/faraday/autoload.rb +36 -47
- data/lib/faraday/connection.rb +179 -321
- data/lib/faraday/error.rb +33 -67
- data/lib/faraday/middleware.rb +28 -4
- data/lib/faraday/options.rb +186 -35
- data/lib/faraday/parameters.rb +197 -4
- data/lib/faraday/rack_builder.rb +56 -67
- data/lib/faraday/request/authorization.rb +30 -42
- data/lib/faraday/request/basic_authentication.rb +7 -14
- data/lib/faraday/request/instrumentation.rb +27 -45
- data/lib/faraday/request/multipart.rb +48 -79
- data/lib/faraday/request/retry.rb +170 -197
- data/lib/faraday/request/token_authentication.rb +10 -15
- data/lib/faraday/request/url_encoded.rb +23 -41
- data/lib/faraday/request.rb +36 -68
- data/lib/faraday/response/logger.rb +69 -22
- data/lib/faraday/response/raise_error.rb +14 -36
- data/lib/faraday/response.rb +16 -23
- data/lib/faraday/upload_io.rb +67 -0
- data/lib/faraday/utils.rb +245 -28
- data/lib/faraday.rb +175 -93
- metadata +5 -22
- data/lib/faraday/adapter_registry.rb +0 -28
- data/lib/faraday/dependency_loader.rb +0 -37
- data/lib/faraday/encoders/flat_params_encoder.rb +0 -94
- data/lib/faraday/encoders/nested_params_encoder.rb +0 -171
- data/lib/faraday/file_part.rb +0 -128
- data/lib/faraday/logging/formatter.rb +0 -92
- data/lib/faraday/middleware_registry.rb +0 -129
- data/lib/faraday/options/connection_options.rb +0 -22
- data/lib/faraday/options/env.rb +0 -181
- data/lib/faraday/options/proxy_options.rb +0 -28
- data/lib/faraday/options/request_options.rb +0 -21
- data/lib/faraday/options/ssl_options.rb +0 -59
- data/lib/faraday/param_part.rb +0 -53
- data/lib/faraday/utils/headers.rb +0 -139
- data/lib/faraday/utils/params_hash.rb +0 -61
- data/spec/external_adapters/faraday_specs_setup.rb +0 -14
data/lib/faraday/adapter/test.rb
CHANGED
@@ -1,56 +1,51 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
module Faraday
|
4
2
|
class Adapter
|
5
|
-
#
|
3
|
+
# Examples
|
4
|
+
#
|
6
5
|
# test = Faraday::Connection.new do
|
7
6
|
# use Faraday::Adapter::Test do |stub|
|
8
|
-
# #
|
7
|
+
# # simply define matcher to match the request
|
9
8
|
# stub.get '/resource.json' do
|
10
9
|
# # return static content
|
11
10
|
# [200, {'Content-Type' => 'application/json'}, 'hi world']
|
12
11
|
# end
|
13
|
-
#
|
12
|
+
#
|
14
13
|
# # response with content generated based on request
|
15
14
|
# stub.get '/showget' do |env|
|
16
15
|
# [200, {'Content-Type' => 'text/plain'}, env[:method].to_s]
|
17
16
|
# end
|
18
|
-
#
|
19
|
-
# #
|
17
|
+
#
|
18
|
+
# # regular expression can be used as matching filter
|
20
19
|
# stub.get /\A\/items\/(\d+)\z/ do |env, meta|
|
21
|
-
# # in case regular expression is used
|
22
|
-
#
|
23
|
-
# [200,
|
24
|
-
# {'Content-Type' => 'text/plain'},
|
25
|
-
# "showing item: #{meta[:match_data][1]}"
|
26
|
-
# ]
|
20
|
+
# # in case regular expression is used an instance of MatchData can be received
|
21
|
+
# [200, {'Content-Type' => 'text/plain'}, "showing item: #{meta[:match_data][1]}"]
|
27
22
|
# end
|
28
23
|
# end
|
29
24
|
# end
|
30
|
-
#
|
25
|
+
#
|
31
26
|
# resp = test.get '/resource.json'
|
32
27
|
# resp.body # => 'hi world'
|
33
|
-
#
|
28
|
+
#
|
34
29
|
# resp = test.get '/showget'
|
35
30
|
# resp.body # => 'get'
|
36
|
-
#
|
31
|
+
#
|
37
32
|
# resp = test.get '/items/1'
|
38
33
|
# resp.body # => 'showing item: 1'
|
39
|
-
#
|
34
|
+
#
|
40
35
|
# resp = test.get '/items/2'
|
41
36
|
# resp.body # => 'showing item: 2'
|
37
|
+
#
|
38
|
+
|
42
39
|
class Test < Faraday::Adapter
|
43
40
|
attr_accessor :stubs
|
44
41
|
|
45
|
-
# A stack of Stubs
|
46
42
|
class Stubs
|
47
43
|
class NotFound < StandardError
|
48
44
|
end
|
49
45
|
|
50
46
|
def initialize
|
51
|
-
# {
|
52
|
-
@stack = {}
|
53
|
-
@consumed = {}
|
47
|
+
# {:get => [Stub, Stub]}
|
48
|
+
@stack, @consumed = {}, {}
|
54
49
|
yield(self) if block_given?
|
55
50
|
end
|
56
51
|
|
@@ -59,8 +54,7 @@ module Faraday
|
|
59
54
|
end
|
60
55
|
|
61
56
|
def match(request_method, host, path, headers, body)
|
62
|
-
return false
|
63
|
-
|
57
|
+
return false if !@stack.key?(request_method)
|
64
58
|
stack = @stack[request_method]
|
65
59
|
consumed = (@consumed[request_method] ||= [])
|
66
60
|
|
@@ -80,15 +74,15 @@ module Faraday
|
|
80
74
|
new_stub(:head, path, headers, &block)
|
81
75
|
end
|
82
76
|
|
83
|
-
def post(path, body
|
77
|
+
def post(path, body=nil, headers = {}, &block)
|
84
78
|
new_stub(:post, path, headers, body, &block)
|
85
79
|
end
|
86
80
|
|
87
|
-
def put(path, body
|
81
|
+
def put(path, body=nil, headers = {}, &block)
|
88
82
|
new_stub(:put, path, headers, body, &block)
|
89
83
|
end
|
90
84
|
|
91
|
-
def patch(path, body
|
85
|
+
def patch(path, body=nil, headers = {}, &block)
|
92
86
|
new_stub(:patch, path, headers, body, &block)
|
93
87
|
end
|
94
88
|
|
@@ -104,32 +98,26 @@ module Faraday
|
|
104
98
|
def verify_stubbed_calls
|
105
99
|
failed_stubs = []
|
106
100
|
@stack.each do |method, stubs|
|
107
|
-
|
108
|
-
|
109
|
-
failed_stubs.concat(
|
110
|
-
stubs.map do |stub|
|
101
|
+
unless stubs.size == 0
|
102
|
+
failed_stubs.concat(stubs.map {|stub|
|
111
103
|
"Expected #{method} #{stub}."
|
112
|
-
|
113
|
-
|
104
|
+
})
|
105
|
+
end
|
114
106
|
end
|
115
|
-
raise failed_stubs.join(
|
107
|
+
raise failed_stubs.join(" ") unless failed_stubs.size == 0
|
116
108
|
end
|
117
109
|
|
118
110
|
protected
|
119
111
|
|
120
|
-
def new_stub(request_method, path, headers = {}, body
|
112
|
+
def new_stub(request_method, path, headers = {}, body=nil, &block)
|
121
113
|
normalized_path, host =
|
122
114
|
if path.is_a?(Regexp)
|
123
115
|
path
|
124
116
|
else
|
125
|
-
[
|
126
|
-
Faraday::Utils.normalize_path(path),
|
127
|
-
Faraday::Utils.URI(path).host
|
128
|
-
]
|
117
|
+
[Faraday::Utils.normalize_path(path), Faraday::Utils.URI(path).host]
|
129
118
|
end
|
130
119
|
|
131
|
-
|
132
|
-
(@stack[request_method] ||= []) << stub
|
120
|
+
(@stack[request_method] ||= []) << Stub.new(host, normalized_path, headers, body, block)
|
133
121
|
end
|
134
122
|
|
135
123
|
def matches?(stack, host, path, headers, body)
|
@@ -141,42 +129,32 @@ module Faraday
|
|
141
129
|
end
|
142
130
|
end
|
143
131
|
|
144
|
-
# Stub request
|
145
|
-
# rubocop:disable Style/StructInheritance
|
146
132
|
class Stub < Struct.new(:host, :path, :params, :headers, :body, :block)
|
147
|
-
# rubocop:enable Style/StructInheritance
|
148
133
|
def initialize(host, full, headers, body, block)
|
149
|
-
path, query = full.respond_to?(:split) ? full.split(
|
150
|
-
params =
|
151
|
-
|
152
|
-
|
153
|
-
else
|
154
|
-
{}
|
155
|
-
end
|
156
|
-
|
134
|
+
path, query = full.respond_to?(:split) ? full.split("?") : full
|
135
|
+
params = query ?
|
136
|
+
Faraday::Utils.parse_nested_query(query) :
|
137
|
+
{}
|
157
138
|
super(host, path, params, headers, body, block)
|
158
139
|
end
|
159
140
|
|
160
141
|
def matches?(request_host, request_uri, request_headers, request_body)
|
161
142
|
request_path, request_query = request_uri.split('?')
|
162
|
-
request_params =
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
{}
|
167
|
-
end
|
168
|
-
# meta is a hash used as carrier
|
143
|
+
request_params = request_query ?
|
144
|
+
Faraday::Utils.parse_nested_query(request_query) :
|
145
|
+
{}
|
146
|
+
# meta is a hash use as carrier
|
169
147
|
# that will be yielded to consumer block
|
170
148
|
meta = {}
|
171
|
-
|
149
|
+
return (host.nil? || host == request_host) &&
|
172
150
|
path_match?(request_path, meta) &&
|
173
151
|
params_match?(request_params) &&
|
174
152
|
(body.to_s.size.zero? || request_body == body) &&
|
175
|
-
headers_match?(request_headers), meta
|
153
|
+
headers_match?(request_headers), meta
|
176
154
|
end
|
177
155
|
|
178
156
|
def path_match?(request_path, meta)
|
179
|
-
if path.is_a?
|
157
|
+
if path.is_a? Regexp
|
180
158
|
!!(meta[:match_data] = path.match(request_path))
|
181
159
|
else
|
182
160
|
path == request_path
|
@@ -200,7 +178,7 @@ module Faraday
|
|
200
178
|
end
|
201
179
|
end
|
202
180
|
|
203
|
-
def initialize(app, stubs
|
181
|
+
def initialize(app, stubs=nil, &block)
|
204
182
|
super(app)
|
205
183
|
@stubs = stubs || Stubs.new
|
206
184
|
configure(&block) if block
|
@@ -214,31 +192,20 @@ module Faraday
|
|
214
192
|
super
|
215
193
|
host = env[:url].host
|
216
194
|
normalized_path = Faraday::Utils.normalize_path(env[:url])
|
217
|
-
params_encoder = env.request.params_encoder ||
|
218
|
-
|
219
|
-
|
220
|
-
stub
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
end
|
227
|
-
|
228
|
-
env[:params] = if (query = env[:url].query)
|
229
|
-
params_encoder.decode(query)
|
230
|
-
else
|
231
|
-
{}
|
232
|
-
end
|
233
|
-
block_arity = stub.block.arity
|
234
|
-
status, headers, body =
|
235
|
-
if block_arity >= 0
|
236
|
-
stub.block.call(*[env, meta].take(block_arity))
|
237
|
-
else
|
195
|
+
params_encoder = env.request.params_encoder || Faraday::Utils.default_params_encoder
|
196
|
+
|
197
|
+
stub, meta = stubs.match(env[:method], host, normalized_path, env.request_headers, env[:body])
|
198
|
+
if stub
|
199
|
+
env[:params] = (query = env[:url].query) ?
|
200
|
+
params_encoder.decode(query) : {}
|
201
|
+
block_arity = stub.block.arity
|
202
|
+
status, headers, body = (block_arity >= 0) ?
|
203
|
+
stub.block.call(*[env, meta].take(block_arity)) :
|
238
204
|
stub.block.call(env, meta)
|
239
|
-
|
240
|
-
|
241
|
-
|
205
|
+
save_response(env, status, body, headers)
|
206
|
+
else
|
207
|
+
raise Stubs::NotFound, "no stubbed request for #{env[:method]} #{normalized_path} #{env[:body]}"
|
208
|
+
end
|
242
209
|
@app.call(env)
|
243
210
|
end
|
244
211
|
end
|
@@ -1,9 +1,6 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
module Faraday
|
4
2
|
class Adapter
|
5
|
-
#
|
6
|
-
# https://github.com/philsturgeon/typhoeus/blob/master/lib/typhoeus/adapters/faraday.rb
|
3
|
+
# This class is just a stub, the real adapter is in https://github.com/philsturgeon/typhoeus/blob/master/lib/typhoeus/adapters/faraday.rb
|
7
4
|
class Typhoeus < Faraday::Adapter
|
8
5
|
# Needs to define this method in order to support Typhoeus <= 1.3.0
|
9
6
|
def call; end
|
data/lib/faraday/adapter.rb
CHANGED
@@ -1,54 +1,43 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
module Faraday
|
4
|
-
#
|
2
|
+
# Public: This is a base class for all Faraday adapters. Adapters are
|
5
3
|
# responsible for fulfilling a Faraday request.
|
6
|
-
class Adapter
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
em_http: [:EMHttp, 'em_http'],
|
23
|
-
excon: [:Excon, 'excon'],
|
24
|
-
rack: [:Rack, 'rack'],
|
25
|
-
httpclient: [:HTTPClient, 'httpclient']
|
26
|
-
|
27
|
-
# This module marks an Adapter as supporting parallel requests.
|
4
|
+
class Adapter < Middleware
|
5
|
+
CONTENT_LENGTH = 'Content-Length'.freeze
|
6
|
+
|
7
|
+
register_middleware File.expand_path('../adapter', __FILE__),
|
8
|
+
:test => [:Test, 'test'],
|
9
|
+
:net_http => [:NetHttp, 'net_http'],
|
10
|
+
:net_http_persistent => [:NetHttpPersistent, 'net_http_persistent'],
|
11
|
+
:typhoeus => [:Typhoeus, 'typhoeus'],
|
12
|
+
:patron => [:Patron, 'patron'],
|
13
|
+
:em_synchrony => [:EMSynchrony, 'em_synchrony'],
|
14
|
+
:em_http => [:EMHttp, 'em_http'],
|
15
|
+
:excon => [:Excon, 'excon'],
|
16
|
+
:rack => [:Rack, 'rack'],
|
17
|
+
:httpclient => [:HTTPClient, 'httpclient']
|
18
|
+
|
19
|
+
# Public: This module marks an Adapter as supporting parallel requests.
|
28
20
|
module Parallelism
|
29
21
|
attr_writer :supports_parallel
|
30
|
-
def supports_parallel?
|
31
|
-
@supports_parallel
|
32
|
-
end
|
22
|
+
def supports_parallel?() @supports_parallel end
|
33
23
|
|
34
24
|
def inherited(subclass)
|
35
25
|
super
|
36
|
-
subclass.supports_parallel = supports_parallel?
|
26
|
+
subclass.supports_parallel = self.supports_parallel?
|
37
27
|
end
|
38
28
|
end
|
39
29
|
|
40
30
|
extend Parallelism
|
41
31
|
self.supports_parallel = false
|
42
32
|
|
43
|
-
def initialize(
|
44
|
-
|
33
|
+
def initialize(app = nil, opts = {}, &block)
|
34
|
+
super(app)
|
45
35
|
@connection_options = opts
|
46
36
|
@config_block = block
|
47
37
|
end
|
48
38
|
|
49
39
|
def call(env)
|
50
40
|
env.clear_body if env.needs_body?
|
51
|
-
env.response = Response.new
|
52
41
|
end
|
53
42
|
|
54
43
|
private
|
@@ -56,14 +45,11 @@ module Faraday
|
|
56
45
|
def save_response(env, status, body, headers = nil, reason_phrase = nil)
|
57
46
|
env.status = status
|
58
47
|
env.body = body
|
59
|
-
env.reason_phrase = reason_phrase
|
48
|
+
env.reason_phrase = reason_phrase && reason_phrase.to_s.strip
|
60
49
|
env.response_headers = Utils::Headers.new.tap do |response_headers|
|
61
50
|
response_headers.update headers unless headers.nil?
|
62
51
|
yield(response_headers) if block_given?
|
63
52
|
end
|
64
|
-
|
65
|
-
env.response.finish(env) unless env.parallel?
|
66
|
-
env.response
|
67
53
|
end
|
68
54
|
end
|
69
55
|
end
|
data/lib/faraday/autoload.rb
CHANGED
@@ -1,95 +1,84 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
module Faraday
|
4
|
-
# Adds the ability for other modules to manage autoloadable
|
2
|
+
# Internal: Adds the ability for other modules to manage autoloadable
|
5
3
|
# constants.
|
6
|
-
#
|
7
|
-
# @api private
|
8
4
|
module AutoloadHelper
|
9
|
-
# Registers the constants to be auto loaded.
|
5
|
+
# Internal: Registers the constants to be auto loaded.
|
10
6
|
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
7
|
+
# prefix - The String require prefix. If the path is inside Faraday, then
|
8
|
+
# it will be prefixed with the root path of this loaded Faraday
|
9
|
+
# version.
|
10
|
+
# options - Hash of Symbol => String library names.
|
15
11
|
#
|
16
|
-
#
|
12
|
+
# Examples.
|
17
13
|
#
|
18
14
|
# Faraday.autoload_all 'faraday/foo',
|
19
|
-
# Bar
|
15
|
+
# :Bar => 'bar'
|
20
16
|
#
|
21
17
|
# # requires faraday/foo/bar to load Faraday::Bar.
|
22
18
|
# Faraday::Bar
|
23
19
|
#
|
24
|
-
#
|
20
|
+
#
|
21
|
+
# Returns nothing.
|
25
22
|
def autoload_all(prefix, options)
|
26
|
-
if prefix =~
|
23
|
+
if prefix =~ /^faraday(\/|$)/i
|
27
24
|
prefix = File.join(Faraday.root_path, prefix)
|
28
25
|
end
|
29
|
-
|
30
26
|
options.each do |const_name, path|
|
31
27
|
autoload const_name, File.join(prefix, path)
|
32
28
|
end
|
33
29
|
end
|
34
30
|
|
35
|
-
# Loads each autoloaded constant. If thread safety is a concern,
|
31
|
+
# Internal: Loads each autoloaded constant. If thread safety is a concern,
|
36
32
|
# wrap this in a Mutex.
|
37
33
|
#
|
38
|
-
#
|
34
|
+
# Returns nothing.
|
39
35
|
def load_autoloaded_constants
|
40
36
|
constants.each do |const|
|
41
37
|
const_get(const) if autoload?(const)
|
42
38
|
end
|
43
39
|
end
|
44
40
|
|
45
|
-
# Filters the module's contents with those that have been already
|
41
|
+
# Internal: Filters the module's contents with those that have been already
|
46
42
|
# autoloaded.
|
47
43
|
#
|
48
|
-
#
|
44
|
+
# Returns an Array of Class/Module objects.
|
49
45
|
def all_loaded_constants
|
50
|
-
constants
|
51
|
-
|
52
|
-
.select { |a| a.respond_to?(:loaded?) && a.loaded? }
|
46
|
+
constants.map { |c| const_get(c) }.
|
47
|
+
select { |a| a.respond_to?(:loaded?) && a.loaded? }
|
53
48
|
end
|
54
49
|
end
|
55
50
|
|
56
|
-
# Adapter is the base class for all Faraday adapters.
|
57
|
-
# @see lib/faraday/adapter.rb Original class location
|
58
51
|
class Adapter
|
59
52
|
extend AutoloadHelper
|
60
53
|
autoload_all 'faraday/adapter',
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
54
|
+
:NetHttp => 'net_http',
|
55
|
+
:NetHttpPersistent => 'net_http_persistent',
|
56
|
+
:EMSynchrony => 'em_synchrony',
|
57
|
+
:EMHttp => 'em_http',
|
58
|
+
:Typhoeus => 'typhoeus',
|
59
|
+
:Patron => 'patron',
|
60
|
+
:Excon => 'excon',
|
61
|
+
:Test => 'test',
|
62
|
+
:Rack => 'rack',
|
63
|
+
:HTTPClient => 'httpclient'
|
71
64
|
end
|
72
65
|
|
73
|
-
# Request represents a single HTTP request for a Faraday adapter to make.
|
74
|
-
# @see lib/faraday/request.rb Original class location
|
75
66
|
class Request
|
76
67
|
extend AutoloadHelper
|
77
68
|
autoload_all 'faraday/request',
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
69
|
+
:UrlEncoded => 'url_encoded',
|
70
|
+
:Multipart => 'multipart',
|
71
|
+
:Retry => 'retry',
|
72
|
+
:Authorization => 'authorization',
|
73
|
+
:BasicAuthentication => 'basic_authentication',
|
74
|
+
:TokenAuthentication => 'token_authentication',
|
75
|
+
:Instrumentation => 'instrumentation'
|
85
76
|
end
|
86
77
|
|
87
|
-
# Response represents the returned value of a sent Faraday request.
|
88
|
-
# @see lib/faraday/response.rb Original class location
|
89
78
|
class Response
|
90
79
|
extend AutoloadHelper
|
91
80
|
autoload_all 'faraday/response',
|
92
|
-
|
93
|
-
|
81
|
+
:RaiseError => 'raise_error',
|
82
|
+
:Logger => 'logger'
|
94
83
|
end
|
95
84
|
end
|