faraday 0.16.0 → 0.17.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.md +1 -1
- data/README.md +347 -18
- data/lib/faraday/adapter/em_http.rb +99 -142
- data/lib/faraday/adapter/em_http_ssl_patch.rb +17 -23
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +15 -18
- data/lib/faraday/adapter/em_synchrony.rb +60 -104
- data/lib/faraday/adapter/excon.rb +55 -100
- data/lib/faraday/adapter/httpclient.rb +39 -61
- data/lib/faraday/adapter/net_http.rb +51 -104
- data/lib/faraday/adapter/net_http_persistent.rb +27 -48
- data/lib/faraday/adapter/patron.rb +35 -54
- data/lib/faraday/adapter/rack.rb +12 -28
- data/lib/faraday/adapter/test.rb +53 -86
- data/lib/faraday/adapter/typhoeus.rb +1 -4
- data/lib/faraday/adapter.rb +22 -36
- data/lib/faraday/autoload.rb +36 -47
- data/lib/faraday/connection.rb +179 -321
- data/lib/faraday/error.rb +33 -67
- data/lib/faraday/middleware.rb +28 -4
- data/lib/faraday/options.rb +186 -35
- data/lib/faraday/parameters.rb +197 -4
- data/lib/faraday/rack_builder.rb +56 -67
- data/lib/faraday/request/authorization.rb +30 -42
- data/lib/faraday/request/basic_authentication.rb +7 -14
- data/lib/faraday/request/instrumentation.rb +27 -45
- data/lib/faraday/request/multipart.rb +48 -79
- data/lib/faraday/request/retry.rb +170 -197
- data/lib/faraday/request/token_authentication.rb +10 -15
- data/lib/faraday/request/url_encoded.rb +23 -41
- data/lib/faraday/request.rb +36 -68
- data/lib/faraday/response/logger.rb +69 -22
- data/lib/faraday/response/raise_error.rb +14 -36
- data/lib/faraday/response.rb +16 -23
- data/lib/faraday/upload_io.rb +67 -0
- data/lib/faraday/utils.rb +245 -28
- data/lib/faraday.rb +175 -93
- metadata +5 -22
- data/lib/faraday/adapter_registry.rb +0 -28
- data/lib/faraday/dependency_loader.rb +0 -37
- data/lib/faraday/encoders/flat_params_encoder.rb +0 -94
- data/lib/faraday/encoders/nested_params_encoder.rb +0 -171
- data/lib/faraday/file_part.rb +0 -128
- data/lib/faraday/logging/formatter.rb +0 -92
- data/lib/faraday/middleware_registry.rb +0 -129
- data/lib/faraday/options/connection_options.rb +0 -22
- data/lib/faraday/options/env.rb +0 -181
- data/lib/faraday/options/proxy_options.rb +0 -28
- data/lib/faraday/options/request_options.rb +0 -21
- data/lib/faraday/options/ssl_options.rb +0 -59
- data/lib/faraday/param_part.rb +0 -53
- data/lib/faraday/utils/headers.rb +0 -139
- data/lib/faraday/utils/params_hash.rb +0 -61
- data/spec/external_adapters/faraday_specs_setup.rb +0 -14
data/lib/faraday/request.rb
CHANGED
@@ -1,9 +1,6 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
module Faraday
|
4
|
-
# Used to setup
|
2
|
+
# Used to setup urls, params, headers, and the request body in a sane manner.
|
5
3
|
#
|
6
|
-
# @example
|
7
4
|
# @connection.post do |req|
|
8
5
|
# req.url 'http://localhost', 'a' => '1' # 'http://localhost?a=1'
|
9
6
|
# req.headers['b'] = '2' # Header
|
@@ -12,53 +9,25 @@ module Faraday
|
|
12
9
|
# req.body = 'abc'
|
13
10
|
# end
|
14
11
|
#
|
15
|
-
# @!attribute method
|
16
|
-
# @return [Symbol] the HTTP method of the Request
|
17
|
-
# @!attribute path
|
18
|
-
# @return [URI, String] the path
|
19
|
-
# @!attribute params
|
20
|
-
# @return [Hash] query parameters
|
21
|
-
# @!attribute headers
|
22
|
-
# @return [Faraday::Utils::Headers] headers
|
23
|
-
# @!attribute body
|
24
|
-
# @return [Hash] body
|
25
|
-
# @!attribute options
|
26
|
-
# @return [RequestOptions] options
|
27
|
-
#
|
28
|
-
# rubocop:disable Style/StructInheritance
|
29
12
|
class Request < Struct.new(:method, :path, :params, :headers, :body, :options)
|
30
|
-
# rubocop:enable Style/StructInheritance
|
31
|
-
|
32
13
|
extend MiddlewareRegistry
|
33
14
|
|
34
|
-
register_middleware File.expand_path('request',
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
],
|
43
|
-
token_auth: [
|
44
|
-
:TokenAuthentication,
|
45
|
-
'token_authentication'
|
46
|
-
],
|
47
|
-
instrumentation: [:Instrumentation, 'instrumentation']
|
15
|
+
register_middleware File.expand_path('../request', __FILE__),
|
16
|
+
:url_encoded => [:UrlEncoded, 'url_encoded'],
|
17
|
+
:multipart => [:Multipart, 'multipart'],
|
18
|
+
:retry => [:Retry, 'retry'],
|
19
|
+
:authorization => [:Authorization, 'authorization'],
|
20
|
+
:basic_auth => [:BasicAuthentication, 'basic_authentication'],
|
21
|
+
:token_auth => [:TokenAuthentication, 'token_authentication'],
|
22
|
+
:instrumentation => [:Instrumentation, 'instrumentation']
|
48
23
|
|
49
|
-
# @param request_method [String]
|
50
|
-
# @yield [request] for block customization, if block given
|
51
|
-
# @yieldparam request [Request]
|
52
|
-
# @return [Request]
|
53
24
|
def self.create(request_method)
|
54
25
|
new(request_method).tap do |request|
|
55
26
|
yield(request) if block_given?
|
56
27
|
end
|
57
28
|
end
|
58
29
|
|
59
|
-
# Replace params, preserving the existing hash type
|
60
|
-
#
|
61
|
-
# @param hash [Hash] new params
|
30
|
+
# Public: Replace params, preserving the existing hash type
|
62
31
|
def params=(hash)
|
63
32
|
if params
|
64
33
|
params.replace hash
|
@@ -67,9 +36,7 @@ module Faraday
|
|
67
36
|
end
|
68
37
|
end
|
69
38
|
|
70
|
-
# Replace request headers, preserving the existing hash type
|
71
|
-
#
|
72
|
-
# @param hash [Hash] new headers
|
39
|
+
# Public: Replace request headers, preserving the existing hash type
|
73
40
|
def headers=(hash)
|
74
41
|
if headers
|
75
42
|
headers.replace hash
|
@@ -78,14 +45,9 @@ module Faraday
|
|
78
45
|
end
|
79
46
|
end
|
80
47
|
|
81
|
-
# Update path and params.
|
82
|
-
#
|
83
|
-
# @param path [URI, String]
|
84
|
-
# @param params [Hash, nil]
|
85
|
-
# @return [void]
|
86
48
|
def url(path, params = nil)
|
87
49
|
if path.respond_to? :query
|
88
|
-
if
|
50
|
+
if query = path.query
|
89
51
|
path = path.dup
|
90
52
|
path.query = nil
|
91
53
|
end
|
@@ -99,35 +61,25 @@ module Faraday
|
|
99
61
|
self.params.update(params) if params
|
100
62
|
end
|
101
63
|
|
102
|
-
# @param key [Object] key to look up in headers
|
103
|
-
# @return [Object] value of the given header name
|
104
64
|
def [](key)
|
105
65
|
headers[key]
|
106
66
|
end
|
107
67
|
|
108
|
-
# @param key [Object] key of header to write
|
109
|
-
# @param value [Object] value of header
|
110
68
|
def []=(key, value)
|
111
69
|
headers[key] = value
|
112
70
|
end
|
113
71
|
|
114
|
-
# Marshal serialization support.
|
115
|
-
#
|
116
|
-
# @return [Hash] the hash ready to be serialized in Marshal.
|
117
72
|
def marshal_dump
|
118
73
|
{
|
119
|
-
method
|
120
|
-
body
|
121
|
-
headers
|
122
|
-
path
|
123
|
-
params
|
124
|
-
options
|
74
|
+
:method => method,
|
75
|
+
:body => body,
|
76
|
+
:headers => headers,
|
77
|
+
:path => path,
|
78
|
+
:params => params,
|
79
|
+
:options => options
|
125
80
|
}
|
126
81
|
end
|
127
82
|
|
128
|
-
# Marshal serialization support.
|
129
|
-
# Restores the instance variables according to the +serialised+.
|
130
|
-
# @param serialised [Hash] the serialised object.
|
131
83
|
def marshal_load(serialised)
|
132
84
|
self.method = serialised[:method]
|
133
85
|
self.body = serialised[:body]
|
@@ -137,10 +89,26 @@ module Faraday
|
|
137
89
|
self.options = serialised[:options]
|
138
90
|
end
|
139
91
|
|
140
|
-
#
|
92
|
+
# ENV Keys
|
93
|
+
# :method - a symbolized request method (:get, :post)
|
94
|
+
# :body - the request body that will eventually be converted to a string.
|
95
|
+
# :url - URI instance for the current request.
|
96
|
+
# :status - HTTP response status code
|
97
|
+
# :request_headers - hash of HTTP Headers to be sent to the server
|
98
|
+
# :response_headers - Hash of HTTP headers from the server
|
99
|
+
# :parallel_manager - sent if the connection is in parallel mode
|
100
|
+
# :request - Hash of options for configuring the request.
|
101
|
+
# :timeout - open/read timeout Integer in seconds
|
102
|
+
# :open_timeout - read timeout Integer in seconds
|
103
|
+
# :proxy - Hash of proxy options
|
104
|
+
# :uri - Proxy Server URI
|
105
|
+
# :user - Proxy server username
|
106
|
+
# :password - Proxy server password
|
107
|
+
# :ssl - Hash of options for configuring SSL requests.
|
141
108
|
def to_env(connection)
|
142
109
|
Env.new(method, body, connection.build_exclusive_url(path, params),
|
143
|
-
|
110
|
+
options, headers, connection.ssl, connection.parallel_manager)
|
144
111
|
end
|
145
112
|
end
|
146
113
|
end
|
114
|
+
|
@@ -1,33 +1,80 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
require 'forwardable'
|
4
|
-
require 'faraday/logging/formatter'
|
5
2
|
|
6
3
|
module Faraday
|
7
|
-
class Response
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
4
|
+
class Response::Logger < Response::Middleware
|
5
|
+
extend Forwardable
|
6
|
+
|
7
|
+
DEFAULT_OPTIONS = { :headers => true, :bodies => false }
|
8
|
+
|
9
|
+
def initialize(app, logger = nil, options = {})
|
10
|
+
super(app)
|
11
|
+
@logger = logger || begin
|
12
|
+
require 'logger'
|
13
|
+
::Logger.new($stdout)
|
14
|
+
end
|
15
|
+
@filter = []
|
16
|
+
@options = DEFAULT_OPTIONS.merge(options)
|
17
|
+
yield self if block_given?
|
18
|
+
end
|
19
|
+
|
20
|
+
def_delegators :@logger, :debug, :info, :warn, :error, :fatal
|
21
|
+
|
22
|
+
def call(env)
|
23
|
+
info('request') { "#{env.method.upcase} #{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('response') { "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]
|
21
62
|
end
|
63
|
+
end
|
22
64
|
|
23
|
-
|
24
|
-
|
25
|
-
|
65
|
+
def log_body?(type)
|
66
|
+
case @options[:bodies]
|
67
|
+
when Hash then @options[:bodies][type]
|
68
|
+
else @options[:bodies]
|
26
69
|
end
|
70
|
+
end
|
27
71
|
|
28
|
-
|
29
|
-
|
72
|
+
def apply_filters(output)
|
73
|
+
@filter.each do |pattern, replacement|
|
74
|
+
output = output.to_s.gsub(pattern, replacement)
|
30
75
|
end
|
76
|
+
output
|
31
77
|
end
|
78
|
+
|
32
79
|
end
|
33
80
|
end
|
@@ -1,43 +1,21 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
module Faraday
|
4
|
-
class Response
|
5
|
-
|
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
|
2
|
+
class Response::RaiseError < Response::Middleware
|
3
|
+
ClientErrorStatuses = 400...600
|
12
4
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
-
end
|
5
|
+
def on_complete(env)
|
6
|
+
case env[:status]
|
7
|
+
when 404
|
8
|
+
raise Faraday::Error::ResourceNotFound, response_values(env)
|
9
|
+
when 407
|
10
|
+
# mimic the behavior that we get with proxy requests with HTTPS
|
11
|
+
raise Faraday::Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
|
12
|
+
when ClientErrorStatuses
|
13
|
+
raise Faraday::Error::ClientError, response_values(env)
|
36
14
|
end
|
15
|
+
end
|
37
16
|
|
38
|
-
|
39
|
-
|
40
|
-
end
|
17
|
+
def response_values(env)
|
18
|
+
{:status => env.status, :headers => env.response_headers, :body => env.body}
|
41
19
|
end
|
42
20
|
end
|
43
21
|
end
|
data/lib/faraday/response.rb
CHANGED
@@ -1,9 +1,6 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
require 'forwardable'
|
4
2
|
|
5
3
|
module Faraday
|
6
|
-
# Response represents an HTTP response from making an HTTP request.
|
7
4
|
class Response
|
8
5
|
# Used for simple response middleware.
|
9
6
|
class Middleware < Faraday::Middleware
|
@@ -23,9 +20,9 @@ module Faraday
|
|
23
20
|
extend Forwardable
|
24
21
|
extend MiddlewareRegistry
|
25
22
|
|
26
|
-
register_middleware File.expand_path('response',
|
27
|
-
|
28
|
-
|
23
|
+
register_middleware File.expand_path('../response', __FILE__),
|
24
|
+
:raise_error => [:RaiseError, 'raise_error'],
|
25
|
+
:logger => [:Logger, 'logger']
|
29
26
|
|
30
27
|
def initialize(env = nil)
|
31
28
|
@env = Env.from(env) if env
|
@@ -34,6 +31,8 @@ module Faraday
|
|
34
31
|
|
35
32
|
attr_reader :env
|
36
33
|
|
34
|
+
def_delegators :env, :to_hash
|
35
|
+
|
37
36
|
def status
|
38
37
|
finished? ? env.status : nil
|
39
38
|
end
|
@@ -55,37 +54,32 @@ module Faraday
|
|
55
54
|
!!env
|
56
55
|
end
|
57
56
|
|
58
|
-
def on_complete
|
59
|
-
if
|
60
|
-
@on_complete_callbacks <<
|
57
|
+
def on_complete
|
58
|
+
if not finished?
|
59
|
+
@on_complete_callbacks << Proc.new
|
61
60
|
else
|
62
61
|
yield(env)
|
63
62
|
end
|
64
|
-
self
|
63
|
+
return self
|
65
64
|
end
|
66
65
|
|
67
66
|
def finish(env)
|
68
|
-
raise
|
69
|
-
|
67
|
+
raise "response already finished" if finished?
|
70
68
|
@env = env.is_a?(Env) ? env : Env.from(env)
|
71
69
|
@on_complete_callbacks.each { |callback| callback.call(@env) }
|
72
|
-
self
|
70
|
+
return self
|
73
71
|
end
|
74
72
|
|
75
73
|
def success?
|
76
74
|
finished? && env.success?
|
77
75
|
end
|
78
76
|
|
79
|
-
def to_hash
|
80
|
-
{
|
81
|
-
status: env.status, body: env.body,
|
82
|
-
response_headers: env.response_headers
|
83
|
-
}
|
84
|
-
end
|
85
|
-
|
86
77
|
# because @on_complete_callbacks cannot be marshalled
|
87
78
|
def marshal_dump
|
88
|
-
finished? ?
|
79
|
+
!finished? ? nil : {
|
80
|
+
:status => @env.status, :body => @env.body,
|
81
|
+
:response_headers => @env.response_headers
|
82
|
+
}
|
89
83
|
end
|
90
84
|
|
91
85
|
def marshal_load(env)
|
@@ -96,9 +90,8 @@ module Faraday
|
|
96
90
|
# Useful for applying request params after restoring a marshalled Response.
|
97
91
|
def apply_request(request_env)
|
98
92
|
raise "response didn't finish yet" unless finished?
|
99
|
-
|
100
93
|
@env = Env.from(request_env).update(@env)
|
101
|
-
self
|
94
|
+
return self
|
102
95
|
end
|
103
96
|
end
|
104
97
|
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
begin
|
2
|
+
require 'composite_io'
|
3
|
+
require 'parts'
|
4
|
+
require 'stringio'
|
5
|
+
rescue LoadError
|
6
|
+
$stderr.puts "Install the multipart-post gem."
|
7
|
+
raise
|
8
|
+
end
|
9
|
+
|
10
|
+
module Faraday
|
11
|
+
# Similar but not compatible with ::CompositeReadIO provided by multipart-post.
|
12
|
+
class CompositeReadIO
|
13
|
+
def initialize(*parts)
|
14
|
+
@parts = parts.flatten
|
15
|
+
@ios = @parts.map { |part| part.to_io }
|
16
|
+
@index = 0
|
17
|
+
end
|
18
|
+
|
19
|
+
def length
|
20
|
+
@parts.inject(0) { |sum, part| sum + part.length }
|
21
|
+
end
|
22
|
+
|
23
|
+
def rewind
|
24
|
+
@ios.each { |io| io.rewind }
|
25
|
+
@index = 0
|
26
|
+
end
|
27
|
+
|
28
|
+
# Read from IOs in order until `length` bytes have been received.
|
29
|
+
def read(length = nil, outbuf = nil)
|
30
|
+
got_result = false
|
31
|
+
outbuf = outbuf ? outbuf.replace("") : ""
|
32
|
+
|
33
|
+
while io = current_io
|
34
|
+
if result = io.read(length)
|
35
|
+
got_result ||= !result.nil?
|
36
|
+
result.force_encoding("BINARY") if result.respond_to?(:force_encoding)
|
37
|
+
outbuf << result
|
38
|
+
length -= result.length if length
|
39
|
+
break if length == 0
|
40
|
+
end
|
41
|
+
advance_io
|
42
|
+
end
|
43
|
+
(!got_result && length) ? nil : outbuf
|
44
|
+
end
|
45
|
+
|
46
|
+
def close
|
47
|
+
@ios.each { |io| io.close }
|
48
|
+
end
|
49
|
+
|
50
|
+
def ensure_open_and_readable
|
51
|
+
# Rubinius compatibility
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def current_io
|
57
|
+
@ios[@index]
|
58
|
+
end
|
59
|
+
|
60
|
+
def advance_io
|
61
|
+
@index += 1
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
UploadIO = ::UploadIO
|
66
|
+
Parts = ::Parts
|
67
|
+
end
|