faraday 0.13.0 → 2.0.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.
- checksums.yaml +5 -5
- data/CHANGELOG.md +496 -0
- data/LICENSE.md +1 -1
- data/README.md +28 -328
- data/Rakefile +7 -0
- data/examples/client_spec.rb +97 -0
- data/examples/client_test.rb +118 -0
- data/lib/faraday/adapter/test.rb +127 -68
- data/lib/faraday/adapter.rb +71 -22
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/connection.rb +314 -226
- data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
- data/lib/faraday/error.rb +121 -37
- data/lib/faraday/logging/formatter.rb +106 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +18 -25
- data/lib/faraday/middleware_registry.rb +65 -0
- data/lib/faraday/options/connection_options.rb +22 -0
- data/lib/faraday/options/env.rb +181 -0
- data/lib/faraday/options/proxy_options.rb +32 -0
- data/lib/faraday/options/request_options.rb +22 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/options.rb +41 -195
- data/lib/faraday/parameters.rb +4 -196
- data/lib/faraday/rack_builder.rb +91 -74
- data/lib/faraday/request/authorization.rb +37 -29
- data/lib/faraday/request/instrumentation.rb +47 -27
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request/url_encoded.rb +45 -23
- data/lib/faraday/request.rb +74 -32
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +57 -14
- data/lib/faraday/response.rb +26 -33
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +47 -251
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +104 -197
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/test_spec.rb +377 -0
- data/spec/faraday/adapter_registry_spec.rb +28 -0
- data/spec/faraday/adapter_spec.rb +55 -0
- data/spec/faraday/connection_spec.rb +787 -0
- data/spec/faraday/error_spec.rb +60 -0
- data/spec/faraday/middleware_spec.rb +52 -0
- data/spec/faraday/options/env_spec.rb +70 -0
- data/spec/faraday/options/options_spec.rb +297 -0
- data/spec/faraday/options/proxy_options_spec.rb +44 -0
- data/spec/faraday/options/request_options_spec.rb +19 -0
- data/spec/faraday/params_encoders/flat_spec.rb +42 -0
- data/spec/faraday/params_encoders/nested_spec.rb +142 -0
- data/spec/faraday/rack_builder_spec.rb +302 -0
- data/spec/faraday/request/authorization_spec.rb +83 -0
- data/spec/faraday/request/instrumentation_spec.rb +74 -0
- data/spec/faraday/request/json_spec.rb +111 -0
- data/spec/faraday/request/url_encoded_spec.rb +82 -0
- data/spec/faraday/request_spec.rb +109 -0
- data/spec/faraday/response/json_spec.rb +117 -0
- data/spec/faraday/response/logger_spec.rb +220 -0
- data/spec/faraday/response/raise_error_spec.rb +172 -0
- data/spec/faraday/response_spec.rb +75 -0
- data/spec/faraday/utils/headers_spec.rb +82 -0
- data/spec/faraday/utils_spec.rb +117 -0
- data/spec/faraday_spec.rb +37 -0
- data/spec/spec_helper.rb +132 -0
- data/spec/support/disabling_stub.rb +14 -0
- data/spec/support/fake_safe_buffer.rb +15 -0
- data/spec/support/helper_methods.rb +96 -0
- data/spec/support/shared_examples/adapter.rb +104 -0
- data/spec/support/shared_examples/params_encoder.rb +18 -0
- data/spec/support/shared_examples/request_method.rb +249 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- metadata +71 -34
- data/lib/faraday/adapter/em_http.rb +0 -243
- data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
- data/lib/faraday/adapter/em_synchrony.rb +0 -106
- data/lib/faraday/adapter/excon.rb +0 -80
- data/lib/faraday/adapter/httpclient.rb +0 -128
- data/lib/faraday/adapter/net_http.rb +0 -135
- data/lib/faraday/adapter/net_http_persistent.rb +0 -54
- data/lib/faraday/adapter/patron.rb +0 -83
- data/lib/faraday/adapter/rack.rb +0 -58
- data/lib/faraday/adapter/typhoeus.rb +0 -123
- data/lib/faraday/autoload.rb +0 -84
- data/lib/faraday/request/basic_authentication.rb +0 -13
- data/lib/faraday/request/multipart.rb +0 -68
- data/lib/faraday/request/retry.rb +0 -164
- data/lib/faraday/request/token_authentication.rb +0 -15
- data/lib/faraday/upload_io.rb +0 -67
data/lib/faraday.rb
CHANGED
@@ -1,244 +1,151 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require 'cgi'
|
4
|
+
require 'date'
|
3
5
|
require 'set'
|
4
6
|
require 'forwardable'
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
#
|
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
|
-
|
34
|
+
CONTENT_TYPE = 'Content-Type'
|
18
35
|
|
19
36
|
class << self
|
20
|
-
#
|
21
|
-
#
|
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
|
-
#
|
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
|
-
#
|
28
|
-
#
|
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
|
-
#
|
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
|
-
#
|
38
|
-
#
|
39
|
-
#
|
40
|
-
|
41
|
-
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
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
|
-
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
# #
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
63
|
-
#
|
64
|
-
def new(url = nil, options =
|
65
|
-
|
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
|
-
#
|
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
|
-
|
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
|
-
|
97
|
-
#
|
98
|
-
#
|
99
|
-
|
100
|
-
|
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
|
-
|
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
|
-
#
|
150
|
-
|
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
|
-
#
|
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
|
-
#
|
182
|
-
|
183
|
-
|
184
|
-
|
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
|
-
|
199
|
-
defined?(@registered_middleware) && @registered_middleware[key]
|
200
|
-
end
|
135
|
+
private
|
201
136
|
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
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
|
-
|
230
|
-
|
231
|
-
|
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
|