faraday 0.12.2 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +350 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +22 -325
  5. data/Rakefile +7 -0
  6. data/examples/client_spec.rb +65 -0
  7. data/examples/client_test.rb +79 -0
  8. data/lib/faraday.rb +120 -188
  9. data/lib/faraday/adapter.rb +84 -22
  10. data/lib/faraday/adapter/em_http.rb +150 -104
  11. data/lib/faraday/adapter/em_http_ssl_patch.rb +24 -18
  12. data/lib/faraday/adapter/em_synchrony.rb +110 -63
  13. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
  14. data/lib/faraday/adapter/excon.rb +98 -54
  15. data/lib/faraday/adapter/httpclient.rb +83 -59
  16. data/lib/faraday/adapter/net_http_persistent.rb +68 -27
  17. data/lib/faraday/adapter/patron.rb +86 -37
  18. data/lib/faraday/adapter/rack.rb +30 -13
  19. data/lib/faraday/adapter/test.rb +103 -62
  20. data/lib/faraday/adapter/typhoeus.rb +7 -115
  21. data/lib/faraday/adapter_registry.rb +30 -0
  22. data/lib/faraday/autoload.rb +46 -36
  23. data/lib/faraday/connection.rb +336 -177
  24. data/lib/faraday/dependency_loader.rb +37 -0
  25. data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
  26. data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
  27. data/lib/faraday/error.rb +126 -37
  28. data/lib/faraday/file_part.rb +128 -0
  29. data/lib/faraday/logging/formatter.rb +105 -0
  30. data/lib/faraday/methods.rb +6 -0
  31. data/lib/faraday/middleware.rb +19 -25
  32. data/lib/faraday/middleware_registry.rb +129 -0
  33. data/lib/faraday/options.rb +39 -193
  34. data/lib/faraday/options/connection_options.rb +22 -0
  35. data/lib/faraday/options/env.rb +181 -0
  36. data/lib/faraday/options/proxy_options.rb +28 -0
  37. data/lib/faraday/options/request_options.rb +22 -0
  38. data/lib/faraday/options/ssl_options.rb +59 -0
  39. data/lib/faraday/param_part.rb +53 -0
  40. data/lib/faraday/parameters.rb +4 -196
  41. data/lib/faraday/rack_builder.rb +77 -63
  42. data/lib/faraday/request.rb +94 -32
  43. data/lib/faraday/request/authorization.rb +44 -30
  44. data/lib/faraday/request/basic_authentication.rb +14 -7
  45. data/lib/faraday/request/instrumentation.rb +45 -27
  46. data/lib/faraday/request/multipart.rb +86 -48
  47. data/lib/faraday/request/retry.rb +209 -134
  48. data/lib/faraday/request/token_authentication.rb +15 -10
  49. data/lib/faraday/request/url_encoded.rb +43 -23
  50. data/lib/faraday/response.rb +27 -23
  51. data/lib/faraday/response/logger.rb +22 -69
  52. data/lib/faraday/response/raise_error.rb +49 -14
  53. data/lib/faraday/utils.rb +38 -247
  54. data/lib/faraday/utils/headers.rb +139 -0
  55. data/lib/faraday/utils/params_hash.rb +61 -0
  56. data/lib/faraday/version.rb +5 -0
  57. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  58. data/spec/faraday/adapter/em_http_spec.rb +47 -0
  59. data/spec/faraday/adapter/em_synchrony_spec.rb +16 -0
  60. data/spec/faraday/adapter/excon_spec.rb +49 -0
  61. data/spec/faraday/adapter/httpclient_spec.rb +73 -0
  62. data/spec/faraday/adapter/net_http_persistent_spec.rb +57 -0
  63. data/spec/faraday/adapter/net_http_spec.rb +64 -0
  64. data/spec/faraday/adapter/patron_spec.rb +18 -0
  65. data/spec/faraday/adapter/rack_spec.rb +8 -0
  66. data/spec/faraday/adapter/test_spec.rb +260 -0
  67. data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
  68. data/spec/faraday/adapter_registry_spec.rb +28 -0
  69. data/spec/faraday/adapter_spec.rb +55 -0
  70. data/spec/faraday/composite_read_io_spec.rb +80 -0
  71. data/spec/faraday/connection_spec.rb +691 -0
  72. data/spec/faraday/error_spec.rb +60 -0
  73. data/spec/faraday/middleware_spec.rb +52 -0
  74. data/spec/faraday/options/env_spec.rb +70 -0
  75. data/spec/faraday/options/options_spec.rb +297 -0
  76. data/spec/faraday/options/proxy_options_spec.rb +37 -0
  77. data/spec/faraday/options/request_options_spec.rb +19 -0
  78. data/spec/faraday/params_encoders/flat_spec.rb +42 -0
  79. data/spec/faraday/params_encoders/nested_spec.rb +142 -0
  80. data/spec/faraday/rack_builder_spec.rb +345 -0
  81. data/spec/faraday/request/authorization_spec.rb +88 -0
  82. data/spec/faraday/request/instrumentation_spec.rb +76 -0
  83. data/spec/faraday/request/multipart_spec.rb +302 -0
  84. data/spec/faraday/request/retry_spec.rb +242 -0
  85. data/spec/faraday/request/url_encoded_spec.rb +83 -0
  86. data/spec/faraday/request_spec.rb +120 -0
  87. data/spec/faraday/response/logger_spec.rb +220 -0
  88. data/spec/faraday/response/middleware_spec.rb +68 -0
  89. data/spec/faraday/response/raise_error_spec.rb +169 -0
  90. data/spec/faraday/response_spec.rb +75 -0
  91. data/spec/faraday/utils/headers_spec.rb +82 -0
  92. data/spec/faraday/utils_spec.rb +56 -0
  93. data/spec/faraday_spec.rb +37 -0
  94. data/spec/spec_helper.rb +132 -0
  95. data/spec/support/disabling_stub.rb +14 -0
  96. data/spec/support/fake_safe_buffer.rb +15 -0
  97. data/spec/support/helper_methods.rb +133 -0
  98. data/spec/support/shared_examples/adapter.rb +105 -0
  99. data/spec/support/shared_examples/params_encoder.rb +18 -0
  100. data/spec/support/shared_examples/request_method.rb +262 -0
  101. data/spec/support/streaming_response_checker.rb +35 -0
  102. data/spec/support/webmock_rack_app.rb +68 -0
  103. metadata +109 -10
  104. data/lib/faraday/adapter/net_http.rb +0 -135
  105. 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://sushi.com') 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,18 +53,19 @@ 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 = [])
