faraday 2.11.0 → 2.12.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/faraday/logging/formatter.rb +6 -6
- data/lib/faraday/rack_builder.rb +15 -15
- data/lib/faraday/response/raise_error.rb +15 -17
- data/lib/faraday/version.rb +1 -1
- data/spec/faraday/error_spec.rb +10 -2
- data/spec/faraday/response/logger_spec.rb +20 -0
- data/spec/faraday/response/raise_error_spec.rb +20 -0
- metadata +24 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bfb424c45e703a33fd819b31c8ee41c9e4e30874b280530a19ce8ae320570ef9
|
4
|
+
data.tar.gz: c1a1a5b7aab4f393908958701959f7b41fd303ca4da906c1b4c0581a13c590db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d4d18e2006ac83dfd22aff834b4fe3c983bfbf578b6850944d35cfbb1ca40ba58ce07414821bc3e604ca3310c85500d5d2b8de7c33b0e6147f24484fcae688f
|
7
|
+
data.tar.gz: 9cc8057d003f09a02bbdb9a29b920003beff58391c3bf86838ae61b2e9aaac4782d5764286f824b24d486e8dee202cc2d892ec3f658db6b710d5088d5836be3c
|
@@ -23,8 +23,8 @@ module Faraday
|
|
23
23
|
def_delegators :@logger, :debug, :info, :warn, :error, :fatal
|
24
24
|
|
25
25
|
def request(env)
|
26
|
-
public_send(log_level
|
27
|
-
"#{env.method.upcase} #{apply_filters(env.url.to_s)}"
|
26
|
+
public_send(log_level) do
|
27
|
+
"request: #{env.method.upcase} #{apply_filters(env.url.to_s)}"
|
28
28
|
end
|
29
29
|
|
30
30
|
log_headers('request', env.request_headers) if log_headers?(:request)
|
@@ -32,7 +32,7 @@ module Faraday
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def response(env)
|
35
|
-
public_send(log_level
|
35
|
+
public_send(log_level) { "response: Status #{env.status}" }
|
36
36
|
|
37
37
|
log_headers('response', env.response_headers) if log_headers?(:response)
|
38
38
|
log_body('response', env[:body]) if env[:body] && log_body?(:response)
|
@@ -41,7 +41,7 @@ module Faraday
|
|
41
41
|
def exception(exc)
|
42
42
|
return unless log_errors?
|
43
43
|
|
44
|
-
public_send(log_level
|
44
|
+
public_send(log_level) { "error: #{exc.full_message}" }
|
45
45
|
|
46
46
|
log_headers('error', exc.response_headers) if exc.respond_to?(:response_headers) && log_headers?(:error)
|
47
47
|
return unless exc.respond_to?(:response_body) && exc.response_body && log_body?(:error)
|
@@ -107,11 +107,11 @@ module Faraday
|
|
107
107
|
end
|
108
108
|
|
109
109
|
def log_headers(type, headers)
|
110
|
-
public_send(log_level
|
110
|
+
public_send(log_level) { "#{type}: #{apply_filters(dump_headers(headers))}" }
|
111
111
|
end
|
112
112
|
|
113
113
|
def log_body(type, body)
|
114
|
-
public_send(log_level
|
114
|
+
public_send(log_level) { "#{type}: #{apply_filters(dump_body(body))}" }
|
115
115
|
end
|
116
116
|
end
|
117
117
|
end
|
data/lib/faraday/rack_builder.rb
CHANGED
@@ -88,22 +88,22 @@ module Faraday
|
|
88
88
|
@handlers.frozen?
|
89
89
|
end
|
90
90
|
|
91
|
-
|
91
|
+
def use(klass, ...)
|
92
92
|
if klass.is_a? Symbol
|
93
|
-
use_symbol(Faraday::Middleware, klass,
|
93
|
+
use_symbol(Faraday::Middleware, klass, ...)
|
94
94
|
else
|
95
95
|
raise_if_locked
|
96
96
|
raise_if_adapter(klass)
|
97
|
-
@handlers << self.class::Handler.new(klass,
|
97
|
+
@handlers << self.class::Handler.new(klass, ...)
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
-
|
102
|
-
use_symbol(Faraday::Request, key,
|
101
|
+
def request(key, ...)
|
102
|
+
use_symbol(Faraday::Request, key, ...)
|
103
103
|
end
|
104
104
|
|
105
|
-
|
106
|
-
use_symbol(Faraday::Response,
|
105
|
+
def response(...)
|
106
|
+
use_symbol(Faraday::Response, ...)
|
107
107
|
end
|
108
108
|
|
109
109
|
ruby2_keywords def adapter(klass = NO_ARGUMENT, *args, &block)
|
@@ -115,25 +115,25 @@ module Faraday
|
|
115
115
|
|
116
116
|
## methods to push onto the various positions in the stack:
|
117
117
|
|
118
|
-
|
118
|
+
def insert(index, ...)
|
119
119
|
raise_if_locked
|
120
120
|
index = assert_index(index)
|
121
|
-
handler = self.class::Handler.new(
|
121
|
+
handler = self.class::Handler.new(...)
|
122
122
|
@handlers.insert(index, handler)
|
123
123
|
end
|
124
124
|
|
125
125
|
alias insert_before insert
|
126
126
|
|
127
|
-
|
127
|
+
def insert_after(index, ...)
|
128
128
|
index = assert_index(index)
|
129
|
-
insert(index + 1,
|
129
|
+
insert(index + 1, ...)
|
130
130
|
end
|
131
131
|
|
132
|
-
|
132
|
+
def swap(index, ...)
|
133
133
|
raise_if_locked
|
134
134
|
index = assert_index(index)
|
135
135
|
@handlers.delete_at(index)
|
136
|
-
insert(index,
|
136
|
+
insert(index, ...)
|
137
137
|
end
|
138
138
|
|
139
139
|
def delete(handler)
|
@@ -237,8 +237,8 @@ module Faraday
|
|
237
237
|
klass <= Faraday::Adapter
|
238
238
|
end
|
239
239
|
|
240
|
-
|
241
|
-
use(mod.lookup_middleware(key),
|
240
|
+
def use_symbol(mod, key, ...)
|
241
|
+
use(mod.lookup_middleware(key), ...)
|
242
242
|
end
|
243
243
|
|
244
244
|
def assert_index(index)
|
@@ -8,32 +8,30 @@ module Faraday
|
|
8
8
|
# rubocop:disable Naming/ConstantName
|
9
9
|
ClientErrorStatuses = (400...500)
|
10
10
|
ServerErrorStatuses = (500...600)
|
11
|
+
ClientErrorStatusesWithCustomExceptions = {
|
12
|
+
400 => Faraday::BadRequestError,
|
13
|
+
401 => Faraday::UnauthorizedError,
|
14
|
+
403 => Faraday::ForbiddenError,
|
15
|
+
404 => Faraday::ResourceNotFound,
|
16
|
+
408 => Faraday::RequestTimeoutError,
|
17
|
+
409 => Faraday::ConflictError,
|
18
|
+
422 => Faraday::UnprocessableEntityError,
|
19
|
+
429 => Faraday::TooManyRequestsError
|
20
|
+
}.freeze
|
11
21
|
# rubocop:enable Naming/ConstantName
|
12
22
|
|
13
|
-
DEFAULT_OPTIONS = { include_request: true }.freeze
|
23
|
+
DEFAULT_OPTIONS = { include_request: true, allowed_statuses: [] }.freeze
|
14
24
|
|
15
25
|
def on_complete(env)
|
26
|
+
return if Array(options[:allowed_statuses]).include?(env[:status])
|
27
|
+
|
16
28
|
case env[:status]
|
17
|
-
when
|
18
|
-
raise
|
19
|
-
when 401
|
20
|
-
raise Faraday::UnauthorizedError, response_values(env)
|
21
|
-
when 403
|
22
|
-
raise Faraday::ForbiddenError, response_values(env)
|
23
|
-
when 404
|
24
|
-
raise Faraday::ResourceNotFound, response_values(env)
|
29
|
+
when *ClientErrorStatusesWithCustomExceptions.keys
|
30
|
+
raise ClientErrorStatusesWithCustomExceptions[env[:status]], response_values(env)
|
25
31
|
when 407
|
26
32
|
# mimic the behavior that we get with proxy requests with HTTPS
|
27
33
|
msg = %(407 "Proxy Authentication Required")
|
28
34
|
raise Faraday::ProxyAuthError.new(msg, response_values(env))
|
29
|
-
when 408
|
30
|
-
raise Faraday::RequestTimeoutError, response_values(env)
|
31
|
-
when 409
|
32
|
-
raise Faraday::ConflictError, response_values(env)
|
33
|
-
when 422
|
34
|
-
raise Faraday::UnprocessableEntityError, response_values(env)
|
35
|
-
when 429
|
36
|
-
raise Faraday::TooManyRequestsError, response_values(env)
|
37
35
|
when ClientErrorStatuses
|
38
36
|
raise Faraday::ClientError, response_values(env)
|
39
37
|
when ServerErrorStatuses
|
data/lib/faraday/version.rb
CHANGED
data/spec/faraday/error_spec.rb
CHANGED
@@ -24,7 +24,11 @@ RSpec.describe Faraday::Error do
|
|
24
24
|
it { expect(subject.wrapped_exception).to be_nil }
|
25
25
|
it { expect(subject.response).to eq(exception) }
|
26
26
|
it { expect(subject.message).to eq('the server responded with status 400') }
|
27
|
-
|
27
|
+
if RUBY_VERSION >= '3.4'
|
28
|
+
it { expect(subject.inspect).to eq('#<Faraday::Error response={status: 400}>') }
|
29
|
+
else
|
30
|
+
it { expect(subject.inspect).to eq('#<Faraday::Error response={:status=>400}>') }
|
31
|
+
end
|
28
32
|
it { expect(subject.response_status).to eq(400) }
|
29
33
|
it { expect(subject.response_headers).to be_nil }
|
30
34
|
it { expect(subject.response_body).to be_nil }
|
@@ -61,7 +65,11 @@ RSpec.describe Faraday::Error do
|
|
61
65
|
it { expect(subject.wrapped_exception).to be_nil }
|
62
66
|
it { expect(subject.response).to eq(response) }
|
63
67
|
it { expect(subject.message).to eq('custom message') }
|
64
|
-
|
68
|
+
if RUBY_VERSION >= '3.4'
|
69
|
+
it { expect(subject.inspect).to eq('#<Faraday::Error response={status: 400}>') }
|
70
|
+
else
|
71
|
+
it { expect(subject.inspect).to eq('#<Faraday::Error response={:status=>400}>') }
|
72
|
+
end
|
65
73
|
it { expect(subject.response_status).to eq(400) }
|
66
74
|
it { expect(subject.response_headers).to be_nil }
|
67
75
|
it { expect(subject.response_body).to be_nil }
|
@@ -55,6 +55,26 @@ RSpec.describe Faraday::Response::Logger do
|
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
+
context 'when logger with program name' do
|
59
|
+
let(:logger) { Logger.new(string_io, progname: 'my_best_program') }
|
60
|
+
|
61
|
+
it 'logs with program name' do
|
62
|
+
conn.get '/hello'
|
63
|
+
|
64
|
+
expect(string_io.string).to match('-- my_best_program: request:')
|
65
|
+
expect(string_io.string).to match('-- my_best_program: response:')
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'when logger without program name' do
|
70
|
+
it 'logs without program name' do
|
71
|
+
conn.get '/hello'
|
72
|
+
|
73
|
+
expect(string_io.string).to match('-- : request:')
|
74
|
+
expect(string_io.string).to match('-- : response:')
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
58
78
|
context 'with default formatter' do
|
59
79
|
let(:formatter) { instance_double(Faraday::Logging::Formatter, request: true, response: true, filter: []) }
|
60
80
|
|
@@ -252,4 +252,24 @@ RSpec.describe Faraday::Response::RaiseError do
|
|
252
252
|
end
|
253
253
|
end
|
254
254
|
end
|
255
|
+
|
256
|
+
describe 'allowing certain status codes' do
|
257
|
+
let(:conn) do
|
258
|
+
Faraday.new do |b|
|
259
|
+
b.response :raise_error, allowed_statuses: [404]
|
260
|
+
b.adapter :test do |stub|
|
261
|
+
stub.get('bad-request') { [400, { 'X-Reason' => 'because' }, 'keep looking'] }
|
262
|
+
stub.get('not-found') { [404, { 'X-Reason' => 'because' }, 'keep looking'] }
|
263
|
+
end
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
it 'raises an error for status codes that are not explicitly allowed' do
|
268
|
+
expect { conn.get('bad-request') }.to raise_error(Faraday::BadRequestError)
|
269
|
+
end
|
270
|
+
|
271
|
+
it 'does not raise an error for allowed status codes' do
|
272
|
+
expect { conn.get('not-found') }.not_to raise_error
|
273
|
+
end
|
274
|
+
end
|
255
275
|
end
|
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.12.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "@technoweenie"
|
8
8
|
- "@iMacTia"
|
9
9
|
- "@olleolleolle"
|
10
|
-
autorequire:
|
10
|
+
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2024-
|
13
|
+
date: 2024-12-09 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: faraday-net_http
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '2.0'
|
22
22
|
- - "<"
|
23
23
|
- !ruby/object:Gem::Version
|
24
|
-
version: '3.
|
24
|
+
version: '3.5'
|
25
25
|
type: :runtime
|
26
26
|
prerelease: false
|
27
27
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -31,7 +31,21 @@ dependencies:
|
|
31
31
|
version: '2.0'
|
32
32
|
- - "<"
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: '3.
|
34
|
+
version: '3.5'
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: json
|
37
|
+
requirement: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
type: :runtime
|
43
|
+
prerelease: false
|
44
|
+
version_requirements: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
35
49
|
- !ruby/object:Gem::Dependency
|
36
50
|
name: logger
|
37
51
|
requirement: !ruby/object:Gem::Requirement
|
@@ -46,7 +60,7 @@ dependencies:
|
|
46
60
|
- - ">="
|
47
61
|
- !ruby/object:Gem::Version
|
48
62
|
version: '0'
|
49
|
-
description:
|
63
|
+
description:
|
50
64
|
email: technoweenie@gmail.com
|
51
65
|
executables: []
|
52
66
|
extensions: []
|
@@ -132,11 +146,11 @@ licenses:
|
|
132
146
|
- MIT
|
133
147
|
metadata:
|
134
148
|
homepage_uri: https://lostisland.github.io/faraday
|
135
|
-
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v2.
|
149
|
+
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v2.12.2
|
136
150
|
source_code_uri: https://github.com/lostisland/faraday
|
137
151
|
bug_tracker_uri: https://github.com/lostisland/faraday/issues
|
138
152
|
rubygems_mfa_required: 'true'
|
139
|
-
post_install_message:
|
153
|
+
post_install_message:
|
140
154
|
rdoc_options: []
|
141
155
|
require_paths:
|
142
156
|
- lib
|
@@ -152,8 +166,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
166
|
- !ruby/object:Gem::Version
|
153
167
|
version: '0'
|
154
168
|
requirements: []
|
155
|
-
rubygems_version: 3.5.
|
156
|
-
signing_key:
|
169
|
+
rubygems_version: 3.5.22
|
170
|
+
signing_key:
|
157
171
|
specification_version: 4
|
158
172
|
summary: HTTP/REST API client library.
|
159
173
|
test_files: []
|