faraday 1.10.4 → 2.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +198 -4
  3. data/LICENSE.md +1 -1
  4. data/README.md +34 -20
  5. data/Rakefile +6 -1
  6. data/examples/client_spec.rb +41 -19
  7. data/examples/client_test.rb +48 -22
  8. data/lib/faraday/adapter/test.rb +62 -13
  9. data/lib/faraday/adapter.rb +6 -10
  10. data/lib/faraday/connection.rb +72 -150
  11. data/lib/faraday/encoders/flat_params_encoder.rb +2 -2
  12. data/lib/faraday/encoders/nested_params_encoder.rb +14 -7
  13. data/lib/faraday/error.rb +66 -10
  14. data/lib/faraday/logging/formatter.rb +30 -17
  15. data/lib/faraday/middleware.rb +43 -2
  16. data/lib/faraday/middleware_registry.rb +17 -63
  17. data/lib/faraday/options/connection_options.rb +7 -6
  18. data/lib/faraday/options/env.rb +85 -62
  19. data/lib/faraday/options/proxy_options.rb +11 -5
  20. data/lib/faraday/options/request_options.rb +7 -6
  21. data/lib/faraday/options/ssl_options.rb +62 -45
  22. data/lib/faraday/options.rb +7 -6
  23. data/lib/faraday/rack_builder.rb +44 -45
  24. data/lib/faraday/request/authorization.rb +33 -41
  25. data/lib/faraday/request/instrumentation.rb +5 -1
  26. data/lib/faraday/request/json.rb +18 -3
  27. data/lib/faraday/request/url_encoded.rb +5 -1
  28. data/lib/faraday/request.rb +15 -30
  29. data/lib/faraday/response/json.rb +25 -5
  30. data/lib/faraday/response/logger.rb +11 -3
  31. data/lib/faraday/response/raise_error.rb +45 -18
  32. data/lib/faraday/response.rb +14 -22
  33. data/lib/faraday/utils/headers.rb +15 -4
  34. data/lib/faraday/utils.rb +11 -7
  35. data/lib/faraday/version.rb +1 -1
  36. data/lib/faraday.rb +10 -45
  37. data/spec/faraday/adapter/test_spec.rb +65 -0
  38. data/spec/faraday/connection_spec.rb +165 -93
  39. data/spec/faraday/error_spec.rb +122 -7
  40. data/spec/faraday/middleware_registry_spec.rb +31 -0
  41. data/spec/faraday/middleware_spec.rb +161 -0
  42. data/spec/faraday/options/env_spec.rb +8 -2
  43. data/spec/faraday/options/options_spec.rb +1 -1
  44. data/spec/faraday/options/proxy_options_spec.rb +35 -0
  45. data/spec/faraday/params_encoders/nested_spec.rb +10 -1
  46. data/spec/faraday/rack_builder_spec.rb +26 -54
  47. data/spec/faraday/request/authorization_spec.rb +50 -28
  48. data/spec/faraday/request/instrumentation_spec.rb +5 -7
  49. data/spec/faraday/request/json_spec.rb +88 -0
  50. data/spec/faraday/request/url_encoded_spec.rb +12 -2
  51. data/spec/faraday/request_spec.rb +5 -15
  52. data/spec/faraday/response/json_spec.rb +93 -6
  53. data/spec/faraday/response/logger_spec.rb +83 -4
  54. data/spec/faraday/response/raise_error_spec.rb +133 -16
  55. data/spec/faraday/response_spec.rb +10 -1
  56. data/spec/faraday/utils/headers_spec.rb +31 -4
  57. data/spec/faraday/utils_spec.rb +65 -1
  58. data/spec/faraday_spec.rb +10 -4
  59. data/spec/spec_helper.rb +5 -6
  60. data/spec/support/fake_safe_buffer.rb +1 -1
  61. data/spec/support/faraday_middleware_subclasses.rb +18 -0
  62. data/spec/support/helper_methods.rb +0 -37
  63. data/spec/support/shared_examples/adapter.rb +2 -2
  64. data/spec/support/shared_examples/request_method.rb +22 -21
  65. metadata +24 -149
  66. data/lib/faraday/adapter/typhoeus.rb +0 -15
  67. data/lib/faraday/autoload.rb +0 -89
  68. data/lib/faraday/dependency_loader.rb +0 -39
  69. data/lib/faraday/deprecate.rb +0 -110
  70. data/lib/faraday/request/basic_authentication.rb +0 -20
  71. data/lib/faraday/request/token_authentication.rb +0 -20
  72. data/spec/faraday/adapter/em_http_spec.rb +0 -49
  73. data/spec/faraday/adapter/em_synchrony_spec.rb +0 -18
  74. data/spec/faraday/adapter/excon_spec.rb +0 -49
  75. data/spec/faraday/adapter/httpclient_spec.rb +0 -73
  76. data/spec/faraday/adapter/net_http_spec.rb +0 -64
  77. data/spec/faraday/adapter/patron_spec.rb +0 -18
  78. data/spec/faraday/adapter/rack_spec.rb +0 -8
  79. data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
  80. data/spec/faraday/composite_read_io_spec.rb +0 -80
  81. data/spec/faraday/deprecate_spec.rb +0 -147
  82. data/spec/faraday/response/middleware_spec.rb +0 -68
  83. data/spec/support/webmock_rack_app.rb +0 -68
