faraday 0.15.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +570 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +25 -351
  5. data/Rakefile +7 -0
  6. data/examples/client_spec.rb +97 -0
  7. data/examples/client_test.rb +118 -0
  8. data/lib/faraday/adapter/test.rb +127 -72
  9. data/lib/faraday/adapter.rb +69 -22
  10. data/lib/faraday/adapter_registry.rb +30 -0
  11. data/lib/faraday/connection.rb +309 -232
  12. data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
  13. data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
  14. data/lib/faraday/error.rb +119 -38
  15. data/lib/faraday/logging/formatter.rb +106 -0
  16. data/lib/faraday/methods.rb +6 -0
  17. data/lib/faraday/middleware.rb +18 -25
  18. data/lib/faraday/middleware_registry.rb +83 -0
  19. data/lib/faraday/options/connection_options.rb +22 -0
  20. data/lib/faraday/options/env.rb +181 -0
  21. data/lib/faraday/options/proxy_options.rb +32 -0
  22. data/lib/faraday/options/request_options.rb +22 -0
  23. data/lib/faraday/options/ssl_options.rb +59 -0
  24. data/lib/faraday/options.rb +41 -195
  25. data/lib/faraday/parameters.rb +4 -196
  26. data/lib/faraday/rack_builder.rb +91 -76
  27. data/lib/faraday/request/authorization.rb +37 -29
  28. data/lib/faraday/request/instrumentation.rb +47 -27
  29. data/lib/faraday/request/json.rb +55 -0
  30. data/lib/faraday/request/url_encoded.rb +45 -23
  31. data/lib/faraday/request.rb +74 -32
  32. data/lib/faraday/response/json.rb +54 -0
  33. data/lib/faraday/response/logger.rb +22 -69
  34. data/lib/faraday/response/raise_error.rb +57 -14
  35. data/lib/faraday/response.rb +26 -33
  36. data/lib/faraday/utils/headers.rb +139 -0
  37. data/lib/faraday/utils/params_hash.rb +61 -0
  38. data/lib/faraday/utils.rb +47 -251
  39. data/lib/faraday/version.rb +5 -0
  40. data/lib/faraday.rb +108 -199
  41. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  42. data/spec/faraday/adapter/test_spec.rb +377 -0
  43. data/spec/faraday/adapter_registry_spec.rb +28 -0
  44. data/spec/faraday/adapter_spec.rb +55 -0
  45. data/spec/faraday/connection_spec.rb +787 -0
  46. data/spec/faraday/error_spec.rb +60 -0
  47. data/spec/faraday/middleware_registry_spec.rb +31 -0
  48. data/spec/faraday/middleware_spec.rb +52 -0
  49. data/spec/faraday/options/env_spec.rb +70 -0
  50. data/spec/faraday/options/options_spec.rb +297 -0
  51. data/spec/faraday/options/proxy_options_spec.rb +44 -0
  52. data/spec/faraday/options/request_options_spec.rb +19 -0
  53. data/spec/faraday/params_encoders/flat_spec.rb +42 -0
  54. data/spec/faraday/params_encoders/nested_spec.rb +142 -0
  55. data/spec/faraday/rack_builder_spec.rb +317 -0
  56. data/spec/faraday/request/authorization_spec.rb +83 -0
  57. data/spec/faraday/request/instrumentation_spec.rb +74 -0
  58. data/spec/faraday/request/json_spec.rb +111 -0
  59. data/spec/faraday/request/url_encoded_spec.rb +82 -0
  60. data/spec/faraday/request_spec.rb +109 -0
  61. data/spec/faraday/response/json_spec.rb +117 -0
  62. data/spec/faraday/response/logger_spec.rb +220 -0
  63. data/spec/faraday/response/raise_error_spec.rb +172 -0
  64. data/spec/faraday/response_spec.rb +75 -0
  65. data/spec/faraday/utils/headers_spec.rb +82 -0
  66. data/spec/faraday/utils_spec.rb +117 -0
  67. data/spec/faraday_spec.rb +37 -0
  68. data/spec/spec_helper.rb +132 -0
  69. data/spec/support/disabling_stub.rb +14 -0
  70. data/spec/support/fake_safe_buffer.rb +15 -0
  71. data/spec/support/helper_methods.rb +96 -0
  72. data/spec/support/shared_examples/adapter.rb +104 -0
  73. data/spec/support/shared_examples/params_encoder.rb +18 -0
  74. data/spec/support/shared_examples/request_method.rb +249 -0
  75. data/spec/support/streaming_response_checker.rb +35 -0
  76. metadata +86 -34
  77. data/lib/faraday/adapter/em_http.rb +0 -243
  78. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
  79. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
  80. data/lib/faraday/adapter/em_synchrony.rb +0 -106
  81. data/lib/faraday/adapter/excon.rb +0 -79
  82. data/lib/faraday/adapter/httpclient.rb +0 -128
  83. data/lib/faraday/adapter/net_http.rb +0 -137
  84. data/lib/faraday/adapter/net_http_persistent.rb +0 -63
  85. data/lib/faraday/adapter/patron.rb +0 -100
  86. data/lib/faraday/adapter/rack.rb +0 -58
  87. data/lib/faraday/adapter/typhoeus.rb +0 -12
  88. data/lib/faraday/autoload.rb +0 -84
  89. data/lib/faraday/request/basic_authentication.rb +0 -13
  90. data/lib/faraday/request/multipart.rb +0 -68
  91. data/lib/faraday/request/retry.rb +0 -211
  92. data/lib/faraday/request/token_authentication.rb +0 -15
  93. data/lib/faraday/upload_io.rb +0 -67
