faraday 1.0.0 → 1.10.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +104 -0
- data/LICENSE.md +1 -1
- data/README.md +4 -6
- data/examples/client_spec.rb +35 -3
- data/examples/client_test.rb +41 -2
- data/lib/faraday/adapter/test.rb +59 -43
- data/lib/faraday/adapter/typhoeus.rb +1 -1
- data/lib/faraday/adapter.rb +2 -12
- data/lib/faraday/adapter_registry.rb +3 -1
- data/lib/faraday/autoload.rb +2 -10
- data/lib/faraday/connection.rb +37 -9
- data/lib/faraday/dependency_loader.rb +3 -1
- data/lib/faraday/deprecate.rb +110 -0
- data/lib/faraday/encoders/flat_params_encoder.rb +9 -2
- data/lib/faraday/encoders/nested_params_encoder.rb +7 -2
- data/lib/faraday/error.rb +20 -6
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +14 -4
- data/lib/faraday/options/proxy_options.rb +4 -0
- data/lib/faraday/options.rb +4 -8
- data/lib/faraday/rack_builder.rb +13 -12
- data/lib/faraday/request/authorization.rb +17 -8
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request/url_encoded.rb +3 -1
- data/lib/faraday/request.rb +19 -11
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +2 -4
- data/lib/faraday/response/raise_error.rb +12 -1
- data/lib/faraday/response.rb +7 -8
- data/lib/faraday/utils/headers.rb +2 -2
- data/lib/faraday/utils.rb +11 -3
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +67 -40
- data/spec/faraday/adapter/em_http_spec.rb +39 -37
- data/spec/faraday/adapter/em_synchrony_spec.rb +11 -9
- data/spec/faraday/adapter/patron_spec.rb +1 -1
- data/spec/faraday/adapter/test_spec.rb +377 -0
- data/spec/faraday/connection_spec.rb +45 -0
- data/spec/faraday/deprecate_spec.rb +147 -0
- data/spec/faraday/error_spec.rb +15 -0
- data/spec/faraday/middleware_spec.rb +32 -6
- data/spec/faraday/options/proxy_options_spec.rb +7 -0
- data/spec/faraday/params_encoders/flat_spec.rb +8 -0
- data/spec/faraday/params_encoders/nested_spec.rb +8 -0
- data/spec/faraday/rack_builder_spec.rb +150 -1
- data/spec/faraday/request/authorization_spec.rb +10 -2
- data/spec/faraday/request/json_spec.rb +111 -0
- data/spec/faraday/request/url_encoded_spec.rb +13 -0
- data/spec/faraday/request_spec.rb +16 -5
- data/spec/faraday/response/json_spec.rb +119 -0
- data/spec/faraday/response/middleware_spec.rb +16 -0
- data/spec/faraday/response/raise_error_spec.rb +63 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/support/shared_examples/adapter.rb +2 -1
- data/spec/support/shared_examples/request_method.rb +39 -11
- metadata +157 -31
- data/UPGRADING.md +0 -55
- data/lib/faraday/adapter/em_http.rb +0 -285
- data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -62
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -69
- data/lib/faraday/adapter/em_synchrony.rb +0 -150
- data/lib/faraday/adapter/excon.rb +0 -124
- data/lib/faraday/adapter/httpclient.rb +0 -151
- data/lib/faraday/adapter/net_http.rb +0 -209
- data/lib/faraday/adapter/net_http_persistent.rb +0 -91
- data/lib/faraday/adapter/patron.rb +0 -132
- data/lib/faraday/adapter/rack.rb +0 -75
- data/lib/faraday/file_part.rb +0 -128
- data/lib/faraday/param_part.rb +0 -53
- data/lib/faraday/request/multipart.rb +0 -99
- data/lib/faraday/request/retry.rb +0 -239
- data/spec/faraday/adapter/net_http_persistent_spec.rb +0 -57
- data/spec/faraday/request/multipart_spec.rb +0 -274
- data/spec/faraday/request/retry_spec.rb +0 -242
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'forwardable'
|
4
|
+
require 'logger'
|
4
5
|
require 'faraday/logging/formatter'
|
5
6
|
|
6
7
|
module Faraday
|
@@ -11,10 +12,7 @@ module Faraday
|
|
11
12
|
class Logger < Middleware
|
12
13
|
def initialize(app, logger = nil, options = {})
|
13
14
|
super(app)
|
14
|
-
logger ||=
|
15
|
-
require 'logger'
|
16
|
-
::Logger.new($stdout)
|
17
|
-
end
|
15
|
+
logger ||= ::Logger.new($stdout)
|
18
16
|
formatter_class = options.delete(:formatter) || Logging::Formatter
|
19
17
|
@formatter = formatter_class.new(logger: logger, options: options)
|
20
18
|
yield @formatter if block_given?
|
@@ -38,7 +38,18 @@ module Faraday
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def response_values(env)
|
41
|
-
{
|
41
|
+
{
|
42
|
+
status: env.status,
|
43
|
+
headers: env.response_headers,
|
44
|
+
body: env.body,
|
45
|
+
request: {
|
46
|
+
method: env.method,
|
47
|
+
url_path: env.url.path,
|
48
|
+
params: env.params,
|
49
|
+
headers: env.request_headers,
|
50
|
+
body: env.request_body
|
51
|
+
}
|
52
|
+
}
|
42
53
|
end
|
43
54
|
end
|
44
55
|
end
|
data/lib/faraday/response.rb
CHANGED
@@ -7,16 +7,13 @@ module Faraday
|
|
7
7
|
class Response
|
8
8
|
# Used for simple response middleware.
|
9
9
|
class Middleware < Faraday::Middleware
|
10
|
-
def call(env)
|
11
|
-
@app.call(env).on_complete do |environment|
|
12
|
-
on_complete(environment)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
10
|
# Override this to modify the environment after the response has finished.
|
17
11
|
# Calls the `parse` method if defined
|
12
|
+
# `parse` method can be defined as private, public and protected
|
18
13
|
def on_complete(env)
|
19
|
-
|
14
|
+
return unless respond_to?(:parse, true) && env.parse_body?
|
15
|
+
|
16
|
+
env.body = parse(env.body)
|
20
17
|
end
|
21
18
|
end
|
22
19
|
|
@@ -25,7 +22,8 @@ module Faraday
|
|
25
22
|
|
26
23
|
register_middleware File.expand_path('response', __dir__),
|
27
24
|
raise_error: [:RaiseError, 'raise_error'],
|
28
|
-
logger: [:Logger, 'logger']
|
25
|
+
logger: [:Logger, 'logger'],
|
26
|
+
json: [:Json, 'json']
|
29
27
|
|
30
28
|
def initialize(env = nil)
|
31
29
|
@env = Env.from(env) if env
|
@@ -45,6 +43,7 @@ module Faraday
|
|
45
43
|
def headers
|
46
44
|
finished? ? env.response_headers : {}
|
47
45
|
end
|
46
|
+
|
48
47
|
def_delegator :headers, :[]
|
49
48
|
|
50
49
|
def body
|
@@ -105,7 +105,7 @@ module Faraday
|
|
105
105
|
end
|
106
106
|
|
107
107
|
def to_hash
|
108
|
-
|
108
|
+
{}.update(self)
|
109
109
|
end
|
110
110
|
|
111
111
|
def parse(header_string)
|
@@ -114,7 +114,7 @@ module Faraday
|
|
114
114
|
headers = header_string.split(/\r\n/)
|
115
115
|
|
116
116
|
# Find the last set of response headers.
|
117
|
-
start_index = headers.rindex { |x| x.
|
117
|
+
start_index = headers.rindex { |x| x.start_with?('HTTP/') } || 0
|
118
118
|
last_response = headers.slice(start_index, headers.size)
|
119
119
|
|
120
120
|
last_response
|
data/lib/faraday/utils.rb
CHANGED
@@ -16,12 +16,20 @@ module Faraday
|
|
16
16
|
NestedParamsEncoder.encode(params)
|
17
17
|
end
|
18
18
|
|
19
|
+
def default_space_encoding
|
20
|
+
@default_space_encoding ||= '+'
|
21
|
+
end
|
22
|
+
|
23
|
+
class << self
|
24
|
+
attr_writer :default_space_encoding
|
25
|
+
end
|
26
|
+
|
19
27
|
ESCAPE_RE = /[^a-zA-Z0-9 .~_-]/.freeze
|
20
28
|
|
21
29
|
def escape(str)
|
22
30
|
str.to_s.gsub(ESCAPE_RE) do |match|
|
23
|
-
|
24
|
-
end.
|
31
|
+
"%#{match.unpack('H2' * match.bytesize).join('%').upcase}"
|
32
|
+
end.gsub(' ', default_space_encoding)
|
25
33
|
end
|
26
34
|
|
27
35
|
def unescape(str)
|
@@ -81,7 +89,7 @@ module Faraday
|
|
81
89
|
# the path with the query string sorted.
|
82
90
|
def normalize_path(url)
|
83
91
|
url = URI(url)
|
84
|
-
(url.path.start_with?('/') ? url.path :
|
92
|
+
(url.path.start_with?('/') ? url.path : "/#{url.path}") +
|
85
93
|
(url.query ? "?#{sort_query_params(url.query)}" : '')
|
86
94
|
end
|
87
95
|
|
data/lib/faraday.rb
CHANGED
@@ -1,11 +1,49 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'cgi'
|
4
|
+
require 'date'
|
4
5
|
require 'set'
|
5
6
|
require 'forwardable'
|
6
7
|
require 'faraday/middleware_registry'
|
7
8
|
require 'faraday/dependency_loader'
|
8
9
|
|
10
|
+
unless defined?(::Faraday::Timer)
|
11
|
+
require 'timeout'
|
12
|
+
::Faraday::Timer = Timeout
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'faraday/version'
|
16
|
+
require 'faraday/methods'
|
17
|
+
require 'faraday/utils'
|
18
|
+
require 'faraday/options'
|
19
|
+
require 'faraday/connection'
|
20
|
+
require 'faraday/rack_builder'
|
21
|
+
require 'faraday/parameters'
|
22
|
+
require 'faraday/middleware'
|
23
|
+
require 'faraday/adapter'
|
24
|
+
require 'faraday/request'
|
25
|
+
require 'faraday/response'
|
26
|
+
require 'faraday/error'
|
27
|
+
require 'faraday/request/url_encoded' # needed by multipart
|
28
|
+
|
29
|
+
# External Middleware gems and their aliases
|
30
|
+
require 'faraday/multipart'
|
31
|
+
require 'faraday/retry'
|
32
|
+
Faraday::Request::Multipart = Faraday::Multipart::Middleware
|
33
|
+
Faraday::Request::Retry = Faraday::Retry::Middleware
|
34
|
+
|
35
|
+
# External Adapters gems
|
36
|
+
unless defined?(JRUBY_VERSION)
|
37
|
+
require 'faraday/em_http'
|
38
|
+
require 'faraday/em_synchrony'
|
39
|
+
end
|
40
|
+
require 'faraday/excon'
|
41
|
+
require 'faraday/httpclient'
|
42
|
+
require 'faraday/net_http'
|
43
|
+
require 'faraday/net_http_persistent'
|
44
|
+
require 'faraday/patron'
|
45
|
+
require 'faraday/rack'
|
46
|
+
|
9
47
|
# This is the main namespace for Faraday.
|
10
48
|
#
|
11
49
|
# It provides methods to create {Connection} objects, and HTTP-related
|
@@ -19,9 +57,7 @@ require 'faraday/dependency_loader'
|
|
19
57
|
# conn.get '/'
|
20
58
|
#
|
21
59
|
module Faraday
|
22
|
-
|
23
|
-
METHODS_WITH_QUERY = %w[get head delete trace].freeze
|
24
|
-
METHODS_WITH_BODY = %w[post put patch].freeze
|
60
|
+
CONTENT_TYPE = 'Content-Type'
|
25
61
|
|
26
62
|
class << self
|
27
63
|
# The root path that Faraday is being loaded from.
|
@@ -107,6 +143,34 @@ module Faraday
|
|
107
143
|
default_connection.respond_to?(symbol, include_private) || super
|
108
144
|
end
|
109
145
|
|
146
|
+
# @overload default_connection
|
147
|
+
# Gets the default connection used for simple scripts.
|
148
|
+
# @return [Faraday::Connection] a connection configured with
|
149
|
+
# the default_adapter.
|
150
|
+
# @overload default_connection=(connection)
|
151
|
+
# @param connection [Faraday::Connection]
|
152
|
+
# Sets the default {Faraday::Connection} for simple scripts that
|
153
|
+
# access the Faraday constant directly, such as
|
154
|
+
# <code>Faraday.get "https://faraday.com"</code>.
|
155
|
+
def default_connection
|
156
|
+
@default_connection ||= Connection.new(default_connection_options)
|
157
|
+
end
|
158
|
+
|
159
|
+
# Gets the default connection options used when calling {Faraday#new}.
|
160
|
+
#
|
161
|
+
# @return [Faraday::ConnectionOptions]
|
162
|
+
def default_connection_options
|
163
|
+
@default_connection_options ||= ConnectionOptions.new
|
164
|
+
end
|
165
|
+
|
166
|
+
# Sets the default options used when calling {Faraday#new}.
|
167
|
+
#
|
168
|
+
# @param options [Hash, Faraday::ConnectionOptions]
|
169
|
+
def default_connection_options=(options)
|
170
|
+
@default_connection = nil
|
171
|
+
@default_connection_options = ConnectionOptions.from(options)
|
172
|
+
end
|
173
|
+
|
110
174
|
private
|
111
175
|
|
112
176
|
# Internal: Proxies method calls on the Faraday constant to
|
@@ -125,42 +189,5 @@ module Faraday
|
|
125
189
|
self.lib_path = File.expand_path 'faraday', __dir__
|
126
190
|
self.default_adapter = :net_http
|
127
191
|
|
128
|
-
# @overload default_connection
|
129
|
-
# Gets the default connection used for simple scripts.
|
130
|
-
# @return [Faraday::Connection] a connection configured with
|
131
|
-
# the default_adapter.
|
132
|
-
# @overload default_connection=(connection)
|
133
|
-
# @param connection [Faraday::Connection]
|
134
|
-
# Sets the default {Faraday::Connection} for simple scripts that
|
135
|
-
# access the Faraday constant directly, such as
|
136
|
-
# <code>Faraday.get "https://faraday.com"</code>.
|
137
|
-
def self.default_connection
|
138
|
-
@default_connection ||= Connection.new(default_connection_options)
|
139
|
-
end
|
140
|
-
|
141
|
-
# Gets the default connection options used when calling {Faraday#new}.
|
142
|
-
#
|
143
|
-
# @return [Faraday::ConnectionOptions]
|
144
|
-
def self.default_connection_options
|
145
|
-
@default_connection_options ||= ConnectionOptions.new
|
146
|
-
end
|
147
|
-
|
148
|
-
# Sets the default options used when calling {Faraday#new}.
|
149
|
-
#
|
150
|
-
# @param options [Hash, Faraday::ConnectionOptions]
|
151
|
-
def self.default_connection_options=(options)
|
152
|
-
@default_connection = nil
|
153
|
-
@default_connection_options = ConnectionOptions.from(options)
|
154
|
-
end
|
155
|
-
|
156
|
-
unless const_defined? :Timer
|
157
|
-
require 'timeout'
|
158
|
-
Timer = Timeout
|
159
|
-
end
|
160
|
-
|
161
|
-
require_libs 'utils', 'options', 'connection', 'rack_builder', 'parameters',
|
162
|
-
'middleware', 'adapter', 'request', 'response', 'error',
|
163
|
-
'file_part', 'param_part'
|
164
|
-
|
165
192
|
require_lib 'autoload' unless ENV['FARADAY_NO_AUTOLOAD']
|
166
193
|
end
|
@@ -1,47 +1,49 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
unless defined?(JRUBY_VERSION)
|
4
|
+
RSpec.describe Faraday::Adapter::EMHttp do
|
5
|
+
features :request_body_on_query_methods, :reason_phrase_parse, :trace_method,
|
6
|
+
:skip_response_body_on_head, :parallel, :local_socket_binding
|
6
7
|
|
7
|
-
|
8
|
+
it_behaves_like 'an adapter'
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
it 'allows to provide adapter specific configs' do
|
11
|
+
url = URI('https://example.com:1234')
|
12
|
+
adapter = described_class.new nil, inactivity_timeout: 20
|
13
|
+
req = adapter.create_request(url: url, request: {})
|
13
14
|
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
context 'Options' do
|
18
|
-
let(:request) { Faraday::RequestOptions.new }
|
19
|
-
let(:env) { { request: request } }
|
20
|
-
let(:options) { {} }
|
21
|
-
let(:adapter) { Faraday::Adapter::EMHttp.new }
|
22
|
-
|
23
|
-
it 'configures timeout' do
|
24
|
-
request.timeout = 5
|
25
|
-
adapter.configure_timeout(options, env)
|
26
|
-
expect(options[:inactivity_timeout]).to eq(5)
|
27
|
-
expect(options[:connect_timeout]).to eq(5)
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'configures timeout and open_timeout' do
|
31
|
-
request.timeout = 5
|
32
|
-
request.open_timeout = 1
|
33
|
-
adapter.configure_timeout(options, env)
|
34
|
-
expect(options[:inactivity_timeout]).to eq(5)
|
35
|
-
expect(options[:connect_timeout]).to eq(1)
|
15
|
+
expect(req.connopts.inactivity_timeout).to eq(20)
|
36
16
|
end
|
37
17
|
|
38
|
-
|
39
|
-
request.
|
40
|
-
request
|
41
|
-
|
42
|
-
adapter.
|
43
|
-
|
44
|
-
|
18
|
+
context 'Options' do
|
19
|
+
let(:request) { Faraday::RequestOptions.new }
|
20
|
+
let(:env) { { request: request } }
|
21
|
+
let(:options) { {} }
|
22
|
+
let(:adapter) { Faraday::Adapter::EMHttp.new }
|
23
|
+
|
24
|
+
it 'configures timeout' do
|
25
|
+
request.timeout = 5
|
26
|
+
adapter.configure_timeout(options, env)
|
27
|
+
expect(options[:inactivity_timeout]).to eq(5)
|
28
|
+
expect(options[:connect_timeout]).to eq(5)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'configures timeout and open_timeout' do
|
32
|
+
request.timeout = 5
|
33
|
+
request.open_timeout = 1
|
34
|
+
adapter.configure_timeout(options, env)
|
35
|
+
expect(options[:inactivity_timeout]).to eq(5)
|
36
|
+
expect(options[:connect_timeout]).to eq(1)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'configures all timeout settings' do
|
40
|
+
request.timeout = 5
|
41
|
+
request.read_timeout = 3
|
42
|
+
request.open_timeout = 1
|
43
|
+
adapter.configure_timeout(options, env)
|
44
|
+
expect(options[:inactivity_timeout]).to eq(3)
|
45
|
+
expect(options[:connect_timeout]).to eq(1)
|
46
|
+
end
|
45
47
|
end
|
46
48
|
end
|
47
49
|
end
|
@@ -1,16 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
unless defined?(JRUBY_VERSION)
|
4
|
+
RSpec.describe Faraday::Adapter::EMSynchrony do
|
5
|
+
features :request_body_on_query_methods, :reason_phrase_parse,
|
6
|
+
:skip_response_body_on_head, :parallel, :local_socket_binding
|
6
7
|
|
7
|
-
|
8
|
+
it_behaves_like 'an adapter'
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
it 'allows to provide adapter specific configs' do
|
11
|
+
url = URI('https://example.com:1234')
|
12
|
+
adapter = described_class.new nil, inactivity_timeout: 20
|
13
|
+
req = adapter.create_request(url: url, request: {})
|
13
14
|
|
14
|
-
|
15
|
+
expect(req.connopts.inactivity_timeout).to eq(20)
|
16
|
+
end
|
15
17
|
end
|
16
18
|
end
|