61
+ def initialize(handlers = [], adapter = nil, &block)
62
+ @adapter = adapter
53
63
  @handlers = handlers
54
64
  if block_given?
55
- build(&Proc.new)
65
+ build(&block)
56
66
  elsif @handlers.empty?
57
67
  # default stack, if nothing else is configured
58
- self.request :url_encoded
68
+ request :url_encoded
59
69
  self.adapter Faraday.default_adapter
60
70
  end
61
71
  end
@@ -64,13 +74,14 @@ module Faraday
64
74
  raise_if_locked
65
75
  @handlers.clear unless options[:keep]
66
76
  yield(self) if block_given?
77
+ adapter(Faraday.default_adapter) unless @adapter
67
78
  end
68
79
 
69
80
  def [](idx)
70
81
  @handlers[idx]
71
82
  end
72
83
 
73
- # Locks the middleware stack to ensure no further modifications are possible.
84
+ # Locks the middleware stack to ensure no further modifications are made.
74
85
  def lock!
75
86
  @handlers.freeze
76
87
  end
@@ -79,46 +90,48 @@ module Faraday
79
90
  @handlers.frozen?
80
91
  end
81
92
 
82
- def use(klass, *args, &block)
93
+ ruby2_keywords def use(klass, *args, &block)
83
94
  if klass.is_a? Symbol
84
95
  use_symbol(Faraday::Middleware, klass, *args, &block)
85
96
  else
86
97
  raise_if_locked
87
- warn_middleware_after_adapter if adapter_set?
98
+ raise_if_adapter(klass)
88
99
  @handlers << self.class::Handler.new(klass, *args, &block)
89
100
  end
90
101
  end
91
102
 
92
- def request(key, *args, &block)
103
+ ruby2_keywords def request(key, *args, &block)
93
104
  use_symbol(Faraday::Request, key, *args, &block)
94
105
  end
95
106
 
96
- def response(key, *args, &block)
107
+ ruby2_keywords def response(key, *args, &block)
97
108
  use_symbol(Faraday::Response, key, *args, &block)