@@ -1,12 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruby2_keywords'
4
+ require 'faraday/adapter_registry'
5
+
1
6
  module Faraday
2
7
  # A Builder that processes requests into responses by passing through an inner
3
8
  # middleware stack (heavily inspired by Rack).
4
9
  #
5
- # Faraday::Connection.new(:url => 'http://sushi.com') do |builder|
10
+ # @example
11
+ # Faraday::Connection.new(url: 'http://httpbingo.org') do |builder|
6
12
  # builder.request :url_encoded # Faraday::Request::UrlEncoded
7
13
  # builder.adapter :net_http # Faraday::Adapter::NetHttp
8
14
  # end
9
15
  class RackBuilder
16
+ # Used to detect missing arguments
17
+ NO_ARGUMENT = Object.new
18
+
10
19
  attr_accessor :handlers
11
20
 
12
21
  # Error raised when trying to modify the stack after calling `lock!`
@@ -15,28 +24,28 @@ module Faraday
15
24
  # borrowed from ActiveSupport::Dependencies::Reference &
16
25
  # ActionDispatch::MiddlewareStack::Middleware
17
26
  class Handler
18
- @@constants_mutex = Mutex.new
19
- @@constants = Hash.new { |h, k|
20
- value = k.respond_to?(:constantize) ? k.constantize : Object.const_get(k)
21
- @@constants_mutex.synchronize { h[k] = value }
22
- }
27
+ REGISTRY = Faraday::AdapterRegistry.new
23
28
 
24
29
  attr_reader :name
25
30
 
26
- def initialize(klass, *args, &block)
31
+ ruby2_keywords def initialize(klass, *args, &block)
27
32
  @name = klass.to_s
28
- if klass.respond_to?(:name)
29
- @@constants_mutex.synchronize { @@constants[@name] = klass }
30
- end
31
- @args, @block = args, block
33
+ REGISTRY.set(klass) if klass.respond_to?(:name)
34
+ @args = args
35
+ @block = block
32
36
  end
33
37
 
34
- def klass() @@constants[@name] end
35
- def inspect() @name end
38
+ def klass
39
+ REGISTRY.get(@name)
40
+ end
41
+
42
+ def inspect
43
+ @name
44
+ end
36
45
 
37
46
  def ==(other)
38
47
  if other.is_a? Handler
39
- self.name == other.name
48
+ name == other.name
40
49
  elsif other.respond_to? :name
41
50
  klass == other
42
51
  else
@@ -44,33 +53,34 @@ module Faraday
44
53
  end
45
54
  end
46
55
 
47
- def build(app)
56
+ def build(app = nil)
48
57
  klass.new(app, *@args, &@block)
49
58
  end
50
59
  end
