faraday 1.6.0 → 1.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/examples/client_spec.rb +34 -2
- data/examples/client_test.rb +41 -2
- data/lib/faraday/adapter/test.rb +59 -43
- data/lib/faraday/connection.rb +23 -2
- data/lib/faraday/error.rb +0 -6
- data/lib/faraday/request/authorization.rb +14 -7
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request.rb +7 -8
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +2 -4
- data/lib/faraday/response.rb +3 -1
- data/lib/faraday/version.rb +1 -1
- data/lib/faraday.rb +10 -2
- data/spec/faraday/adapter/test_spec.rb +117 -0
- data/spec/faraday/request/authorization_spec.rb +8 -0
- data/spec/faraday/request/json_spec.rb +111 -0
- data/spec/faraday/response/json_spec.rb +119 -0
- metadata +30 -24
- data/lib/faraday/file_part.rb +0 -128
- data/lib/faraday/param_part.rb +0 -53
- data/lib/faraday/request/multipart.rb +0 -106
- data/lib/faraday/request/retry.rb +0 -239
- data/spec/faraday/request/multipart_spec.rb +0 -302
- data/spec/faraday/request/retry_spec.rb +0 -242
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc98f5f6c55ba3ed19497f21d89c09658d62712bca56983558b0eb3ce3aec955
|
4
|
+
data.tar.gz: 4460cd29073e33bd00d3bc7485d38da232125134d7ad8a12ae65fb775a412ca7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d86a269474244ea039af4c61fdd4c91f59fad6f1573b82ebe52b33d152f60fdb97ee2763d9419d1c59599a4a949e7ed527f5f45967ef084fcd9047bc2dfc3b0
|
7
|
+
data.tar.gz: 3d67a73d391d1c5d1c61aee31fd4ae2af73610b42e3559614c07cfd06faf735c1031200e2eb0138d8bfe507ee9ca97395b61e92ae6c9f7108fc537333960d4c7
|
data/examples/client_spec.rb
CHANGED
@@ -12,8 +12,8 @@ class Client
|
|
12
12
|
@conn = conn
|
13
13
|
end
|
14
14
|
|
15
|
-
def sushi(jname)
|
16
|
-
res = @conn.get("/#{jname}")
|
15
|
+
def sushi(jname, params: {})
|
16
|
+
res = @conn.get("/#{jname}", params)
|
17
17
|
data = JSON.parse(res.body)
|
18
18
|
data['name']
|
19
19
|
end
|
@@ -62,4 +62,36 @@ RSpec.describe Client do
|
|
62
62
|
expect { client.sushi('ebi') }.to raise_error(Faraday::ConnectionFailed)
|
63
63
|
stubs.verify_stubbed_calls
|
64
64
|
end
|
65
|
+
|
66
|
+
context 'When the test stub is run in strict_mode' do
|
67
|
+
let(:stubs) { Faraday::Adapter::Test::Stubs.new(strict_mode: true) }
|
68
|
+
|
69
|
+
it 'verifies the all parameter values are identical' do
|
70
|
+
stubs.get('/ebi?abc=123') do
|
71
|
+
[
|
72
|
+
200,
|
73
|
+
{ 'Content-Type': 'application/javascript' },
|
74
|
+
'{"name": "shrimp"}'
|
75
|
+
]
|
76
|
+
end
|
77
|
+
|
78
|
+
# uncomment to raise Stubs::NotFound
|
79
|
+
# expect(client.sushi('ebi', params: { abc: 123, foo: 'Kappa' })).to eq('shrimp')
|
80
|
+
expect(client.sushi('ebi', params: { abc: 123 })).to eq('shrimp')
|
81
|
+
stubs.verify_stubbed_calls
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'When the Faraday connection is configured with FlatParamsEncoder' do
|
86
|
+
let(:conn) { Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) { |b| b.adapter(:test, stubs) } }
|
87
|
+
|
88
|
+
it 'handles the same multiple URL parameters' do
|
89
|
+
stubs.get('/ebi?a=x&a=y&a=z') { [200, { 'Content-Type' => 'application/json' }, '{"name": "shrimp"}'] }
|
90
|
+
|
91
|
+
# uncomment to raise Stubs::NotFound
|
92
|
+
# expect(client.sushi('ebi', params: { a: %w[x y] })).to eq('shrimp')
|
93
|
+
expect(client.sushi('ebi', params: { a: %w[x y z] })).to eq('shrimp')
|
94
|
+
stubs.verify_stubbed_calls
|
95
|
+
end
|
96
|
+
end
|
65
97
|
end
|
data/examples/client_test.rb
CHANGED
@@ -13,8 +13,8 @@ class Client
|
|
13
13
|
@conn = conn
|
14
14
|
end
|
15
15
|
|
16
|
-
def sushi(jname)
|
17
|
-
res = @conn.get("/#{jname}")
|
16
|
+
def sushi(jname, params: {})
|
17
|
+
res = @conn.get("/#{jname}", params)
|
18
18
|
data = JSON.parse(res.body)
|
19
19
|
data['name']
|
20
20
|
end
|
@@ -70,6 +70,45 @@ class ClientTest < Test::Unit::TestCase
|
|
70
70
|
stubs.verify_stubbed_calls
|
71
71
|
end
|
72
72
|
|
73
|
+
def test_strict_mode
|
74
|
+
stubs = Faraday::Adapter::Test::Stubs.new(strict_mode: true)
|
75
|
+
stubs.get('/ebi?abc=123') do
|
76
|
+
[
|
77
|
+
200,
|
78
|
+
{ 'Content-Type': 'application/javascript' },
|
79
|
+
'{"name": "shrimp"}'
|
80
|
+
]
|
81
|
+
end
|
82
|
+
|
83
|
+
cli = client(stubs)
|
84
|
+
assert_equal 'shrimp', cli.sushi('ebi', params: { abc: 123 })
|
85
|
+
|
86
|
+
# uncomment to raise Stubs::NotFound
|
87
|
+
# assert_equal 'shrimp', cli.sushi('ebi', params: { abc: 123, foo: 'Kappa' })
|
88
|
+
stubs.verify_stubbed_calls
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_non_default_params_encoder
|
92
|
+
stubs = Faraday::Adapter::Test::Stubs.new(strict_mode: true)
|
93
|
+
stubs.get('/ebi?a=x&a=y&a=z') do
|
94
|
+
[
|
95
|
+
200,
|
96
|
+
{ 'Content-Type': 'application/javascript' },
|
97
|
+
'{"name": "shrimp"}'
|
98
|
+
]
|
99
|
+
end
|
100
|
+
conn = Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) do |builder|
|
101
|
+
builder.adapter :test, stubs
|
102
|
+
end
|
103
|
+
|
104
|
+
cli = Client.new(conn)
|
105
|
+
assert_equal 'shrimp', cli.sushi('ebi', params: { a: %w[x y z] })
|
106
|
+
|
107
|
+
# uncomment to raise Stubs::NotFound
|
108
|
+
# assert_equal 'shrimp', cli.sushi('ebi', params: { a: %w[x y] })
|
109
|
+
stubs.verify_stubbed_calls
|
110
|
+
end
|
111
|
+
|
73
112
|
def client(stubs)
|
74
113
|
conn = Faraday.new do |builder|
|
75
114
|
builder.adapter :test, stubs
|
data/lib/faraday/adapter/test.rb
CHANGED
@@ -25,6 +25,9 @@ module Faraday
|
|
25
25
|
# "showing item: #{meta[:match_data][1]}"
|
26
26
|
# ]
|
27
27
|
# end
|
28
|
+
#
|
29
|
+
# # You can set strict_mode to exactly match the stubbed requests.
|
30
|
+
# stub.strict_mode = true
|
28
31
|
# end
|
29
32
|
# end
|
30
33
|
#
|
@@ -47,10 +50,11 @@ module Faraday
|
|
47
50
|
class NotFound < StandardError
|
48
51
|
end
|
49
52
|
|
50
|
-
def initialize
|
53
|
+
def initialize(strict_mode: false)
|
51
54
|
# { get: [Stub, Stub] }
|
52
55
|
@stack = {}
|
53
56
|
@consumed = {}
|
57
|
+
@strict_mode = strict_mode
|
54
58
|
yield(self) if block_given?
|
55
59
|
end
|
56
60
|
|
@@ -58,18 +62,20 @@ module Faraday
|
|
58
62
|
@stack.empty?
|
59
63
|
end
|
60
64
|
|
61
|
-
|
65
|
+
# @param env [Faraday::Env]
|
66
|
+
def match(env)
|
67
|
+
request_method = env[:method]
|
62
68
|
return false unless @stack.key?(request_method)
|
63
69
|
|
64
70
|
stack = @stack[request_method]
|
65
71
|
consumed = (@consumed[request_method] ||= [])
|
66
72
|
|
67
|
-
stub, meta = matches?(stack,
|
73
|
+
stub, meta = matches?(stack, env)
|
68
74
|
if stub
|
69
75
|
consumed << stack.delete(stub)
|
70
76
|
return stub, meta
|
71
77
|
end
|
72
|
-
matches?(consumed,
|
78
|
+
matches?(consumed, env)
|
73
79
|
end
|
74
80
|
|
75
81
|
def get(path, headers = {}, &block)
|
@@ -115,6 +121,17 @@ module Faraday
|
|
115
121
|
raise failed_stubs.join(' ') unless failed_stubs.empty?
|
116
122
|
end
|
117
123
|
|
124
|
+
# Set strict_mode. If the value is true, this adapter tries to find matched requests strictly,
|
125
|
+
# which means that all of a path, parameters, and headers must be the same as an actual request.
|
126
|
+
def strict_mode=(value)
|
127
|
+
@strict_mode = value
|
128
|
+
@stack.each do |_method, stubs|
|
129
|
+
stubs.each do |stub|
|
130
|
+
stub.strict_mode = value
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
118
135
|
protected
|
119
136
|
|
120
137
|
def new_stub(request_method, path, headers = {}, body = nil, &block)
|
@@ -127,14 +144,18 @@ module Faraday
|
|
127
144
|
Faraday::Utils.URI(path).host
|
128
145
|
]
|
129
146
|
end
|
147
|
+
path, query = normalized_path.respond_to?(:split) ? normalized_path.split('?') : normalized_path
|
148
|
+
headers = Utils::Headers.new(headers)
|
130
149
|
|
131
|
-
stub = Stub.new(host,
|
150
|
+
stub = Stub.new(host, path, query, headers, body, @strict_mode, block)
|
132
151
|
(@stack[request_method] ||= []) << stub
|
133
152
|
end
|
134
153
|
|
135
|
-
|
154
|
+
# @param stack [Hash]
|
155
|
+
# @param env [Faraday::Env]
|
156
|
+
def matches?(stack, env)
|
136
157
|
stack.each do |stub|
|
137
|
-
match_result, meta = stub.matches?(
|
158
|
+
match_result, meta = stub.matches?(env)
|
138
159
|
return stub, meta if match_result
|
139
160
|
end
|
140
161
|
nil
|
@@ -142,35 +163,20 @@ module Faraday
|
|
142
163
|
end
|
143
164
|
|
144
165
|
# Stub request
|
145
|
-
# rubocop:disable Style/StructInheritance
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
Faraday::Utils.parse_nested_query(query)
|
153
|
-
else
|
154
|
-
{}
|
155
|
-
end
|
156
|
-
|
157
|
-
super(host, path, params, headers, body, block)
|
158
|
-
end
|
166
|
+
class Stub < Struct.new(:host, :path, :query, :headers, :body, :strict_mode, :block) # rubocop:disable Style/StructInheritance
|
167
|
+
# @param env [Faraday::Env]
|
168
|
+
def matches?(env)
|
169
|
+
request_host = env[:url].host
|
170
|
+
request_path = Faraday::Utils.normalize_path(env[:url].path)
|
171
|
+
request_headers = env.request_headers
|
172
|
+
request_body = env[:body]
|
159
173
|
|
160
|
-
def matches?(request_host, request_uri, request_headers, request_body)
|
161
|
-
request_path, request_query = request_uri.split('?')
|
162
|
-
request_params =
|
163
|
-
if request_query
|
164
|
-
Faraday::Utils.parse_nested_query(request_query)
|
165
|
-
else
|
166
|
-
{}
|
167
|
-
end
|
168
174
|
# meta is a hash used as carrier
|
169
175
|
# that will be yielded to consumer block
|
170
176
|
meta = {}
|
171
177
|
[(host.nil? || host == request_host) &&
|
172
178
|
path_match?(request_path, meta) &&
|
173
|
-
params_match?(
|
179
|
+
params_match?(env) &&
|
174
180
|
(body.to_s.size.zero? || request_body == body) &&
|
175
181
|
headers_match?(request_headers), meta]
|
176
182
|
end
|
@@ -183,13 +189,30 @@ module Faraday
|
|
183
189
|
end
|
184
190
|
end
|
185
191
|
|
186
|
-
|
192
|
+
# @param env [Faraday::Env]
|
193
|
+
def params_match?(env)
|
194
|
+
request_params = env[:params]
|
195
|
+
params = env.params_encoder.decode(query) || {}
|
196
|
+
|
197
|
+
if strict_mode
|
198
|
+
return Set.new(params) == Set.new(request_params)
|
199
|
+
end
|
200
|
+
|
187
201
|
params.keys.all? do |key|
|
188
202
|
request_params[key] == params[key]
|
189
203
|
end
|
190
204
|
end
|
191
205
|
|
192
206
|
def headers_match?(request_headers)
|
207
|
+
if strict_mode
|
208
|
+
headers_with_user_agent = headers.dup.tap do |hs|
|
209
|
+
# NOTE: Set User-Agent in case it's not set when creating Stubs.
|
210
|
+
# Users would not want to set Faraday's User-Agent explicitly.
|
211
|
+
hs[:user_agent] ||= Connection::USER_AGENT
|
212
|
+
end
|
213
|
+
return Set.new(headers_with_user_agent) == Set.new(request_headers)
|
214
|
+
end
|
215
|
+
|
193
216
|
headers.keys.all? do |key|
|
194
217
|
request_headers[key] == headers[key]
|
195
218
|
end
|
@@ -210,26 +233,19 @@ module Faraday
|
|
210
233
|
yield(stubs)
|
211
234
|
end
|
212
235
|
|
236
|
+
# @param env [Faraday::Env]
|
213
237
|
def call(env)
|
214
238
|
super
|
215
|
-
host = env[:url].host
|
216
|
-
normalized_path = Faraday::Utils.normalize_path(env[:url])
|
217
|
-
params_encoder = env.request.params_encoder ||
|
218
|
-
Faraday::Utils.default_params_encoder
|
219
239
|
|
220
|
-
|
221
|
-
|
240
|
+
env.request.params_encoder ||= Faraday::Utils.default_params_encoder
|
241
|
+
env[:params] = env.params_encoder.decode(env[:url].query) || {}
|
242
|
+
stub, meta = stubs.match(env)
|
222
243
|
|
223
244
|
unless stub
|
224
245
|
raise Stubs::NotFound, "no stubbed request for #{env[:method]} "\
|
225
|
-
"#{
|
246
|
+
"#{env[:url]} #{env[:body]}"
|
226
247
|
end
|
227
248
|
|
228
|
-
env[:params] = if (query = env[:url].query)
|
229
|
-
params_encoder.decode(query)
|
230
|
-
else
|
231
|
-
{}
|
232
|
-
end
|
233
249
|
block_arity = stub.block.arity
|
234
250
|
status, headers, body =
|
235
251
|
if block_arity >= 0
|
data/lib/faraday/connection.rb
CHANGED
@@ -15,6 +15,7 @@ module Faraday
|
|
15
15
|
class Connection
|
16
16
|
# A Set of allowed HTTP verbs.
|
17
17
|
METHODS = Set.new %i[get post put delete head patch options trace]
|
18
|
+
USER_AGENT = "Faraday v#{VERSION}"
|
18
19
|
|
19
20
|
# @return [Hash] URI query unencoded key/value pairs.
|
20
21
|
attr_reader :params
|
@@ -89,7 +90,7 @@ module Faraday
|
|
89
90
|
|
90
91
|
yield(self) if block_given?
|
91
92
|
|
92
|
-
@headers[:user_agent] ||=
|
93
|
+
@headers[:user_agent] ||= USER_AGENT
|
93
94
|
end
|
94
95
|
|
95
96
|
def initialize_proxy(url, options)
|
@@ -296,6 +297,11 @@ module Faraday
|
|
296
297
|
#
|
297
298
|
# @return [void]
|
298
299
|
def basic_auth(login, pass)
|
300
|
+
warn <<~TEXT
|
301
|
+
WARNING: `Faraday::Connection#basic_auth` is deprecated; it will be removed in version 2.0.
|
302
|
+
While initializing your connection, use `#request(:basic_auth, ...)` instead.
|
303
|
+
See https://lostisland.github.io/faraday/middleware/authentication for more usage info.
|
304
|
+
TEXT
|
299
305
|
set_authorization_header(:basic_auth, login, pass)
|
300
306
|
end
|
301
307
|
|
@@ -313,6 +319,11 @@ module Faraday
|
|
313
319
|
#
|
314
320
|
# @return [void]
|
315
321
|
def token_auth(token, options = nil)
|
322
|
+
warn <<~TEXT
|
323
|
+
WARNING: `Faraday::Connection#token_auth` is deprecated; it will be removed in version 2.0.
|
324
|
+
While initializing your connection, use `#request(:token_auth, ...)` instead.
|
325
|
+
See https://lostisland.github.io/faraday/middleware/authentication for more usage info.
|
326
|
+
TEXT
|
316
327
|
set_authorization_header(:token_auth, token, options)
|
317
328
|
end
|
318
329
|
|
@@ -335,6 +346,11 @@ module Faraday
|
|
335
346
|
#
|
336
347
|
# @return [void]
|
337
348
|
def authorization(type, token)
|
349
|
+
warn <<~TEXT
|
350
|
+
WARNING: `Faraday::Connection#authorization` is deprecated; it will be removed in version 2.0.
|
351
|
+
While initializing your connection, use `#request(:authorization, ...)` instead.
|
352
|
+
See https://lostisland.github.io/faraday/middleware/authentication for more usage info.
|
353
|
+
TEXT
|
338
354
|
set_authorization_header(:authorization, type, token)
|
339
355
|
end
|
340
356
|
|
@@ -417,13 +433,18 @@ module Faraday
|
|
417
433
|
uri.query = nil
|
418
434
|
|
419
435
|
with_uri_credentials(uri) do |user, password|
|
420
|
-
|
436
|
+
set_basic_auth(user, password)
|
421
437
|
uri.user = uri.password = nil
|
422
438
|
end
|
423
439
|
|
424
440
|
@proxy = proxy_from_env(url) unless @manual_proxy
|
425
441
|
end
|
426
442
|
|
443
|
+
def set_basic_auth(user, password)
|
444
|
+
header = Faraday::Request::BasicAuthentication.header(user, password)
|
445
|
+
headers[Faraday::Request::Authorization::KEY] = header
|
446
|
+
end
|
447
|
+
|
427
448
|
# Sets the path prefix and ensures that it always has a leading
|
428
449
|
# slash.
|
429
450
|
#
|
data/lib/faraday/error.rb
CHANGED
@@ -143,10 +143,4 @@ module Faraday
|
|
143
143
|
# Raised by FaradayMiddleware::ResponseMiddleware
|
144
144
|
class ParsingError < Error
|
145
145
|
end
|
146
|
-
|
147
|
-
# Exception used to control the Retry middleware.
|
148
|
-
#
|
149
|
-
# @see Faraday::Request::Retry
|
150
|
-
class RetriableResponse < Error
|
151
|
-
end
|
152
146
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'base64'
|
4
|
+
|
3
5
|
module Faraday
|
4
6
|
class Request
|
5
7
|
# Request middleware for the Authorization HTTP header
|
@@ -13,7 +15,8 @@ module Faraday
|
|
13
15
|
# @return [String] a header value
|
14
16
|
def self.header(type, token)
|
15
17
|
case token
|
16
|
-
when String, Symbol
|
18
|
+
when String, Symbol, Proc
|
19
|
+
token = token.call if token.is_a?(Proc)
|
17
20
|
"#{type} #{token}"
|
18
21
|
when Hash
|
19
22
|
build_hash(type.to_s, token)
|
@@ -32,6 +35,7 @@ module Faraday
|
|
32
35
|
comma = ', '
|
33
36
|
values = []
|
34
37
|
hash.each do |key, value|
|
38
|
+
value = value.call if value.is_a?(Proc)
|
35
39
|
values << "#{key}=#{value.to_s.inspect}"
|
36
40
|
end
|
37
41
|
"#{type} #{values * comma}"
|
@@ -39,16 +43,19 @@ module Faraday
|
|
39
43
|
|
40
44
|
# @param app [#call]
|
41
45
|
# @param type [String, Symbol] Type of Authorization
|
42
|
-
# @param
|
43
|
-
|
44
|
-
|
46
|
+
# @param param [String, Symbol, Hash, Proc] parameter to build the Authorization header.
|
47
|
+
# This value can be a proc, in which case it will be invoked on each request.
|
48
|
+
def initialize(app, type, param)
|
49
|
+
@type = type
|
50
|
+
@param = param
|
45
51
|
super(app)
|
46
52
|
end
|
47
53
|
|
48
54
|
# @param env [Faraday::Env]
|
49
|
-
def
|
50
|
-
|
51
|
-
|
55
|
+
def on_request(env)
|
56
|
+
return if env.request_headers[KEY]
|
57
|
+
|
58
|
+
env.request_headers[KEY] = self.class.header(@type, @param)
|
52
59
|
end
|
53
60
|
end
|
54
61
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Faraday
|
6
|
+
class Request
|
7
|
+
# Request middleware that encodes the body as JSON.
|
8
|
+
#
|
9
|
+
# Processes only requests with matching Content-type or those without a type.
|
10
|
+
# If a request doesn't have a type but has a body, it sets the Content-type
|
11
|
+
# to JSON MIME-type.
|
12
|
+
#
|
13
|
+
# Doesn't try to encode bodies that already are in string form.
|
14
|
+
class Json < Middleware
|
15
|
+
MIME_TYPE = 'application/json'
|
16
|
+
MIME_TYPE_REGEX = %r{^application/(vnd\..+\+)?json$}.freeze
|
17
|
+
|
18
|
+
def on_request(env)
|
19
|
+
match_content_type(env) do |data|
|
20
|
+
env[:body] = encode(data)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def encode(data)
|
27
|
+
::JSON.generate(data)
|
28
|
+
end
|
29
|
+
|
30
|
+
def match_content_type(env)
|
31
|
+
return unless process_request?(env)
|
32
|
+
|
33
|
+
env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
|
34
|
+
yield env[:body] unless env[:body].respond_to?(:to_str)
|
35
|
+
end
|
36
|
+
|
37
|
+
def process_request?(env)
|
38
|
+
type = request_type(env)
|
39
|
+
body?(env) && (type.empty? || type.match?(MIME_TYPE_REGEX))
|
40
|
+
end
|
41
|
+
|
42
|
+
def body?(env)
|
43
|
+
(body = env[:body]) && !(body.respond_to?(:to_str) && body.empty?)
|
44
|
+
end
|
45
|
+
|
46
|
+
def request_type(env)
|
47
|
+
type = env[:request_headers][CONTENT_TYPE].to_s
|
48
|
+
type = type.split(';', 2).first if type.index(';')
|
49
|
+
type
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
Faraday::Request.register_middleware(json: Faraday::Request::Json)
|
data/lib/faraday/request.rb
CHANGED
@@ -35,8 +35,6 @@ module Faraday
|
|
35
35
|
|
36
36
|
register_middleware File.expand_path('request', __dir__),
|
37
37
|
url_encoded: [:UrlEncoded, 'url_encoded'],
|
38
|
-
multipart: [:Multipart, 'multipart'],
|
39
|
-
retry: [:Retry, 'retry'],
|
40
38
|
authorization: [:Authorization, 'authorization'],
|
41
39
|
basic_auth: [
|
42
40
|
:BasicAuthentication,
|
@@ -46,7 +44,8 @@ module Faraday
|
|
46
44
|
:TokenAuthentication,
|
47
45
|
'token_authentication'
|
48
46
|
],
|
49
|
-
instrumentation: [:Instrumentation, 'instrumentation']
|
47
|
+
instrumentation: [:Instrumentation, 'instrumentation'],
|
48
|
+
json: [:Json, 'json']
|
50
49
|
|
51
50
|
# @param request_method [String]
|
52
51
|
# @yield [request] for block customization, if block given
|
@@ -140,11 +139,11 @@ module Faraday
|
|
140
139
|
# @param serialised [Hash] the serialised object.
|
141
140
|
def marshal_load(serialised)
|
142
141
|
self.http_method = serialised[:http_method]
|
143
|
-
self.body
|
144
|
-
self.headers
|
145
|
-
self.path
|
146
|
-
self.params
|
147
|
-
self.options
|
142
|
+
self.body = serialised[:body]
|
143
|
+
self.headers = serialised[:headers]
|
144
|
+
self.path = serialised[:path]
|
145
|
+
self.params = serialised[:params]
|
146
|
+
self.options = serialised[:options]
|
148
147
|
end
|
149
148
|
|
150
149
|
# @return [Env] the Env for this Request
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Faraday
|
6
|
+
class Response
|
7
|
+
# Parse response bodies as JSON.
|
8
|
+
class Json < Middleware
|
9
|
+
def initialize(app = nil, options = {})
|
10
|
+
super(app)
|
11
|
+
@parser_options = options[:parser_options]
|
12
|
+
@content_types = Array(options[:content_type] || /\bjson$/)
|
13
|
+
@preserve_raw = options[:preserve_raw]
|
14
|
+
end
|
15
|
+
|
16
|
+
def on_complete(env)
|
17
|
+
process_response(env) if parse_response?(env)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def process_response(env)
|
23
|
+
env[:raw_body] = env[:body] if @preserve_raw
|
24
|
+
env[:body] = parse(env[:body])
|
25
|
+
rescue StandardError, SyntaxError => e
|
26
|
+
raise Faraday::ParsingError.new(e, env[:response])
|
27
|
+
end
|
28
|
+
|
29
|
+
def parse(body)
|
30
|
+
::JSON.parse(body, @parser_options || {}) unless body.strip.empty?
|
31
|
+
end
|
32
|
+
|
33
|
+
def parse_response?(env)
|
34
|
+
process_response_type?(env) &&
|
35
|
+
env[:body].respond_to?(:to_str)
|
36
|
+
end
|
37
|
+
|
38
|
+
def process_response_type?(env)
|
39
|
+
type = response_type(env)
|
40
|
+
@content_types.empty? || @content_types.any? do |pattern|
|
41
|
+
pattern.is_a?(Regexp) ? type.match?(pattern) : type == pattern
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def response_type(env)
|
46
|
+
type = env[:response_headers][CONTENT_TYPE].to_s
|
47
|
+
type = type.split(';', 2).first if type.index(';')
|
48
|
+
type
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
Faraday::Response.register_middleware(json: Faraday::Response::Json)
|
@@ -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?
|
data/lib/faraday/response.rb
CHANGED
@@ -22,7 +22,8 @@ module Faraday
|
|
22
22
|
|
23
23
|
register_middleware File.expand_path('response', __dir__),
|
24
24
|
raise_error: [:RaiseError, 'raise_error'],
|
25
|
-
logger: [:Logger, 'logger']
|
25
|
+
logger: [:Logger, 'logger'],
|
26
|
+
json: [:Json, 'json']
|
26
27
|
|
27
28
|
def initialize(env = nil)
|
28
29
|
@env = Env.from(env) if env
|
@@ -42,6 +43,7 @@ module Faraday
|
|
42
43
|
def headers
|
43
44
|
finished? ? env.response_headers : {}
|
44
45
|
end
|
46
|
+
|
45
47
|
def_delegator :headers, :[]
|
46
48
|
|
47
49
|
def body
|
data/lib/faraday/version.rb
CHANGED
data/lib/faraday.rb
CHANGED
@@ -24,9 +24,15 @@ require 'faraday/adapter'
|
|
24
24
|
require 'faraday/request'
|
25
25
|
require 'faraday/response'
|
26
26
|
require 'faraday/error'
|
27
|
-
require 'faraday/
|
28
|
-
require 'faraday/param_part'
|
27
|
+
require 'faraday/request/url_encoded' # needed by multipart
|
29
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
|
30
36
|
unless defined?(JRUBY_VERSION)
|
31
37
|
require 'faraday/em_http'
|
32
38
|
require 'faraday/em_synchrony'
|
@@ -51,6 +57,8 @@ require 'faraday/rack'
|
|
51
57
|
# conn.get '/'
|
52
58
|
#
|
53
59
|
module Faraday
|
60
|
+
CONTENT_TYPE = 'Content-Type'
|
61
|
+
|
54
62
|
class << self
|
55
63
|
# The root path that Faraday is being loaded from.
|
56
64
|
#
|