faraday 1.3.1 → 1.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +0 -1
- data/examples/client_spec.rb +34 -2
- data/examples/client_test.rb +41 -2
- data/lib/faraday/adapter/test.rb +59 -43
- data/lib/faraday/adapter.rb +1 -11
- data/lib/faraday/autoload.rb +1 -8
- data/lib/faraday/connection.rb +33 -6
- data/lib/faraday/error.rb +0 -6
- data/lib/faraday/options/proxy_options.rb +4 -0
- data/lib/faraday/request/authorization.rb +14 -7
- data/lib/faraday/request.rb +0 -2
- data/lib/faraday/version.rb +1 -1
- data/lib/faraday.rb +18 -3
- data/spec/faraday/adapter/em_http_spec.rb +39 -37
- data/spec/faraday/adapter/em_synchrony_spec.rb +11 -9
- data/spec/faraday/adapter/test_spec.rb +117 -0
- data/spec/faraday/connection_spec.rb +15 -0
- data/spec/faraday/options/proxy_options_spec.rb +7 -0
- data/spec/faraday/request/authorization_spec.rb +8 -0
- metadata +118 -28
- data/lib/faraday/adapter/em_http.rb +0 -289
- data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -62
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -69
- data/lib/faraday/adapter/em_synchrony.rb +0 -153
- data/lib/faraday/adapter/excon.rb +0 -124
- data/lib/faraday/adapter/httpclient.rb +0 -152
- data/lib/faraday/adapter/net_http_persistent.rb +0 -91
- data/lib/faraday/adapter/patron.rb +0 -132
- data/lib/faraday/adapter/rack.rb +0 -75
- data/lib/faraday/file_part.rb +0 -128
- data/lib/faraday/param_part.rb +0 -53
- data/lib/faraday/request/multipart.rb +0 -106
- data/lib/faraday/request/retry.rb +0 -239
- data/spec/faraday/adapter/net_http_persistent_spec.rb +0 -57
- data/spec/faraday/request/multipart_spec.rb +0 -302
- data/spec/faraday/request/retry_spec.rb +0 -242
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 54fc3007a99cab5f45291408bfb7e2b1f6d243abcae060f5d485731328acb031
|
4
|
+
data.tar.gz: 0fc0eb9ab470bca83b7b6fbc8cafcd9d141dd97eda575f63cf4b2bc6dcb36a65
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed9fee068b4cbbe13bacfa7eb01d02b919c0ffd72345a4449127f9ad11c7a7e585fa891c8d7791861fa91c8ebe47b9d7e78f760680eaf856db3c029c5f5cc1e0
|
7
|
+
data.tar.gz: 47d4550ed834bdd5938312aafd9ea9ca4be061a12cd564fa83d08e7c67a44445a8870d12ef0030b6d9d84796813e97ff7ca859722ca786e449cfc25c66ebdb23
|
data/README.md
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/faraday.svg)](https://rubygems.org/gems/faraday)
|
4
4
|
[![GitHub Actions CI](https://github.com/lostisland/faraday/workflows/CI/badge.svg)](https://github.com/lostisland/faraday/actions?query=workflow%3ACI)
|
5
|
-
[![Maintainability](https://api.codeclimate.com/v1/badges/f869daab091ceef1da73/maintainability)](https://codeclimate.com/github/lostisland/faraday/maintainability)
|
6
5
|
[![Gitter](https://badges.gitter.im/lostisland/faraday.svg)](https://gitter.im/lostisland/faraday?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
7
6
|
|
8
7
|
|
data/examples/client_spec.rb
CHANGED
@@ -12,8 +12,8 @@ class Client
|
|
12
12
|
@conn = conn
|
13
13
|
end
|
14
14
|
|
15
|
-
def sushi(jname)
|
16
|
-
res = @conn.get("/#{jname}")
|
15
|
+
def sushi(jname, params: {})
|
16
|
+
res = @conn.get("/#{jname}", params)
|
17
17
|
data = JSON.parse(res.body)
|
18
18
|
data['name']
|
19
19
|
end
|
@@ -62,4 +62,36 @@ RSpec.describe Client do
|
|
62
62
|
expect { client.sushi('ebi') }.to raise_error(Faraday::ConnectionFailed)
|
63
63
|
stubs.verify_stubbed_calls
|
64
64
|
end
|
65
|
+
|
66
|
+
context 'When the test stub is run in strict_mode' do
|
67
|
+
let(:stubs) { Faraday::Adapter::Test::Stubs.new(strict_mode: true) }
|
68
|
+
|
69
|
+
it 'verifies the all parameter values are identical' do
|
70
|
+
stubs.get('/ebi?abc=123') do
|
71
|
+
[
|
72
|
+
200,
|
73
|
+
{ 'Content-Type': 'application/javascript' },
|
74
|
+
'{"name": "shrimp"}'
|
75
|
+
]
|
76
|
+
end
|
77
|
+
|
78
|
+
# uncomment to raise Stubs::NotFound
|
79
|
+
# expect(client.sushi('ebi', params: { abc: 123, foo: 'Kappa' })).to eq('shrimp')
|
80
|
+
expect(client.sushi('ebi', params: { abc: 123 })).to eq('shrimp')
|
81
|
+
stubs.verify_stubbed_calls
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'When the Faraday connection is configured with FlatParamsEncoder' do
|
86
|
+
let(:conn) { Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) { |b| b.adapter(:test, stubs) } }
|
87
|
+
|
88
|
+
it 'handles the same multiple URL parameters' do
|
89
|
+
stubs.get('/ebi?a=x&a=y&a=z') { [200, { 'Content-Type' => 'application/json' }, '{"name": "shrimp"}'] }
|
90
|
+
|
91
|
+
# uncomment to raise Stubs::NotFound
|
92
|
+
# expect(client.sushi('ebi', params: { a: %w[x y] })).to eq('shrimp')
|
93
|
+
expect(client.sushi('ebi', params: { a: %w[x y z] })).to eq('shrimp')
|
94
|
+
stubs.verify_stubbed_calls
|
95
|
+
end
|
96
|
+
end
|
65
97
|
end
|
data/examples/client_test.rb
CHANGED
@@ -13,8 +13,8 @@ class Client
|
|
13
13
|
@conn = conn
|
14
14
|
end
|
15
15
|
|
16
|
-
def sushi(jname)
|
17
|
-
res = @conn.get("/#{jname}")
|
16
|
+
def sushi(jname, params: {})
|
17
|
+
res = @conn.get("/#{jname}", params)
|
18
18
|
data = JSON.parse(res.body)
|
19
19
|
data['name']
|
20
20
|
end
|
@@ -70,6 +70,45 @@ class ClientTest < Test::Unit::TestCase
|
|
70
70
|
stubs.verify_stubbed_calls
|
71
71
|
end
|
72
72
|
|
73
|
+
def test_strict_mode
|
74
|
+
stubs = Faraday::Adapter::Test::Stubs.new(strict_mode: true)
|
75
|
+
stubs.get('/ebi?abc=123') do
|
76
|
+
[
|
77
|
+
200,
|
78
|
+
{ 'Content-Type': 'application/javascript' },
|
79
|
+
'{"name": "shrimp"}'
|
80
|
+
]
|
81
|
+
end
|
82
|
+
|
83
|
+
cli = client(stubs)
|
84
|
+
assert_equal 'shrimp', cli.sushi('ebi', params: { abc: 123 })
|
85
|
+
|
86
|
+
# uncomment to raise Stubs::NotFound
|
87
|
+
# assert_equal 'shrimp', cli.sushi('ebi', params: { abc: 123, foo: 'Kappa' })
|
88
|
+
stubs.verify_stubbed_calls
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_non_default_params_encoder
|
92
|
+
stubs = Faraday::Adapter::Test::Stubs.new(strict_mode: true)
|
93
|
+
stubs.get('/ebi?a=x&a=y&a=z') do
|
94
|
+
[
|
95
|
+
200,
|
96
|
+
{ 'Content-Type': 'application/javascript' },
|
97
|
+
'{"name": "shrimp"}'
|
98
|
+
]
|
99
|
+
end
|
100
|
+
conn = Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) do |builder|
|
101
|
+
builder.adapter :test, stubs
|
102
|
+
end
|
103
|
+
|
104
|
+
cli = Client.new(conn)
|
105
|
+
assert_equal 'shrimp', cli.sushi('ebi', params: { a: %w[x y z] })
|
106
|
+
|
107
|
+
# uncomment to raise Stubs::NotFound
|
108
|
+
# assert_equal 'shrimp', cli.sushi('ebi', params: { a: %w[x y] })
|
109
|
+
stubs.verify_stubbed_calls
|
110
|
+
end
|
111
|
+
|
73
112
|
def client(stubs)
|
74
113
|
conn = Faraday.new do |builder|
|
75
114
|
builder.adapter :test, stubs
|
data/lib/faraday/adapter/test.rb
CHANGED
@@ -25,6 +25,9 @@ module Faraday
|
|
25
25
|
# "showing item: #{meta[:match_data][1]}"
|
26
26
|
# ]
|
27
27
|
# end
|
28
|
+
#
|
29
|
+
# # You can set strict_mode to exactly match the stubbed requests.
|
30
|
+
# stub.strict_mode = true
|
28
31
|
# end
|
29
32
|
# end
|
30
33
|
#
|
@@ -47,10 +50,11 @@ module Faraday
|
|
47
50
|
class NotFound < StandardError
|
48
51
|
end
|
49
52
|
|
50
|
-
def initialize
|
53
|
+
def initialize(strict_mode: false)
|
51
54
|
# { get: [Stub, Stub] }
|
52
55
|
@stack = {}
|
53
56
|
@consumed = {}
|
57
|
+
@strict_mode = strict_mode
|
54
58
|
yield(self) if block_given?
|
55
59
|
end
|
56
60
|
|
@@ -58,18 +62,20 @@ module Faraday
|
|
58
62
|
@stack.empty?
|
59
63
|
end
|
60
64
|
|
61
|
-
|
65
|
+
# @param env [Faraday::Env]
|
66
|
+
def match(env)
|
67
|
+
request_method = env[:method]
|
62
68
|
return false unless @stack.key?(request_method)
|
63
69
|
|
64
70
|
stack = @stack[request_method]
|
65
71
|
consumed = (@consumed[request_method] ||= [])
|
66
72
|
|
67
|
-
stub, meta = matches?(stack,
|
73
|
+
stub, meta = matches?(stack, env)
|
68
74
|
if stub
|
69
75
|
consumed << stack.delete(stub)
|
70
76
|
return stub, meta
|
71
77
|
end
|
72
|
-
matches?(consumed,
|
78
|
+
matches?(consumed, env)
|
73
79
|
end
|
74
80
|
|
75
81
|
def get(path, headers = {}, &block)
|
@@ -115,6 +121,17 @@ module Faraday
|
|
115
121
|
raise failed_stubs.join(' ') unless failed_stubs.empty?
|
116
122
|
end
|
117
123
|
|
124
|
+
# Set strict_mode. If the value is true, this adapter tries to find matched requests strictly,
|
125
|
+
# which means that all of a path, parameters, and headers must be the same as an actual request.
|
126
|
+
def strict_mode=(value)
|
127
|
+
@strict_mode = value
|
128
|
+
@stack.each do |_method, stubs|
|
129
|
+
stubs.each do |stub|
|
130
|
+
stub.strict_mode = value
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
118
135
|
protected
|
119
136
|
|
120
137
|
def new_stub(request_method, path, headers = {}, body = nil, &block)
|
@@ -127,14 +144,18 @@ module Faraday
|
|
127
144
|
Faraday::Utils.URI(path).host
|
128
145
|
]
|
129
146
|
end
|
147
|
+
path, query = normalized_path.respond_to?(:split) ? normalized_path.split('?') : normalized_path
|
148
|
+
headers = Utils::Headers.new(headers)
|
130
149
|
|
131
|
-
stub = Stub.new(host,
|
150
|
+
stub = Stub.new(host, path, query, headers, body, @strict_mode, block)
|
132
151
|
(@stack[request_method] ||= []) << stub
|
133
152
|
end
|
134
153
|
|
135
|
-
|
154
|
+
# @param stack [Hash]
|
155
|
+
# @param env [Faraday::Env]
|
156
|
+
def matches?(stack, env)
|
136
157
|
stack.each do |stub|
|
137
|
-
match_result, meta = stub.matches?(
|
158
|
+
match_result, meta = stub.matches?(env)
|
138
159
|
return stub, meta if match_result
|
139
160
|
end
|
140
161
|
nil
|
@@ -142,35 +163,20 @@ module Faraday
|
|
142
163
|
end
|
143
164
|
|
144
165
|
# Stub request
|
145
|
-
# rubocop:disable Style/StructInheritance
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
Faraday::Utils.parse_nested_query(query)
|
153
|
-
else
|
154
|
-
{}
|
155
|
-
end
|
156
|
-
|
157
|
-
super(host, path, params, headers, body, block)
|
158
|
-
end
|
166
|
+
class Stub < Struct.new(:host, :path, :query, :headers, :body, :strict_mode, :block) # rubocop:disable Style/StructInheritance
|
167
|
+
# @param env [Faraday::Env]
|
168
|
+
def matches?(env)
|
169
|
+
request_host = env[:url].host
|
170
|
+
request_path = Faraday::Utils.normalize_path(env[:url].path)
|
171
|
+
request_headers = env.request_headers
|
172
|
+
request_body = env[:body]
|
159
173
|
|
160
|
-
def matches?(request_host, request_uri, request_headers, request_body)
|
161
|
-
request_path, request_query = request_uri.split('?')
|
162
|
-
request_params =
|
163
|
-
if request_query
|
164
|
-
Faraday::Utils.parse_nested_query(request_query)
|
165
|
-
else
|
166
|
-
{}
|
167
|
-
end
|
168
174
|
# meta is a hash used as carrier
|
169
175
|
# that will be yielded to consumer block
|
170
176
|
meta = {}
|
171
177
|
[(host.nil? || host == request_host) &&
|
172
178
|
path_match?(request_path, meta) &&
|
173
|
-
params_match?(
|
179
|
+
params_match?(env) &&
|
174
180
|
(body.to_s.size.zero? || request_body == body) &&
|
175
181
|
headers_match?(request_headers), meta]
|
176
182
|
end
|
@@ -183,13 +189,30 @@ module Faraday
|
|
183
189
|
end
|
184
190
|
end
|
185
191
|
|
186
|
-
|
192
|
+
# @param env [Faraday::Env]
|
193
|
+
def params_match?(env)
|
194
|
+
request_params = env[:params]
|
195
|
+
params = env.params_encoder.decode(query) || {}
|
196
|
+
|
197
|
+
if strict_mode
|
198
|
+
return Set.new(params) == Set.new(request_params)
|
199
|
+
end
|
200
|
+
|
187
201
|
params.keys.all? do |key|
|
188
202
|
request_params[key] == params[key]
|
189
203
|
end
|
190
204
|
end
|
191
205
|
|
192
206
|
def headers_match?(request_headers)
|
207
|
+
if strict_mode
|
208
|
+
headers_with_user_agent = headers.dup.tap do |hs|
|
209
|
+
# NOTE: Set User-Agent in case it's not set when creating Stubs.
|
210
|
+
# Users would not want to set Faraday's User-Agent explicitly.
|
211
|
+
hs[:user_agent] ||= Connection::USER_AGENT
|
212
|
+
end
|
213
|
+
return Set.new(headers_with_user_agent) == Set.new(request_headers)
|
214
|
+
end
|
215
|
+
|
193
216
|
headers.keys.all? do |key|
|
194
217
|
request_headers[key] == headers[key]
|
195
218
|
end
|
@@ -210,26 +233,19 @@ module Faraday
|
|
210
233
|
yield(stubs)
|
211
234
|
end
|
212
235
|
|
236
|
+
# @param env [Faraday::Env]
|
213
237
|
def call(env)
|
214
238
|
super
|
215
|
-
host = env[:url].host
|
216
|
-
normalized_path = Faraday::Utils.normalize_path(env[:url])
|
217
|
-
params_encoder = env.request.params_encoder ||
|
218
|
-
Faraday::Utils.default_params_encoder
|
219
239
|
|
220
|
-
|
221
|
-
|
240
|
+
env.request.params_encoder ||= Faraday::Utils.default_params_encoder
|
241
|
+
env[:params] = env.params_encoder.decode(env[:url].query) || {}
|
242
|
+
stub, meta = stubs.match(env)
|
222
243
|
|
223
244
|
unless stub
|
224
245
|
raise Stubs::NotFound, "no stubbed request for #{env[:method]} "\
|
225
|
-
"#{
|
246
|
+
"#{env[:url]} #{env[:body]}"
|
226
247
|
end
|
227
248
|
|
228
|
-
env[:params] = if (query = env[:url].query)
|
229
|
-
params_encoder.decode(query)
|
230
|
-
else
|
231
|
-
{}
|
232
|
-
end
|
233
249
|
block_arity = stub.block.arity
|
234
250
|
status, headers, body =
|
235
251
|
if block_arity >= 0
|
data/lib/faraday/adapter.rb
CHANGED
@@ -11,17 +11,7 @@ module Faraday
|
|
11
11
|
|
12
12
|
register_middleware File.expand_path('adapter', __dir__),
|
13
13
|
test: [:Test, 'test'],
|
14
|
-
|
15
|
-
:NetHttpPersistent,
|
16
|
-
'net_http_persistent'
|
17
|
-
],
|
18
|
-
typhoeus: [:Typhoeus, 'typhoeus'],
|
19
|
-
patron: [:Patron, 'patron'],
|
20
|
-
em_synchrony: [:EMSynchrony, 'em_synchrony'],
|
21
|
-
em_http: [:EMHttp, 'em_http'],
|
22
|
-
excon: [:Excon, 'excon'],
|
23
|
-
rack: [:Rack, 'rack'],
|
24
|
-
httpclient: [:HTTPClient, 'httpclient']
|
14
|
+
typhoeus: [:Typhoeus, 'typhoeus']
|
25
15
|
|
26
16
|
# This module marks an Adapter as supporting parallel requests.
|
27
17
|
module Parallelism
|
data/lib/faraday/autoload.rb
CHANGED
@@ -58,15 +58,8 @@ module Faraday
|
|
58
58
|
class Adapter
|
59
59
|
extend AutoloadHelper
|
60
60
|
autoload_all 'faraday/adapter',
|
61
|
-
NetHttpPersistent: 'net_http_persistent',
|
62
|
-
EMSynchrony: 'em_synchrony',
|
63
|
-
EMHttp: 'em_http',
|
64
61
|
Typhoeus: 'typhoeus',
|
65
|
-
|
66
|
-
Excon: 'excon',
|
67
|
-
Test: 'test',
|
68
|
-
Rack: 'rack',
|
69
|
-
HTTPClient: 'httpclient'
|
62
|
+
Test: 'test'
|
70
63
|
end
|
71
64
|
|
72
65
|
# Request represents a single HTTP request for a Faraday adapter to make.
|
data/lib/faraday/connection.rb
CHANGED
@@ -15,6 +15,7 @@ module Faraday
|
|
15
15
|
class Connection
|
16
16
|
# A Set of allowed HTTP verbs.
|
17
17
|
METHODS = Set.new %i[get post put delete head patch options trace]
|
18
|
+
USER_AGENT = "Faraday v#{VERSION}"
|
18
19
|
|
19
20
|
# @return [Hash] URI query unencoded key/value pairs.
|
20
21
|
attr_reader :params
|
@@ -26,7 +27,7 @@ module Faraday
|
|
26
27
|
# Connection. This includes a default host name, scheme, port, and path.
|
27
28
|
attr_reader :url_prefix
|
28
29
|
|
29
|
-
# @return [Faraday::
|
30
|
+
# @return [Faraday::RackBuilder] Builder for this Connection.
|
30
31
|
attr_reader :builder
|
31
32
|
|
32
33
|
# @return [Hash] SSL options.
|
@@ -73,6 +74,7 @@ module Faraday
|
|
73
74
|
@options = options.request
|
74
75
|
@ssl = options.ssl
|
75
76
|
@default_parallel_manager = options.parallel_manager
|
77
|
+
@manual_proxy = nil
|
76
78
|
|
77
79
|
@builder = options.builder || begin
|
78
80
|
# pass an empty block to Builder so it doesn't assume default middleware
|
@@ -88,7 +90,7 @@ module Faraday
|
|
88
90
|
|
89
91
|
yield(self) if block_given?
|
90
92
|
|
91
|
-
@headers[:user_agent] ||=
|
93
|
+
@headers[:user_agent] ||= USER_AGENT
|
92
94
|
end
|
93
95
|
|
94
96
|
def initialize_proxy(url, options)
|
@@ -295,6 +297,11 @@ module Faraday
|
|
295
297
|
#
|
296
298
|
# @return [void]
|
297
299
|
def basic_auth(login, pass)
|
300
|
+
warn <<~TEXT
|
301
|
+
WARNING: `Faraday::Connection#basic_auth` is deprecated; it will be removed in version 2.0.
|
302
|
+
While initializing your connection, use `#request(:basic_auth, ...)` instead.
|
303
|
+
See https://lostisland.github.io/faraday/middleware/authentication for more usage info.
|
304
|
+
TEXT
|
298
305
|
set_authorization_header(:basic_auth, login, pass)
|
299
306
|
end
|
300
307
|
|
@@ -312,6 +319,11 @@ module Faraday
|
|
312
319
|
#
|
313
320
|
# @return [void]
|
314
321
|
def token_auth(token, options = nil)
|
322
|
+
warn <<~TEXT
|
323
|
+
WARNING: `Faraday::Connection#token_auth` is deprecated; it will be removed in version 2.0.
|
324
|
+
While initializing your connection, use `#request(:token_auth, ...)` instead.
|
325
|
+
See https://lostisland.github.io/faraday/middleware/authentication for more usage info.
|
326
|
+
TEXT
|
315
327
|
set_authorization_header(:token_auth, token, options)
|
316
328
|
end
|
317
329
|
|
@@ -334,6 +346,11 @@ module Faraday
|
|
334
346
|
#
|
335
347
|
# @return [void]
|
336
348
|
def authorization(type, token)
|
349
|
+
warn <<~TEXT
|
350
|
+
WARNING: `Faraday::Connection#authorization` is deprecated; it will be removed in version 2.0.
|
351
|
+
While initializing your connection, use `#request(:authorization, ...)` instead.
|
352
|
+
See https://lostisland.github.io/faraday/middleware/authentication for more usage info.
|
353
|
+
TEXT
|
337
354
|
set_authorization_header(:authorization, type, token)
|
338
355
|
end
|
339
356
|
|
@@ -416,9 +433,16 @@ module Faraday
|
|
416
433
|
uri.query = nil
|
417
434
|
|
418
435
|
with_uri_credentials(uri) do |user, password|
|
419
|
-
|
436
|
+
set_basic_auth(user, password)
|
420
437
|
uri.user = uri.password = nil
|
421
438
|
end
|
439
|
+
|
440
|
+
@proxy = proxy_from_env(url) unless @manual_proxy
|
441
|
+
end
|
442
|
+
|
443
|
+
def set_basic_auth(user, password)
|
444
|
+
header = Faraday::Request::BasicAuthentication.header(user, password)
|
445
|
+
headers[Faraday::Request::Authorization::KEY] = header
|
422
446
|
end
|
423
447
|
|
424
448
|
# Sets the path prefix and ensures that it always has a leading
|
@@ -517,9 +541,8 @@ module Faraday
|
|
517
541
|
# @return [URI]
|
518
542
|
def build_exclusive_url(url = nil, params = nil, params_encoder = nil)
|
519
543
|
url = nil if url.respond_to?(:empty?) && url.empty?
|
520
|
-
base = url_prefix
|
544
|
+
base = url_prefix.dup
|
521
545
|
if url && base.path && base.path !~ %r{/$}
|
522
|
-
base = base.dup
|
523
546
|
base.path = "#{base.path}/" # ensure trailing slash
|
524
547
|
end
|
525
548
|
url = url && URI.parse(url.to_s).opaque ? url.to_s.gsub(':', '%3A') : url
|
@@ -577,7 +600,11 @@ module Faraday
|
|
577
600
|
case url
|
578
601
|
when String
|
579
602
|
uri = Utils.URI(url)
|
580
|
-
uri =
|
603
|
+
uri = if uri.host.nil?
|
604
|
+
find_default_proxy
|
605
|
+
else
|
606
|
+
URI.parse("#{uri.scheme}://#{uri.host}").find_proxy
|
607
|
+
end
|
581
608
|
when URI
|
582
609
|
uri = url.find_proxy
|
583
610
|
when nil
|
data/lib/faraday/error.rb
CHANGED
@@ -143,10 +143,4 @@ module Faraday
|
|
143
143
|
# Raised by FaradayMiddleware::ResponseMiddleware
|
144
144
|
class ParsingError < Error
|
145
145
|
end
|
146
|
-
|
147
|
-
# Exception used to control the Retry middleware.
|
148
|
-
#
|
149
|
-
# @see Faraday::Request::Retry
|
150
|
-
class RetriableResponse < Error
|
151
|
-
end
|
152
146
|
end
|
@@ -11,6 +11,9 @@ module Faraday
|
|
11
11
|
def self.from(value)
|
12
12
|
case value
|
13
13
|
when String
|
14
|
+
# URIs without a scheme should default to http (like 'example:123').
|
15
|
+
# This fixes #1282 and prevents a silent failure in some adapters.
|
16
|
+
value = "http://#{value}" unless value.include?('://')
|
14
17
|
value = { uri: Utils.URI(value) }
|
15
18
|
when URI
|
16
19
|
value = { uri: value }
|
@@ -19,6 +22,7 @@ module Faraday
|
|
19
22
|
value[:uri] = Utils.URI(uri)
|
20
23
|
end
|
21
24
|
end
|
25
|
+
|
22
26
|
super(value)
|
23
27
|
end
|
24
28
|
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'base64'
|
4
|
+
|
3
5
|
module Faraday
|
4
6
|
class Request
|
5
7
|
# Request middleware for the Authorization HTTP header
|
@@ -13,7 +15,8 @@ module Faraday
|
|
13
15
|
# @return [String] a header value
|
14
16
|
def self.header(type, token)
|
15
17
|
case token
|
16
|
-
when String, Symbol
|
18
|
+
when String, Symbol, Proc
|
19
|
+
token = token.call if token.is_a?(Proc)
|
17
20
|
"#{type} #{token}"
|
18
21
|
when Hash
|
19
22
|
build_hash(type.to_s, token)
|
@@ -32,6 +35,7 @@ module Faraday
|
|
32
35
|
comma = ', '
|
33
36
|
values = []
|
34
37
|
hash.each do |key, value|
|
38
|
+
value = value.call if value.is_a?(Proc)
|
35
39
|
values << "#{key}=#{value.to_s.inspect}"
|
36
40
|
end
|
37
41
|
"#{type} #{values * comma}"
|
@@ -39,16 +43,19 @@ module Faraday
|
|
39
43
|
|
40
44
|
# @param app [#call]
|
41
45
|
# @param type [String, Symbol] Type of Authorization
|
42
|
-
# @param
|
43
|
-
|
44
|
-
|
46
|
+
# @param param [String, Symbol, Hash, Proc] parameter to build the Authorization header.
|
47
|
+
# This value can be a proc, in which case it will be invoked on each request.
|
48
|
+
def initialize(app, type, param)
|
49
|
+
@type = type
|
50
|
+
@param = param
|
45
51
|
super(app)
|
46
52
|
end
|
47
53
|
|
48
54
|
# @param env [Faraday::Env]
|
49
|
-
def
|
50
|
-
|
51
|
-
|
55
|
+
def on_request(env)
|
56
|
+
return if env.request_headers[KEY]
|
57
|
+
|
58
|
+
env.request_headers[KEY] = self.class.header(@type, @param)
|
52
59
|
end
|
53
60
|
end
|
54
61
|
end
|
data/lib/faraday/request.rb
CHANGED
@@ -35,8 +35,6 @@ module Faraday
|
|
35
35
|
|
36
36
|
register_middleware File.expand_path('request', __dir__),
|
37
37
|
url_encoded: [:UrlEncoded, 'url_encoded'],
|
38
|
-
multipart: [:Multipart, 'multipart'],
|
39
|
-
retry: [:Retry, 'retry'],
|
40
38
|
authorization: [:Authorization, 'authorization'],
|
41
39
|
basic_auth: [
|
42
40
|
:BasicAuthentication,
|
data/lib/faraday/version.rb
CHANGED
data/lib/faraday.rb
CHANGED
@@ -24,10 +24,25 @@ require 'faraday/adapter'
|
|
24
24
|
require 'faraday/request'
|
25
25
|
require 'faraday/response'
|
26
26
|
require 'faraday/error'
|
27
|
-
require 'faraday/
|
28
|
-
|
29
|
-
|
27
|
+
require 'faraday/request/url_encoded' # needed by multipart
|
28
|
+
|
29
|
+
# External Middleware gems and their aliases
|
30
|
+
require 'faraday/multipart'
|
31
|
+
require 'faraday/retry'
|
32
|
+
Faraday::Request::Multipart = Faraday::Multipart::Middleware
|
33
|
+
Faraday::Request::Retry = Faraday::Retry::Middleware
|
34
|
+
|
35
|
+
# External Adapters gems
|
36
|
+
unless defined?(JRUBY_VERSION)
|
37
|
+
require 'faraday/em_http'
|
38
|
+
require 'faraday/em_synchrony'
|
39
|
+
end
|
40
|
+
require 'faraday/excon'
|
41
|
+
require 'faraday/httpclient'
|
30
42
|
require 'faraday/net_http'
|
43
|
+
require 'faraday/net_http_persistent'
|
44
|
+
require 'faraday/patron'
|
45
|
+
require 'faraday/rack'
|
31
46
|
|
32
47
|
# This is the main namespace for Faraday.
|
33
48
|
#
|