51
60
 
52
- def initialize(handlers = [])
53
- @handlers = handlers
54
- if block_given?
55
- build(&Proc.new)
56
- elsif @handlers.empty?
57
- # default stack, if nothing else is configured
58
- self.request :url_encoded
59
- self.adapter Faraday.default_adapter
60
- end
61
+ def initialize(&block)
62
+ @adapter = nil
63
+ @handlers = []
64
+ build(&block)
61
65
  end
62
66
 
63
- def build(options = {})
67
+ def initialize_dup(original)
68
+ super
69
+ @adapter = original.adapter
70
+ @handlers = original.handlers.dup
71
+ end
72
+
73
+ def build
64
74
  raise_if_locked
65
- @handlers.clear unless options[:keep]
66
- yield(self) if block_given?
75
+ block_given? ? yield(self) : request(:url_encoded)
76
+ adapter(Faraday.default_adapter, **Faraday.default_adapter_options) unless @adapter
67
77
  end
68
78
 
69
79
  def [](idx)
70
80
  @handlers[idx]
71
81
  end
72
82
 
73
- # Locks the middleware stack to ensure no further modifications are possible.
83
+ # Locks the middleware stack to ensure no further modifications are made.
74
84
  def lock!
75
85
  @handlers.freeze
76
86
  end
@@ -79,46 +89,48 @@ module Faraday
79
89
  @handlers.frozen?
80
90
  end
81
91
 
82
- def use(klass, *args, &block)
92
+ ruby2_keywords def use(klass, *args, &block)
83
93
  if klass.is_a? Symbol
84
94
  use_symbol(Faraday::Middleware, klass, *args, &block)
85
95
  else
86
96
  raise_if_locked
87
- warn_middleware_after_adapter if adapter_set?
97
+ raise_if_adapter(klass)
88
98
  @handlers << self.class::Handler.new(klass, *args, &block)
89
99
  end
90
100
  end
91
101
 
92
- def request(key, *args, &block)
102
+ ruby2_keywords def request(key, *args, &block)
93
103
  use_symbol(Faraday::Request, key, *args, &block)
94
104
  end
95
105
 
96
- def response(key, *args, &block)
106
+ ruby2_keywords def response(key, *args, &block)
97
107
  use_symbol(Faraday::Response, key, *args, &block)
98
108
  end
99
109
 
100
- def adapter(key, *args, &block)
101
- use_symbol(Faraday::Adapter, key, *args, &block)
110
+ ruby2_keywords def adapter(klass = NO_ARGUMENT, *args, &block)
111
+ return @adapter if klass == NO_ARGUMENT || klass.nil?
112
+
113
+ klass = Faraday::Adapter.lookup_middleware(klass) if klass.is_a?(Symbol)
114
+ @adapter = self.class::Handler.new(klass, *args, &block)
102
115
  end
103
116
 
104
117
  ## methods to push onto the various positions in the stack:
105
118
 
106
- def insert(index, *args, &block)
119
+ ruby2_keywords def insert(index, *args, &block)
107
120
  raise_if_locked
108
121
  index = assert_index(index)
109
- warn_middleware_after_adapter if inserting_after_adapter?(index)
110
122
  handler = self.class::Handler.new(*args, &block)
111
123
  @handlers.insert(index, handler)
112
124
  end
113
125
 
114
- alias_method :insert_before, :insert
126
+ alias insert_before insert
115
127
 
116
- def insert_after(index, *args, &block)
128
+ ruby2_keywords def insert_after(index, *args, &block)
117
129
  index = assert_index(index)
118
130
  insert(index + 1, *args, &block)
119
131
  end
120
132
 
121
- def swap(index, *args, &block)
133
+ ruby2_keywords def swap(index, *args, &block)
122
134
  raise_if_locked
123
135
  index = assert_index(index)
124
136
  @handlers.delete_at(index)
@@ -133,13 +145,11 @@ module Faraday
133
145
  # Processes a Request into a Response by passing it through this Builder's
134
146
  # middleware stack.
135
147
  #
136
- # connection - Faraday::Connection
137
- # request - Faraday::Request
148
+ # @param connection [Faraday::Connection]
149
+ # @param request [Faraday::Request]
138
150
  #