@@ -1,65 +1,70 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Faraday
4
- # @!attribute method
5
- # @return [Symbol] HTTP method (`:get`, `:post`)
6
- #
7
- # @!attribute body
8
- # @return [String] The request body that will eventually be converted to a
9
- # string.
10
- #
11
- # @!attribute url
12
- # @return [URI] URI instance for the current request.
13
- #
14
- # @!attribute request
15
- # @return [Hash] options for configuring the request.
16
- # Options for configuring the request.
17
- #
18
- # - `:timeout` open/read timeout Integer in seconds
19
- # - `:open_timeout` - read timeout Integer in seconds
20
- # - `:on_data` - Proc for streaming
21
- # - `:proxy` - Hash of proxy options
22
- # - `:uri` - Proxy Server URI
23
- # - `:user` - Proxy server username
24
- # - `:password` - Proxy server password
25
- #
26
- # @!attribute request_headers
27
- # @return [Hash] HTTP Headers to be sent to the server.
28
- #
29
- # @!attribute ssl
30
- # @return [Hash] options for configuring SSL requests
31
- #
32
- # @!attribute parallel_manager
33
- # @return [Object] sent if the connection is in parallel mode
34
- #
35
- # @!attribute params
36
- # @return [Hash]
37
- #
38
- # @!attribute response
39
- # @return [Response]
40
- #
41
- # @!attribute response_headers
42
- # @return [Hash] HTTP headers from the server
43
- #
44
- # @!attribute status
45
- # @return [Integer] HTTP response status code
46
- #
47
- # @!attribute reason_phrase
48
- # @return [String]
49
- class Env < Options.new(:method, :request_body, :url, :request,
50
- :request_headers, :ssl, :parallel_manager, :params,
51
- :response, :response_headers, :status,
52
- :reason_phrase, :response_body)
53
-
54
- # rubocop:disable Naming/ConstantName
55
- ContentLength = 'Content-Length'
56
- StatusesWithoutBody = Set.new [204, 304]
57
- SuccessfulStatuses = (200..299).freeze
58
- # rubocop:enable Naming/ConstantName
4
+ # @!parse
5
+ # # @!attribute method
6
+ # # @return [Symbol] HTTP method (`:get`, `:post`)
7
+ # #
8
+ # # @!attribute body
9
+ # # @return [String] The request body that will eventually be converted to a
10
+ # # string.
11
+ # #
12
+ # # @!attribute url
13
+ # # @return [URI] URI instance for the current request.
14
+ # #
15
+ # # @!attribute request
16
+ # # @return [Hash] options for configuring the request.
17
+ # # Options for configuring the request.
18
+ # #
19
+ # # - `:timeout` - time limit for the entire request (Integer in
20
+ # # seconds)
21
+ # # - `:open_timeout` - time limit for just the connection phase (e.g.
22
+ # # handshake) (Integer in seconds)
23
+ # # - `:read_timeout` - time limit for the first response byte received from
24
+ # # the server (Integer in seconds)
25
+ # # - `:write_timeout` - time limit for the client to send the request to the
26
+ # # server (Integer in seconds)
27
+ # # - `:on_data` - Proc for streaming
28
+ # # - `:proxy` - Hash of proxy options
29
+ # # - `:uri` - Proxy server URI
30
+ # # - `:user` - Proxy server username
31
+ # # - `:password` - Proxy server password
32
+ # #
33
+ # # @!attribute request_headers
34
+ # # @return [Hash] HTTP Headers to be sent to the server.
35
+ # #
36
+ # # @!attribute ssl
37
+ # # @return [Hash] options for configuring SSL requests
38
+ # #
39
+ # # @!attribute parallel_manager
40
+ # # @return [Object] sent if the connection is in parallel mode
41
+ # #
42
+ # # @!attribute params
43
+ # # @return [Hash]
44
+ # #
45
+ # # @!attribute response
46
+ # # @return [Response]
47
+ # #
48
+ # # @!attribute response_headers
49
+ # # @return [Hash] HTTP headers from the server
50
+ # #
51
+ # # @!attribute status
52
+ # # @return [Integer] HTTP response status code
53
+ # #
54
+ # # @!attribute reason_phrase
55
+ # # @return [String]
56
+ # class Env < Options; end
57
+ Env = Options.new(:method, :request_body, :url, :request,
58
+ :request_headers, :ssl, :parallel_manager, :params,
59
+ :response, :response_headers, :status,
60
+ :reason_phrase, :response_body) do
61
+ const_set(:ContentLength, 'Content-Length')
62
+ const_set(:StatusesWithoutBody, Set.new([204, 304]))
63
+ const_set(:SuccessfulStatuses, 200..299)
59
64
 
