faraday 0.15.4 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +380 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +16 -344
  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 +127 -190
  9. data/lib/faraday/adapter.rb +72 -22
  10. data/lib/faraday/adapter/test.rb +86 -53
  11. data/lib/faraday/adapter/typhoeus.rb +4 -1
  12. data/lib/faraday/adapter_registry.rb +30 -0
  13. data/lib/faraday/autoload.rb +39 -36
  14. data/lib/faraday/connection.rb +320 -183
  15. data/lib/faraday/dependency_loader.rb +37 -0
  16. data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
  17. data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
  18. data/lib/faraday/error.rb +123 -37
  19. data/lib/faraday/file_part.rb +128 -0
  20. data/lib/faraday/logging/formatter.rb +105 -0
  21. data/lib/faraday/methods.rb +6 -0
  22. data/lib/faraday/middleware.rb +19 -25
  23. data/lib/faraday/middleware_registry.rb +129 -0
  24. data/lib/faraday/options.rb +39 -194
  25. data/lib/faraday/options/connection_options.rb +22 -0
  26. data/lib/faraday/options/env.rb +181 -0
  27. data/lib/faraday/options/proxy_options.rb +32 -0
  28. data/lib/faraday/options/request_options.rb +22 -0
  29. data/lib/faraday/options/ssl_options.rb +59 -0
  30. data/lib/faraday/param_part.rb +53 -0
  31. data/lib/faraday/parameters.rb +4 -197
  32. data/lib/faraday/rack_builder.rb +77 -65
  33. data/lib/faraday/request.rb +86 -44
  34. data/lib/faraday/request/authorization.rb +44 -30
  35. data/lib/faraday/request/basic_authentication.rb +14 -7
  36. data/lib/faraday/request/instrumentation.rb +45 -27
  37. data/lib/faraday/request/multipart.rb +86 -48
  38. data/lib/faraday/request/retry.rb +198 -169
  39. data/lib/faraday/request/token_authentication.rb +15 -10
  40. data/lib/faraday/request/url_encoded.rb +43 -23
  41. data/lib/faraday/response.rb +27 -23
  42. data/lib/faraday/response/logger.rb +22 -69
  43. data/lib/faraday/response/raise_error.rb +49 -14
  44. data/lib/faraday/utils.rb +38 -247
  45. data/lib/faraday/utils/headers.rb +139 -0
  46. data/lib/faraday/utils/params_hash.rb +61 -0
  47. data/lib/faraday/version.rb +5 -0
  48. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  49. data/spec/faraday/adapter/em_http_spec.rb +49 -0
  50. data/spec/faraday/adapter/em_synchrony_spec.rb +18 -0
  51. data/spec/faraday/adapter/excon_spec.rb +49 -0
  52. data/spec/faraday/adapter/httpclient_spec.rb +73 -0
  53. data/spec/faraday/adapter/net_http_spec.rb +64 -0
  54. data/spec/faraday/adapter/patron_spec.rb +18 -0
  55. data/spec/faraday/adapter/rack_spec.rb +8 -0
  56. data/spec/faraday/adapter/test_spec.rb +260 -0
  57. data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
  58. data/spec/faraday/adapter_registry_spec.rb +28 -0
  59. data/spec/faraday/adapter_spec.rb +55 -0
  60. data/spec/faraday/composite_read_io_spec.rb +80 -0
  61. data/spec/faraday/connection_spec.rb +736 -0
  62. data/spec/faraday/error_spec.rb +60 -0
  63. data/spec/faraday/middleware_spec.rb +52 -0
  64. data/spec/faraday/options/env_spec.rb +70 -0
  65. data/spec/faraday/options/options_spec.rb +297 -0
  66. data/spec/faraday/options/proxy_options_spec.rb +44 -0
  67. data/spec/faraday/options/request_options_spec.rb +19 -0
  68. data/spec/faraday/params_encoders/flat_spec.rb +42 -0
  69. data/spec/faraday/params_encoders/nested_spec.rb +142 -0
  70. data/spec/faraday/rack_builder_spec.rb +345 -0
  71. data/spec/faraday/request/authorization_spec.rb +88 -0
  72. data/spec/faraday/request/instrumentation_spec.rb +76 -0
  73. data/spec/faraday/request/multipart_spec.rb +302 -0
  74. data/spec/faraday/request/retry_spec.rb +242 -0
  75. data/spec/faraday/request/url_encoded_spec.rb +83 -0
  76. data/spec/faraday/request_spec.rb +120 -0
  77. data/spec/faraday/response/logger_spec.rb +220 -0
  78. data/spec/faraday/response/middleware_spec.rb +68 -0
  79. data/spec/faraday/response/raise_error_spec.rb +169 -0
  80. data/spec/faraday/response_spec.rb +75 -0
  81. data/spec/faraday/utils/headers_spec.rb +82 -0
  82. data/spec/faraday/utils_spec.rb +56 -0
  83. data/spec/faraday_spec.rb +37 -0
  84. data/spec/spec_helper.rb +132 -0
  85. data/spec/support/disabling_stub.rb +14 -0
  86. data/spec/support/fake_safe_buffer.rb +15 -0
  87. data/spec/support/helper_methods.rb +133 -0
  88. data/spec/support/shared_examples/adapter.rb +105 -0
  89. data/spec/support/shared_examples/params_encoder.rb +18 -0
  90. data/spec/support/shared_examples/request_method.rb +262 -0
  91. data/spec/support/streaming_response_checker.rb +35 -0
  92. data/spec/support/webmock_rack_app.rb +68 -0
  93. metadata +206 -19
  94. data/lib/faraday/adapter/em_http.rb +0 -243
  95. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
  96. data/lib/faraday/adapter/em_synchrony.rb +0 -106
  97. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
  98. data/lib/faraday/adapter/excon.rb +0 -82
  99. data/lib/faraday/adapter/httpclient.rb +0 -128
  100. data/lib/faraday/adapter/net_http.rb +0 -152
  101. data/lib/faraday/adapter/net_http_persistent.rb +0 -68
  102. data/lib/faraday/adapter/patron.rb +0 -95
  103. data/lib/faraday/adapter/rack.rb +0 -58
  104. 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,13 +146,11 @@ 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