139
- # Returns a Faraday::Response.
151
+ # @return [Faraday::Response]
140
152
  def build_response(connection, request)
141
- warn 'WARNING: No adapter was configured for this request' unless adapter_set?
142
-
143
153
  app.call(build_env(connection, request))
144
154
  end
145
155
 
@@ -153,30 +163,27 @@ module Faraday
153
163
  def app
154
164
  @app ||= begin
155
165
  lock!
156
- to_app(lambda { |env|
157
- response = Response.new
158
- env.response = response
159
- response.finish(env) unless env.parallel?
160
- response
161
- })
166
+ ensure_adapter!
167
+ to_app
162
168
  end
163
169
  end
164
170
 
165
- def to_app(inner_app)
171
+ def to_app
166
172
  # last added handler is the deepest and thus closest to the inner app
167
- @handlers.reverse.inject(inner_app) { |app, handler| handler.build(app) }
173
+ # adapter is always the last one
174
+ @handlers.reverse.inject(@adapter.build) do |app, handler|
175
+ handler.build(app)
176
+ end
168
177
  end
169
178
 
170
179
  def ==(other)
171
- other.is_a?(self.class) && @handlers == other.handlers
172
- end
173
-
174
- def dup
175
- self.class.new(@handlers.dup)
180
+ other.is_a?(self.class) &&
181
+ @handlers == other.handlers &&
182
+ @adapter == other.adapter
176
183
  end
177
184
 
178
185
  # ENV Keys
179
- # :method - a symbolized request method (:get, :post)
186
+ # :http_method - a symbolized request HTTP method (:get, :post)
180
187
  # :body - the request body that will eventually be converted to a string.
181
188
  # :url - URI instance for the current request.
182
189
  # :status - HTTP response status code
@@ -192,45 +199,53 @@ module Faraday
192
199
  # :password - Proxy server password
193
200
  # :ssl - Hash of options for configuring SSL requests.
194
201
  def build_env(connection, request)
195
- Env.new(request.method, request.body,
196
- connection.build_exclusive_url(request.path, request.params, request.options.params_encoder),
197
- request.options, request.headers, connection.ssl,
198
- connection.parallel_manager)
202
+ exclusive_url = connection.build_exclusive_url(
203
+ request.path, request.params,
204
+ request.options.params_encoder
205
+ )
206
+
207
+ Env.new(request.http_method, request.body, exclusive_url,
208
+ request.options, request.headers, connection.ssl,
209
+ connection.parallel_manager)
199
210
  end
200
211
 
201
212
  private
202
213
 
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/.'
218
+
203
219
  def raise_if_locked
204
- raise StackLocked, "can't modify middleware stack after making a request" if locked?
220
+ raise StackLocked, LOCK_ERR if locked?
205
221
  end
206
222
 
207
- def warn_middleware_after_adapter
208
- warn "WARNING: Unexpected middleware set after the adapter. " \
209
- "This won't be supported from Faraday 1.0."
210
- end
223
+ def raise_if_adapter(klass)
224
+ return unless is_adapter?(klass)
211
225
 
212
- def adapter_set?
213
- @handlers.any? { |handler| is_adapter?(handler) }
226
+ raise 'Adapter should be set using the `adapter` method, not `use`'
214
227
  end
215
228
 
216
- def inserting_after_adapter?(index)
217
- adapter_index = @handlers.find_index { |handler| is_adapter?(handler) }
218
- return false if adapter_index.nil?
229
+ def ensure_adapter!
230
+ raise MISSING_ADAPTER_ERROR unless @adapter
231
+ end
219
232
 
220
- index > adapter_index
233
+ def adapter_set?
234
+ !@adapter.nil?
221
235
  end
222
236
 
223
- def is_adapter?(handler)
224
- handler.klass.ancestors.include? Faraday::Adapter
237
+ def is_adapter?(klass) # rubocop:disable Naming/PredicateName
238
+ klass <= Faraday::Adapter
225
239
  end
226
240
 
227
- def use_symbol(mod, key, *args, &block)
241
+ ruby2_keywords def use_symbol(mod, key, *args, &block)
228
242
  use(mod.lookup_middleware(key), *args, &block)
