faraday 0.13.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +496 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +28 -328
  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 -68
  9. data/lib/faraday/adapter.rb +71 -22
  10. data/lib/faraday/adapter_registry.rb +30 -0
  11. data/lib/faraday/connection.rb +314 -226
  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 +121 -37
  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 +65 -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 -74
  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 +104 -197
  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_spec.rb +52 -0
  48. data/spec/faraday/options/env_spec.rb +70 -0
  49. data/spec/faraday/options/options_spec.rb +297 -0
  50. data/spec/faraday/options/proxy_options_spec.rb +44 -0
  51. data/spec/faraday/options/request_options_spec.rb +19 -0
  52. data/spec/faraday/params_encoders/flat_spec.rb +42 -0
  53. data/spec/faraday/params_encoders/nested_spec.rb +142 -0
  54. data/spec/faraday/rack_builder_spec.rb +302 -0
  55. data/spec/faraday/request/authorization_spec.rb +83 -0
  56. data/spec/faraday/request/instrumentation_spec.rb +74 -0
  57. data/spec/faraday/request/json_spec.rb +111 -0
  58. data/spec/faraday/request/url_encoded_spec.rb +82 -0
  59. data/spec/faraday/request_spec.rb +109 -0
  60. data/spec/faraday/response/json_spec.rb +117 -0
  61. data/spec/faraday/response/logger_spec.rb +220 -0
  62. data/spec/faraday/response/raise_error_spec.rb +172 -0
  63. data/spec/faraday/response_spec.rb +75 -0
  64. data/spec/faraday/utils/headers_spec.rb +82 -0
  65. data/spec/faraday/utils_spec.rb +117 -0
  66. data/spec/faraday_spec.rb +37 -0
  67. data/spec/spec_helper.rb +132 -0
  68. data/spec/support/disabling_stub.rb +14 -0
  69. data/spec/support/fake_safe_buffer.rb +15 -0
  70. data/spec/support/helper_methods.rb +96 -0
  71. data/spec/support/shared_examples/adapter.rb +104 -0
  72. data/spec/support/shared_examples/params_encoder.rb +18 -0
  73. data/spec/support/shared_examples/request_method.rb +249 -0
  74. data/spec/support/streaming_response_checker.rb +35 -0
  75. metadata +71 -34
  76. data/lib/faraday/adapter/em_http.rb +0 -243
  77. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
  78. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
  79. data/lib/faraday/adapter/em_synchrony.rb +0 -106
  80. data/lib/faraday/adapter/excon.rb +0 -80
  81. data/lib/faraday/adapter/httpclient.rb +0 -128
  82. data/lib/faraday/adapter/net_http.rb +0 -135
  83. data/lib/faraday/adapter/net_http_persistent.rb +0 -54
  84. data/lib/faraday/adapter/patron.rb +0 -83
  85. data/lib/faraday/adapter/rack.rb +0 -58
  86. data/lib/faraday/adapter/typhoeus.rb +0 -123
  87. data/lib/faraday/autoload.rb +0 -84
  88. data/lib/faraday/request/basic_authentication.rb +0 -13
  89. data/lib/faraday/request/multipart.rb +0 -68
  90. data/lib/faraday/request/retry.rb +0 -164
  91. data/lib/faraday/request/token_authentication.rb +0 -15
  92. data/lib/faraday/upload_io.rb +0 -67
data/lib/faraday.rb CHANGED
@@ -1,244 +1,151 @@
1
- require 'thread'
1
+ # frozen_string_literal: true
2
+
2
3
  require 'cgi'
4
+ require 'date'
3
5
  require 'set'
4
6
  require 'forwardable'
5
-
6
- # Public: This is the main namespace for Faraday. You can either use it to
7
- # create Faraday::Connection objects, or access it directly.
7
+ require 'faraday/version'
8
+ require 'faraday/methods'
9
+ require 'faraday/error'
10
+ require 'faraday/middleware_registry'
11
+ require 'faraday/utils'
12
+ require 'faraday/options'
13
+ require 'faraday/connection'
14
+ require 'faraday/rack_builder'
15
+ require 'faraday/parameters'
16
+ require 'faraday/middleware'
17
+ require 'faraday/adapter'
18
+ require 'faraday/request'
19
+ require 'faraday/response'
20
+
21
+ # This is the main namespace for Faraday.
8
22
  #
