faraday 0.15.0 → 2.2.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 +4 -4
- data/CHANGELOG.md +570 -0
- data/LICENSE.md +1 -1
- data/README.md +25 -351
- 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 -72
- data/lib/faraday/adapter.rb +69 -22
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/connection.rb +309 -232
- 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 +119 -38
- 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 +83 -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 -76
- 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 +108 -199
- 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_registry_spec.rb +31 -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 +317 -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 +86 -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 -79
- data/lib/faraday/adapter/httpclient.rb +0 -128
- data/lib/faraday/adapter/net_http.rb +0 -137
- data/lib/faraday/adapter/net_http_persistent.rb +0 -63
- data/lib/faraday/adapter/patron.rb +0 -100
- data/lib/faraday/adapter/rack.rb +0 -58
- data/lib/faraday/adapter/typhoeus.rb +0 -12
- 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 -211
- data/lib/faraday/request/token_authentication.rb +0 -15
- data/lib/faraday/upload_io.rb +0 -67
data/lib/faraday.rb
CHANGED
@@ -1,248 +1,157 @@
|
|
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
|
+
require 'faraday/net_http'
|
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
|
-
#
|
33
|
-
|
34
|
-
|
57
|
+
# Option for the default_adapter
|
58
|
+
# @return [Hash] default_adapter options
|
59
|
+
attr_accessor :default_adapter_options
|
60
|
+
|
61
|
+
# Documented below, see default_connection
|
35
62
|
attr_writer :default_connection
|
36
63
|
|
37
|
-
#
|
64
|
+
# Tells Faraday to ignore the environment proxy (http_proxy).
|
65
|
+
# Defaults to `false`.
|
66
|
+
# @return [Boolean]
|
38
67
|
attr_accessor :ignore_env_proxy
|
39
68
|
|
40
|
-
#
|
41
|
-
#
|
42
|
-
# url
|
43
|
-
# requests. Can also be the options Hash.
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
69
|
+
# Initializes a new {Connection}.
|
70
|
+
#
|
71
|
+
# @param url [String,Hash] The optional String base URL to use as a prefix
|
72
|
+
# for all requests. Can also be the options Hash. Any of these
|
73
|
+
# values will be set on every request made, unless overridden
|
74
|
+
# for a specific request.
|
75
|
+
# @param options [Hash]
|
76
|
+
# @option options [String] :url Base URL
|
77
|
+
# @option options [Hash] :params Hash of unencoded URI query params.
|
78
|
+
# @option options [Hash] :headers Hash of unencoded HTTP headers.
|
79
|
+
# @option options [Hash] :request Hash of request options.
|
80
|
+
# @option options [Hash] :ssl Hash of SSL options.
|
81
|
+
# @option options [Hash] :proxy Hash of Proxy options.
|
82
|
+
# @return [Faraday::Connection]
|
83
|
+
#
|
84
|
+
# @example With an URL argument
|
56
85
|
# Faraday.new 'http://faraday.com'
|
57
|
-
#
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
# #
|
62
|
-
#
|
63
|
-
#
|
64
|
-
#
|
65
|
-
#
|
66
|
-
#
|
67
|
-
def new(url = nil, options =
|
68
|
-
|
69
|
-
options = options ? default_connection_options.merge(options) : default_connection_options
|
86
|
+
# # => Faraday::Connection to http://faraday.com
|
87
|
+
#
|
88
|
+
# @example With an URL argument and an options hash
|
89
|
+
# Faraday.new 'http://faraday.com', params: { page: 1 }
|
90
|
+
# # => Faraday::Connection to http://faraday.com?page=1
|
91
|
+
#
|
92
|
+
# @example With everything in an options hash
|
93
|
+
# Faraday.new url: 'http://faraday.com',
|
94
|
+
# params: { page: 1 }
|
95
|
+
# # => Faraday::Connection to http://faraday.com?page=1
|
96
|
+
def new(url = nil, options = {}, &block)
|
97
|
+
options = Utils.deep_merge(default_connection_options, options)
|
70
98
|
Faraday::Connection.new(url, options, &block)
|
71
99
|
end
|
72
100
|
|
73
|
-
#
|
74
|
-
#
|
75
|
-
# *libs - One or more relative String names to Faraday classes.
|
76
|
-
#
|
77
|
-
# Returns nothing.
|
78
|
-
def require_libs(*libs)
|
79
|
-
libs.each do |lib|
|
80
|
-
require "#{lib_path}/#{lib}"
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
# Public: Updates default adapter while resetting
|
85
|
-
# #default_connection.
|
86
|
-
#
|
87
|
-
# Returns the new default_adapter.
|
101
|
+
# Documented elsewhere, see default_adapter reader
|
88
102
|
def default_adapter=(adapter)
|
89
103
|
@default_connection = nil
|
90
104
|
@default_adapter = adapter
|
91
105
|
end
|
92
106
|
|
93
|
-
|
94
|
-
|
95
|
-
def respond_to?(symbol, include_private = false)
|
107
|
+
def respond_to_missing?(symbol, include_private = false)
|
96
108
|
default_connection.respond_to?(symbol, include_private) || super
|
97
109
|
end
|
98
110
|
|
99
|
-
|
100
|
-
#
|
101
|
-
#
|
102
|
-
|
103
|
-
|
111
|
+
# @overload default_connection
|
112
|
+
# Gets the default connection used for simple scripts.
|
113
|
+
# @return [Faraday::Connection] a connection configured with
|
114
|
+
# the default_adapter.
|
115
|
+
# @overload default_connection=(connection)
|
116
|
+
# @param connection [Faraday::Connection]
|
117
|
+
# Sets the default {Faraday::Connection} for simple scripts that
|
118
|
+
# access the Faraday constant directly, such as
|
119
|
+
# <code>Faraday.get "https://faraday.com"</code>.
|
120
|
+
def default_connection
|
121
|
+
@default_connection ||= Connection.new(default_connection_options)
|
104
122
|
end
|
105
|
-
end
|
106
|
-
|
107
|
-
self.ignore_env_proxy = false
|
108
|
-
self.root_path = File.expand_path "..", __FILE__
|
109
|
-
self.lib_path = File.expand_path "../faraday", __FILE__
|
110
|
-
self.default_adapter = :net_http
|
111
|
-
|
112
|
-
# Gets the default connection used for simple scripts.
|
113
|
-
#
|
114
|
-
# Returns a Faraday::Connection, configured with the #default_adapter.
|
115
|
-
def self.default_connection
|
116
|
-
@default_connection ||= Connection.new(default_connection_options)
|
117
|
-
end
|
118
|
-
|
119
|
-
# Gets the default connection options used when calling Faraday#new.
|
120
|
-
#
|
121
|
-
# Returns a Faraday::ConnectionOptions.
|
122
|
-
def self.default_connection_options
|
123
|
-
@default_connection_options ||= ConnectionOptions.new
|
124
|
-
end
|
125
123
|
|
126
|
-
|
127
|
-
def self.default_connection_options=(options)
|
128
|
-
@default_connection = nil
|
129
|
-
@default_connection_options = ConnectionOptions.from(options)
|
130
|
-
end
|
131
|
-
|
132
|
-
unless const_defined? :Timer
|
133
|
-
require 'timeout'
|
134
|
-
Timer = Timeout
|
135
|
-
end
|
136
|
-
|
137
|
-
# Public: Adds the ability for other modules to register and lookup
|
138
|
-
# middleware classes.
|
139
|
-
module MiddlewareRegistry
|
140
|
-
# Public: Register middleware class(es) on the current module.
|
141
|
-
#
|
142
|
-
# mapping - A Hash mapping Symbol keys to classes. Classes can be expressed
|
143
|
-
# as fully qualified constant, or a Proc that will be lazily
|
144
|
-
# called to return the former.
|
145
|
-
#
|
146
|
-
# Examples
|
147
|
-
#
|
148
|
-
# module Faraday
|
149
|
-
# class Whatever
|
150
|
-
# # Middleware looked up by :foo returns Faraday::Whatever::Foo.
|
151
|
-
# register_middleware :foo => Foo
|
152
|
-
#
|
153
|
-
# # Middleware looked up by :bar returns Faraday::Whatever.const_get(:Bar)
|
154
|
-
# register_middleware :bar => :Bar
|
124
|
+
# Gets the default connection options used when calling {Faraday#new}.
|
155
125
|
#
|
156
|
-
#
|
157
|
-
|
158
|
-
|
159
|
-
# end
|
160
|
-
#
|
161
|
-
# Returns nothing.
|
162
|
-
def register_middleware(autoload_path = nil, mapping = nil)
|
163
|
-
if mapping.nil?
|
164
|
-
mapping = autoload_path
|
165
|
-
autoload_path = nil
|
166
|
-
end
|
167
|
-
middleware_mutex do
|
168
|
-
@middleware_autoload_path = autoload_path if autoload_path
|
169
|
-
(@registered_middleware ||= {}).update(mapping)
|
170
|
-
end
|
126
|
+
# @return [Faraday::ConnectionOptions]
|
127
|
+
def default_connection_options
|
128
|
+
@default_connection_options ||= ConnectionOptions.new
|
171
129
|
end
|
172
130
|
|
173
|
-
#
|
174
|
-
#
|
175
|
-
# key - The Symbol key for the registered middleware.
|
176
|
-
#
|
177
|
-
# Examples
|
178
|
-
#
|
179
|
-
# module Faraday
|
180
|
-
# class Whatever
|
181
|
-
# register_middleware :foo => Foo
|
182
|
-
# end
|
183
|
-
# end
|
184
|
-
#
|
185
|
-
# Faraday::Whatever.lookup_middleware(:foo)
|
186
|
-
# # => Faraday::Whatever::Foo
|
131
|
+
# Sets the default options used when calling {Faraday#new}.
|
187
132
|
#
|
188
|
-
#
|
189
|
-
def
|
190
|
-
|
191
|
-
|
133
|
+
# @param options [Hash, Faraday::ConnectionOptions]
|
134
|
+
def default_connection_options=(options)
|
135
|
+
@default_connection = nil
|
136
|
+
@default_connection_options = ConnectionOptions.from(options)
|
192
137
|
end
|
193
138
|
|
194
|
-
|
195
|
-
@middleware_mutex ||= begin
|
196
|
-
require 'monitor'
|
197
|
-
Monitor.new
|
198
|
-
end
|
199
|
-
@middleware_mutex.synchronize(&block)
|
200
|
-
end
|
139
|
+
private
|
201
140
|
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
when Module
|
210
|
-
value
|
211
|
-
when Symbol, String
|
212
|
-
middleware_mutex do
|
213
|
-
@registered_middleware[key] = const_get(value)
|
214
|
-
end
|
215
|
-
when Proc
|
216
|
-
middleware_mutex do
|
217
|
-
@registered_middleware[key] = value.call
|
218
|
-
end
|
219
|
-
when Array
|
220
|
-
middleware_mutex do
|
221
|
-
const, path = value
|
222
|
-
if root = @middleware_autoload_path
|
223
|
-
path = "#{root}/#{path}"
|
224
|
-
end
|
225
|
-
require(path)
|
226
|
-
@registered_middleware[key] = const
|
227
|
-
end
|
228
|
-
load_middleware(key)
|
141
|
+
# Internal: Proxies method calls on the Faraday constant to
|
142
|
+
# .default_connection.
|
143
|
+
def method_missing(name, *args, &block)
|
144
|
+
if default_connection.respond_to?(name)
|
145
|
+
default_connection.send(name, *args, &block)
|
146
|
+
else
|
147
|
+
super
|
229
148
|
end
|
230
149
|
end
|
231
150
|
end
|
232
151
|
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
super
|
239
|
-
end
|
240
|
-
end
|
241
|
-
|
242
|
-
require_libs "utils", "options", "connection", "rack_builder", "parameters",
|
243
|
-
"middleware", "adapter", "request", "response", "upload_io", "error"
|
244
|
-
|
245
|
-
if !ENV["FARADAY_NO_AUTOLOAD"]
|
246
|
-
require_lib 'autoload'
|
247
|
-
end
|
152
|
+
self.ignore_env_proxy = false
|
153
|
+
self.root_path = File.expand_path __dir__
|
154
|
+
self.lib_path = File.expand_path 'faraday', __dir__
|
155
|
+
self.default_adapter = :net_http
|
156
|
+
self.default_adapter_options = {}
|
248
157
|
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
|