229
243
  end
230
244
 
231
245
  def assert_index(index)
232
246
  idx = index.is_a?(Integer) ? index : @handlers.index(index)
233
247
  raise "No such handler: #{index.inspect}" unless idx
248
+
234
249
  idx
235
250
  end
236
251
  end
@@ -1,41 +1,49 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Faraday
2
- class Request::Authorization < Faraday::Middleware
3
- KEY = "Authorization".freeze unless defined? KEY
4
+ class Request
5
+ # Request middleware for the Authorization HTTP header
6
+ class Authorization < Faraday::Middleware
7
+ KEY = 'Authorization'
4
8
 
5
- # Public
6
- def self.header(type, token)
7
- case token
8
- when String, Symbol
9
- "#{type} #{token}"
10
- when Hash
11
- build_hash(type.to_s, token)
12
- else
13
- raise ArgumentError, "Can't build an Authorization #{type} header from #{token.inspect}"
9
+ # @param app [#call]
10
+ # @param type [String, Symbol] Type of Authorization
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
19
+ super(app)
14
20
  end
15
- end
16
21
 
17
- # Internal
18
- def self.build_hash(type, hash)
19
- comma = ", "
20
- values = []
21
- hash.each do |key, value|
22
- values << "#{key}=#{value.to_s.inspect}"
22
+ # @param env [Faraday::Env]
23
+ def on_request(env)
24
+ return if env.request_headers[KEY]
25
+
26
+ env.request_headers[KEY] = header_from(@type, *@params)
23
27
  end
24
- "#{type} #{values * comma}"
25
- end
26
28
 
27
- def initialize(app, type, token)
28
- @header_value = self.class.header(type, token)
29
- super(app)
30
- end
29
+ private
31
30
 
32
- # Public
33
- def call(env)
34
- unless env.request_headers[KEY]
35
- env.request_headers[KEY] = @header_value
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
36
44
  end
37
- @app.call(env)
38
45
  end
39
46
  end
40
47
  end
41
48
 
49
+ Faraday::Request.register_middleware(authorization: Faraday::Request::Authorization)
@@ -1,36 +1,56 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Faraday
2
- class Request::Instrumentation < Faraday::Middleware
3
- class Options < Faraday::Options.new(:name, :instrumenter)
4
- def name
5
- self[:name] ||= 'request.faraday'
6
- end
4
+ class Request
5
+ # Middleware for instrumenting Requests.
6
+ class Instrumentation < Faraday::Middleware
7
+ # Options class used in Request::Instrumentation class.
8
+ class Options < Faraday::Options.new(:name, :instrumenter)
9
+ # @return [String]
10
+ def name
11
+ self[:name] ||= 'request.faraday'
12
+ end
7
13
 
8
- def instrumenter
9
- self[:instrumenter] ||= ActiveSupport::Notifications
14
+ # @return [Class]
15
+ def instrumenter
16
+ self[:instrumenter] ||= ActiveSupport::Notifications
17
+ end
10
18
  end
11
- end
12
19
 
13
- # Public: Instruments requests using Active Support.
14
- #
15
- # Measures time spent only for synchronous requests.
16
- #
17
- # Examples
18
- #
19
- # ActiveSupport::Notifications.subscribe('request.faraday') do |name, starts, ends, _, env|
20
- # url = env[:url]
21
- # http_method = env[:method].to_s.upcase
22
- # duration = ends - starts
23
- # $stderr.puts '[%s] %s %s (%.3f s)' % [url.host, http_method, url.request_uri, duration]
24
- # end
25
- def initialize(app, options = nil)
26
- super(app)
27
- @name, @instrumenter = Options.from(options).values_at(:name, :instrumenter)
28
- end
20
+ # Instruments requests using Active Support.
21
+ #
22
+ # Measures time spent only for synchronous requests.
23
+ #
24
+ # @example Using ActiveSupport::Notifications to measure time spent
25
+ # for Faraday requests.
26
+ # ActiveSupport::Notifications
27
+ # .subscribe('request.faraday') do |name, starts, ends, _, env|
28
+ # url = env[:url]
29
+ # http_method = env[:method].to_s.upcase
30
+ # duration = ends - starts
31
+ # $stderr.puts '[%s] %s %s (%.3f s)' %
32
+ # [url.host, http_method, url.request_uri, duration]
33
+ # end
34
+ # @param app [#call]
35
+ # @param options [nil, Hash] Options hash
36
+ # @option options [String] :name ('request.faraday')
37
+ # Name of the instrumenter
38
+ # @option options [Class] :instrumenter (ActiveSupport::Notifications)
39
+ # Active Support instrumenter class.
40
+ def initialize(app, options = nil)
41
+ super(app)
42
+ @name, @instrumenter = Options.from(options)
43
+ .values_at(:name, :instrumenter)
44
+ end
29
45
 