98
109
  end
99
110
 
100
- def adapter(key, *args, &block)
101
- use_symbol(Faraday::Adapter, key, *args, &block)
111
+ ruby2_keywords def adapter(klass = NO_ARGUMENT, *args, &block)
112
+ return @adapter if klass == NO_ARGUMENT
113
+
114
+ klass = Faraday::Adapter.lookup_middleware(klass) if klass.is_a?(Symbol)
115
+ @adapter = self.class::Handler.new(klass, *args, &block)
102
116
  end
103
117
 
104
118
  ## methods to push onto the various positions in the stack:
105
119
 
106
- def insert(index, *args, &block)
120
+ ruby2_keywords def insert(index, *args, &block)
107
121
  raise_if_locked
108
122
  index = assert_index(index)
109
- warn_middleware_after_adapter if inserting_after_adapter?(index)
110
123
  handler = self.class::Handler.new(*args, &block)
111
124
  @handlers.insert(index, handler)
112
125
  end
113
126
 
114
- alias_method :insert_before, :insert
127
+ alias insert_before insert
115
128
 
116
- def insert_after(index, *args, &block)
129
+ ruby2_keywords def insert_after(index, *args, &block)
117
130
  index = assert_index(index)
118
131
  insert(index + 1, *args, &block)
119
132
  end
120
133
 
121
- def swap(index, *args, &block)
134
+ ruby2_keywords def swap(index, *args, &block)
122
135
  raise_if_locked
123
136
  index = assert_index(index)
124
137
  @handlers.delete_at(index)
@@ -133,10 +146,10 @@ module Faraday
133
146
  # Processes a Request into a Response by passing it through this Builder's
134
147
  # middleware stack.
135
148
  #
136
- # connection - Faraday::Connection
137
- # request - Faraday::Request
149
+ # @param connection [Faraday::Connection]
150
+ # @param request [Faraday::Request]
138
151
  #
139
- # Returns a Faraday::Response.
152
+ # @return [Faraday::Response]
140
153
  def build_response(connection, request)
141
154
  app.call(build_env(connection, request))
142
155
  end
@@ -151,30 +164,30 @@ module Faraday
151
164
  def app
152
165
  @app ||= begin
153
166
  lock!
154
- to_app(lambda { |env|
155
- response = Response.new
156
- env.response = response
157
- response.finish(env) unless env.parallel?
158
- response
159
- })
167
+ to_app
160
168
  end
161
169
  end
162
170
 
163
- def to_app(inner_app)
171
+ def to_app
164
172
  # last added handler is the deepest and thus closest to the inner app
165
- @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
166
177
  end
167
178
 
168
179
  def ==(other)
169
- other.is_a?(self.class) && @handlers == other.handlers
180
+ other.is_a?(self.class) &&
181
+ @handlers == other.handlers &&
182
+ @adapter == other.adapter
170
183
  end
171
184
 
172
185
  def dup
173
- self.class.new(@handlers.dup)
186
+ self.class.new(@handlers.dup, @adapter.dup)
174
187
  end
175
188
 
176
189
  # ENV Keys
177
- # :method - a symbolized request method (:get, :post)
190
+ # :http_method - a symbolized request HTTP method (:get, :post)
178
191
  # :body - the request body that will eventually be converted to a string.
179
192
  # :url - URI instance for the current request.
180
193
  # :status - HTTP response status code
@@ -190,45 +203,46 @@ module Faraday
190
203
  # :password - Proxy server password
191
204
  # :ssl - Hash of options for configuring SSL requests.
192
205
  def build_env(connection, request)
193
- Env.new(request.method, request.body,
194
- connection.build_exclusive_url(request.path, request.params, request.options.params_encoder),
195
- request.options, request.headers, connection.ssl,
196
- connection.parallel_manager)
206
+ exclusive_url = connection.build_exclusive_url(
207
+ request.path, request.params,
208
+ request.options.params_encoder
209
+ )
210
+
211
+ Env.new(request.http_method, request.body, exclusive_url,
212
+ request.options, request.headers, connection.ssl,
213
+ connection.parallel_manager)
197
214
  end
198
215
 
199
216
  private
200
217
 
218
+ LOCK_ERR = "can't modify middleware stack after making a request"
219
+
201
220
  def raise_if_locked
