faraday 1.7.1 → 2.2.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 +193 -3
- data/README.md +11 -9
- data/examples/client_spec.rb +19 -19
- data/examples/client_test.rb +22 -22
- data/lib/faraday/adapter/test.rb +10 -4
- data/lib/faraday/adapter.rb +2 -5
- data/lib/faraday/connection.rb +17 -93
- data/lib/faraday/encoders/nested_params_encoder.rb +2 -2
- data/lib/faraday/error.rb +3 -8
- data/lib/faraday/logging/formatter.rb +1 -0
- data/lib/faraday/middleware.rb +0 -1
- data/lib/faraday/middleware_registry.rb +17 -63
- data/lib/faraday/options.rb +3 -3
- data/lib/faraday/rack_builder.rb +23 -20
- data/lib/faraday/request/authorization.rb +32 -38
- data/lib/faraday/request/instrumentation.rb +2 -0
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request/url_encoded.rb +2 -0
- data/lib/faraday/request.rb +11 -31
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +4 -4
- data/lib/faraday/response/raise_error.rb +9 -1
- data/lib/faraday/response.rb +8 -19
- data/lib/faraday/utils/headers.rb +1 -1
- data/lib/faraday/utils.rb +10 -5
- data/lib/faraday/version.rb +1 -1
- data/lib/faraday.rb +11 -39
- data/spec/faraday/connection_spec.rb +136 -85
- data/spec/faraday/middleware_registry_spec.rb +31 -0
- data/spec/faraday/options/env_spec.rb +2 -2
- data/spec/faraday/rack_builder_spec.rb +26 -54
- 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 +0 -1
- data/spec/faraday/request_spec.rb +4 -15
- data/spec/faraday/response/json_spec.rb +117 -0
- data/spec/faraday/response/raise_error_spec.rb +7 -4
- data/spec/faraday/utils/headers_spec.rb +2 -2
- 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 +0 -1
- data/spec/support/shared_examples/request_method.rb +5 -18
- metadata +12 -147
- data/lib/faraday/adapter/typhoeus.rb +0 -15
- data/lib/faraday/autoload.rb +0 -87
- 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 -106
- 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 -49
- data/spec/faraday/adapter/em_synchrony_spec.rb +0 -18
- data/spec/faraday/adapter/excon_spec.rb +0 -49
- data/spec/faraday/adapter/httpclient_spec.rb +0 -73
- 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 -302
- data/spec/faraday/request/retry_spec.rb +0 -242
- data/spec/faraday/response/middleware_spec.rb +0 -68
- data/spec/support/webmock_rack_app.rb +0 -68
data/lib/faraday/connection.rb
CHANGED
@@ -6,9 +6,9 @@ module Faraday
|
|
6
6
|
#
|
7
7
|
# @example
|
8
8
|
#
|
9
|
-
# conn = Faraday::Connection.new 'http://
|
9
|
+
# conn = Faraday::Connection.new 'http://httpbingo.org'
|
10
10
|
#
|
11
|
-
# # GET http://
|
11
|
+
# # GET http://httpbingo.org/nigiri
|
12
12
|
# conn.get 'nigiri'
|
13
13
|
# # => #<Faraday::Response>
|
14
14
|
#
|
@@ -64,7 +64,7 @@ module Faraday
|
|
64
64
|
options = ConnectionOptions.from(options)
|
65
65
|
|
66
66
|
if url.is_a?(Hash) || url.is_a?(ConnectionOptions)
|
67
|
-
options =
|
67
|
+
options = Utils.deep_merge(options, url)
|
68
68
|
url = options.url
|
69
69
|
end
|
70
70
|
|
@@ -117,7 +117,7 @@ module Faraday
|
|
117
117
|
|
118
118
|
extend Forwardable
|
119
119
|
|
120
|
-
def_delegators :builder, :
|
120
|
+
def_delegators :builder, :use, :request, :response, :adapter, :app
|
121
121
|
|
122
122
|
# Closes the underlying resources and/or connections. In the case of
|
123
123
|
# persistent connections, this closes all currently open connections
|
@@ -283,77 +283,6 @@ module Faraday
|
|
283
283
|
RUBY
|
284
284
|
end
|
285
285
|
|
286
|
-
# Sets up the Authorization header with these credentials, encoded
|
287
|
-
# with base64.
|
288
|
-
#
|
289
|
-
# @param login [String] The authentication login.
|
290
|
-
# @param pass [String] The authentication password.
|
291
|
-
#
|
292
|
-
# @example
|
293
|
-
#
|
294
|
-
# conn.basic_auth 'Aladdin', 'open sesame'
|
295
|
-
# conn.headers['Authorization']
|
296
|
-
# # => "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
|
297
|
-
#
|
298
|
-
# @return [void]
|
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
|
305
|
-
set_authorization_header(:basic_auth, login, pass)
|
306
|
-
end
|
307
|
-
|
308
|
-
# Sets up the Authorization header with the given token.
|
309
|
-
#
|
310
|
-
# @param token [String]
|
311
|
-
# @param options [Hash] extra token options.
|
312
|
-
#
|
313
|
-
# @example
|
314
|
-
#
|
315
|
-
# conn.token_auth 'abcdef', foo: 'bar'
|
316
|
-
# conn.headers['Authorization']
|
317
|
-
# # => "Token token=\"abcdef\",
|
318
|
-
# foo=\"bar\""
|
319
|
-
#
|
320
|
-
# @return [void]
|
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
|
327
|
-
set_authorization_header(:token_auth, token, options)
|
328
|
-
end
|
329
|
-
|
330
|
-
# Sets up a custom Authorization header.
|
331
|
-
#
|
332
|
-
# @param type [String] authorization type
|
333
|
-
# @param token [String, Hash] token. A String value is taken literally, and
|
334
|
-
# a Hash is encoded into comma-separated key/value pairs.
|
335
|
-
#
|
336
|
-
# @example
|
337
|
-
#
|
338
|
-
# conn.authorization :Bearer, 'mF_9.B5f-4.1JqM'
|
339
|
-
# conn.headers['Authorization']
|
340
|
-
# # => "Bearer mF_9.B5f-4.1JqM"
|
341
|
-
#
|
342
|
-
# conn.authorization :Token, token: 'abcdef', foo: 'bar'
|
343
|
-
# conn.headers['Authorization']
|
344
|
-
# # => "Token token=\"abcdef\",
|
345
|
-
# foo=\"bar\""
|
346
|
-
#
|
347
|
-
# @return [void]
|
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
|
354
|
-
set_authorization_header(:authorization, type, token)
|
355
|
-
end
|
356
|
-
|
357
286
|
# Check if the adapter is parallel-capable.
|
358
287
|
#
|
359
288
|
# @yield if the adapter isn't parallel-capable, or if no adapter is set yet.
|
@@ -420,11 +349,11 @@ module Faraday
|
|
420
349
|
# @example
|
421
350
|
#
|
422
351
|
# conn = Faraday::Connection.new { ... }
|
423
|
-
# conn.url_prefix = "https://
|
352
|
+
# conn.url_prefix = "https://httpbingo.org/api"
|
424
353
|
# conn.scheme # => https
|
425
354
|
# conn.path_prefix # => "/api"
|
426
355
|
#
|
427
|
-
# conn.get("nigiri?page=2") # accesses https://
|
356
|
+
# conn.get("nigiri?page=2") # accesses https://httpbingo.org/api/nigiri
|
428
357
|
def url_prefix=(url, encoder = nil)
|
429
358
|
uri = @url_prefix = Utils.URI(url)
|
430
359
|
self.path_prefix = uri.path
|
@@ -433,13 +362,18 @@ module Faraday
|
|
433
362
|
uri.query = nil
|
434
363
|
|
435
364
|
with_uri_credentials(uri) do |user, password|
|
436
|
-
|
365
|
+
set_basic_auth(user, password)
|
437
366
|
uri.user = uri.password = nil
|
438
367
|
end
|
439
368
|
|
440
369
|
@proxy = proxy_from_env(url) unless @manual_proxy
|
441
370
|
end
|
442
371
|
|
372
|
+
def set_basic_auth(user, password)
|
373
|
+
header = Faraday::Utils.basic_header_from(user, password)
|
374
|
+
headers[Faraday::Request::Authorization::KEY] = header
|
375
|
+
end
|
376
|
+
|
443
377
|
# Sets the path prefix and ensures that it always has a leading
|
444
378
|
# slash.
|
445
379
|
#
|
@@ -461,15 +395,15 @@ module Faraday
|
|
461
395
|
#
|
462
396
|
# @example
|
463
397
|
# conn = Faraday::Connection.new { ... }
|
464
|
-
# conn.url_prefix = "https://
|
398
|
+
# conn.url_prefix = "https://httpbingo.org/api?token=abc"
|
465
399
|
# conn.scheme # => https
|
466
400
|
# conn.path_prefix # => "/api"
|
467
401
|
#
|
468
402
|
# conn.build_url("nigiri?page=2")
|
469
|
-
# # => https://
|
403
|
+
# # => https://httpbingo.org/api/nigiri?token=abc&page=2
|
470
404
|
#
|
471
405
|
# conn.build_url("nigiri", page: 2)
|
472
|
-
# # => https://
|
406
|
+
# # => https://httpbingo.org/api/nigiri?token=abc&page=2
|
473
407
|
#
|
474
408
|
def build_url(url = nil, extra_params = nil)
|
475
409
|
uri = build_exclusive_url(url)
|
@@ -540,14 +474,12 @@ module Faraday
|
|
540
474
|
if url && base.path && base.path !~ %r{/$}
|
541
475
|
base.path = "#{base.path}/" # ensure trailing slash
|
542
476
|
end
|
543
|
-
url = url
|
477
|
+
url = url.to_s.gsub(':', '%3A') if url && URI.parse(url.to_s).opaque
|
544
478
|
uri = url ? base + url : base
|
545
479
|
if params
|
546
480
|
uri.query = params.to_query(params_encoder || options.params_encoder)
|
547
481
|
end
|
548
|
-
# rubocop:disable Style/SafeNavigation
|
549
482
|
uri.query = nil if uri.query && uri.query.empty?
|
550
|
-
# rubocop:enable Style/SafeNavigation
|
551
483
|
uri
|
552
484
|
end
|
553
485
|
|
@@ -579,14 +511,6 @@ module Faraday
|
|
579
511
|
yield(Utils.unescape(uri.user), Utils.unescape(uri.password))
|
580
512
|
end
|
581
513
|
|
582
|
-
def set_authorization_header(header_type, *args)
|
583
|
-
header = Faraday::Request
|
584
|
-
.lookup_middleware(header_type)
|
585
|
-
.header(*args)
|
586
|
-
|
587
|
-
headers[Faraday::Request::Authorization::KEY] = header
|
588
|
-
end
|
589
|
-
|
590
514
|
def proxy_from_env(url)
|
591
515
|
return if Faraday.ignore_env_proxy
|
592
516
|
|
@@ -631,7 +555,7 @@ module Faraday
|
|
631
555
|
end
|
632
556
|
|
633
557
|
def support_parallel?(adapter)
|
634
|
-
adapter
|
558
|
+
adapter.respond_to?(:supports_parallel?) && adapter&.supports_parallel?
|
635
559
|
end
|
636
560
|
end
|
637
561
|
end
|
@@ -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
|
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))
|
@@ -52,6 +52,7 @@ module Faraday
|
|
52
52
|
# :body - Optional string HTTP response body.
|
53
53
|
# :request - Hash
|
54
54
|
# :method - Symbol with the request HTTP method.
|
55
|
+
# :url - URI object with the url requested.
|
55
56
|
# :url_path - String with the url path requested.
|
56
57
|
# :params - String key/value hash of query params
|
57
58
|
# present in the request.
|
@@ -140,13 +141,7 @@ module Faraday
|
|
140
141
|
class SSLError < Error
|
141
142
|
end
|
142
143
|
|
143
|
-
# Raised by
|
144
|
+
# Raised by middlewares that parse the response, like the JSON response middleware.
|
144
145
|
class ParsingError < Error
|
145
146
|
end
|
146
|
-
|
147
|
-
# Exception used to control the Retry middleware.
|
148
|
-
#
|
149
|
-
# @see Faraday::Request::Retry
|
150
|
-
class RetriableResponse < Error
|
151
|
-
end
|
152
147
|
end
|
data/lib/faraday/middleware.rb
CHANGED
@@ -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,30 +45,27 @@ 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
|
load_middleware(key) ||
|
91
57
|
raise(Faraday::Error, "#{key.inspect} is not registered on #{self}")
|
92
58
|
end
|
93
59
|
|
60
|
+
private
|
61
|
+
|
94
62
|
def middleware_mutex(&block)
|
95
63
|
@middleware_mutex ||= Monitor.new
|
96
64
|
@middleware_mutex.synchronize(&block)
|
97
65
|
end
|
98
66
|
|
99
|
-
def fetch_middleware(key)
|
100
|
-
defined?(@registered_middleware) && @registered_middleware[key]
|
101
|
-
end
|
102
|
-
|
103
67
|
def load_middleware(key)
|
104
|
-
value =
|
68
|
+
value = registered_middleware[key]
|
105
69
|
case value
|
106
70
|
when Module
|
107
71
|
value
|
@@ -113,16 +77,6 @@ module Faraday
|
|
113
77
|
middleware_mutex do
|
114
78
|
@registered_middleware[key] = value.call
|
115
79
|
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
80
|
end
|
127
81
|
end
|
128
82
|
end
|
data/lib/faraday/options.rb
CHANGED
@@ -104,7 +104,7 @@ module Faraday
|
|
104
104
|
|
105
105
|
# Public
|
106
106
|
def each_key(&block)
|
107
|
-
return to_enum(:each_key) unless
|
107
|
+
return to_enum(:each_key) unless block
|
108
108
|
|
109
109
|
keys.each(&block)
|
110
110
|
end
|
@@ -118,7 +118,7 @@ module Faraday
|
|
118
118
|
|
119
119
|
# Public
|
120
120
|
def each_value(&block)
|
121
|
-
return to_enum(:each_value) unless
|
121
|
+
return to_enum(:each_value) unless block
|
122
122
|
|
123
123
|
values.each(&block)
|
124
124
|
end
|
@@ -168,7 +168,7 @@ module Faraday
|
|
168
168
|
end
|
169
169
|
|
170
170
|
def self.memoized(key, &block)
|
171
|
-
unless
|
171
|
+
unless block
|
172
172
|
raise ArgumentError, '#memoized must be called with a block'
|
173
173
|
end
|
174
174
|
|
data/lib/faraday/rack_builder.rb
CHANGED
@@ -8,7 +8,7 @@ module Faraday
|
|
8
8
|
# middleware stack (heavily inspired by Rack).
|
9
9
|
#
|
10
10
|
# @example
|
11
|
-
# Faraday::Connection.new(url: 'http://
|
11
|
+
# Faraday::Connection.new(url: 'http://httpbingo.org') do |builder|
|
12
12
|
# builder.request :url_encoded # Faraday::Request::UrlEncoded
|
13
13
|
# builder.adapter :net_http # Faraday::Adapter::NetHttp
|
14
14
|
# end
|
@@ -58,23 +58,22 @@ module Faraday
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
|
-
def initialize(
|
62
|
-
@adapter =
|
63
|
-
@handlers =
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
61
|
+
def initialize(&block)
|
62
|
+
@adapter = nil
|
63
|
+
@handlers = []
|
64
|
+
build(&block)
|
65
|
+
end
|
66
|
+
|
67
|
+
def initialize_dup(original)
|
68
|
+
super
|
69
|
+
@adapter = original.adapter
|
70
|
+
@handlers = original.handlers.dup
|
71
71
|
end
|
72
72
|
|
73
|
-
def build
|
73
|
+
def build
|
74
74
|
raise_if_locked
|
75
|
-
|
76
|
-
|
77
|
-
adapter(Faraday.default_adapter) unless @adapter
|
75
|
+
block_given? ? yield(self) : request(:url_encoded)
|
76
|
+
adapter(Faraday.default_adapter, **Faraday.default_adapter_options) unless @adapter
|
78
77
|
end
|
79
78
|
|
80
79
|
def [](idx)
|
@@ -109,7 +108,7 @@ module Faraday
|
|
109
108
|
end
|
110
109
|
|
111
110
|
ruby2_keywords def adapter(klass = NO_ARGUMENT, *args, &block)
|
112
|
-
return @adapter if klass == NO_ARGUMENT
|
111
|
+
return @adapter if klass == NO_ARGUMENT || klass.nil?
|
113
112
|
|
114
113
|
klass = Faraday::Adapter.lookup_middleware(klass) if klass.is_a?(Symbol)
|
115
114
|
@adapter = self.class::Handler.new(klass, *args, &block)
|
@@ -164,6 +163,7 @@ module Faraday
|
|
164
163
|
def app
|
165
164
|
@app ||= begin
|
166
165
|
lock!
|
166
|
+
ensure_adapter!
|
167
167
|
to_app
|
168
168
|
end
|
169
169
|
end
|
@@ -182,10 +182,6 @@ module Faraday
|
|
182
182
|
@adapter == other.adapter
|
183
183
|
end
|
184
184
|
|
185
|
-
def dup
|
186
|
-
self.class.new(@handlers.dup, @adapter.dup)
|
187
|
-
end
|
188
|
-
|
189
185
|
# ENV Keys
|
190
186
|
# :http_method - a symbolized request HTTP method (:get, :post)
|
191
187
|
# :body - the request body that will eventually be converted to a string.
|
@@ -216,6 +212,9 @@ module Faraday
|
|
216
212
|
private
|
217
213
|
|
218
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/.'
|
219
218
|
|
220
219
|
def raise_if_locked
|
221
220
|
raise StackLocked, LOCK_ERR if locked?
|
@@ -227,6 +226,10 @@ module Faraday
|
|
227
226
|
raise 'Adapter should be set using the `adapter` method, not `use`'
|
228
227
|
end
|
229
228
|
|
229
|
+
def ensure_adapter!
|
230
|
+
raise MISSING_ADAPTER_ERROR unless @adapter
|
231
|
+
end
|
232
|
+
|
230
233
|
def adapter_set?
|
231
234
|
!@adapter.nil?
|
232
235
|
end
|
@@ -4,52 +4,46 @@ module Faraday
|
|
4
4
|
class Request
|
5
5
|
# Request middleware for the Authorization HTTP header
|
6
6
|
class Authorization < Faraday::Middleware
|
7
|
-
|
8
|
-
KEY = 'Authorization'
|
9
|
-
end
|
10
|
-
|
11
|
-
# @param type [String, Symbol]
|
12
|
-
# @param token [String, Symbol, Hash]
|
13
|
-
# @return [String] a header value
|
14
|
-
def self.header(type, token)
|
15
|
-
case token
|
16
|
-
when String, Symbol
|
17
|
-
"#{type} #{token}"
|
18
|
-
when Hash
|
19
|
-
build_hash(type.to_s, token)
|
20
|
-
else
|
21
|
-
raise ArgumentError,
|
22
|
-
"Can't build an Authorization #{type}" \
|
23
|
-
"header from #{token.inspect}"
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
# @param type [String]
|
28
|
-
# @param hash [Hash]
|
29
|
-
# @return [String] type followed by comma-separated key=value pairs
|
30
|
-
# @api private
|
31
|
-
def self.build_hash(type, hash)
|
32
|
-
comma = ', '
|
33
|
-
values = []
|
34
|
-
hash.each do |key, value|
|
35
|
-
values << "#{key}=#{value.to_s.inspect}"
|
36
|
-
end
|
37
|
-
"#{type} #{values * comma}"
|
38
|
-
end
|
7
|
+
KEY = 'Authorization'
|
39
8
|
|
40
9
|
# @param app [#call]
|
41
10
|
# @param type [String, Symbol] Type of Authorization
|
42
|
-
# @param
|
43
|
-
|
44
|
-
|
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
|
45
19
|
super(app)
|
46
20
|
end
|
47
21
|
|
48
22
|
# @param env [Faraday::Env]
|
49
|
-
def
|
50
|
-
|
51
|
-
|
23
|
+
def on_request(env)
|
24
|
+
return if env.request_headers[KEY]
|
25
|
+
|
26
|
+
env.request_headers[KEY] = header_from(@type, *@params)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# @param type [String, Symbol]
|
32
|
+
# @param params [Array]
|
33
|
+
# @return [String] a header value
|
34
|
+
def header_from(type, *params)
|
35
|
+
if type.to_s.casecmp('basic').zero? && params.size == 2
|
36
|
+
Utils.basic_header_from(*params)
|
37
|
+
elsif params.size != 1
|
38
|
+
raise ArgumentError, "Unexpected params received (got #{params.size} instead of 1)"
|
39
|
+
else
|
40
|
+
value = params.first
|
41
|
+
value = value.call if value.is_a?(Proc) || value.respond_to?(:call)
|
42
|
+
"#{type} #{value}"
|
43
|
+
end
|
52
44
|
end
|
53
45
|
end
|
54
46
|
end
|
55
47
|
end
|
48
|
+
|
49
|
+
Faraday::Request.register_middleware(authorization: Faraday::Request::Authorization)
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Faraday
|
6
|
+
class Request
|
7
|
+
# Request middleware that encodes the body as JSON.
|
8
|
+
#
|
9
|
+
# Processes only requests with matching Content-type or those without a type.
|
10
|
+
# If a request doesn't have a type but has a body, it sets the Content-type
|
11
|
+
# to JSON MIME-type.
|
12
|
+
#
|
13
|
+
# Doesn't try to encode bodies that already are in string form.
|
14
|
+
class Json < Middleware
|
15
|
+
MIME_TYPE = 'application/json'
|
16
|
+
MIME_TYPE_REGEX = %r{^application/(vnd\..+\+)?json$}.freeze
|
17
|
+
|
18
|
+
def on_request(env)
|
19
|
+
match_content_type(env) do |data|
|
20
|
+
env[:body] = encode(data)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def encode(data)
|
27
|
+
::JSON.generate(data)
|
28
|
+
end
|
29
|
+
|
30
|
+
def match_content_type(env)
|
31
|
+
return unless process_request?(env)
|
32
|
+
|
33
|
+
env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
|
34
|
+
yield env[:body] unless env[:body].respond_to?(:to_str)
|
35
|
+
end
|
36
|
+
|
37
|
+
def process_request?(env)
|
38
|
+
type = request_type(env)
|
39
|
+
body?(env) && (type.empty? || type.match?(MIME_TYPE_REGEX))
|
40
|
+
end
|
41
|
+
|
42
|
+
def body?(env)
|
43
|
+
(body = env[:body]) && !(body.respond_to?(:to_str) && body.empty?)
|
44
|
+
end
|
45
|
+
|
46
|
+
def request_type(env)
|
47
|
+
type = env[:request_headers][CONTENT_TYPE].to_s
|
48
|
+
type = type.split(';', 2).first if type.index(';')
|
49
|
+
type
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
Faraday::Request.register_middleware(json: Faraday::Request::Json)
|