30
- def call(env)
31
- @instrumenter.instrument(@name, env) do
32
- @app.call(env)
46
+ # @param env [Faraday::Env]
47
+ def call(env)
48
+ @instrumenter.instrument(@name, env) do
49
+ @app.call(env)
50
+ end
33
51
  end
34
52
  end
35
53
  end
36
54
  end
55
+
56
+ Faraday::Request.register_middleware(instrumentation: Faraday::Request::Instrumentation)
@@ -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)
@@ -1,36 +1,58 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Faraday
2
- class Request::UrlEncoded < Faraday::Middleware
3
- CONTENT_TYPE = 'Content-Type'.freeze unless defined? CONTENT_TYPE
4
+ class Request
5
+ # Middleware for supporting urlencoded requests.
6
+ class UrlEncoded < Faraday::Middleware
7
+ unless defined?(::Faraday::Request::UrlEncoded::CONTENT_TYPE)
8
+ CONTENT_TYPE = 'Content-Type'
9
+ end
4
10
 
5
- class << self
6
- attr_accessor :mime_type
7
- end
8
- self.mime_type = 'application/x-www-form-urlencoded'.freeze
11
+ class << self
12
+ attr_accessor :mime_type
13
+ end
14
+ self.mime_type = 'application/x-www-form-urlencoded'
9
15
 
10
- def call(env)
11
- match_content_type(env) do |data|
12
- params = Faraday::Utils::ParamsHash[data]
13
- env.body = params.to_query(env.params_encoder)
16
+ # Encodes as "application/x-www-form-urlencoded" if not already encoded or
17
+ # of another type.
18
+ #
19
+ # @param env [Faraday::Env]
20
+ def call(env)
21
+ match_content_type(env) do |data|
22
+ params = Faraday::Utils::ParamsHash[data]
23
+ env.body = params.to_query(env.params_encoder)
24
+ end
25
+ @app.call env
14
26
  end
15
- @app.call env
16
- end
17
27
 
18
- def match_content_type(env)
19
- if process_request?(env)
28
+ # @param env [Faraday::Env]
29
+ # @yield [request_body] Body of the request
30
+ def match_content_type(env)
31
+ return unless process_request?(env)
32
+
20
33
  env.request_headers[CONTENT_TYPE] ||= self.class.mime_type
21
34
  yield(env.body) unless env.body.respond_to?(:to_str)
22
35
  end
23
- end
24
36
 
25
- def process_request?(env)
26
- type = request_type(env)
27
- env.body and (type.empty? or type == self.class.mime_type)
28
- end
37
+ # @param env [Faraday::Env]
38
+ #
39
+ # @return [Boolean] True if the request has a body and its Content-Type is
40
+ # urlencoded.
41
+ def process_request?(env)
42
+ type = request_type(env)
43
+ env.body && (type.empty? || (type == self.class.mime_type))
44
+ end
29
45
 
30
- def request_type(env)
31
- type = env.request_headers[CONTENT_TYPE].to_s
32
- type = type.split(';', 2).first if type.index(';')
33
- type
46
+ # @param env [Faraday::Env]
47
+ #
48
+ # @return [String]
49
+ def request_type(env)
50
+ type = env.request_headers[CONTENT_TYPE].to_s
51
+ type = type.split(';', 2).first if type.index(';')
52
+ type
53
+ end
34
54
  end
35
55
  end
36
56
  end
57
+
58
+ Faraday::Request.register_middleware(url_encoded: Faraday::Request::UrlEncoded)