60
65
  # A Set of HTTP verbs that typically send a body. If no body is set for
61
66
  # these requests, the Content-Length header is set to 0.
62
- MethodsWithBodies = Set.new(Faraday::METHODS_WITH_BODY.map(&:to_sym))
67
+ const_set(:MethodsWithBodies, Set.new(Faraday::METHODS_WITH_BODY.map(&:to_sym)))
63
68
 
64
69
  options request: RequestOptions,
65
70
  request_headers: Utils::Headers, response_headers: Utils::Headers
@@ -120,25 +125,25 @@ module Faraday
120
125
 
121
126
  # @return [Boolean] true if status is in the set of {SuccessfulStatuses}.
122
127
  def success?
123
- SuccessfulStatuses.include?(status)
128
+ Env::SuccessfulStatuses.include?(status)
124
129
  end
125
130
 
126
131
  # @return [Boolean] true if there's no body yet, and the method is in the
127
- # set of {MethodsWithBodies}.
132
+ # set of {Env::MethodsWithBodies}.
128
133
  def needs_body?
129
- !body && MethodsWithBodies.include?(method)
134
+ !body && Env::MethodsWithBodies.include?(method)
130
135
  end
131
136
 
132
137
  # Sets content length to zero and the body to the empty string.
133
138
  def clear_body
134
- request_headers[ContentLength] = '0'
139
+ request_headers[Env::ContentLength] = '0'
135
140
  self.body = +''
136
141
  end
137
142
 
138
143
  # @return [Boolean] true if the status isn't in the set of
139
- # {StatusesWithoutBody}.
144
+ # {Env::StatusesWithoutBody}.
140
145
  def parse_body?
141
- !StatusesWithoutBody.include?(status)
146
+ !Env::StatusesWithoutBody.include?(status)
142
147
  end
143
148
 
144
149
  # @return [Boolean] true if there is a parallel_manager