9
- # Examples
23
+ # It provides methods to create {Connection} objects, and HTTP-related
24
+ # methods to use directly.
10
25
  #
26
+ # @example Helpful class methods for easy usage
11
27
  # Faraday.get "http://faraday.com"
12
28
  #
29
+ # @example Helpful class method `.new` to create {Connection} objects.
13
30
  # conn = Faraday.new "http://faraday.com"
14
31
  # conn.get '/'
15
32
  #
16
33
  module Faraday
17
- VERSION = "0.13.0"
34
+ CONTENT_TYPE = 'Content-Type'
18
35
 
19
36
  class << self
20
- # Public: Gets or sets the root path that Faraday is being loaded from.
21
- # This is the root from where the libraries are auto-loaded from.
37
+ # The root path that Faraday is being loaded from.
38
+ #
39
+ # This is the root from where the libraries are auto-loaded.
40
+ #
41
+ # @return [String]
22
42
  attr_accessor :root_path
23
43
 
24
- # Public: Gets or sets the path that the Faraday libs are loaded from.
44
+ # Gets or sets the path that the Faraday libs are loaded from.
45
+ # @return [String]
25
46
  attr_accessor :lib_path
26
47
 
27
- # Public: Gets or sets the Symbol key identifying a default Adapter to use
28
- # for the default Faraday::Connection.
48
+ # @overload default_adapter
49
+ # Gets the Symbol key identifying a default Adapter to use
50
+ # for the default {Faraday::Connection}. Defaults to `:test`.
51
+ # @return [Symbol] the default adapter
52
+ # @overload default_adapter=(adapter)
53
+ # Updates default adapter while resetting {.default_connection}.
54
+ # @return [Symbol] the new default_adapter.
29
55
  attr_reader :default_adapter
30
56
 
31
- # Public: Sets the default Faraday::Connection for simple scripts that
32
- # access the Faraday constant directly.
33
- #
34
- # Faraday.get "https://faraday.com"
57
+ # Documented below, see default_connection
35
58
  attr_writer :default_connection
36
59
 
37
- # Public: Initializes a new Faraday::Connection.
38
- #
39
- # url - The optional String base URL to use as a prefix for all
40
- # requests. Can also be the options Hash.
41
- # options - The optional Hash used to configure this Faraday::Connection.
42
- # Any of these values will be set on every request made, unless
43
- # overridden for a specific request.
44
- # :url - String base URL.
45
- # :params - Hash of URI query unencoded key/value pairs.
46
- # :headers - Hash of unencoded HTTP header key/value pairs.
47
- # :request - Hash of request options.
48
- # :ssl - Hash of SSL options.
49
- # :proxy - Hash of Proxy options.
50
- #
51
- # Examples
52
- #
60
+ # Tells Faraday to ignore the environment proxy (http_proxy).
61
+ # Defaults to `false`.
62
+ # @return [Boolean]
63
+ attr_accessor :ignore_env_proxy
64
+
65
+ # Initializes a new {Connection}.
66
+ #
67
+ # @param url [String,Hash] The optional String base URL to use as a prefix
68
+ # for all requests. Can also be the options Hash. Any of these
69
+ # values will be set on every request made, unless overridden
70
+ # for a specific request.
71
+ # @param options [Hash]
72
+ # @option options [String] :url Base URL
73
+ # @option options [Hash] :params Hash of unencoded URI query params.
74
+ # @option options [Hash] :headers Hash of unencoded HTTP headers.
75
+ # @option options [Hash] :request Hash of request options.
76
+ # @option options [Hash] :ssl Hash of SSL options.
77
+ # @option options [Hash] :proxy Hash of Proxy options.
78
+ # @return [Faraday::Connection]
79
+ #
80
+ # @example With an URL argument
53
81
  # Faraday.new 'http://faraday.com'