202
- raise StackLocked, "can't modify middleware stack after making a request" if locked?
221
+ raise StackLocked, LOCK_ERR if locked?
203
222
  end
204
223
 
205
- def warn_middleware_after_adapter
206
- warn "WARNING: Unexpected middleware set after the adapter. " \
207
- "This won't be supported from Faraday 1.0."
208
- end
224
+ def raise_if_adapter(klass)
225
+ return unless is_adapter?(klass)
209
226
 
210
- def adapter_set?
211
- @handlers.any? { |handler| is_adapter?(handler) }
227
+ raise 'Adapter should be set using the `adapter` method, not `use`'
212
228
  end
213
229
 
214
- def inserting_after_adapter?(index)
215
- adapter_index = @handlers.find_index { |handler| is_adapter?(handler) }
216
- return false if adapter_index.nil?
217
-
218
- index > adapter_index
230
+ def adapter_set?
231
+ !@adapter.nil?
219
232
  end
220
233
 
221
- def is_adapter?(handler)
222
- handler.klass.ancestors.include? Faraday::Adapter
234
+ def is_adapter?(klass) # rubocop:disable Naming/PredicateName
235
+ klass <= Faraday::Adapter
223
236
  end
224
237
 
225
- def use_symbol(mod, key, *args, &block)
238
+ ruby2_keywords def use_symbol(mod, key, *args, &block)
226
239
  use(mod.lookup_middleware(key), *args, &block)
227
240
  end
228
241
 
229
242
  def assert_index(index)
230
243
  idx = index.is_a?(Integer) ? index : @handlers.index(index)
231
244
  raise "No such handler: #{index.inspect}" unless idx
245
+
232
246
  idx
233
247
  end
234
248
  end
@@ -1,6 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Faraday
2
- # Used to setup urls, params, headers, and the request body in a sane manner.
4
+ # Used to setup URLs, params, headers, and the request body in a sane manner.
3
5
  #
6
+ # @example
4
7
  # @connection.post do |req|
5
8
  # req.url 'http://localhost', 'a' => '1' # 'http://localhost?a=1'
6
9
  # req.headers['b'] = '2' # Header
@@ -9,25 +12,63 @@ module Faraday
9
12
  # req.body = 'abc'
10
13
  # end
11
14
  #
12
- class Request < Struct.new(:method, :path, :params, :headers, :body, :options)
15
+ # @!attribute http_method
16
+ # @return [Symbol] the HTTP method of the Request
17
+ # @!attribute path
18
+ # @return [URI, String] the path
19
+ # @!attribute params
20
+ # @return [Hash] query parameters
21
+ # @!attribute headers
22
+ # @return [Faraday::Utils::Headers] headers
23
+ # @!attribute body
24
+ # @return [Hash] body
25
+ # @!attribute options
26
+ # @return [RequestOptions] options
27
+ #
28
+ # rubocop:disable Style/StructInheritance
29
+ class Request < Struct.new(
30
+ :http_method, :path, :params, :headers, :body, :options
31
+ )
32
+ # rubocop:enable Style/StructInheritance
33
+
13
34
  extend MiddlewareRegistry
14
35
 
15
- register_middleware File.expand_path('../request', __FILE__),
16
- :url_encoded => [:UrlEncoded, 'url_encoded'],
17
- :multipart => [:Multipart, 'multipart'],
18
- :retry => [:Retry, 'retry'],
19
- :authorization => [:Authorization, 'authorization'],
20
- :basic_auth => [:BasicAuthentication, 'basic_authentication'],
21
- :token_auth => [:TokenAuthentication, 'token_authentication'],
22
- :instrumentation => [:Instrumentation, 'instrumentation']
36
+ register_middleware File.expand_path('request', __dir__),
37
+ url_encoded: [:UrlEncoded, 'url_encoded'],
38
+ multipart: [:Multipart, 'multipart'],
39
+ retry: [:Retry, 'retry'],
40
+ authorization: [:Authorization, 'authorization'],
41
+ basic_auth: [
42
+ :BasicAuthentication,
43
+ 'basic_authentication'
44
+ ],
45
+ token_auth: [
46
+ :TokenAuthentication,
47
+ 'token_authentication'
48
+ ],
49
+ instrumentation: [:Instrumentation, 'instrumentation']
23
50
 
