better-faraday 2.0.1 → 3.1.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/.gitignore +2 -0
- data/.rubocop.yml +40 -0
- data/.ruby-version +1 -1
- data/Gemfile +6 -0
- data/Gemfile.lock +124 -0
- data/README.md +110 -3
- data/Rakefile +8 -0
- data/better-faraday.gemspec +18 -5
- data/lib/better_faraday.rb +440 -0
- data/spec/adapters_spec.rb +104 -0
- data/spec/array_masking_spec.rb +47 -0
- data/spec/assert_status_spec.rb +71 -0
- data/spec/custom_sensitive_signs_spec.rb +46 -0
- data/spec/http_error_spec.rb +56 -0
- data/spec/inspect_truncation_spec.rb +40 -0
- data/spec/invalid_json_spec.rb +37 -0
- data/spec/logger_spec.rb +52 -0
- data/spec/middleware_spec.rb +61 -0
- data/spec/multipart_spec.rb +39 -0
- data/spec/nested_sensitive_data_spec.rb +57 -0
- data/spec/sensitive_data_signs_spec.rb +109 -0
- data/spec/spec_helper.rb +23 -0
- data/spec/timing_inspect_spec.rb +51 -0
- metadata +130 -21
- data/lib/better-faraday.rb +0 -257
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: better-faraday
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 3.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Yaroslav Konoplov
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: faraday
|
|
@@ -31,43 +30,137 @@ dependencies:
|
|
|
31
30
|
- !ruby/object:Gem::Version
|
|
32
31
|
version: '3.0'
|
|
33
32
|
- !ruby/object:Gem::Dependency
|
|
34
|
-
name:
|
|
33
|
+
name: bundler
|
|
35
34
|
requirement: !ruby/object:Gem::Requirement
|
|
36
35
|
requirements:
|
|
37
|
-
- - "
|
|
36
|
+
- - "~>"
|
|
38
37
|
- !ruby/object:Gem::Version
|
|
39
38
|
version: '4.0'
|
|
40
|
-
|
|
41
|
-
- !ruby/object:Gem::Version
|
|
42
|
-
version: '7.0'
|
|
43
|
-
type: :runtime
|
|
39
|
+
type: :development
|
|
44
40
|
prerelease: false
|
|
45
41
|
version_requirements: !ruby/object:Gem::Requirement
|
|
46
42
|
requirements:
|
|
47
|
-
- - "
|
|
43
|
+
- - "~>"
|
|
48
44
|
- !ruby/object:Gem::Version
|
|
49
45
|
version: '4.0'
|
|
50
|
-
|
|
46
|
+
- !ruby/object:Gem::Dependency
|
|
47
|
+
name: faraday-multipart
|
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - "~>"
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: '1.2'
|
|
53
|
+
type: :development
|
|
54
|
+
prerelease: false
|
|
55
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
56
|
+
requirements:
|
|
57
|
+
- - "~>"
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: '1.2'
|
|
60
|
+
- !ruby/object:Gem::Dependency
|
|
61
|
+
name: faraday-net_http_persistent
|
|
62
|
+
requirement: !ruby/object:Gem::Requirement
|
|
63
|
+
requirements:
|
|
64
|
+
- - "~>"
|
|
65
|
+
- !ruby/object:Gem::Version
|
|
66
|
+
version: '2.3'
|
|
67
|
+
type: :development
|
|
68
|
+
prerelease: false
|
|
69
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
70
|
+
requirements:
|
|
71
|
+
- - "~>"
|
|
51
72
|
- !ruby/object:Gem::Version
|
|
52
|
-
version: '
|
|
53
|
-
|
|
54
|
-
|
|
73
|
+
version: '2.3'
|
|
74
|
+
- !ruby/object:Gem::Dependency
|
|
75
|
+
name: pry-byebug
|
|
76
|
+
requirement: !ruby/object:Gem::Requirement
|
|
77
|
+
requirements:
|
|
78
|
+
- - "~>"
|
|
79
|
+
- !ruby/object:Gem::Version
|
|
80
|
+
version: '3.12'
|
|
81
|
+
type: :development
|
|
82
|
+
prerelease: false
|
|
83
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
84
|
+
requirements:
|
|
85
|
+
- - "~>"
|
|
86
|
+
- !ruby/object:Gem::Version
|
|
87
|
+
version: '3.12'
|
|
88
|
+
- !ruby/object:Gem::Dependency
|
|
89
|
+
name: rake
|
|
90
|
+
requirement: !ruby/object:Gem::Requirement
|
|
91
|
+
requirements:
|
|
92
|
+
- - "~>"
|
|
93
|
+
- !ruby/object:Gem::Version
|
|
94
|
+
version: '13.4'
|
|
95
|
+
type: :development
|
|
96
|
+
prerelease: false
|
|
97
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
98
|
+
requirements:
|
|
99
|
+
- - "~>"
|
|
100
|
+
- !ruby/object:Gem::Version
|
|
101
|
+
version: '13.4'
|
|
102
|
+
- !ruby/object:Gem::Dependency
|
|
103
|
+
name: rspec
|
|
104
|
+
requirement: !ruby/object:Gem::Requirement
|
|
105
|
+
requirements:
|
|
106
|
+
- - "~>"
|
|
107
|
+
- !ruby/object:Gem::Version
|
|
108
|
+
version: '3.13'
|
|
109
|
+
type: :development
|
|
110
|
+
prerelease: false
|
|
111
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
112
|
+
requirements:
|
|
113
|
+
- - "~>"
|
|
114
|
+
- !ruby/object:Gem::Version
|
|
115
|
+
version: '3.13'
|
|
116
|
+
- !ruby/object:Gem::Dependency
|
|
117
|
+
name: webmock
|
|
118
|
+
requirement: !ruby/object:Gem::Requirement
|
|
119
|
+
requirements:
|
|
120
|
+
- - "~>"
|
|
121
|
+
- !ruby/object:Gem::Version
|
|
122
|
+
version: '3.26'
|
|
123
|
+
type: :development
|
|
124
|
+
prerelease: false
|
|
125
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
126
|
+
requirements:
|
|
127
|
+
- - "~>"
|
|
128
|
+
- !ruby/object:Gem::Version
|
|
129
|
+
version: '3.26'
|
|
130
|
+
description: A gem extending Faraday HTTP client with useful features
|
|
55
131
|
email: eahome00@gmail.com
|
|
56
132
|
executables: []
|
|
57
133
|
extensions: []
|
|
58
134
|
extra_rdoc_files: []
|
|
59
135
|
files:
|
|
60
136
|
- ".gitignore"
|
|
137
|
+
- ".rubocop.yml"
|
|
61
138
|
- ".ruby-version"
|
|
139
|
+
- Gemfile
|
|
140
|
+
- Gemfile.lock
|
|
62
141
|
- LICENSE
|
|
63
142
|
- README.md
|
|
143
|
+
- Rakefile
|
|
64
144
|
- better-faraday.gemspec
|
|
65
|
-
- lib/
|
|
145
|
+
- lib/better_faraday.rb
|
|
146
|
+
- spec/adapters_spec.rb
|
|
147
|
+
- spec/array_masking_spec.rb
|
|
148
|
+
- spec/assert_status_spec.rb
|
|
149
|
+
- spec/custom_sensitive_signs_spec.rb
|
|
150
|
+
- spec/http_error_spec.rb
|
|
151
|
+
- spec/inspect_truncation_spec.rb
|
|
152
|
+
- spec/invalid_json_spec.rb
|
|
153
|
+
- spec/logger_spec.rb
|
|
154
|
+
- spec/middleware_spec.rb
|
|
155
|
+
- spec/multipart_spec.rb
|
|
156
|
+
- spec/nested_sensitive_data_spec.rb
|
|
157
|
+
- spec/sensitive_data_signs_spec.rb
|
|
158
|
+
- spec/spec_helper.rb
|
|
159
|
+
- spec/timing_inspect_spec.rb
|
|
66
160
|
homepage: https://github.com/yivo/better-faraday
|
|
67
161
|
licenses:
|
|
68
162
|
- MIT
|
|
69
163
|
metadata: {}
|
|
70
|
-
post_install_message:
|
|
71
164
|
rdoc_options: []
|
|
72
165
|
require_paths:
|
|
73
166
|
- lib
|
|
@@ -75,15 +168,31 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
75
168
|
requirements:
|
|
76
169
|
- - ">="
|
|
77
170
|
- !ruby/object:Gem::Version
|
|
78
|
-
version:
|
|
171
|
+
version: 2.7.0
|
|
172
|
+
- - "<"
|
|
173
|
+
- !ruby/object:Gem::Version
|
|
174
|
+
version: '5.0'
|
|
79
175
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
80
176
|
requirements:
|
|
81
177
|
- - ">="
|
|
82
178
|
- !ruby/object:Gem::Version
|
|
83
179
|
version: '0'
|
|
84
180
|
requirements: []
|
|
85
|
-
rubygems_version:
|
|
86
|
-
signing_key:
|
|
181
|
+
rubygems_version: 4.0.22
|
|
87
182
|
specification_version: 4
|
|
88
|
-
summary: Extends Faraday with useful features
|
|
89
|
-
test_files:
|
|
183
|
+
summary: Extends Faraday with useful features
|
|
184
|
+
test_files:
|
|
185
|
+
- spec/adapters_spec.rb
|
|
186
|
+
- spec/array_masking_spec.rb
|
|
187
|
+
- spec/assert_status_spec.rb
|
|
188
|
+
- spec/custom_sensitive_signs_spec.rb
|
|
189
|
+
- spec/http_error_spec.rb
|
|
190
|
+
- spec/inspect_truncation_spec.rb
|
|
191
|
+
- spec/invalid_json_spec.rb
|
|
192
|
+
- spec/logger_spec.rb
|
|
193
|
+
- spec/middleware_spec.rb
|
|
194
|
+
- spec/multipart_spec.rb
|
|
195
|
+
- spec/nested_sensitive_data_spec.rb
|
|
196
|
+
- spec/sensitive_data_signs_spec.rb
|
|
197
|
+
- spec/spec_helper.rb
|
|
198
|
+
- spec/timing_inspect_spec.rb
|
data/lib/better-faraday.rb
DELETED
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
# encoding: UTF-8
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
require "json"
|
|
5
|
-
require "net/http"
|
|
6
|
-
require "faraday"
|
|
7
|
-
require "faraday/error"
|
|
8
|
-
require "faraday/options"
|
|
9
|
-
require "faraday/response"
|
|
10
|
-
require "active_support/core_ext/object/deep_dup"
|
|
11
|
-
require "active_support/core_ext/string/filters"
|
|
12
|
-
require "active_support/core_ext/string/inflections"
|
|
13
|
-
require "active_support/core_ext/object/inclusion"
|
|
14
|
-
|
|
15
|
-
Module.new do
|
|
16
|
-
def call(environment)
|
|
17
|
-
environment.instance_variable_set(:@bf_request_sent_at, Time.now.utc)
|
|
18
|
-
super
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
def save_response(environment, *)
|
|
22
|
-
environment.instance_variable_set \
|
|
23
|
-
:@bf_response_received_at,
|
|
24
|
-
Time.now.utc
|
|
25
|
-
|
|
26
|
-
body = environment.body
|
|
27
|
-
value = body.respond_to?(:read) ? body.read : body
|
|
28
|
-
value = value.to_s unless String === value
|
|
29
|
-
|
|
30
|
-
environment.instance_variable_set \
|
|
31
|
-
:@bf_request_body,
|
|
32
|
-
value
|
|
33
|
-
|
|
34
|
-
super
|
|
35
|
-
end
|
|
36
|
-
end.tap { |m| Faraday::Adapter.prepend(m) }
|
|
37
|
-
|
|
38
|
-
Module.new do
|
|
39
|
-
def end_transport(request, response)
|
|
40
|
-
headers = request.to_hash
|
|
41
|
-
headers.keys.each { |k| headers[k] = headers[k].join(", ") }
|
|
42
|
-
response.instance_variable_set :@bf_request_headers, headers
|
|
43
|
-
super
|
|
44
|
-
end
|
|
45
|
-
end.tap { |m| Net::HTTP.prepend(m) }
|
|
46
|
-
|
|
47
|
-
Module.new do
|
|
48
|
-
def perform_request(connection, environment)
|
|
49
|
-
super(connection, environment).tap do |response|
|
|
50
|
-
environment.instance_variable_set \
|
|
51
|
-
:@bf_request_headers,
|
|
52
|
-
response.instance_variable_get(:@bf_request_headers)
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
end.tap { |m| Faraday::Adapter::NetHttp.prepend(m) }
|
|
56
|
-
|
|
57
|
-
module Faraday
|
|
58
|
-
class Env
|
|
59
|
-
attr_reader :bf_request_headers, :bf_request_body, :bf_request_sent_at, :bf_response_received_at
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
class Response
|
|
63
|
-
def assert_status!(code_or_range)
|
|
64
|
-
within_range = if Range === code_or_range
|
|
65
|
-
status.in?(code_or_range)
|
|
66
|
-
else
|
|
67
|
-
status == code_or_range
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
return self if within_range
|
|
71
|
-
|
|
72
|
-
klass = if status_4xx?
|
|
73
|
-
"BetterFaraday::HTTP#{status}".safe_constantize || BetterFaraday::HTTP4xx
|
|
74
|
-
elsif status_5xx?
|
|
75
|
-
"BetterFaraday::HTTP#{status}".safe_constantize || BetterFaraday::HTTP5xx
|
|
76
|
-
else
|
|
77
|
-
BetterFaraday::HTTPError
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
raise klass.new(self)
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
def assert_2xx!
|
|
84
|
-
assert_status!(200..299)
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
def assert_200!
|
|
88
|
-
assert_status! 200
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
def status_2xx?
|
|
92
|
-
status >= 200 && status <= 299
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
def status_3xx?
|
|
96
|
-
status >= 300 && status <= 399
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
def status_4xx?
|
|
100
|
-
status >= 400 && status <= 499
|
|
101
|
-
end
|
|
102
|
-
|
|
103
|
-
def status_5xx?
|
|
104
|
-
status >= 500 && status <= 599
|
|
105
|
-
end
|
|
106
|
-
|
|
107
|
-
def inspect
|
|
108
|
-
@inspection_text ||= begin
|
|
109
|
-
request_headers = bf_protect_data(env.bf_request_headers.dup)
|
|
110
|
-
request_body = env.bf_request_body.yield_self { |body| String === body ? body : body.to_s }
|
|
111
|
-
request_body_bytes_count = env.bf_request_body.bytesize
|
|
112
|
-
response_body = env.body.yield_self { |body| String === body ? body : body.to_s }
|
|
113
|
-
response_body_bytes_count = response_body.bytesize
|
|
114
|
-
|
|
115
|
-
if env.bf_request_headers["content-type"].to_s.match?(/\bapplication\/json\b/i)
|
|
116
|
-
request_json = bf_json_parse(request_body).yield_self do |data|
|
|
117
|
-
bf_json_dump(bf_protect_data(data)) if data
|
|
118
|
-
end
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
if env.response_headers
|
|
122
|
-
response_headers = bf_protect_data(env.response_headers.to_hash)
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
if env.response_headers && env.response_headers["content-type"].to_s.match?(/\bapplication\/json\b/i)
|
|
126
|
-
response_json = bf_json_parse(response_body).yield_self do |data|
|
|
127
|
-
bf_json_dump(bf_protect_data(data)) if data
|
|
128
|
-
end
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
lines = [
|
|
132
|
-
"-- #{status} #{reason_phrase} --".upcase,
|
|
133
|
-
"",
|
|
134
|
-
"-- Request URL --",
|
|
135
|
-
env.url.to_s,
|
|
136
|
-
"",
|
|
137
|
-
"-- Request Method --",
|
|
138
|
-
env.method.to_s.upcase,
|
|
139
|
-
"",
|
|
140
|
-
"-- Request Headers --",
|
|
141
|
-
bf_json_dump(request_headers).truncate(2048, omission: "... (truncated)"),
|
|
142
|
-
"",
|
|
143
|
-
|
|
144
|
-
%[-- Request Body (#{request_body_bytes_count} #{"byte".pluralize(request_body_bytes_count)}) --],
|
|
145
|
-
if request_json
|
|
146
|
-
request_json
|
|
147
|
-
else
|
|
148
|
-
# String#inspect returns \x{XXXX} for the encoding other than Unicode.
|
|
149
|
-
# [1..-2] removed leading and trailing " added by String#inspect.
|
|
150
|
-
# gsub(/\\"/, "\"") unescapes ".
|
|
151
|
-
request_body.inspect.gsub(/\\"/, "\"")[1..-2]
|
|
152
|
-
end.truncate(2048, omission: "... (truncated)"),
|
|
153
|
-
"",
|
|
154
|
-
|
|
155
|
-
"-- Request Sent At --",
|
|
156
|
-
env.bf_request_sent_at.strftime("%Y-%m-%d %H:%M:%S.%3N") + " UTC",
|
|
157
|
-
"",
|
|
158
|
-
|
|
159
|
-
"-- Response Headers --",
|
|
160
|
-
if response_headers
|
|
161
|
-
bf_json_dump(response_headers)
|
|
162
|
-
else
|
|
163
|
-
env.response_headers.to_s.inspect.gsub(/\\"/, "\"")[1..-2]
|
|
164
|
-
end.truncate(2048, omission: "... (truncated)"),
|
|
165
|
-
"",
|
|
166
|
-
|
|
167
|
-
%[-- Response Body (#{response_body_bytes_count} #{"byte".pluralize(response_body_bytes_count)}) --],
|
|
168
|
-
if response_json
|
|
169
|
-
response_json
|
|
170
|
-
else
|
|
171
|
-
response_body.inspect.gsub(/\\"/, "\"")[1..-2]
|
|
172
|
-
end.truncate(2048, omission: "... (truncated)"),
|
|
173
|
-
""
|
|
174
|
-
]
|
|
175
|
-
|
|
176
|
-
if env.bf_response_received_at
|
|
177
|
-
lines.concat [
|
|
178
|
-
"-- Response Received At --",
|
|
179
|
-
env.bf_response_received_at.strftime("%Y-%m-%d %H:%M:%S.%3N") + " UTC",
|
|
180
|
-
"",
|
|
181
|
-
"-- Response Received In --",
|
|
182
|
-
"#{((env.bf_response_received_at.to_f - env.bf_request_sent_at.to_f) * 1000.0).ceil(3)}ms",
|
|
183
|
-
""
|
|
184
|
-
]
|
|
185
|
-
end
|
|
186
|
-
|
|
187
|
-
lines.join("\n").freeze
|
|
188
|
-
end
|
|
189
|
-
|
|
190
|
-
@inspection_text.dup
|
|
191
|
-
end
|
|
192
|
-
|
|
193
|
-
private
|
|
194
|
-
|
|
195
|
-
def bf_json_parse(json)
|
|
196
|
-
return nil unless ::String === json
|
|
197
|
-
data = ::JSON.parse(json)
|
|
198
|
-
data if ::Hash === data || ::Array === data
|
|
199
|
-
rescue ::JSON::ParserError
|
|
200
|
-
nil
|
|
201
|
-
end
|
|
202
|
-
|
|
203
|
-
def bf_json_dump(data)
|
|
204
|
-
::JSON.generate(data, space: " ", object_nl: " ", array_nl: " ")
|
|
205
|
-
end
|
|
206
|
-
|
|
207
|
-
def bf_protect_data(data)
|
|
208
|
-
return data.map(&method(:bf_protect_data)) if ::Array === data
|
|
209
|
-
return data unless ::Hash === data
|
|
210
|
-
|
|
211
|
-
signs = BetterFaraday.sensitive_data_signs
|
|
212
|
-
|
|
213
|
-
data.each_with_object({}) do |(key, value), memo|
|
|
214
|
-
memo[key] = if key.to_s.underscore.tr("_", " ").yield_self { |k| signs.any? { |r| k.match?(r) } }
|
|
215
|
-
"SECRET"
|
|
216
|
-
else
|
|
217
|
-
bf_protect_data(value)
|
|
218
|
-
end
|
|
219
|
-
end
|
|
220
|
-
end
|
|
221
|
-
end
|
|
222
|
-
end
|
|
223
|
-
|
|
224
|
-
module BetterFaraday
|
|
225
|
-
class HTTPError < Faraday::Error
|
|
226
|
-
def initialize(response)
|
|
227
|
-
super(response.inspect, response)
|
|
228
|
-
end
|
|
229
|
-
|
|
230
|
-
def inspect
|
|
231
|
-
%[#{self.class}\n\n#{response.inspect}]
|
|
232
|
-
end
|
|
233
|
-
end
|
|
234
|
-
|
|
235
|
-
class HTTP4xx < HTTPError; end
|
|
236
|
-
class HTTP400 < HTTP4xx; end
|
|
237
|
-
class HTTP401 < HTTP4xx; end
|
|
238
|
-
class HTTP403 < HTTP4xx; end
|
|
239
|
-
class HTTP404 < HTTP4xx; end
|
|
240
|
-
class HTTP422 < HTTP4xx; end
|
|
241
|
-
class HTTP429 < HTTP4xx; end
|
|
242
|
-
class HTTP5xx < HTTPError; end
|
|
243
|
-
class HTTP500 < HTTP5xx; end
|
|
244
|
-
class HTTP502 < HTTP5xx; end
|
|
245
|
-
class HTTP503 < HTTP5xx; end
|
|
246
|
-
|
|
247
|
-
class << self
|
|
248
|
-
attr_accessor :sensitive_data_signs
|
|
249
|
-
end
|
|
250
|
-
|
|
251
|
-
self.sensitive_data_signs = [
|
|
252
|
-
/\bpass(?:word|phrase)\b/i,
|
|
253
|
-
/\bauthorization\b/i,
|
|
254
|
-
/\bsecret\b/i,
|
|
255
|
-
/\b(:?access)?token\b/i
|
|
256
|
-
]
|
|
257
|
-
end
|