54
- #
55
- # # http://faraday.com?page=1
56
- # Faraday.new 'http://faraday.com', :params => {:page => 1}
57
- #
58
- # # same
59
- #
60
- # Faraday.new :url => 'http://faraday.com',
61
- # :params => {:page => 1}
62
- #
63
- # Returns a Faraday::Connection.
64
- def new(url = nil, options = nil)
65
- block = block_given? ? Proc.new : nil
66
- options = options ? default_connection_options.merge(options) : default_connection_options
82
+ # # => Faraday::Connection to http://faraday.com
83
+ #
84
+ # @example With an URL argument and an options hash
85
+ # Faraday.new 'http://faraday.com', params: { page: 1 }
86
+ # # => Faraday::Connection to http://faraday.com?page=1
87
+ #
88
+ # @example With everything in an options hash
89
+ # Faraday.new url: 'http://faraday.com',
90
+ # params: { page: 1 }
91
+ # # => Faraday::Connection to http://faraday.com?page=1
92
+ def new(url = nil, options = {}, &block)
93
+ options = Utils.deep_merge(default_connection_options, options)
67
94
  Faraday::Connection.new(url, options, &block)
68
95
  end
69
96
 
70
- # Internal: Requires internal Faraday libraries.
71
- #
72
- # *libs - One or more relative String names to Faraday classes.
73
- #
74
- # Returns nothing.
75
- def require_libs(*libs)
76
- libs.each do |lib|
77
- require "#{lib_path}/#{lib}"
78
- end
79
- end
80
-
81
- # Public: Updates default adapter while resetting
82
- # #default_connection.
83
- #
84
- # Returns the new default_adapter.
97
+ # Documented elsewhere, see default_adapter reader
85
98
  def default_adapter=(adapter)
86
99
  @default_connection = nil
87
100
  @default_adapter = adapter
88
101
  end
89
102
 
90
- alias require_lib require_libs
91
-
92
- def respond_to?(symbol, include_private = false)
103
+ def respond_to_missing?(symbol, include_private = false)
93
104
  default_connection.respond_to?(symbol, include_private) || super
94
105
  end
95
106
 
96
- private
97
- # Internal: Proxies method calls on the Faraday constant to
98
- # #default_connection.
99
- def method_missing(name, *args, &block)
100
- default_connection.send(name, *args, &block)
107
+ # @overload default_connection
108
+ # Gets the default connection used for simple scripts.
109
+ # @return [Faraday::Connection] a connection configured with
110
+ # the default_adapter.
111
+ # @overload default_connection=(connection)
112
+ # @param connection [Faraday::Connection]
113
+ # Sets the default {Faraday::Connection} for simple scripts that
114
+ # access the Faraday constant directly, such as
115
+ # <code>Faraday.get "https://faraday.com"</code>.
116
+ def default_connection
117
+ @default_connection ||= Connection.new(default_connection_options)
101
118
  end
102
- end
103
-
104
- self.root_path = File.expand_path "..", __FILE__
105
- self.lib_path = File.expand_path "../faraday", __FILE__
106
- self.default_adapter = :net_http
107
-
108
- # Gets the default connection used for simple scripts.
109
- #
110
- # Returns a Faraday::Connection, configured with the #default_adapter.
111
- def self.default_connection
112
- @default_connection ||= Connection.new(default_connection_options)
113
- end
114
-
115
- # Gets the default connection options used when calling Faraday#new.
116
- #
117
- # Returns a Faraday::ConnectionOptions.
118
- def self.default_connection_options
119
- @default_connection_options ||= ConnectionOptions.new
120
- end
121
119
 
122
- # Public: Sets the default options used when calling Faraday#new.
123
- def self.default_connection_options=(options)
124
- @default_connection = nil
125
- @default_connection_options = ConnectionOptions.from(options)
126
- end
127
-
128
- unless const_defined? :Timer
129
- require 'timeout'
130
- Timer = Timeout
131
- end
132
-
133
- # Public: Adds the ability for other modules to register and lookup
134
- # middleware classes.
135
- module MiddlewareRegistry
136
- # Public: Register middleware class(es) on the current module.
137
- #
138
- # mapping - A Hash mapping Symbol keys to classes. Classes can be expressed
139
- # as fully qualified constant, or a Proc that will be lazily
140
- # called to return the former.
141
- #
142
- # Examples
143
- #
144
- # module Faraday
145
- # class Whatever
146
- # # Middleware looked up by :foo returns Faraday::Whatever::Foo.
147
- # register_middleware :foo => Foo
120
+ # Gets the default connection options used when calling {Faraday#new}.
148
121
  #