- warn 'WARNING: No adapter was configured for this request' unless adapter_set?
142
-
143
154
  app.call(build_env(connection, request))
144
155
  end
145
156
 
@@ -153,30 +164,30 @@ module Faraday
153
164
  def app
154
165
  @app ||= begin
155
166
  lock!
156
- to_app(lambda { |env|
157
- response = Response.new
158
- env.response = response
159
- response.finish(env) unless env.parallel?
160
- response
161
- })
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
180
+ other.is_a?(self.class) &&
181
+ @handlers == other.handlers &&
182
+ @adapter == other.adapter
172
183
  end
173
184
 
174
185
  def dup
175
- self.class.new(@handlers.dup)
186
+ self.class.new(@handlers.dup, @adapter.dup)
176
187
  end
177
188
 
178
189
  # ENV Keys
179
- # :method - a symbolized request method (:get, :post)
190
+ # :http_method - a symbolized request HTTP method (:get, :post)
180
191
  # :body - the request body that will eventually be converted to a string.
181
192
  # :url - URI instance for the current request.
182
193
  # :status - HTTP response status code
@@ -192,45 +203,46 @@ module Faraday
192
203
  # :password - Proxy server password
193
204
  # :ssl - Hash of options for configuring SSL requests.
194
205
  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)
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)
199
214
  end
200
215
 
201
216
  private
202
217
 
218
+ LOCK_ERR = "can't modify middleware stack after making a request"
219
+
203
220
  def raise_if_locked
204
- raise StackLocked, "can't modify middleware stack after making a request" if locked?
221
+ raise StackLocked, LOCK_ERR if locked?
205
222
  end
206
223
 
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
224
+ def raise_if_adapter(klass)
225
+ return unless is_adapter?(klass)
211
226
 
212
- def adapter_set?
213
- @handlers.any? { |handler| is_adapter?(handler) }
227
+ raise 'Adapter should be set using the `adapter` method, not `use`'
214
228
  end
215
229
 
216
- def inserting_after_adapter?(index)
217
- adapter_index = @handlers.find_index { |handler| is_adapter?(handler) }
218
- return false if adapter_index.nil?
219
-
220
- index > adapter_index
230
+ def adapter_set?
231
+ !@adapter.nil?
221
232
  end
222
233
 
223
- def is_adapter?(handler)
224
- handler.klass.ancestors.include? Faraday::Adapter
234
+ def is_adapter?(klass) # rubocop:disable Naming/PredicateName
235
+ klass <= Faraday::Adapter
225
236
  end
226
237
 
227
- def use_symbol(mod, key, *args, &block)
238
+ ruby2_keywords def use_symbol(mod, key, *args, &block)
228
239
  use(mod.lookup_middleware(key), *args, &block)
229
240
  end
230
241
 
231
242
  def assert_index(index)
232
243
  idx = index.is_a?(Integer) ? index : @handlers.index(index)
233
244
  raise "No such handler: #{index.inspect}" unless idx
245
+
234
246
  idx
235
247
  end
236
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,54 +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
 
124
+ # Marshal serialization support.
125
+ #
126
+ # @return [Hash] the hash ready to be serialized in Marshal.
72
127
  def marshal_dump
73
128
  {
74
- :method => method,
75
- :body => body,
76
- :headers => headers,
77
- :path => path,
78
- :params => params,
79
- :options => options
129
+ http_method: http_method,
130
+ body: body,
131
+ headers: headers,
132
+ path: path,
133
+ params: params,
134
+ options: options
80
135
  }
81
136
  end
82
137
 
138
+ # Marshal serialization support.
139
+ # Restores the instance variables according to the +serialised+.
140
+ # @param serialised [Hash] the serialised object.
83
141
  def marshal_load(serialised)
84
- self.method = serialised[:method]
85
- self.body = serialised[:body]
86
- self.headers = serialised[:headers]
87
- self.path = serialised[:path]
88
- self.params = serialised[:params]
89
- self.options = serialised[:options]
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]
90
148
  end
91
149
 
92
- # ENV Keys
93
- # :method - a symbolized request method (:get, :post)
94
- # :body - the request body that will eventually be converted to a string.
95
- # :url - URI instance for the current request.
96
- # :status - HTTP response status code
97
- # :request_headers - hash of HTTP Headers to be sent to the server
98
- # :response_headers - Hash of HTTP headers from the server
99
- # :parallel_manager - sent if the connection is in parallel mode
100
- # :request - Hash of options for configuring the request.
101
- # :timeout - open/read timeout Integer in seconds
102
- # :open_timeout - read timeout Integer in seconds
103
- # :proxy - Hash of proxy options
104
- # :uri - Proxy Server URI
105
- # :user - Proxy server username
106
- # :password - Proxy server password
107
- # :ssl - Hash of options for configuring SSL requests.
150
+ # @return [Env] the Env for this Request
108
151
  def to_env(connection)
109
- Env.new(method, body, connection.build_exclusive_url(path, params),
110
- 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)
111
154
  end
112
155
  end
113
156
  end
114
-