51
+ # @param request_method [String]
52
+ # @yield [request] for block customization, if block given
53
+ # @yieldparam request [Request]
54
+ # @return [Request]
24
55
  def self.create(request_method)
25
56
  new(request_method).tap do |request|
26
57
  yield(request) if block_given?
27
58
  end
28
59
  end
29
60
 
30
- # Public: Replace params, preserving the existing hash type
61
+ def method
62
+ warn <<~TEXT
63
+ WARNING: `Faraday::Request##{__method__}` is deprecated; use `#http_method` instead. It will be removed in or after version 2.0.
64
+ `Faraday::Request##{__method__}` called from #{caller_locations(1..1).first}
65
+ TEXT
66
+ http_method
67
+ end
68
+
69
+ # Replace params, preserving the existing hash type.
70
+ #
71
+ # @param hash [Hash] new params
31
72
  def params=(hash)
32
73
  if params
33
74
  params.replace hash
@@ -36,7 +77,9 @@ module Faraday
36
77
  end
37
78
  end
38
79
 
39
- # Public: Replace request headers, preserving the existing hash type
80
+ # Replace request headers, preserving the existing hash type.
81
+ #
82
+ # @param hash [Hash] new headers
40
83
  def headers=(hash)
41
84
  if headers
42
85
  headers.replace hash
@@ -45,9 +88,14 @@ module Faraday
45
88
  end
46
89
  end
47
90
 
91
+ # Update path and params.
92
+ #
93
+ # @param path [URI, String]
94
+ # @param params [Hash, nil]
95
+ # @return [void]
48
96
  def url(path, params = nil)
49
97
  if path.respond_to? :query
50
- if query = path.query
98
+ if (query = path.query)
51
99
  path = path.dup
52
100
  path.query = nil
53
101
  end
@@ -61,34 +109,48 @@ module Faraday
61
109
  self.params.update(params) if params
62
110
  end
63
111
 
112
+ # @param key [Object] key to look up in headers
113
+ # @return [Object] value of the given header name
64
114
  def [](key)
65
115
  headers[key]
66
116
  end
67
117
 
118
+ # @param key [Object] key of header to write
119
+ # @param value [Object] value of header
68
120
  def []=(key, value)
69
121
  headers[key] = value
70
122
  end
71
123
 
72
- # ENV Keys
73
- # :method - a symbolized request method (:get, :post)
74
- # :body - the request body that will eventually be converted to a string.
75
- # :url - URI instance for the current request.
76
- # :status - HTTP response status code
77
- # :request_headers - hash of HTTP Headers to be sent to the server
78
- # :response_headers - Hash of HTTP headers from the server
79
- # :parallel_manager - sent if the connection is in parallel mode
80
- # :request - Hash of options for configuring the request.
81
- # :timeout - open/read timeout Integer in seconds
82
- # :open_timeout - read timeout Integer in seconds
83
- # :proxy - Hash of proxy options
84
- # :uri - Proxy Server URI
85
- # :user - Proxy server username
86
- # :password - Proxy server password
87
- # :ssl - Hash of options for configuring SSL requests.
124
+ # Marshal serialization support.
125
+ #
126
+ # @return [Hash] the hash ready to be serialized in Marshal.
127
+ def marshal_dump
128
+ {
129
+ http_method: http_method,
130
+ body: body,
131
+ headers: headers,
132
+ path: path,
133
+ params: params,
134
+ options: options
135
+ }
136
+ end
137
+
138
+ # Marshal serialization support.
139
+ # Restores the instance variables according to the +serialised+.
140
+ # @param serialised [Hash] the serialised object.
141
+ def marshal_load(serialised)
142
+ self.http_method = serialised[:http_method]
143
+ self.body = serialised[:body]
144
+ self.headers = serialised[:headers]
145
+ self.path = serialised[:path]
146
+ self.params = serialised[:params]
147
+ self.options = serialised[:options]
148
+ end
149
+
150
+ # @return [Env] the Env for this Request
88
151
  def to_env(connection)
89
- Env.new(method, body, connection.build_exclusive_url(path, params),
90
- options, headers, connection.ssl, connection.parallel_manager)
152
+ Env.new(http_method, body, connection.build_exclusive_url(path, params),
153
+ options, headers, connection.ssl, connection.parallel_manager)
91
154
  end
92
155
  end
93
156
  end
94
-