149
- # # Middleware looked up by :bar returns Faraday::Whatever.const_get(:Bar)
150
- # register_middleware :bar => :Bar
151
- #
152
- # # Middleware looked up by :baz requires 'baz' and returns Faraday::Whatever.const_get(:Baz)
153
- # register_middleware :baz => [:Baz, 'baz']
154
- # end
155
- # end
156
- #
157
- # Returns nothing.
158
- def register_middleware(autoload_path = nil, mapping = nil)
159
- if mapping.nil?
160
- mapping = autoload_path
161
- autoload_path = nil
162
- end
163
- middleware_mutex do
164
- @middleware_autoload_path = autoload_path if autoload_path
165
- (@registered_middleware ||= {}).update(mapping)
166
- end
122
+ # @return [Faraday::ConnectionOptions]
123
+ def default_connection_options
124
+ @default_connection_options ||= ConnectionOptions.new
167
125
  end
168
126
 
169
- # Public: Lookup middleware class with a registered Symbol shortcut.
170
- #
171
- # key - The Symbol key for the registered middleware.
172
- #
173
- # Examples
174
- #
175
- # module Faraday
176
- # class Whatever
177
- # register_middleware :foo => Foo
178
- # end
179
- # end
127
+ # Sets the default options used when calling {Faraday#new}.
180
128
  #
181
- # Faraday::Whatever.lookup_middleware(:foo)
182
- # # => Faraday::Whatever::Foo
183
- #
184
- # Returns a middleware Class.
185
- def lookup_middleware(key)
186
- load_middleware(key) ||
187
- raise(Faraday::Error.new("#{key.inspect} is not registered on #{self}"))
188
- end
189
-
190
- def middleware_mutex(&block)
191
- @middleware_mutex ||= begin
192
- require 'monitor'
193
- Monitor.new
194
- end
195
- @middleware_mutex.synchronize(&block)
129
+ # @param options [Hash, Faraday::ConnectionOptions]
130
+ def default_connection_options=(options)
131
+ @default_connection = nil
132
+ @default_connection_options = ConnectionOptions.from(options)
196
133
  end
197
134
 
198
- def fetch_middleware(key)
199
- defined?(@registered_middleware) && @registered_middleware[key]
200
- end
135
+ private
201
136
 
202
- def load_middleware(key)
203
- value = fetch_middleware(key)
204
- case value
205
- when Module
206
- value
207
- when Symbol, String
208
- middleware_mutex do
209
- @registered_middleware[key] = const_get(value)
210
- end
211
- when Proc
212
- middleware_mutex do
213
- @registered_middleware[key] = value.call
214
- end
215
- when Array
216
- middleware_mutex do
217
- const, path = value
218
- if root = @middleware_autoload_path
219
- path = "#{root}/#{path}"
220
- end
221
- require(path)
222
- @registered_middleware[key] = const
223
- end
224
- load_middleware(key)
137
+ # Internal: Proxies method calls on the Faraday constant to
138
+ # .default_connection.
139
+ def method_missing(name, *args, &block)
140
+ if default_connection.respond_to?(name)
141
+ default_connection.send(name, *args, &block)
142
+ else
143
+ super
225
144
  end
226
145
  end
227
146
  end
228
147
 
229
- def self.const_missing(name)
230
- if name.to_sym == :Builder
231
- warn "Faraday::Builder is now Faraday::RackBuilder."
232
- const_set name, RackBuilder
233
- else
234
- super
235
- end
236
- end
237
-
238
- require_libs "utils", "options", "connection", "rack_builder", "parameters",
239
- "middleware", "adapter", "request", "response", "upload_io", "error"
240
-
241
- if !ENV["FARADAY_NO_AUTOLOAD"]
242
- require_lib 'autoload'
243
- end
148
+ self.ignore_env_proxy = false
149
+ self.root_path = File.expand_path __dir__
150
+ self.lib_path = File.expand_path 'faraday', __dir__
244
151
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'webmock/rspec'
4
+ WebMock.disable_net_connect!(allow_localhost: true)
5
+
6
+ require_relative '../support/helper_methods'
7
+ require_relative '../support/disabling_stub'
8
+ require_relative '../support/streaming_response_checker'
9
+ require_relative '../support/shared_examples/adapter'
10
+ require_relative '../support/shared_examples/request_method'
11
+
12
+ RSpec.configure do |config|
13
+ config.include Faraday::HelperMethods
14
+ end