faraday 1.0.0 → 2.0.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 +221 -1
- data/LICENSE.md +1 -1
- data/README.md +19 -14
- data/examples/client_spec.rb +36 -4
- data/examples/client_test.rb +43 -4
- data/lib/faraday/adapter/test.rb +61 -43
- data/lib/faraday/adapter.rb +3 -16
- data/lib/faraday/adapter_registry.rb +3 -1
- data/lib/faraday/connection.rb +25 -78
- data/lib/faraday/encoders/flat_params_encoder.rb +9 -2
- data/lib/faraday/encoders/nested_params_encoder.rb +9 -4
- data/lib/faraday/error.rb +23 -8
- data/lib/faraday/logging/formatter.rb +1 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +14 -5
- data/lib/faraday/middleware_registry.rb +15 -79
- data/lib/faraday/options/proxy_options.rb +4 -0
- data/lib/faraday/options.rb +7 -11
- data/lib/faraday/rack_builder.rb +34 -30
- data/lib/faraday/request/authorization.rb +32 -36
- data/lib/faraday/request/instrumentation.rb +2 -0
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request/url_encoded.rb +5 -1
- data/lib/faraday/request.rb +13 -23
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +4 -4
- data/lib/faraday/response/raise_error.rb +20 -1
- data/lib/faraday/response.rb +8 -22
- data/lib/faraday/utils/headers.rb +3 -3
- data/lib/faraday/utils.rb +21 -8
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +44 -59
- data/spec/faraday/adapter/test_spec.rb +377 -0
- data/spec/faraday/connection_spec.rb +147 -51
- data/spec/faraday/error_spec.rb +15 -0
- data/spec/faraday/middleware_spec.rb +32 -6
- data/spec/faraday/options/env_spec.rb +2 -2
- data/spec/faraday/options/proxy_options_spec.rb +7 -0
- data/spec/faraday/params_encoders/flat_spec.rb +8 -0
- data/spec/faraday/params_encoders/nested_spec.rb +8 -0
- data/spec/faraday/rack_builder_spec.rb +144 -38
- data/spec/faraday/request/authorization_spec.rb +19 -24
- data/spec/faraday/request/instrumentation_spec.rb +5 -7
- data/spec/faraday/request/json_spec.rb +111 -0
- data/spec/faraday/request/url_encoded_spec.rb +13 -1
- data/spec/faraday/request_spec.rb +6 -6
- data/spec/faraday/response/json_spec.rb +117 -0
- data/spec/faraday/response/raise_error_spec.rb +66 -0
- data/spec/faraday/utils_spec.rb +62 -1
- data/spec/support/fake_safe_buffer.rb +1 -1
- data/spec/support/helper_methods.rb +0 -37
- data/spec/support/shared_examples/adapter.rb +2 -2
- data/spec/support/shared_examples/request_method.rb +43 -28
- metadata +16 -48
- data/UPGRADING.md +0 -55
- data/lib/faraday/adapter/em_http.rb +0 -285
- 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 -150
- data/lib/faraday/adapter/excon.rb +0 -124
- data/lib/faraday/adapter/httpclient.rb +0 -151
- data/lib/faraday/adapter/net_http.rb +0 -209
- 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/adapter/typhoeus.rb +0 -15
- data/lib/faraday/autoload.rb +0 -95
- data/lib/faraday/dependency_loader.rb +0 -37
- data/lib/faraday/file_part.rb +0 -128
- data/lib/faraday/param_part.rb +0 -53
- data/lib/faraday/request/basic_authentication.rb +0 -20
- data/lib/faraday/request/multipart.rb +0 -99
- data/lib/faraday/request/retry.rb +0 -239
- data/lib/faraday/request/token_authentication.rb +0 -20
- data/spec/faraday/adapter/em_http_spec.rb +0 -47
- data/spec/faraday/adapter/em_synchrony_spec.rb +0 -16
- data/spec/faraday/adapter/excon_spec.rb +0 -49
- data/spec/faraday/adapter/httpclient_spec.rb +0 -73
- data/spec/faraday/adapter/net_http_persistent_spec.rb +0 -57
- data/spec/faraday/adapter/net_http_spec.rb +0 -64
- data/spec/faraday/adapter/patron_spec.rb +0 -18
- data/spec/faraday/adapter/rack_spec.rb +0 -8
- data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
- data/spec/faraday/composite_read_io_spec.rb +0 -80
- data/spec/faraday/request/multipart_spec.rb +0 -274
- data/spec/faraday/request/retry_spec.rb +0 -242
- data/spec/faraday/response/middleware_spec.rb +0 -52
- data/spec/support/webmock_rack_app.rb +0 -68
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.
|
@@ -63,7 +64,7 @@ module Faraday
|
|
63
64
|
options = ConnectionOptions.from(options)
|
64
65
|
|
65
66
|
if url.is_a?(Hash) || url.is_a?(ConnectionOptions)
|
66
|
-
options =
|
67
|
+
options = Utils.deep_merge(options, url)
|
67
68
|
url = options.url
|
68
69
|
end
|
69
70
|
|
@@ -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)
|
@@ -115,7 +117,7 @@ module Faraday
|
|
115
117
|
|
116
118
|
extend Forwardable
|
117
119
|
|
118
|
-
def_delegators :builder, :
|
120
|
+
def_delegators :builder, :use, :request, :response, :adapter, :app
|
119
121
|
|
120
122
|
# Closes the underlying resources and/or connections. In the case of
|
121
123
|
# persistent connections, this closes all currently open connections
|
@@ -281,62 +283,6 @@ module Faraday
|
|
281
283
|
RUBY
|
282
284
|
end
|
283
285
|
|
284
|
-
# Sets up the Authorization header with these credentials, encoded
|
285
|
-
# with base64.
|
286
|
-
#
|
287
|
-
# @param login [String] The authentication login.
|
288
|
-
# @param pass [String] The authentication password.
|
289
|
-
#
|
290
|
-
# @example
|
291
|
-
#
|
292
|
-
# conn.basic_auth 'Aladdin', 'open sesame'
|
293
|
-
# conn.headers['Authorization']
|
294
|
-
# # => "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
|
295
|
-
#
|
296
|
-
# @return [void]
|
297
|
-
def basic_auth(login, pass)
|
298
|
-
set_authorization_header(:basic_auth, login, pass)
|
299
|
-
end
|
300
|
-
|
301
|
-
# Sets up the Authorization header with the given token.
|
302
|
-
#
|
303
|
-
# @param token [String]
|
304
|
-
# @param options [Hash] extra token options.
|
305
|
-
#
|
306
|
-
# @example
|
307
|
-
#
|
308
|
-
# conn.token_auth 'abcdef', foo: 'bar'
|
309
|
-
# conn.headers['Authorization']
|
310
|
-
# # => "Token token=\"abcdef\",
|
311
|
-
# foo=\"bar\""
|
312
|
-
#
|
313
|
-
# @return [void]
|
314
|
-
def token_auth(token, options = nil)
|
315
|
-
set_authorization_header(:token_auth, token, options)
|
316
|
-
end
|
317
|
-
|
318
|
-
# Sets up a custom Authorization header.
|
319
|
-
#
|
320
|
-
# @param type [String] authorization type
|
321
|
-
# @param token [String, Hash] token. A String value is taken literally, and
|
322
|
-
# a Hash is encoded into comma-separated key/value pairs.
|
323
|
-
#
|
324
|
-
# @example
|
325
|
-
#
|
326
|
-
# conn.authorization :Bearer, 'mF_9.B5f-4.1JqM'
|
327
|
-
# conn.headers['Authorization']
|
328
|
-
# # => "Bearer mF_9.B5f-4.1JqM"
|
329
|
-
#
|
330
|
-
# conn.authorization :Token, token: 'abcdef', foo: 'bar'
|
331
|
-
# conn.headers['Authorization']
|
332
|
-
# # => "Token token=\"abcdef\",
|
333
|
-
# foo=\"bar\""
|
334
|
-
#
|
335
|
-
# @return [void]
|
336
|
-
def authorization(type, token)
|
337
|
-
set_authorization_header(:authorization, type, token)
|
338
|
-
end
|
339
|
-
|
340
286
|
# Check if the adapter is parallel-capable.
|
341
287
|
#
|
342
288
|
# @yield if the adapter isn't parallel-capable, or if no adapter is set yet.
|
@@ -416,9 +362,16 @@ module Faraday
|
|
416
362
|
uri.query = nil
|
417
363
|
|
418
364
|
with_uri_credentials(uri) do |user, password|
|
419
|
-
|
365
|
+
set_basic_auth(user, password)
|
420
366
|
uri.user = uri.password = nil
|
421
367
|
end
|
368
|
+
|
369
|
+
@proxy = proxy_from_env(url) unless @manual_proxy
|
370
|
+
end
|
371
|
+
|
372
|
+
def set_basic_auth(user, password)
|
373
|
+
header = Faraday::Utils.basic_header_from(user, password)
|
374
|
+
headers[Faraday::Request::Authorization::KEY] = header
|
422
375
|
end
|
423
376
|
|
424
377
|
# Sets the path prefix and ensures that it always has a leading
|
@@ -429,7 +382,7 @@ module Faraday
|
|
429
382
|
# @return [String] the new path prefix
|
430
383
|
def path_prefix=(value)
|
431
384
|
url_prefix.path = if value
|
432
|
-
value =
|
385
|
+
value = "/#{value}" unless value[0, 1] == '/'
|
433
386
|
value
|
434
387
|
end
|
435
388
|
end
|
@@ -517,18 +470,16 @@ module Faraday
|
|
517
470
|
# @return [URI]
|
518
471
|
def build_exclusive_url(url = nil, params = nil, params_encoder = nil)
|
519
472
|
url = nil if url.respond_to?(:empty?) && url.empty?
|
520
|
-
base = url_prefix
|
473
|
+
base = url_prefix.dup
|
521
474
|
if url && base.path && base.path !~ %r{/$}
|
522
|
-
base = base.
|
523
|
-
base.path = base.path + '/' # ensure trailing slash
|
475
|
+
base.path = "#{base.path}/" # ensure trailing slash
|
524
476
|
end
|
477
|
+
url = url.to_s.gsub(':', '%3A') if url && URI.parse(url.to_s).opaque
|
525
478
|
uri = url ? base + url : base
|
526
479
|
if params
|
527
480
|
uri.query = params.to_query(params_encoder || options.params_encoder)
|
528
481
|
end
|
529
|
-
# rubocop:disable Style/SafeNavigation
|
530
482
|
uri.query = nil if uri.query && uri.query.empty?
|
531
|
-
# rubocop:enable Style/SafeNavigation
|
532
483
|
uri
|
533
484
|
end
|
534
485
|
|
@@ -560,14 +511,6 @@ module Faraday
|
|
560
511
|
yield(Utils.unescape(uri.user), Utils.unescape(uri.password))
|
561
512
|
end
|
562
513
|
|
563
|
-
def set_authorization_header(header_type, *args)
|
564
|
-
header = Faraday::Request
|
565
|
-
.lookup_middleware(header_type)
|
566
|
-
.header(*args)
|
567
|
-
|
568
|
-
headers[Faraday::Request::Authorization::KEY] = header
|
569
|
-
end
|
570
|
-
|
571
514
|
def proxy_from_env(url)
|
572
515
|
return if Faraday.ignore_env_proxy
|
573
516
|
|
@@ -576,7 +519,11 @@ module Faraday
|
|
576
519
|
case url
|
577
520
|
when String
|
578
521
|
uri = Utils.URI(url)
|
579
|
-
uri =
|
522
|
+
uri = if uri.host.nil?
|
523
|
+
find_default_proxy
|
524
|
+
else
|
525
|
+
URI.parse("#{uri.scheme}://#{uri.host}").find_proxy
|
526
|
+
end
|
580
527
|
when URI
|
581
528
|
uri = url.find_proxy
|
582
529
|
when nil
|
@@ -593,7 +540,7 @@ module Faraday
|
|
593
540
|
uri = ENV['http_proxy']
|
594
541
|
return unless uri && !uri.empty?
|
595
542
|
|
596
|
-
uri =
|
543
|
+
uri = "http://#{uri}" unless uri.match?(/^http/i)
|
597
544
|
uri
|
598
545
|
end
|
599
546
|
|
@@ -608,7 +555,7 @@ module Faraday
|
|
608
555
|
end
|
609
556
|
|
610
557
|
def support_parallel?(adapter)
|
611
|
-
adapter
|
558
|
+
adapter.respond_to?(:supports_parallel?) && adapter&.supports_parallel?
|
612
559
|
end
|
613
560
|
end
|
614
561
|
end
|
@@ -33,9 +33,9 @@ module Faraday
|
|
33
33
|
key = key.to_s if key.is_a?(Symbol)
|
34
34
|
[key, value]
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
# Only to be used for non-Array inputs. Arrays should preserve order.
|
38
|
-
params.sort!
|
38
|
+
params.sort! if @sort_params
|
39
39
|
end
|
40
40
|
|
41
41
|
# The params have form [['key1', 'value1'], ['key2', 'value2']].
|
@@ -94,5 +94,12 @@ module Faraday
|
|
94
94
|
end
|
95
95
|
end
|
96
96
|
end
|
97
|
+
|
98
|
+
class << self
|
99
|
+
attr_accessor :sort_params
|
100
|
+
end
|
101
|
+
|
102
|
+
# Useful default for OAuth and caching.
|
103
|
+
@sort_params = true
|
97
104
|
end
|
98
105
|
end
|
@@ -21,9 +21,9 @@ module Faraday
|
|
21
21
|
key = key.to_s if key.is_a?(Symbol)
|
22
22
|
[key, value]
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
# Only to be used for non-Array inputs. Arrays should preserve order.
|
26
|
-
params.sort!
|
26
|
+
params.sort! if @sort_params
|
27
27
|
end
|
28
28
|
|
29
29
|
# The params have form [['key1', 'value1'], ['key2', 'value2']].
|
@@ -102,7 +102,7 @@ module Faraday
|
|
102
102
|
subkeys = key.scan(SUBKEYS_REGEX)
|
103
103
|
subkeys.each_with_index do |subkey, i|
|
104
104
|
is_array = subkey =~ /[\[\]]+\Z/
|
105
|
-
subkey =
|
105
|
+
subkey = Regexp.last_match.pre_match if is_array
|
106
106
|
last_subkey = i == subkeys.length - 1
|
107
107
|
|
108
108
|
context = prepare_context(context, subkey, is_array, last_subkey)
|
@@ -124,7 +124,7 @@ module Faraday
|
|
124
124
|
value_type = is_array ? Array : Hash
|
125
125
|
if context[subkey] && !context[subkey].is_a?(value_type)
|
126
126
|
raise TypeError, "expected #{value_type.name} " \
|
127
|
-
|
127
|
+
"(got #{context[subkey].class.name}) for param `#{subkey}'"
|
128
128
|
end
|
129
129
|
|
130
130
|
context[subkey] ||= value_type.new
|
@@ -161,10 +161,15 @@ module Faraday
|
|
161
161
|
# for your requests.
|
162
162
|
module NestedParamsEncoder
|
163
163
|
class << self
|
164
|
+
attr_accessor :sort_params
|
165
|
+
|
164
166
|
extend Forwardable
|
165
167
|
def_delegators :'Faraday::Utils', :escape, :unescape
|
166
168
|
end
|
167
169
|
|
170
|
+
# Useful default for OAuth and caching.
|
171
|
+
@sort_params = true
|
172
|
+
|
168
173
|
extend EncodeMethods
|
169
174
|
extend DecodeMethods
|
170
175
|
end
|
data/lib/faraday/error.rb
CHANGED
@@ -6,7 +6,7 @@ module Faraday
|
|
6
6
|
class Error < StandardError
|
7
7
|
attr_reader :response, :wrapped_exception
|
8
8
|
|
9
|
-
def initialize(exc, response = nil)
|
9
|
+
def initialize(exc = nil, response = nil)
|
10
10
|
@wrapped_exception = nil unless defined?(@wrapped_exception)
|
11
11
|
@response = nil unless defined?(@response)
|
12
12
|
super(exc_msg_and_response!(exc, response))
|
@@ -28,6 +28,18 @@ module Faraday
|
|
28
28
|
%(#<#{self.class}#{inner}>)
|
29
29
|
end
|
30
30
|
|
31
|
+
def response_status
|
32
|
+
@response[:status] if @response
|
33
|
+
end
|
34
|
+
|
35
|
+
def response_headers
|
36
|
+
@response[:headers] if @response
|
37
|
+
end
|
38
|
+
|
39
|
+
def response_body
|
40
|
+
@response[:body] if @response
|
41
|
+
end
|
42
|
+
|
31
43
|
protected
|
32
44
|
|
33
45
|
# Pulls out potential parent exception and response hash, storing them in
|
@@ -38,6 +50,15 @@ module Faraday
|
|
38
50
|
# :headers - String key/value hash of HTTP response header
|
39
51
|
# values.
|
40
52
|
# :body - Optional string HTTP response body.
|
53
|
+
# :request - Hash
|
54
|
+
# :method - Symbol with the request HTTP method.
|
55
|
+
# :url - URI object with the url requested.
|
56
|
+
# :url_path - String with the url path requested.
|
57
|
+
# :params - String key/value hash of query params
|
58
|
+
# present in the request.
|
59
|
+
# :headers - String key/value hash of HTTP request
|
60
|
+
# header values.
|
61
|
+
# :body - String HTTP request body.
|
41
62
|
#
|
42
63
|
# If a subclass has to call this, then it should pass a string message
|
43
64
|
# to `super`. See NilStatusError.
|
@@ -120,13 +141,7 @@ module Faraday
|
|
120
141
|
class SSLError < Error
|
121
142
|
end
|
122
143
|
|
123
|
-
# Raised by
|
144
|
+
# Raised by middlewares that parse the response, like the JSON response middleware.
|
124
145
|
class ParsingError < Error
|
125
146
|
end
|
126
|
-
|
127
|
-
# Exception used to control the Retry middleware.
|
128
|
-
#
|
129
|
-
# @see Faraday::Request::Retry
|
130
|
-
class RetriableResponse < Error
|
131
|
-
end
|
132
147
|
end
|
data/lib/faraday/middleware.rb
CHANGED
@@ -4,17 +4,26 @@ module Faraday
|
|
4
4
|
# Middleware is the basic base class of any Faraday middleware.
|
5
5
|
class Middleware
|
6
6
|
extend MiddlewareRegistry
|
7
|
-
extend DependencyLoader
|
8
7
|
|
9
|
-
|
8
|
+
attr_reader :app, :options
|
9
|
+
|
10
|
+
def initialize(app = nil, options = {})
|
10
11
|
@app = app
|
12
|
+
@options = options
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(env)
|
16
|
+
on_request(env) if respond_to?(:on_request)
|
17
|
+
app.call(env).on_complete do |environment|
|
18
|
+
on_complete(environment) if respond_to?(:on_complete)
|
19
|
+
end
|
11
20
|
end
|
12
21
|
|
13
22
|
def close
|
14
|
-
if
|
15
|
-
|
23
|
+
if app.respond_to?(:close)
|
24
|
+
app.close
|
16
25
|
else
|
17
|
-
warn "#{
|
26
|
+
warn "#{app} does not implement \#close!"
|
18
27
|
end
|
19
28
|
end
|
20
29
|
end
|
@@ -6,59 +6,26 @@ module Faraday
|
|
6
6
|
# Adds the ability for other modules to register and lookup
|
7
7
|
# middleware classes.
|
8
8
|
module MiddlewareRegistry
|
9
|
+
def registered_middleware
|
10
|
+
@registered_middleware ||= {}
|
11
|
+
end
|
12
|
+
|
9
13
|
# Register middleware class(es) on the current module.
|
10
14
|
#
|
11
|
-
# @param
|
12
|
-
# @param mapping [Hash{
|
13
|
-
# Symbol => Module,
|
14
|
-
# Symbol => Array<Module, Symbol, String>,
|
15
|
-
# }] Middleware mapping from a lookup symbol to a reference to the
|
16
|
-
# middleware.
|
17
|
-
# Classes can be expressed as:
|
18
|
-
# - a fully qualified constant
|
19
|
-
# - a Symbol
|
20
|
-
# - a Proc that will be lazily called to return the former
|
21
|
-
# - an array is given, its first element is the constant or symbol,
|
22
|
-
# and its second is a file to `require`.
|
15
|
+
# @param mappings [Hash] Middleware mappings from a lookup symbol to a middleware class.
|
23
16
|
# @return [void]
|
24
17
|
#
|
25
18
|
# @example Lookup by a constant
|
26
19
|
#
|
27
20
|
# module Faraday
|
28
|
-
# class Whatever
|
21
|
+
# class Whatever < Middleware
|
29
22
|
# # Middleware looked up by :foo returns Faraday::Whatever::Foo.
|
30
|
-
# register_middleware
|
31
|
-
# end
|
32
|
-
# end
|
33
|
-
#
|
34
|
-
# @example Lookup by a symbol
|
35
|
-
#
|
36
|
-
# module Faraday
|
37
|
-
# class Whatever
|
38
|
-
# # Middleware looked up by :bar returns
|
39
|
-
# # Faraday::Whatever.const_get(:Bar)
|
40
|
-
# register_middleware bar: :Bar
|
41
|
-
# end
|
42
|
-
# end
|
43
|
-
#
|
44
|
-
# @example Lookup by a symbol and string in an array
|
45
|
-
#
|
46
|
-
# module Faraday
|
47
|
-
# class Whatever
|
48
|
-
# # Middleware looked up by :baz requires 'baz' and returns
|
49
|
-
# # Faraday::Whatever.const_get(:Baz)
|
50
|
-
# register_middleware baz: [:Baz, 'baz']
|
23
|
+
# register_middleware(foo: Whatever)
|
51
24
|
# end
|
52
25
|
# end
|
53
|
-
|
54
|
-
def register_middleware(autoload_path = nil, mapping = nil)
|
55
|
-
if mapping.nil?
|
56
|
-
mapping = autoload_path
|
57
|
-
autoload_path = nil
|
58
|
-
end
|
26
|
+
def register_middleware(**mappings)
|
59
27
|
middleware_mutex do
|
60
|
-
|
61
|
-
(@registered_middleware ||= {}).update(mapping)
|
28
|
+
registered_middleware.update(mappings)
|
62
29
|
end
|
63
30
|
end
|
64
31
|
|
@@ -66,7 +33,7 @@ module Faraday
|
|
66
33
|
#
|
67
34
|
# @param key [Symbol] key for the registered middleware.
|
68
35
|
def unregister_middleware(key)
|
69
|
-
|
36
|
+
registered_middleware.delete(key)
|
70
37
|
end
|
71
38
|
|
72
39
|
# Lookup middleware class with a registered Symbol shortcut.
|
@@ -78,16 +45,15 @@ module Faraday
|
|
78
45
|
# @example
|
79
46
|
#
|
80
47
|
# module Faraday
|
81
|
-
# class Whatever
|
82
|
-
# register_middleware
|
48
|
+
# class Whatever < Middleware
|
49
|
+
# register_middleware(foo: Whatever)
|
83
50
|
# end
|
84
51
|
# end
|
85
52
|
#
|
86
|
-
# Faraday::
|
87
|
-
# # => Faraday::Whatever
|
88
|
-
#
|
53
|
+
# Faraday::Middleware.lookup_middleware(:foo)
|
54
|
+
# # => Faraday::Whatever
|
89
55
|
def lookup_middleware(key)
|
90
|
-
|
56
|
+
registered_middleware[key] ||
|
91
57
|
raise(Faraday::Error, "#{key.inspect} is not registered on #{self}")
|
92
58
|
end
|
93
59
|
|
@@ -95,35 +61,5 @@ module Faraday
|
|
95
61
|
@middleware_mutex ||= Monitor.new
|
96
62
|
@middleware_mutex.synchronize(&block)
|
97
63
|
end
|
98
|
-
|
99
|
-
def fetch_middleware(key)
|
100
|
-
defined?(@registered_middleware) && @registered_middleware[key]
|
101
|
-
end
|
102
|
-
|
103
|
-
def load_middleware(key)
|
104
|
-
value = fetch_middleware(key)
|
105
|
-
case value
|
106
|
-
when Module
|
107
|
-
value
|
108
|
-
when Symbol, String
|
109
|
-
middleware_mutex do
|
110
|
-
@registered_middleware[key] = const_get(value)
|
111
|
-
end
|
112
|
-
when Proc
|
113
|
-
middleware_mutex do
|
114
|
-
@registered_middleware[key] = value.call
|
115
|
-
end
|
116
|
-
when Array
|
117
|
-
middleware_mutex do
|
118
|
-
const, path = value
|
119
|
-
if (root = @middleware_autoload_path)
|
120
|
-
path = "#{root}/#{path}"
|
121
|
-
end
|
122
|
-
require(path)
|
123
|
-
@registered_middleware[key] = const
|
124
|
-
end
|
125
|
-
load_middleware(key)
|
126
|
-
end
|
127
|
-
end
|
128
64
|
end
|
129
65
|
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
|
|
data/lib/faraday/options.rb
CHANGED
@@ -103,12 +103,10 @@ module Faraday
|
|
103
103
|
end
|
104
104
|
|
105
105
|
# Public
|
106
|
-
def each_key
|
107
|
-
return to_enum(:each_key) unless
|
106
|
+
def each_key(&block)
|
107
|
+
return to_enum(:each_key) unless block
|
108
108
|
|
109
|
-
keys.each
|
110
|
-
yield(key)
|
111
|
-
end
|
109
|
+
keys.each(&block)
|
112
110
|
end
|
113
111
|
|
114
112
|
# Public
|
@@ -119,12 +117,10 @@ module Faraday
|
|
119
117
|
alias has_key? key?
|
120
118
|
|
121
119
|
# Public
|
122
|
-
def each_value
|
123
|
-
return to_enum(:each_value) unless
|
120
|
+
def each_value(&block)
|
121
|
+
return to_enum(:each_value) unless block
|
124
122
|
|
125
|
-
values.each
|
126
|
-
yield(value)
|
127
|
-
end
|
123
|
+
values.each(&block)
|
128
124
|
end
|
129
125
|
|
130
126
|
# Public
|
@@ -172,7 +168,7 @@ module Faraday
|
|
172
168
|
end
|
173
169
|
|
174
170
|
def self.memoized(key, &block)
|
175
|
-
unless
|
171
|
+
unless block
|
176
172
|
raise ArgumentError, '#memoized must be called with a block'
|
177
173
|
end
|
178
174
|
|