@@ -157,6 +162,24 @@ module Faraday
157
162
  %(#<#{self.class}#{attrs.join(' ')}>)
158
163
  end
159
164
 
165
+ def stream_response?
166
+ request.stream_response?
167
+ end
168
+
169
+ def stream_response(&block)
170
+ size = 0
171
+ yielded = false
172
+ block_result = block.call do |chunk|
173
+ if chunk.bytesize.positive? || size.positive?
174
+ yielded = true
175
+ size += chunk.bytesize
176
+ request.on_data.call(chunk, size, self)
177
+ end
178
+ end
179
+ request.on_data.call(+'', 0, self) unless yielded
180
+ block_result
181
+ end
182
+
160
183
  # @private
161
184
  def custom_members
162
185
  @custom_members ||= {}
@@ -1,15 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Faraday
4
- # ProxyOptions contains the configurable properties for the proxy
5
- # configuration used when making an HTTP request.
6
- class ProxyOptions < Options.new(:uri, :user, :password)
4
+ # @!parse
5
+ # # ProxyOptions contains the configurable properties for the proxy
6
+ # # configuration used when making an HTTP request.
7
+ # class ProxyOptions < Options; end
8
+ ProxyOptions = Options.new(:uri, :user, :password) do
7
9
  extend Forwardable
8
10
  def_delegators :uri, :scheme, :scheme=, :host, :host=, :port, :port=,
9
11
  :path, :path=
10
12
 
11
13
  def self.from(value)
12
14
  case value
15
+ when ''
16
+ value = nil
13
17
  when String
14
18
  # URIs without a scheme should default to http (like 'example:123').
15
19
  # This fixes #1282 and prevents a silent failure in some adapters.
@@ -18,8 +22,10 @@ module Faraday
18
22
  when URI
19
23
  value = { uri: value }
20
24
  when Hash, Options
21
- if (uri = value.delete(:uri))
22
- value[:uri] = Utils.URI(uri)
25
+ if value[:uri]
26
+ value = value.dup.tap do |duped|
27
+ duped[:uri] = Utils.URI(duped[:uri])
28
+ end
23
29
  end
24
30
  end
25
31
 
@@ -1,12 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Faraday
4
- # RequestOptions contains the configurable properties for a Faraday request.
5
- class RequestOptions < Options.new(:params_encoder, :proxy, :bind,
6
- :timeout, :open_timeout, :read_timeout,
7
- :write_timeout, :boundary, :oauth,
8
- :context, :on_data)
9
-
4
+ # @!parse
5
+ # # RequestOptions contains the configurable properties for a Faraday request.
6
+ # class RequestOptions < Options; end
7
+ RequestOptions = Options.new(:params_encoder, :proxy, :bind,
8
+ :timeout, :open_timeout, :read_timeout,
9
+ :write_timeout, :boundary, :oauth,
10
+ :context, :on_data) do
10
11
  def []=(key, value)
11
12
  if key && key.to_sym == :proxy
12
13
  super(key, value ? ProxyOptions.from(value) : nil)
@@ -1,51 +1,63 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Faraday
4
- # SSL-related options.
5
- #
6
- # @!attribute verify
7
- # @return [Boolean] whether to verify SSL certificates or not
8
- #
9
- # @!attribute ca_file
10
- # @return [String] CA file
11
- #
12
- # @!attribute ca_path
13
- # @return [String] CA path
14
- #
15
- # @!attribute verify_mode
16
- # @return [Integer] Any `OpenSSL::SSL::` constant (see https://ruby-doc.org/stdlib-2.5.1/libdoc/openssl/rdoc/OpenSSL/SSL.html)
17
- #
18
- # @!attribute cert_store
19
- # @return [OpenSSL::X509::Store] certificate store
20
- #
21
- # @!attribute client_cert
22
- # @return [String, OpenSSL::X509::Certificate] client certificate
23
- #
24
- # @!attribute client_key
25
- # @return [String, OpenSSL::PKey::RSA, OpenSSL::PKey::DSA] client key
26
- #
27
- # @!attribute certificate
28
- # @return [OpenSSL::X509::Certificate] certificate (Excon only)
29
- #
30
- # @!attribute private_key
31
- # @return [OpenSSL::PKey::RSA, OpenSSL::PKey::DSA] private key (Excon only)
32
- #
33
- # @!attribute verify_depth
34
- # @return [Integer] maximum depth for the certificate chain verification
35
- #
36
- # @!attribute version
37
- # @return [String, Symbol] SSL version (see https://ruby-doc.org/stdlib-2.5.1/libdoc/openssl/rdoc/OpenSSL/SSL/SSLContext.html#method-i-ssl_version-3D)
38
- #
39
- # @!attribute min_version
40
- # @return [String, Symbol] minimum SSL version (see https://ruby-doc.org/stdlib-2.5.1/libdoc/openssl/rdoc/OpenSSL/SSL/SSLContext.html#method-i-min_version-3D)
41
- #
42
- # @!attribute max_version
43
- # @return [String, Symbol] maximum SSL version (see https://ruby-doc.org/stdlib-2.5.1/libdoc/openssl/rdoc/OpenSSL/SSL/SSLContext.html#method-i-max_version-3D)
44
- class SSLOptions < Options.new(:verify, :ca_file, :ca_path, :verify_mode,
45
- :cert_store, :client_cert, :client_key,
46
- :certificate, :private_key, :verify_depth,
47
- :version, :min_version, :max_version)
48
-
4
+ # @!parse
5
+ # # SSL-related options.
6
+ # #
7
+ # # @!attribute verify
8
+ # # @return [Boolean] whether to verify SSL certificates or not
9
+ # #
10
+ # # @!attribute verify_hostname
11
+ # # @return [Boolean] whether to enable hostname verification on server certificates
12
+ # # during the handshake or not (see https://github.com/ruby/openssl/pull/60)
13
+ # #
14
+ # # @!attribute hostname
15
+ # # @return [String] Server hostname used for SNI (see https://ruby-doc.org/stdlib-2.5.1/libdoc/openssl/rdoc/OpenSSL/SSL/SSLSocket.html#method-i-hostname-3D)
16
+ # #
17
+ # # @!attribute ca_file
18
+ # # @return [String] CA file
19
+ # #
20
+ # # @!attribute ca_path
21
+ # # @return [String] CA path
22
+ # #
23
+ # # @!attribute verify_mode
24
+ # # @return [Integer] Any `OpenSSL::SSL::` constant (see https://ruby-doc.org/stdlib-2.5.1/libdoc/openssl/rdoc/OpenSSL/SSL.html)
25
+ # #
26
+ # # @!attribute cert_store
27
+ # # @return [OpenSSL::X509::Store] certificate store
28
+ # #
29
+ # # @!attribute client_cert
30
+ # # @return [String, OpenSSL::X509::Certificate] client certificate
31
+ # #
32
+ # # @!attribute client_key
33
+ # # @return [String, OpenSSL::PKey::RSA, OpenSSL::PKey::DSA] client key
34
+ # #
35
+ # # @!attribute certificate
36
+ # # @return [OpenSSL::X509::Certificate] certificate (Excon only)
37
+ # #
38
+ # # @!attribute private_key
39
+ # # @return [OpenSSL::PKey::RSA, OpenSSL::PKey::DSA] private key (Excon only)
40
+ # #
41
+ # # @!attribute verify_depth
42
+ # # @return [Integer] maximum depth for the certificate chain verification
43
+ # #
44
+ # # @!attribute version
45
+ # # @return [String, Symbol] SSL version (see https://ruby-doc.org/stdlib-2.5.1/libdoc/openssl/rdoc/OpenSSL/SSL/SSLContext.html#method-i-ssl_version-3D)
46
+ # #
47
+ # # @!attribute min_version
48
+ # # @return [String, Symbol] minimum SSL version (see https://ruby-doc.org/stdlib-2.5.1/libdoc/openssl/rdoc/OpenSSL/SSL/SSLContext.html#method-i-min_version-3D)
49
+ # #
50
+ # # @!attribute max_version
51
+ # # @return [String, Symbol] maximum SSL version (see https://ruby-doc.org/stdlib-2.5.1/libdoc/openssl/rdoc/OpenSSL/SSL/SSLContext.html#method-i-max_version-3D)
52
+ # #
53
+ # # @!attribute ciphers
54
+ # # @return [String] cipher list in OpenSSL format (see https://ruby-doc.org/stdlib-2.5.1/libdoc/openssl/rdoc/OpenSSL/SSL/SSLContext.html#method-i-ciphers-3D)
55
+ # class SSLOptions < Options; end
56
+ SSLOptions = Options.new(:verify, :verify_hostname, :hostname,
57
+ :ca_file, :ca_path, :verify_mode,
58
+ :cert_store, :client_cert, :client_key,
59
+ :certificate, :private_key, :verify_depth,
60
+ :version, :min_version, :max_version, :ciphers) do
49
61
  # @return [Boolean] true if should verify
50
62
  def verify?
51
63
  verify != false
@@ -55,5 +67,10 @@ module Faraday
55
67
  def disable?
56
68
  !verify?
57
69
  end
70
+
71
+ # @return [Boolean] true if should verify_hostname
72
+ def verify_hostname?
73
+ verify_hostname != false
74
+ end
58
75
  end
59
76
  end
@@ -30,7 +30,7 @@ module Faraday
30
30
  new_value = value
31
31
  end
32
32
 
33
- send("#{key}=", new_value) unless new_value.nil?
33
+ send(:"#{key}=", new_value) unless new_value.nil?
34
34
  end
35
35
  self
36
36
  end
@@ -38,7 +38,7 @@ module Faraday
38
38
  # Public
39
39
  def delete(key)
40
40
  value = send(key)
41
- send("#{key}=", nil)
41
+ send(:"#{key}=", nil)
42
42
  value
43
43
  end
44
44
 
@@ -57,7 +57,7 @@ module Faraday
57
57
  else
58
58
  other_value
59
59
  end
60
- send("#{key}=", new_value) unless new_value.nil?
60
+ send(:"#{key}=", new_value) unless new_value.nil?
61
61
  end
62
62
  self
63
63
  end
@@ -104,7 +104,7 @@ module Faraday
104
104
 
105
105
  # Public
106
106
  def each_key(&block)
107
- return to_enum(:each_key) unless block_given?
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 block_given?
121
+ return to_enum(:each_value) unless block
122
122
 
123
123
  values.each(&block)
124
124
  end
@@ -168,12 +168,13 @@ module Faraday
168
168
  end
169
169
 
170
170
  def self.memoized(key, &block)
171
- unless block_given?
171
+ unless block
172
172
  raise ArgumentError, '#memoized must be called with a block'
173
173
  end
174
174
 
175
175
  memoized_attributes[key.to_sym] = block
176
176
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
177
+ remove_method(key) if method_defined?(key, false)
177
178
  def #{key}() self[:#{key}]; end
178
179
  RUBY
179
180
  end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'ruby2_keywords'
4
3
  require 'faraday/adapter_registry'
5
4
 
6
5
  module Faraday
@@ -8,7 +7,7 @@ module Faraday
8
7
  # middleware stack (heavily inspired by Rack).
9
8
  #
10
9
  # @example
11
- # Faraday::Connection.new(url: 'http://sushi.com') do |builder|
10
+ # Faraday::Connection.new(url: 'http://httpbingo.org') do |builder|
12
11
  # builder.request :url_encoded # Faraday::Request::UrlEncoded
13
12
  # builder.adapter :net_http # Faraday::Adapter::NetHttp
14
13
  # end
@@ -28,10 +27,11 @@ module Faraday
28
27
 
29
28
  attr_reader :name
30
29
 
31
- ruby2_keywords def initialize(klass, *args, &block)
30
+ def initialize(klass, *args, **kwargs, &block)
32
31
  @name = klass.to_s
33
32
  REGISTRY.set(klass) if klass.respond_to?(:name)
34
33
  @args = args
34
+ @kwargs = kwargs
35
35
  @block = block
36
36
  end
37
37
 
@@ -54,27 +54,26 @@ module Faraday
54
54
  end
55
55
 
56
56
  def build(app = nil)
57
- klass.new(app, *@args, &@block)
57
+ klass.new(app, *@args, **@kwargs, &@block)
58
58
  end
59
59
  end
60
60
 
61
- def initialize(handlers = [], adapter = nil, &block)
62
- @adapter = adapter
63
- @handlers = handlers
64
- if block_given?
65
- build(&block)
66
- elsif @handlers.empty?
67
- # default stack, if nothing else is configured
68
- request :url_encoded
69
- self.adapter Faraday.default_adapter
70
- end
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(options = {})
73
+ def build
74
74
  raise_if_locked
75
- @handlers.clear unless options[:keep]
76
- yield(self) if block_given?
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)
@@ -90,52 +89,52 @@ module Faraday
90
89
  @handlers.frozen?
