faraday 0.12.0 → 1.4.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +380 -0
- data/LICENSE.md +1 -1
- data/README.md +21 -308
- data/Rakefile +7 -0
- data/examples/client_spec.rb +65 -0
- data/examples/client_test.rb +79 -0
- data/lib/faraday/adapter/httpclient.rb +83 -59
- data/lib/faraday/adapter/patron.rb +92 -36
- data/lib/faraday/adapter/rack.rb +30 -13
- data/lib/faraday/adapter/test.rb +103 -62
- data/lib/faraday/adapter/typhoeus.rb +7 -115
- data/lib/faraday/adapter.rb +77 -22
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/autoload.rb +42 -36
- data/lib/faraday/connection.rb +345 -179
- data/lib/faraday/dependency_loader.rb +37 -0
- 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 +127 -38
- data/lib/faraday/file_part.rb +128 -0
- data/lib/faraday/logging/formatter.rb +105 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +19 -25
- data/lib/faraday/middleware_registry.rb +129 -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 +39 -193
- data/lib/faraday/param_part.rb +53 -0
- data/lib/faraday/parameters.rb +4 -196
- data/lib/faraday/rack_builder.rb +84 -48
- data/lib/faraday/request/authorization.rb +44 -30
- data/lib/faraday/request/basic_authentication.rb +14 -7
- data/lib/faraday/request/instrumentation.rb +45 -27
- data/lib/faraday/request/multipart.rb +86 -48
- data/lib/faraday/request/retry.rb +209 -133
- data/lib/faraday/request/token_authentication.rb +15 -10
- data/lib/faraday/request/url_encoded.rb +43 -23
- data/lib/faraday/request.rb +94 -32
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +49 -14
- data/lib/faraday/response.rb +27 -23
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +38 -238
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +124 -187
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/em_http_spec.rb +47 -0
- data/spec/faraday/adapter/em_synchrony_spec.rb +16 -0
- data/spec/faraday/adapter/excon_spec.rb +49 -0
- data/spec/faraday/adapter/httpclient_spec.rb +73 -0
- data/spec/faraday/adapter/net_http_spec.rb +64 -0
- data/spec/faraday/adapter/patron_spec.rb +18 -0
- data/spec/faraday/adapter/rack_spec.rb +8 -0
- data/spec/faraday/adapter/test_spec.rb +260 -0
- data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
- data/spec/faraday/adapter_registry_spec.rb +28 -0
- data/spec/faraday/adapter_spec.rb +55 -0
- data/spec/faraday/composite_read_io_spec.rb +80 -0
- data/spec/faraday/connection_spec.rb +736 -0
- data/spec/faraday/error_spec.rb +60 -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 +345 -0
- data/spec/faraday/request/authorization_spec.rb +88 -0
- data/spec/faraday/request/instrumentation_spec.rb +76 -0
- data/spec/faraday/request/multipart_spec.rb +302 -0
- data/spec/faraday/request/retry_spec.rb +242 -0
- data/spec/faraday/request/url_encoded_spec.rb +83 -0
- data/spec/faraday/request_spec.rb +120 -0
- data/spec/faraday/response/logger_spec.rb +220 -0
- data/spec/faraday/response/middleware_spec.rb +68 -0
- data/spec/faraday/response/raise_error_spec.rb +169 -0
- data/spec/faraday/response_spec.rb +75 -0
- data/spec/faraday/utils/headers_spec.rb +82 -0
- data/spec/faraday/utils_spec.rb +56 -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 +133 -0
- data/spec/support/shared_examples/adapter.rb +105 -0
- data/spec/support/shared_examples/params_encoder.rb +18 -0
- data/spec/support/shared_examples/request_method.rb +262 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- data/spec/support/webmock_rack_app.rb +68 -0
- metadata +164 -16
- 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 -80
- data/lib/faraday/adapter/net_http.rb +0 -135
- data/lib/faraday/adapter/net_http_persistent.rb +0 -50
- data/lib/faraday/upload_io.rb +0 -67
@@ -1,80 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'forwardable'
|
4
|
+
require 'faraday/logging/formatter'
|
2
5
|
|
3
6
|
module Faraday
|
4
|
-
class Response
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
def_delegators :@logger, :debug, :info, :warn, :error, :fatal
|
21
|
-
|
22
|
-
def call(env)
|
23
|
-
info "#{env.method} #{apply_filters(env.url.to_s)}"
|
24
|
-
debug('request') { apply_filters( dump_headers env.request_headers ) } if log_headers?(:request)
|
25
|
-
debug('request') { apply_filters( dump_body(env[:body]) ) } if env[:body] && log_body?(:request)
|
26
|
-
super
|
27
|
-
end
|
28
|
-
|
29
|
-
def on_complete(env)
|
30
|
-
info('Status') { env.status.to_s }
|
31
|
-
debug('response') { apply_filters( dump_headers env.response_headers ) } if log_headers?(:response)
|
32
|
-
debug('response') { apply_filters( dump_body env[:body] ) } if env[:body] && log_body?(:response)
|
33
|
-
end
|
34
|
-
|
35
|
-
def filter(filter_word, filter_replacement)
|
36
|
-
@filter.push([ filter_word, filter_replacement ])
|
37
|
-
end
|
38
|
-
|
39
|
-
private
|
40
|
-
|
41
|
-
def dump_headers(headers)
|
42
|
-
headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n")
|
43
|
-
end
|
44
|
-
|
45
|
-
def dump_body(body)
|
46
|
-
if body.respond_to?(:to_str)
|
47
|
-
body.to_str
|
48
|
-
else
|
49
|
-
pretty_inspect(body)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def pretty_inspect(body)
|
54
|
-
require 'pp' unless body.respond_to?(:pretty_inspect)
|
55
|
-
body.pretty_inspect
|
56
|
-
end
|
57
|
-
|
58
|
-
def log_headers?(type)
|
59
|
-
case @options[:headers]
|
60
|
-
when Hash then @options[:headers][type]
|
61
|
-
else @options[:headers]
|
7
|
+
class Response
|
8
|
+
# Logger is a middleware that logs internal events in the HTTP request
|
9
|
+
# lifecycle to a given Logger object. By default, this logs to STDOUT. See
|
10
|
+
# Faraday::Logging::Formatter to see specifically what is logged.
|
11
|
+
class Logger < Middleware
|
12
|
+
def initialize(app, logger = nil, options = {})
|
13
|
+
super(app)
|
14
|
+
logger ||= begin
|
15
|
+
require 'logger'
|
16
|
+
::Logger.new($stdout)
|
17
|
+
end
|
18
|
+
formatter_class = options.delete(:formatter) || Logging::Formatter
|
19
|
+
@formatter = formatter_class.new(logger: logger, options: options)
|
20
|
+
yield @formatter if block_given?
|
62
21
|
end
|
63
|
-
end
|
64
22
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
else @options[:bodies]
|
23
|
+
def call(env)
|
24
|
+
@formatter.request(env)
|
25
|
+
super
|
69
26
|
end
|
70
|
-
end
|
71
27
|
|
72
|
-
|
73
|
-
|
74
|
-
output = output.to_s.gsub(pattern, replacement)
|
28
|
+
def on_complete(env)
|
29
|
+
@formatter.response(env)
|
75
30
|
end
|
76
|
-
output
|
77
31
|
end
|
78
|
-
|
79
32
|
end
|
80
33
|
end
|
@@ -1,21 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Faraday
|
2
|
-
class Response
|
3
|
-
|
4
|
+
class Response
|
5
|
+
# RaiseError is a Faraday middleware that raises exceptions on common HTTP
|
6
|
+
# client or server error responses.
|
7
|
+
class RaiseError < Middleware
|
8
|
+
# rubocop:disable Naming/ConstantName
|
9
|
+
ClientErrorStatuses = (400...500).freeze
|
10
|
+
ServerErrorStatuses = (500...600).freeze
|
11
|
+
# rubocop:enable Naming/ConstantName
|
4
12
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
13
|
+
def on_complete(env)
|
14
|
+
case env[:status]
|
15
|
+
when 400
|
16
|
+
raise Faraday::BadRequestError, response_values(env)
|
17
|
+
when 401
|
18
|
+
raise Faraday::UnauthorizedError, response_values(env)
|
19
|
+
when 403
|
20
|
+
raise Faraday::ForbiddenError, response_values(env)
|
21
|
+
when 404
|
22
|
+
raise Faraday::ResourceNotFound, response_values(env)
|
23
|
+
when 407
|
24
|
+
# mimic the behavior that we get with proxy requests with HTTPS
|
25
|
+
msg = %(407 "Proxy Authentication Required")
|
26
|
+
raise Faraday::ProxyAuthError.new(msg, response_values(env))
|
27
|
+
when 409
|
28
|
+
raise Faraday::ConflictError, response_values(env)
|
29
|
+
when 422
|
30
|
+
raise Faraday::UnprocessableEntityError, response_values(env)
|
31
|
+
when ClientErrorStatuses
|
32
|
+
raise Faraday::ClientError, response_values(env)
|
33
|
+
when ServerErrorStatuses
|
34
|
+
raise Faraday::ServerError, response_values(env)
|
35
|
+
when nil
|
36
|
+
raise Faraday::NilStatusError, response_values(env)
|
37
|
+
end
|
14
38
|
end
|
15
|
-
end
|
16
39
|
|
17
|
-
|
18
|
-
|
40
|
+
def response_values(env)
|
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
|
+
}
|
53
|
+
end
|
19
54
|
end
|
20
55
|
end
|
21
56
|
end
|
data/lib/faraday/response.rb
CHANGED
@@ -1,28 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'forwardable'
|
2
4
|
|
3
5
|
module Faraday
|
6
|
+
# Response represents an HTTP response from making an HTTP request.
|
4
7
|
class Response
|
5
8
|
# Used for simple response middleware.
|
6
9
|
class Middleware < Faraday::Middleware
|
7
|
-
def call(env)
|
8
|
-
@app.call(env).on_complete do |environment|
|
9
|
-
on_complete(environment)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
10
|
# Override this to modify the environment after the response has finished.
|
14
11
|
# Calls the `parse` method if defined
|
12
|
+
# `parse` method can be defined as private, public and protected
|
15
13
|
def on_complete(env)
|
16
|
-
|
14
|
+
return unless respond_to?(:parse, true) && env.parse_body?
|
15
|
+
|
16
|
+
env.body = parse(env.body)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
20
|
extend Forwardable
|
21
21
|
extend MiddlewareRegistry
|
22
22
|
|
23
|
-
register_middleware File.expand_path('
|
24
|
-
|
25
|
-
|
23
|
+
register_middleware File.expand_path('response', __dir__),
|
24
|
+
raise_error: [:RaiseError, 'raise_error'],
|
25
|
+
logger: [:Logger, 'logger']
|
26
26
|
|
27
27
|
def initialize(env = nil)
|
28
28
|
@env = Env.from(env) if env
|
@@ -31,8 +31,6 @@ module Faraday
|
|
31
31
|
|
32
32
|
attr_reader :env
|
33
33
|
|
34
|
-
def_delegators :env, :to_hash
|
35
|
-
|
36
34
|
def status
|
37
35
|
finished? ? env.status : nil
|
38
36
|
end
|
@@ -54,32 +52,37 @@ module Faraday
|
|
54
52
|
!!env
|
55
53
|
end
|
56
54
|
|
57
|
-
def on_complete
|
58
|
-
if
|
59
|
-
@on_complete_callbacks <<
|
55
|
+
def on_complete(&block)
|
56
|
+
if !finished?
|
57
|
+
@on_complete_callbacks << block
|
60
58
|
else
|
61
59
|
yield(env)
|
62
60
|
end
|
63
|
-
|
61
|
+
self
|
64
62
|
end
|
65
63
|
|
66
64
|
def finish(env)
|
67
|
-
raise
|
65
|
+
raise 'response already finished' if finished?
|
66
|
+
|
68
67
|
@env = env.is_a?(Env) ? env : Env.from(env)
|
69
68
|
@on_complete_callbacks.each { |callback| callback.call(@env) }
|
70
|
-
|
69
|
+
self
|
71
70
|
end
|
72
71
|
|
73
72
|
def success?
|
74
73
|
finished? && env.success?
|
75
74
|
end
|
76
75
|
|
76
|
+
def to_hash
|
77
|
+
{
|
78
|
+
status: env.status, body: env.body,
|
79
|
+
response_headers: env.response_headers
|
80
|
+
}
|
81
|
+
end
|
82
|
+
|
77
83
|
# because @on_complete_callbacks cannot be marshalled
|
78
84
|
def marshal_dump
|
79
|
-
|
80
|
-
:status => @env.status, :body => @env.body,
|
81
|
-
:response_headers => @env.response_headers
|
82
|
-
}
|
85
|
+
finished? ? to_hash : nil
|
83
86
|
end
|
84
87
|
|
85
88
|
def marshal_load(env)
|
@@ -90,8 +93,9 @@ module Faraday
|
|
90
93
|
# Useful for applying request params after restoring a marshalled Response.
|
91
94
|
def apply_request(request_env)
|
92
95
|
raise "response didn't finish yet" unless finished?
|
96
|
+
|
93
97
|
@env = Env.from(request_env).update(@env)
|
94
|
-
|
98
|
+
self
|
95
99
|
end
|
96
100
|
end
|
97
101
|
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
module Utils
|
5
|
+
# A case-insensitive Hash that preserves the original case of a header
|
6
|
+
# when set.
|
7
|
+
#
|
8
|
+
# Adapted from Rack::Utils::HeaderHash
|
9
|
+
class Headers < ::Hash
|
10
|
+
def self.from(value)
|
11
|
+
new(value)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.allocate
|
15
|
+
new_self = super
|
16
|
+
new_self.initialize_names
|
17
|
+
new_self
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(hash = nil)
|
21
|
+
super()
|
22
|
+
@names = {}
|
23
|
+
update(hash || {})
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize_names
|
27
|
+
@names = {}
|
28
|
+
end
|
29
|
+
|
30
|
+
# on dup/clone, we need to duplicate @names hash
|
31
|
+
def initialize_copy(other)
|
32
|
+
super
|
33
|
+
@names = other.names.dup
|
34
|
+
end
|
35
|
+
|
36
|
+
# need to synchronize concurrent writes to the shared KeyMap
|
37
|
+
keymap_mutex = Mutex.new
|
38
|
+
|
39
|
+
# symbol -> string mapper + cache
|
40
|
+
KeyMap = Hash.new do |map, key|
|
41
|
+
value = if key.respond_to?(:to_str)
|
42
|
+
key
|
43
|
+
else
|
44
|
+
key.to_s.split('_') # user_agent: %w(user agent)
|
45
|
+
.each(&:capitalize!) # => %w(User Agent)
|
46
|
+
.join('-') # => "User-Agent"
|
47
|
+
end
|
48
|
+
keymap_mutex.synchronize { map[key] = value }
|
49
|
+
end
|
50
|
+
KeyMap[:etag] = 'ETag'
|
51
|
+
|
52
|
+
def [](key)
|
53
|
+
key = KeyMap[key]
|
54
|
+
super(key) || super(@names[key.downcase])
|
55
|
+
end
|
56
|
+
|
57
|
+
def []=(key, val)
|
58
|
+
key = KeyMap[key]
|
59
|
+
key = (@names[key.downcase] ||= key)
|
60
|
+
# join multiple values with a comma
|
61
|
+
val = val.to_ary.join(', ') if val.respond_to?(:to_ary)
|
62
|
+
super(key, val)
|
63
|
+
end
|
64
|
+
|
65
|
+
def fetch(key, *args, &block)
|
66
|
+
key = KeyMap[key]
|
67
|
+
key = @names.fetch(key.downcase, key)
|
68
|
+
super(key, *args, &block)
|
69
|
+
end
|
70
|
+
|
71
|
+
def delete(key)
|
72
|
+
key = KeyMap[key]
|
73
|
+
key = @names[key.downcase]
|
74
|
+
return unless key
|
75
|
+
|
76
|
+
@names.delete key.downcase
|
77
|
+
super(key)
|
78
|
+
end
|
79
|
+
|
80
|
+
def include?(key)
|
81
|
+
@names.include? key.downcase
|
82
|
+
end
|
83
|
+
|
84
|
+
alias has_key? include?
|
85
|
+
alias member? include?
|
86
|
+
alias key? include?
|
87
|
+
|
88
|
+
def merge!(other)
|
89
|
+
other.each { |k, v| self[k] = v }
|
90
|
+
self
|
91
|
+
end
|
92
|
+
|
93
|
+
alias update merge!
|
94
|
+
|
95
|
+
def merge(other)
|
96
|
+
hash = dup
|
97
|
+
hash.merge! other
|
98
|
+
end
|
99
|
+
|
100
|
+
def replace(other)
|
101
|
+
clear
|
102
|
+
@names.clear
|
103
|
+
update other
|
104
|
+
self
|
105
|
+
end
|
106
|
+
|
107
|
+
def to_hash
|
108
|
+
{}.update(self)
|
109
|
+
end
|
110
|
+
|
111
|
+
def parse(header_string)
|
112
|
+
return unless header_string && !header_string.empty?
|
113
|
+
|
114
|
+
headers = header_string.split(/\r\n/)
|
115
|
+
|
116
|
+
# Find the last set of response headers.
|
117
|
+
start_index = headers.rindex { |x| x.start_with?('HTTP/') } || 0
|
118
|
+
last_response = headers.slice(start_index, headers.size)
|
119
|
+
|
120
|
+
last_response
|
121
|
+
.tap { |a| a.shift if a.first.start_with?('HTTP/') }
|
122
|
+
.map { |h| h.split(/:\s*/, 2) } # split key and value
|
123
|
+
.reject { |p| p[0].nil? } # ignore blank lines
|
124
|
+
.each { |key, value| add_parsed(key, value) }
|
125
|
+
end
|
126
|
+
|
127
|
+
protected
|
128
|
+
|
129
|
+
attr_reader :names
|
130
|
+
|
131
|
+
private
|
132
|
+
|
133
|
+
# Join multiple values with a comma.
|
134
|
+
def add_parsed(key, value)
|
135
|
+
self[key] ? self[key] << ', ' << value : self[key] = value
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
module Utils
|
5
|
+
# A hash with stringified keys.
|
6
|
+
class ParamsHash < Hash
|
7
|
+
def [](key)
|
8
|
+
super(convert_key(key))
|
9
|
+
end
|
10
|
+
|
11
|
+
def []=(key, value)
|
12
|
+
super(convert_key(key), value)
|
13
|
+
end
|
14
|
+
|
15
|
+
def delete(key)
|
16
|
+
super(convert_key(key))
|
17
|
+
end
|
18
|
+
|
19
|
+
def include?(key)
|
20
|
+
super(convert_key(key))
|
21
|
+
end
|
22
|
+
|
23
|
+
alias has_key? include?
|
24
|
+
alias member? include?
|
25
|
+
alias key? include?
|
26
|
+
|
27
|
+
def update(params)
|
28
|
+
params.each do |key, value|
|
29
|
+
self[key] = value
|
30
|
+
end
|
31
|
+
self
|
32
|
+
end
|
33
|
+
alias merge! update
|
34
|
+
|
35
|
+
def merge(params)
|
36
|
+
dup.update(params)
|
37
|
+
end
|
38
|
+
|
39
|
+
def replace(other)
|
40
|
+
clear
|
41
|
+
update(other)
|
42
|
+
end
|
43
|
+
|
44
|
+
def merge_query(query, encoder = nil)
|
45
|
+
return self unless query && !query.empty?
|
46
|
+
|
47
|
+
update((encoder || Utils.default_params_encoder).decode(query))
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_query(encoder = nil)
|
51
|
+
(encoder || Utils.default_params_encoder).encode(self)
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def convert_key(key)
|
57
|
+
key.to_s
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|