faraday 0.17.6 → 1.0.0.pre.rc1
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 +18 -358
- data/lib/faraday/adapter/em_http.rb +142 -99
- data/lib/faraday/adapter/em_http_ssl_patch.rb +23 -17
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
- data/lib/faraday/adapter/em_synchrony.rb +104 -60
- data/lib/faraday/adapter/excon.rb +97 -57
- data/lib/faraday/adapter/httpclient.rb +61 -39
- data/lib/faraday/adapter/net_http.rb +103 -51
- data/lib/faraday/adapter/net_http_persistent.rb +49 -28
- data/lib/faraday/adapter/patron.rb +54 -35
- data/lib/faraday/adapter/rack.rb +28 -12
- data/lib/faraday/adapter/test.rb +86 -53
- data/lib/faraday/adapter/typhoeus.rb +4 -1
- data/lib/faraday/adapter.rb +36 -22
- data/lib/faraday/adapter_registry.rb +28 -0
- data/lib/faraday/autoload.rb +47 -36
- data/lib/faraday/connection.rb +321 -179
- data/lib/faraday/dependency_loader.rb +37 -0
- data/lib/faraday/encoders/flat_params_encoder.rb +94 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +171 -0
- data/lib/faraday/error.rb +21 -79
- data/lib/faraday/logging/formatter.rb +92 -0
- data/lib/faraday/middleware.rb +4 -28
- 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 +28 -0
- data/lib/faraday/options/request_options.rb +21 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/options.rb +33 -184
- data/lib/faraday/parameters.rb +4 -197
- data/lib/faraday/rack_builder.rb +66 -55
- data/lib/faraday/request/authorization.rb +42 -30
- data/lib/faraday/request/basic_authentication.rb +14 -7
- data/lib/faraday/request/instrumentation.rb +45 -27
- data/lib/faraday/request/multipart.rb +72 -49
- data/lib/faraday/request/retry.rb +197 -171
- data/lib/faraday/request/token_authentication.rb +15 -10
- data/lib/faraday/request/url_encoded.rb +41 -23
- data/lib/faraday/request.rb +68 -38
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +36 -18
- data/lib/faraday/response.rb +22 -15
- data/lib/faraday/upload_io.rb +31 -30
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +28 -245
- data/lib/faraday.rb +93 -174
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- metadata +25 -51
- data/CHANGELOG.md +0 -232
- data/Rakefile +0 -13
- data/lib/faraday/deprecate.rb +0 -109
- data/spec/faraday/deprecate_spec.rb +0 -147
- data/spec/faraday/error_spec.rb +0 -102
- data/spec/faraday/response/raise_error_spec.rb +0 -106
- data/spec/spec_helper.rb +0 -105
- data/test/adapters/default_test.rb +0 -14
- data/test/adapters/em_http_test.rb +0 -30
- data/test/adapters/em_synchrony_test.rb +0 -32
- data/test/adapters/excon_test.rb +0 -30
- data/test/adapters/httpclient_test.rb +0 -34
- data/test/adapters/integration.rb +0 -263
- data/test/adapters/logger_test.rb +0 -136
- data/test/adapters/net_http_persistent_test.rb +0 -114
- data/test/adapters/net_http_test.rb +0 -79
- data/test/adapters/patron_test.rb +0 -40
- data/test/adapters/rack_test.rb +0 -38
- data/test/adapters/test_middleware_test.rb +0 -157
- data/test/adapters/typhoeus_test.rb +0 -38
- data/test/authentication_middleware_test.rb +0 -65
- data/test/composite_read_io_test.rb +0 -109
- data/test/connection_test.rb +0 -738
- data/test/env_test.rb +0 -268
- data/test/helper.rb +0 -75
- data/test/live_server.rb +0 -67
- data/test/middleware/instrumentation_test.rb +0 -88
- data/test/middleware/retry_test.rb +0 -282
- data/test/middleware_stack_test.rb +0 -260
- data/test/multibyte.txt +0 -1
- data/test/options_test.rb +0 -333
- data/test/parameters_test.rb +0 -157
- data/test/request_middleware_test.rb +0 -126
- data/test/response_middleware_test.rb +0 -72
- data/test/strawberry.rb +0 -2
- data/test/utils_test.rb +0 -98
@@ -1,36 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Faraday
|
2
|
-
class Request
|
3
|
-
|
4
|
+
class Request
|
5
|
+
# Middleware for supporting urlencoded requests.
|
6
|
+
class UrlEncoded < Faraday::Middleware
|
7
|
+
CONTENT_TYPE = 'Content-Type' unless defined? CONTENT_TYPE
|
4
8
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
+
class << self
|
10
|
+
attr_accessor :mime_type
|
11
|
+
end
|
12
|
+
self.mime_type = 'application/x-www-form-urlencoded'
|
9
13
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
+
# Encodes as "application/x-www-form-urlencoded" if not already encoded or
|
15
|
+
# of another type.
|
16
|
+
#
|
17
|
+
# @param env [Faraday::Env]
|
18
|
+
def call(env)
|
19
|
+
match_content_type(env) do |data|
|
20
|
+
params = Faraday::Utils::ParamsHash[data]
|
21
|
+
env.body = params.to_query(env.params_encoder)
|
22
|
+
end
|
23
|
+
@app.call env
|
14
24
|
end
|
15
|
-
@app.call env
|
16
|
-
end
|
17
25
|
|
18
|
-
|
19
|
-
|
26
|
+
# @param env [Faraday::Env]
|
27
|
+
# @yield [request_body] Body of the request
|
28
|
+
def match_content_type(env)
|
29
|
+
return unless process_request?(env)
|
30
|
+
|
20
31
|
env.request_headers[CONTENT_TYPE] ||= self.class.mime_type
|
21
32
|
yield(env.body) unless env.body.respond_to?(:to_str)
|
22
33
|
end
|
23
|
-
end
|
24
34
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
35
|
+
# @param env [Faraday::Env]
|
36
|
+
#
|
37
|
+
# @return [Boolean] True if the request has a body and its Content-Type is
|
38
|
+
# urlencoded.
|
39
|
+
def process_request?(env)
|
40
|
+
type = request_type(env)
|
41
|
+
env.body && (type.empty? || (type == self.class.mime_type))
|
42
|
+
end
|
29
43
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
44
|
+
# @param env [Faraday::Env]
|
45
|
+
#
|
46
|
+
# @return [String]
|
47
|
+
def request_type(env)
|
48
|
+
type = env.request_headers[CONTENT_TYPE].to_s
|
49
|
+
type = type.split(';', 2).first if type.index(';')
|
50
|
+
type
|
51
|
+
end
|
34
52
|
end
|
35
53
|
end
|
36
54
|
end
|
data/lib/faraday/request.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Faraday
|
2
|
-
# Used to setup
|
4
|
+
# Used to setup URLs, params, headers, and the request body in a sane manner.
|
3
5
|
#
|
6
|
+
# @example
|
4
7
|
# @connection.post do |req|
|
5
8
|
# req.url 'http://localhost', 'a' => '1' # 'http://localhost?a=1'
|
6
9
|
# req.headers['b'] = '2' # Header
|
@@ -9,27 +12,53 @@ module Faraday
|
|
9
12
|
# req.body = 'abc'
|
10
13
|
# end
|
11
14
|
#
|
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
|
12
29
|
class Request < Struct.new(:method, :path, :params, :headers, :body, :options)
|
13
|
-
|
30
|
+
# rubocop:enable Style/StructInheritance
|
14
31
|
|
15
|
-
|
32
|
+
extend MiddlewareRegistry
|
16
33
|
|
17
|
-
register_middleware File.expand_path('
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
34
|
+
register_middleware File.expand_path('request', __dir__),
|
35
|
+
url_encoded: [:UrlEncoded, 'url_encoded'],
|
36
|
+
multipart: [:Multipart, 'multipart'],
|
37
|
+
retry: [:Retry, 'retry'],
|
38
|
+
authorization: [:Authorization, 'authorization'],
|
39
|
+
basic_auth: [
|
40
|
+
:BasicAuthentication,
|
41
|
+
'basic_authentication'
|
42
|
+
],
|
43
|
+
token_auth: [
|
44
|
+
:TokenAuthentication,
|
45
|
+
'token_authentication'
|
46
|
+
],
|
47
|
+
instrumentation: [:Instrumentation, 'instrumentation']
|
25
48
|
|
49
|
+
# @param request_method [String]
|
50
|
+
# @yield [request] for block customization, if block given
|
51
|
+
# @yieldparam request [Request]
|
52
|
+
# @return [Request]
|
26
53
|
def self.create(request_method)
|
27
54
|
new(request_method).tap do |request|
|
28
55
|
yield(request) if block_given?
|
29
56
|
end
|
30
57
|
end
|
31
58
|
|
32
|
-
#
|
59
|
+
# Replace params, preserving the existing hash type.
|
60
|
+
#
|
61
|
+
# @param hash [Hash] new params
|
33
62
|
def params=(hash)
|
34
63
|
if params
|
35
64
|
params.replace hash
|
@@ -38,7 +67,9 @@ module Faraday
|
|
38
67
|
end
|
39
68
|
end
|
40
69
|
|
41
|
-
#
|
70
|
+
# Replace request headers, preserving the existing hash type.
|
71
|
+
#
|
72
|
+
# @param hash [Hash] new headers
|
42
73
|
def headers=(hash)
|
43
74
|
if headers
|
44
75
|
headers.replace hash
|
@@ -47,9 +78,14 @@ module Faraday
|
|
47
78
|
end
|
48
79
|
end
|
49
80
|
|
81
|
+
# Update path and params.
|
82
|
+
#
|
83
|
+
# @param path [URI, String]
|
84
|
+
# @param params [Hash, nil]
|
85
|
+
# @return [void]
|
50
86
|
def url(path, params = nil)
|
51
87
|
if path.respond_to? :query
|
52
|
-
if query = path.query
|
88
|
+
if (query = path.query)
|
53
89
|
path = path.dup
|
54
90
|
path.query = nil
|
55
91
|
end
|
@@ -63,25 +99,35 @@ module Faraday
|
|
63
99
|
self.params.update(params) if params
|
64
100
|
end
|
65
101
|
|
102
|
+
# @param key [Object] key to look up in headers
|
103
|
+
# @return [Object] value of the given header name
|
66
104
|
def [](key)
|
67
105
|
headers[key]
|
68
106
|
end
|
69
107
|
|
108
|
+
# @param key [Object] key of header to write
|
109
|
+
# @param value [Object] value of header
|
70
110
|
def []=(key, value)
|
71
111
|
headers[key] = value
|
72
112
|
end
|
73
113
|
|
114
|
+
# Marshal serialization support.
|
115
|
+
#
|
116
|
+
# @return [Hash] the hash ready to be serialized in Marshal.
|
74
117
|
def marshal_dump
|
75
118
|
{
|
76
|
-
:
|
77
|
-
:
|
78
|
-
:
|
79
|
-
:
|
80
|
-
:
|
81
|
-
:
|
119
|
+
method: method,
|
120
|
+
body: body,
|
121
|
+
headers: headers,
|
122
|
+
path: path,
|
123
|
+
params: params,
|
124
|
+
options: options
|
82
125
|
}
|
83
126
|
end
|
84
127
|
|
128
|
+
# Marshal serialization support.
|
129
|
+
# Restores the instance variables according to the +serialised+.
|
130
|
+
# @param serialised [Hash] the serialised object.
|
85
131
|
def marshal_load(serialised)
|
86
132
|
self.method = serialised[:method]
|
87
133
|
self.body = serialised[:body]
|
@@ -91,26 +137,10 @@ module Faraday
|
|
91
137
|
self.options = serialised[:options]
|
92
138
|
end
|
93
139
|
|
94
|
-
#
|
95
|
-
# :method - a symbolized request method (:get, :post)
|
96
|
-
# :body - the request body that will eventually be converted to a string.
|
97
|
-
# :url - URI instance for the current request.
|
98
|
-
# :status - HTTP response status code
|
99
|
-
# :request_headers - hash of HTTP Headers to be sent to the server
|
100
|
-
# :response_headers - Hash of HTTP headers from the server
|
101
|
-
# :parallel_manager - sent if the connection is in parallel mode
|
102
|
-
# :request - Hash of options for configuring the request.
|
103
|
-
# :timeout - open/read timeout Integer in seconds
|
104
|
-
# :open_timeout - read timeout Integer in seconds
|
105
|
-
# :proxy - Hash of proxy options
|
106
|
-
# :uri - Proxy Server URI
|
107
|
-
# :user - Proxy server username
|
108
|
-
# :password - Proxy server password
|
109
|
-
# :ssl - Hash of options for configuring SSL requests.
|
140
|
+
# @return [Env] the Env for this Request
|
110
141
|
def to_env(connection)
|
111
142
|
Env.new(method, body, connection.build_exclusive_url(path, params),
|
112
|
-
|
143
|
+
options, headers, connection.ssl, connection.parallel_manager)
|
113
144
|
end
|
114
145
|
end
|
115
146
|
end
|
116
|
-
|
@@ -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('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]
|
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,25 +1,43 @@
|
|
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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
+
end
|
18
36
|
end
|
19
|
-
end
|
20
37
|
|
21
|
-
|
22
|
-
|
38
|
+
def response_values(env)
|
39
|
+
{ status: env.status, headers: env.response_headers, body: env.body }
|
40
|
+
end
|
23
41
|
end
|
24
42
|
end
|
25
43
|
end
|
data/lib/faraday/response.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
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
|
@@ -20,9 +23,9 @@ module Faraday
|
|
20
23
|
extend Forwardable
|
21
24
|
extend MiddlewareRegistry
|
22
25
|
|
23
|
-
register_middleware File.expand_path('
|
24
|
-
|
25
|
-
|
26
|
+
register_middleware File.expand_path('response', __dir__),
|
27
|
+
raise_error: [:RaiseError, 'raise_error'],
|
28
|
+
logger: [:Logger, 'logger']
|
26
29
|
|
27
30
|
def initialize(env = nil)
|
28
31
|
@env = Env.from(env) if env
|
@@ -31,8 +34,6 @@ module Faraday
|
|
31
34
|
|
32
35
|
attr_reader :env
|
33
36
|
|
34
|
-
def_delegators :env, :to_hash
|
35
|
-
|
36
37
|
def status
|
37
38
|
finished? ? env.status : nil
|
38
39
|
end
|
@@ -54,32 +55,37 @@ module Faraday
|
|
54
55
|
!!env
|
55
56
|
end
|
56
57
|
|
57
|
-
def on_complete
|
58
|
+
def on_complete
|
58
59
|
if !finished?
|
59
|
-
@on_complete_callbacks <<
|
60
|
+
@on_complete_callbacks << Proc.new
|
60
61
|
else
|
61
62
|
yield(env)
|
62
63
|
end
|
63
|
-
|
64
|
+
self
|
64
65
|
end
|
65
66
|
|
66
67
|
def finish(env)
|
67
|
-
raise
|
68
|
+
raise 'response already finished' if finished?
|
69
|
+
|
68
70
|
@env = env.is_a?(Env) ? env : Env.from(env)
|
69
71
|
@on_complete_callbacks.each { |callback| callback.call(@env) }
|
70
|
-
|
72
|
+
self
|
71
73
|
end
|
72
74
|
|
73
75
|
def success?
|
74
76
|
finished? && env.success?
|
75
77
|
end
|
76
78
|
|
79
|
+
def to_hash
|
80
|
+
{
|
81
|
+
status: env.status, body: env.body,
|
82
|
+
response_headers: env.response_headers
|
83
|
+
}
|
84
|
+
end
|
85
|
+
|
77
86
|
# because @on_complete_callbacks cannot be marshalled
|
78
87
|
def marshal_dump
|
79
|
-
|
80
|
-
:status => @env.status, :body => @env.body,
|
81
|
-
:response_headers => @env.response_headers
|
82
|
-
}
|
88
|
+
finished? ? to_hash : nil
|
83
89
|
end
|
84
90
|
|
85
91
|
def marshal_load(env)
|
@@ -90,8 +96,9 @@ module Faraday
|
|
90
96
|
# Useful for applying request params after restoring a marshalled Response.
|
91
97
|
def apply_request(request_env)
|
92
98
|
raise "response didn't finish yet" unless finished?
|
99
|
+
|
93
100
|
@env = Env.from(request_env).update(@env)
|
94
|
-
|
101
|
+
self
|
95
102
|
end
|
96
103
|
end
|
97
104
|
end
|
data/lib/faraday/upload_io.rb
CHANGED
@@ -1,55 +1,61 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
require 'stringio'
|
9
|
-
rescue LoadError
|
10
|
-
$stderr.puts "Install the multipart-post gem."
|
11
|
-
raise
|
12
|
-
end
|
13
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
# multipart-post gem
|
6
|
+
require 'composite_io'
|
7
|
+
require 'parts'
|
14
8
|
|
15
9
|
module Faraday
|
16
|
-
# Similar but not compatible with
|
10
|
+
# Similar to, but not compatible with CompositeReadIO provided by the
|
11
|
+
# multipart-post gem.
|
12
|
+
# https://github.com/nicksieger/multipart-post/blob/master/lib/composite_io.rb
|
17
13
|
class CompositeReadIO
|
18
14
|
def initialize(*parts)
|
19
15
|
@parts = parts.flatten
|
20
|
-
@ios = @parts.map
|
16
|
+
@ios = @parts.map(&:to_io)
|
21
17
|
@index = 0
|
22
18
|
end
|
23
19
|
|
20
|
+
# @return [Integer] sum of the lengths of all the parts
|
24
21
|
def length
|
25
22
|
@parts.inject(0) { |sum, part| sum + part.length }
|
26
23
|
end
|
27
24
|
|
25
|
+
# Rewind each of the IOs and reset the index to 0.
|
26
|
+
#
|
27
|
+
# @return [void]
|
28
28
|
def rewind
|
29
|
-
@ios.each
|
29
|
+
@ios.each(&:rewind)
|
30
30
|
@index = 0
|
31
31
|
end
|
32
32
|
|
33
33
|
# Read from IOs in order until `length` bytes have been received.
|
34
|
+
#
|
35
|
+
# @param length [Integer, nil]
|
36
|
+
# @param outbuf [String, nil]
|
34
37
|
def read(length = nil, outbuf = nil)
|
35
38
|
got_result = false
|
36
|
-
outbuf = outbuf ? outbuf.replace(
|
39
|
+
outbuf = outbuf ? (+outbuf).replace('') : +''
|
37
40
|
|
38
|
-
while io = current_io
|
39
|
-
if result = io.read(length)
|
41
|
+
while (io = current_io)
|
42
|
+
if (result = io.read(length))
|
40
43
|
got_result ||= !result.nil?
|
41
|
-
result.force_encoding(
|
44
|
+
result.force_encoding('BINARY') if result.respond_to?(:force_encoding)
|
42
45
|
outbuf << result
|
43
46
|
length -= result.length if length
|
44
|
-
break if length
|
47
|
+
break if length&.zero?
|
45
48
|
end
|
46
49
|
advance_io
|
47
50
|
end
|
48
|
-
|
51
|
+
!got_result && length ? nil : outbuf
|
49
52
|
end
|
50
53
|
|
54
|
+
# Close each of the IOs.
|
55
|
+
#
|
56
|
+
# @return [void]
|
51
57
|
def close
|
52
|
-
@ios.each
|
58
|
+
@ios.each(&:close)
|
53
59
|
end
|
54
60
|
|
55
61
|
def ensure_open_and_readable
|
@@ -67,11 +73,6 @@ module Faraday
|
|
67
73
|
end
|
68
74
|
end
|
69
75
|
|
70
|
-
|
71
|
-
|
72
|
-
Parts = ::Multipart::Post::Parts
|
73
|
-
else
|
74
|
-
UploadIO = ::UploadIO
|
75
|
-
Parts = ::Parts
|
76
|
-
end
|
76
|
+
UploadIO = ::UploadIO
|
77
|
+
Parts = ::Parts
|
77
78
|
end
|