91
90
  end
92
91
 
93
- ruby2_keywords def use(klass, *args, &block)
92
+ def use(klass, ...)
94
93
  if klass.is_a? Symbol
95
- use_symbol(Faraday::Middleware, klass, *args, &block)
94
+ use_symbol(Faraday::Middleware, klass, ...)
96
95
  else
97
96
  raise_if_locked
98
97
  raise_if_adapter(klass)
99
- @handlers << self.class::Handler.new(klass, *args, &block)
98
+ @handlers << self.class::Handler.new(klass, ...)
100
99
  end
101
100
  end
102
101
 
103
- ruby2_keywords def request(key, *args, &block)
104
- use_symbol(Faraday::Request, key, *args, &block)
102
+ def request(key, ...)
103
+ use_symbol(Faraday::Request, key, ...)
105
104
  end
106
105
 
107
- ruby2_keywords def response(key, *args, &block)
108
- use_symbol(Faraday::Response, key, *args, &block)
106
+ def response(...)
107
+ use_symbol(Faraday::Response, ...)
109
108
  end
110
109
 
111
- ruby2_keywords def adapter(klass = NO_ARGUMENT, *args, &block)
112
- return @adapter if klass == NO_ARGUMENT
110
+ def adapter(klass = NO_ARGUMENT, *args, **kwargs, &block)
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
- @adapter = self.class::Handler.new(klass, *args, &block)
114
+ @adapter = self.class::Handler.new(klass, *args, **kwargs, &block)
116
115
  end
117
116
 
118
117
  ## methods to push onto the various positions in the stack:
119
118
 
120
- ruby2_keywords def insert(index, *args, &block)
119
+ def insert(index, ...)
121
120
  raise_if_locked
122
121
  index = assert_index(index)
123
- handler = self.class::Handler.new(*args, &block)
122
+ handler = self.class::Handler.new(...)
124
123
  @handlers.insert(index, handler)
125
124
  end
126
125
 
127
126
  alias insert_before insert
128
127
 
129
- ruby2_keywords def insert_after(index, *args, &block)
128
+ def insert_after(index, ...)
130
129
  index = assert_index(index)
131
- insert(index + 1, *args, &block)
130
+ insert(index + 1, ...)
132
131
  end
133
132
 
134
- ruby2_keywords def swap(index, *args, &block)
133
+ def swap(index, ...)
135
134
  raise_if_locked
136
135
  index = assert_index(index)
137
136
  @handlers.delete_at(index)
138
- insert(index, *args, &block)
137
+ insert(index, ...)
139
138
  end
140
139
 
141
140
  def delete(handler)
@@ -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,27 +212,30 @@ 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?
222
221
  end
223
222
 
224
223
  def raise_if_adapter(klass)
225
- return unless is_adapter?(klass)
224
+ return unless klass <= Faraday::Adapter
226
225
 
227
226
  raise 'Adapter should be set using the `adapter` method, not `use`'
228
227
  end
229
228
 
230
- def adapter_set?
231
- !@adapter.nil?
229
+ def ensure_adapter!
230
+ raise MISSING_ADAPTER_ERROR unless @adapter
232
231
  end
233
232
 
234
- def is_adapter?(klass) # rubocop:disable Naming/PredicateName
235
- klass <= Faraday::Adapter
233
+ def adapter_set?
234
+ !@adapter.nil?
236
235
  end
237
236
 
238
- ruby2_keywords def use_symbol(mod, key, *args, &block)
239
- use(mod.lookup_middleware(key), *args, &block)
237
+ def use_symbol(mod, key, ...)
238
+ use(mod.lookup_middleware(key), ...)
240
239
  end
241
240
 
242
241
  def assert_index(index)