faraday 1.4.2 → 1.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/examples/client_spec.rb +34 -2
- data/examples/client_test.rb +41 -2
- data/lib/faraday/adapter/test.rb +59 -43
- data/lib/faraday/adapter.rb +1 -4
- data/lib/faraday/autoload.rb +1 -4
- data/lib/faraday/connection.rb +26 -5
- data/lib/faraday/error.rb +0 -6
- data/lib/faraday/request/authorization.rb +14 -7
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request.rb +7 -8
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +2 -4
- data/lib/faraday/response.rb +3 -1
- data/lib/faraday/version.rb +1 -1
- data/lib/faraday.rb +18 -5
- data/spec/faraday/adapter/em_http_spec.rb +39 -37
- data/spec/faraday/adapter/em_synchrony_spec.rb +11 -9
- data/spec/faraday/adapter/test_spec.rb +117 -0
- data/spec/faraday/connection_spec.rb +7 -0
- data/spec/faraday/request/authorization_spec.rb +8 -0
- data/spec/faraday/request/json_spec.rb +111 -0
- data/spec/faraday/response/json_spec.rb +119 -0
- metadata +68 -23
- data/lib/faraday/adapter/httpclient.rb +0 -152
- data/lib/faraday/adapter/patron.rb +0 -132
- data/lib/faraday/adapter/rack.rb +0 -75
- data/lib/faraday/file_part.rb +0 -128
- data/lib/faraday/param_part.rb +0 -53
- data/lib/faraday/request/multipart.rb +0 -106
- data/lib/faraday/request/retry.rb +0 -239
- data/spec/faraday/request/multipart_spec.rb +0 -302
- data/spec/faraday/request/retry_spec.rb +0 -242
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "@technoweenie"
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2022-02-17 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: faraday-em_http
|
@@ -54,6 +54,34 @@ dependencies:
|
|
54
54
|
- - "~>"
|
55
55
|
- !ruby/object:Gem::Version
|
56
56
|
version: '1.1'
|
57
|
+
- !ruby/object:Gem::Dependency
|
58
|
+
name: faraday-httpclient
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - "~>"
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '1.0'
|
64
|
+
type: :runtime
|
65
|
+
prerelease: false
|
66
|
+
version_requirements: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - "~>"
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '1.0'
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: faraday-multipart
|
73
|
+
requirement: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - "~>"
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '1.0'
|
78
|
+
type: :runtime
|
79
|
+
prerelease: false
|
80
|
+
version_requirements: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - "~>"
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '1.0'
|
57
85
|
- !ruby/object:Gem::Dependency
|
58
86
|
name: faraday-net_http
|
59
87
|
requirement: !ruby/object:Gem::Requirement
|
@@ -74,34 +102,56 @@ dependencies:
|
|
74
102
|
requirements:
|
75
103
|
- - "~>"
|
76
104
|
- !ruby/object:Gem::Version
|
77
|
-
version: '1.
|
105
|
+
version: '1.0'
|
78
106
|
type: :runtime
|
79
107
|
prerelease: false
|
80
108
|
version_requirements: !ruby/object:Gem::Requirement
|
81
109
|
requirements:
|
82
110
|
- - "~>"
|
83
111
|
- !ruby/object:Gem::Version
|
84
|
-
version: '1.
|
112
|
+
version: '1.0'
|
85
113
|
- !ruby/object:Gem::Dependency
|
86
|
-
name:
|
114
|
+
name: faraday-patron
|
87
115
|
requirement: !ruby/object:Gem::Requirement
|
88
116
|
requirements:
|
89
|
-
- - "
|
117
|
+
- - "~>"
|
90
118
|
- !ruby/object:Gem::Version
|
91
|
-
version: '1.
|
92
|
-
|
119
|
+
version: '1.0'
|
120
|
+
type: :runtime
|
121
|
+
prerelease: false
|
122
|
+
version_requirements: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - "~>"
|
93
125
|
- !ruby/object:Gem::Version
|
94
|
-
version: '
|
126
|
+
version: '1.0'
|
127
|
+
- !ruby/object:Gem::Dependency
|
128
|
+
name: faraday-rack
|
129
|
+
requirement: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - "~>"
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '1.0'
|
95
134
|
type: :runtime
|
96
135
|
prerelease: false
|
97
136
|
version_requirements: !ruby/object:Gem::Requirement
|
98
137
|
requirements:
|
99
|
-
- - "
|
138
|
+
- - "~>"
|
100
139
|
- !ruby/object:Gem::Version
|
101
|
-
version: '1.
|
102
|
-
|
140
|
+
version: '1.0'
|
141
|
+
- !ruby/object:Gem::Dependency
|
142
|
+
name: faraday-retry
|
143
|
+
requirement: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - "~>"
|
103
146
|
- !ruby/object:Gem::Version
|
104
|
-
version: '
|
147
|
+
version: '1.0'
|
148
|
+
type: :runtime
|
149
|
+
prerelease: false
|
150
|
+
version_requirements: !ruby/object:Gem::Requirement
|
151
|
+
requirements:
|
152
|
+
- - "~>"
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '1.0'
|
105
155
|
- !ruby/object:Gem::Dependency
|
106
156
|
name: ruby2_keywords
|
107
157
|
requirement: !ruby/object:Gem::Requirement
|
@@ -130,9 +180,6 @@ files:
|
|
130
180
|
- examples/client_test.rb
|
131
181
|
- lib/faraday.rb
|
132
182
|
- lib/faraday/adapter.rb
|
133
|
-
- lib/faraday/adapter/httpclient.rb
|
134
|
-
- lib/faraday/adapter/patron.rb
|
135
|
-
- lib/faraday/adapter/rack.rb
|
136
183
|
- lib/faraday/adapter/test.rb
|
137
184
|
- lib/faraday/adapter/typhoeus.rb
|
138
185
|
- lib/faraday/adapter_registry.rb
|
@@ -142,7 +189,6 @@ files:
|
|
142
189
|
- lib/faraday/encoders/flat_params_encoder.rb
|
143
190
|
- lib/faraday/encoders/nested_params_encoder.rb
|
144
191
|
- lib/faraday/error.rb
|
145
|
-
- lib/faraday/file_part.rb
|
146
192
|
- lib/faraday/logging/formatter.rb
|
147
193
|
- lib/faraday/methods.rb
|
148
194
|
- lib/faraday/middleware.rb
|
@@ -153,18 +199,17 @@ files:
|
|
153
199
|
- lib/faraday/options/proxy_options.rb
|
154
200
|
- lib/faraday/options/request_options.rb
|
155
201
|
- lib/faraday/options/ssl_options.rb
|
156
|
-
- lib/faraday/param_part.rb
|
157
202
|
- lib/faraday/parameters.rb
|
158
203
|
- lib/faraday/rack_builder.rb
|
159
204
|
- lib/faraday/request.rb
|
160
205
|
- lib/faraday/request/authorization.rb
|
161
206
|
- lib/faraday/request/basic_authentication.rb
|
162
207
|
- lib/faraday/request/instrumentation.rb
|
163
|
-
- lib/faraday/request/
|
164
|
-
- lib/faraday/request/retry.rb
|
208
|
+
- lib/faraday/request/json.rb
|
165
209
|
- lib/faraday/request/token_authentication.rb
|
166
210
|
- lib/faraday/request/url_encoded.rb
|
167
211
|
- lib/faraday/response.rb
|
212
|
+
- lib/faraday/response/json.rb
|
168
213
|
- lib/faraday/response/logger.rb
|
169
214
|
- lib/faraday/response/raise_error.rb
|
170
215
|
- lib/faraday/utils.rb
|
@@ -196,10 +241,10 @@ files:
|
|
196
241
|
- spec/faraday/rack_builder_spec.rb
|
197
242
|
- spec/faraday/request/authorization_spec.rb
|
198
243
|
- spec/faraday/request/instrumentation_spec.rb
|
199
|
-
- spec/faraday/request/
|
200
|
-
- spec/faraday/request/retry_spec.rb
|
244
|
+
- spec/faraday/request/json_spec.rb
|
201
245
|
- spec/faraday/request/url_encoded_spec.rb
|
202
246
|
- spec/faraday/request_spec.rb
|
247
|
+
- spec/faraday/response/json_spec.rb
|
203
248
|
- spec/faraday/response/logger_spec.rb
|
204
249
|
- spec/faraday/response/middleware_spec.rb
|
205
250
|
- spec/faraday/response/raise_error_spec.rb
|
@@ -221,7 +266,7 @@ licenses:
|
|
221
266
|
- MIT
|
222
267
|
metadata:
|
223
268
|
homepage_uri: https://lostisland.github.io/faraday
|
224
|
-
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.
|
269
|
+
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.10.0
|
225
270
|
source_code_uri: https://github.com/lostisland/faraday
|
226
271
|
bug_tracker_uri: https://github.com/lostisland/faraday/issues
|
227
272
|
post_install_message:
|
@@ -1,152 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Faraday
|
4
|
-
class Adapter
|
5
|
-
# HTTPClient adapter.
|
6
|
-
class HTTPClient < Faraday::Adapter
|
7
|
-
dependency 'httpclient'
|
8
|
-
|
9
|
-
def build_connection(env)
|
10
|
-
@client ||= ::HTTPClient.new.tap do |cli|
|
11
|
-
# enable compression
|
12
|
-
cli.transparent_gzip_decompression = true
|
13
|
-
end
|
14
|
-
|
15
|
-
if (req = env[:request])
|
16
|
-
if (proxy = req[:proxy])
|
17
|
-
configure_proxy @client, proxy
|
18
|
-
end
|
19
|
-
|
20
|
-
if (bind = req[:bind])
|
21
|
-
configure_socket @client, bind
|
22
|
-
end
|
23
|
-
|
24
|
-
configure_timeouts @client, req
|
25
|
-
end
|
26
|
-
|
27
|
-
if env[:url].scheme == 'https' && (ssl = env[:ssl])
|
28
|
-
configure_ssl @client, ssl
|
29
|
-
end
|
30
|
-
|
31
|
-
configure_client @client
|
32
|
-
|
33
|
-
@client
|
34
|
-
end
|
35
|
-
|
36
|
-
def call(env)
|
37
|
-
super
|
38
|
-
|
39
|
-
# TODO: Don't stream yet.
|
40
|
-
# https://github.com/nahi/httpclient/pull/90
|
41
|
-
env[:body] = env[:body].read if env[:body].respond_to? :read
|
42
|
-
|
43
|
-
connection(env) do |http|
|
44
|
-
resp = http.request env[:method], env[:url],
|
45
|
-
body: env[:body],
|
46
|
-
header: env[:request_headers]
|
47
|
-
|
48
|
-
if (req = env[:request]).stream_response?
|
49
|
-
warn "Streaming downloads for #{self.class.name} " \
|
50
|
-
'are not yet implemented.'
|
51
|
-
req.on_data.call(resp.body, resp.body.bytesize)
|
52
|
-
end
|
53
|
-
save_response env, resp.status, resp.body, resp.headers, resp.reason
|
54
|
-
|
55
|
-
@app.call env
|
56
|
-
end
|
57
|
-
rescue ::HTTPClient::TimeoutError, Errno::ETIMEDOUT
|
58
|
-
raise Faraday::TimeoutError, $ERROR_INFO
|
59
|
-
rescue ::HTTPClient::BadResponseError => e
|
60
|
-
if e.message.include?('status 407')
|
61
|
-
raise Faraday::ConnectionFailed,
|
62
|
-
%(407 "Proxy Authentication Required ")
|
63
|
-
end
|
64
|
-
|
65
|
-
raise Faraday::ClientError, $ERROR_INFO
|
66
|
-
rescue Errno::EADDRNOTAVAIL, Errno::ECONNREFUSED, IOError, SocketError
|
67
|
-
raise Faraday::ConnectionFailed, $ERROR_INFO
|
68
|
-
rescue StandardError => e
|
69
|
-
if defined?(::OpenSSL::SSL::SSLError) && \
|
70
|
-
e.is_a?(::OpenSSL::SSL::SSLError)
|
71
|
-
raise Faraday::SSLError, e
|
72
|
-
end
|
73
|
-
|
74
|
-
raise
|
75
|
-
end
|
76
|
-
|
77
|
-
# @param bind [Hash]
|
78
|
-
def configure_socket(client, bind)
|
79
|
-
client.socket_local.host = bind[:host]
|
80
|
-
client.socket_local.port = bind[:port]
|
81
|
-
end
|
82
|
-
|
83
|
-
# Configure proxy URI and any user credentials.
|
84
|
-
#
|
85
|
-
# @param proxy [Hash]
|
86
|
-
def configure_proxy(client, proxy)
|
87
|
-
client.proxy = proxy[:uri]
|
88
|
-
return unless proxy[:user] && proxy[:password]
|
89
|
-
|
90
|
-
client.set_proxy_auth(proxy[:user], proxy[:password])
|
91
|
-
end
|
92
|
-
|
93
|
-
# @param ssl [Hash]
|
94
|
-
def configure_ssl(client, ssl)
|
95
|
-
ssl_config = client.ssl_config
|
96
|
-
ssl_config.verify_mode = ssl_verify_mode(ssl)
|
97
|
-
ssl_config.cert_store = ssl_cert_store(ssl)
|
98
|
-
|
99
|
-
ssl_config.add_trust_ca ssl[:ca_file] if ssl[:ca_file]
|
100
|
-
ssl_config.add_trust_ca ssl[:ca_path] if ssl[:ca_path]
|
101
|
-
ssl_config.client_cert = ssl[:client_cert] if ssl[:client_cert]
|
102
|
-
ssl_config.client_key = ssl[:client_key] if ssl[:client_key]
|
103
|
-
ssl_config.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
|
104
|
-
end
|
105
|
-
|
106
|
-
# @param req [Hash]
|
107
|
-
def configure_timeouts(client, req)
|
108
|
-
if (sec = request_timeout(:open, req))
|
109
|
-
client.connect_timeout = sec
|
110
|
-
end
|
111
|
-
|
112
|
-
if (sec = request_timeout(:write, req))
|
113
|
-
client.send_timeout = sec
|
114
|
-
end
|
115
|
-
|
116
|
-
return unless (sec = request_timeout(:read, req))
|
117
|
-
|
118
|
-
client.receive_timeout = sec
|
119
|
-
end
|
120
|
-
|
121
|
-
def configure_client(client)
|
122
|
-
@config_block&.call(client)
|
123
|
-
end
|
124
|
-
|
125
|
-
# @param ssl [Hash]
|
126
|
-
# @return [OpenSSL::X509::Store]
|
127
|
-
def ssl_cert_store(ssl)
|
128
|
-
return ssl[:cert_store] if ssl[:cert_store]
|
129
|
-
|
130
|
-
# Memoize the cert store so that the same one is passed to
|
131
|
-
# HTTPClient each time, to avoid resyncing SSL sessions when
|
132
|
-
# it's changed
|
133
|
-
@ssl_cert_store ||= begin
|
134
|
-
# Use the default cert store by default, i.e. system ca certs
|
135
|
-
OpenSSL::X509::Store.new.tap(&:set_default_paths)
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
# @param ssl [Hash]
|
140
|
-
def ssl_verify_mode(ssl)
|
141
|
-
ssl[:verify_mode] || begin
|
142
|
-
if ssl.fetch(:verify, true)
|
143
|
-
OpenSSL::SSL::VERIFY_PEER |
|
144
|
-
OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
|
145
|
-
else
|
146
|
-
OpenSSL::SSL::VERIFY_NONE
|
147
|
-
end
|
148
|
-
end
|
149
|
-
end
|
150
|
-
end
|
151
|
-
end
|
152
|
-
end
|
@@ -1,132 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Faraday
|
4
|
-
class Adapter
|
5
|
-
# Patron adapter.
|
6
|
-
class Patron < Faraday::Adapter
|
7
|
-
dependency 'patron'
|
8
|
-
|
9
|
-
def build_connection(env)
|
10
|
-
session = ::Patron::Session.new
|
11
|
-
@config_block&.call(session)
|
12
|
-
if (env[:url].scheme == 'https') && env[:ssl]
|
13
|
-
configure_ssl(session, env[:ssl])
|
14
|
-
end
|
15
|
-
|
16
|
-
if (req = env[:request])
|
17
|
-
configure_timeouts(session, req)
|
18
|
-
configure_proxy(session, req[:proxy])
|
19
|
-
end
|
20
|
-
|
21
|
-
session
|
22
|
-
end
|
23
|
-
|
24
|
-
def call(env)
|
25
|
-
super
|
26
|
-
# TODO: support streaming requests
|
27
|
-
env[:body] = env[:body].read if env[:body].respond_to? :read
|
28
|
-
|
29
|
-
response = connection(env) do |session|
|
30
|
-
begin
|
31
|
-
data = env[:body] ? env[:body].to_s : nil
|
32
|
-
session.request(env[:method], env[:url].to_s,
|
33
|
-
env[:request_headers], data: data)
|
34
|
-
rescue Errno::ECONNREFUSED, ::Patron::ConnectionFailed
|
35
|
-
raise Faraday::ConnectionFailed, $ERROR_INFO
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
if (req = env[:request]).stream_response?
|
40
|
-
warn "Streaming downloads for #{self.class.name} " \
|
41
|
-
'are not yet implemented.'
|
42
|
-
req.on_data.call(response.body, response.body.bytesize)
|
43
|
-
end
|
44
|
-
# Remove the "HTTP/1.1 200", leaving just the reason phrase
|
45
|
-
reason_phrase = response.status_line.gsub(/^.* \d{3} /, '')
|
46
|
-
|
47
|
-
save_response(env, response.status, response.body,
|
48
|
-
response.headers, reason_phrase)
|
49
|
-
|
50
|
-
@app.call env
|
51
|
-
rescue ::Patron::TimeoutError => e
|
52
|
-
if connection_timed_out_message?(e.message)
|
53
|
-
raise Faraday::ConnectionFailed, e
|
54
|
-
end
|
55
|
-
|
56
|
-
raise Faraday::TimeoutError, e
|
57
|
-
rescue ::Patron::Error => e
|
58
|
-
if e.message.include?('code 407')
|
59
|
-
raise Faraday::ConnectionFailed,
|
60
|
-
%(407 "Proxy Authentication Required ")
|
61
|
-
end
|
62
|
-
|
63
|
-
raise Faraday::ConnectionFailed, e
|
64
|
-
end
|
65
|
-
|
66
|
-
if loaded? && defined?(::Patron::Request::VALID_ACTIONS)
|
67
|
-
# HAX: helps but doesn't work completely
|
68
|
-
# https://github.com/toland/patron/issues/34
|
69
|
-
::Patron::Request::VALID_ACTIONS.tap do |actions|
|
70
|
-
if actions[0].is_a?(Symbol)
|
71
|
-
actions << :patch unless actions.include? :patch
|
72
|
-
actions << :options unless actions.include? :options
|
73
|
-
else
|
74
|
-
# Patron 0.4.20 and up
|
75
|
-
actions << 'PATCH' unless actions.include? 'PATCH'
|
76
|
-
actions << 'OPTIONS' unless actions.include? 'OPTIONS'
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
def configure_ssl(session, ssl)
|
82
|
-
if ssl.fetch(:verify, true)
|
83
|
-
session.cacert = ssl[:ca_file]
|
84
|
-
else
|
85
|
-
session.insecure = true
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
def configure_timeouts(session, req)
|
90
|
-
return unless req
|
91
|
-
|
92
|
-
if (sec = request_timeout(:read, req))
|
93
|
-
session.timeout = sec
|
94
|
-
end
|
95
|
-
|
96
|
-
return unless (sec = request_timeout(:open, req))
|
97
|
-
|
98
|
-
session.connect_timeout = sec
|
99
|
-
end
|
100
|
-
|
101
|
-
def configure_proxy(session, proxy)
|
102
|
-
return unless proxy
|
103
|
-
|
104
|
-
proxy_uri = proxy[:uri].dup
|
105
|
-
proxy_uri.user = proxy[:user] &&
|
106
|
-
Utils.escape(proxy[:user]).gsub('+', '%20')
|
107
|
-
proxy_uri.password = proxy[:password] &&
|
108
|
-
Utils.escape(proxy[:password]).gsub('+', '%20')
|
109
|
-
session.proxy = proxy_uri.to_s
|
110
|
-
end
|
111
|
-
|
112
|
-
private
|
113
|
-
|
114
|
-
CURL_TIMEOUT_MESSAGES = [
|
115
|
-
'Connection time-out',
|
116
|
-
'Connection timed out',
|
117
|
-
'Timed out before name resolve',
|
118
|
-
'server connect has timed out',
|
119
|
-
'Resolving timed out',
|
120
|
-
'name lookup timed out',
|
121
|
-
'timed out before SSL',
|
122
|
-
'connect() timed out'
|
123
|
-
].freeze
|
124
|
-
|
125
|
-
def connection_timed_out_message?(message)
|
126
|
-
CURL_TIMEOUT_MESSAGES.any? do |curl_message|
|
127
|
-
message.include?(curl_message)
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
data/lib/faraday/adapter/rack.rb
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Faraday
|
4
|
-
class Adapter
|
5
|
-
# Sends requests to a Rack app.
|
6
|
-
#
|
7
|
-
# @example
|
8
|
-
#
|
9
|
-
# class MyRackApp
|
10
|
-
# def call(env)
|
11
|
-
# [200, {'Content-Type' => 'text/html'}, ["hello world"]]
|
12
|
-
# end
|
13
|
-
# end
|
14
|
-
#
|
15
|
-
# Faraday.new do |conn|
|
16
|
-
# conn.adapter :rack, MyRackApp.new
|
17
|
-
# end
|
18
|
-
class Rack < Faraday::Adapter
|
19
|
-
dependency 'rack/test'
|
20
|
-
|
21
|
-
# not prefixed with "HTTP_"
|
22
|
-
SPECIAL_HEADERS = %w[CONTENT_LENGTH CONTENT_TYPE].freeze
|
23
|
-
|
24
|
-
def initialize(faraday_app, rack_app)
|
25
|
-
super(faraday_app)
|
26
|
-
mock_session = ::Rack::MockSession.new(rack_app)
|
27
|
-
@session = ::Rack::Test::Session.new(mock_session)
|
28
|
-
end
|
29
|
-
|
30
|
-
def call(env)
|
31
|
-
super
|
32
|
-
rack_env = build_rack_env(env)
|
33
|
-
|
34
|
-
env[:request_headers]&.each do |name, value|
|
35
|
-
name = name.upcase.tr('-', '_')
|
36
|
-
name = "HTTP_#{name}" unless SPECIAL_HEADERS.include? name
|
37
|
-
rack_env[name] = value
|
38
|
-
end
|
39
|
-
|
40
|
-
timeout = request_timeout(:open, env[:request])
|
41
|
-
timeout ||= request_timeout(:read, env[:request])
|
42
|
-
response = if timeout
|
43
|
-
Timer.timeout(timeout, Faraday::TimeoutError) do
|
44
|
-
execute_request(env, rack_env)
|
45
|
-
end
|
46
|
-
else
|
47
|
-
execute_request(env, rack_env)
|
48
|
-
end
|
49
|
-
|
50
|
-
if (req = env[:request]).stream_response?
|
51
|
-
warn "Streaming downloads for #{self.class.name} " \
|
52
|
-
'are not yet implemented.'
|
53
|
-
req.on_data.call(response.body, response.body.bytesize)
|
54
|
-
end
|
55
|
-
|
56
|
-
save_response(env, response.status, response.body, response.headers)
|
57
|
-
@app.call env
|
58
|
-
end
|
59
|
-
|
60
|
-
private
|
61
|
-
|
62
|
-
def execute_request(env, rack_env)
|
63
|
-
@session.request(env[:url].to_s, rack_env)
|
64
|
-
end
|
65
|
-
|
66
|
-
def build_rack_env(env)
|
67
|
-
{
|
68
|
-
method: env[:method],
|
69
|
-
input: env[:body].respond_to?(:read) ? env[:body].read : env[:body],
|
70
|
-
'rack.url_scheme' => env[:url].scheme
|
71
|
-
}
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
data/lib/faraday/file_part.rb
DELETED
@@ -1,128 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'stringio'
|
4
|
-
|
5
|
-
# multipart-post gem
|
6
|
-
require 'composite_io'
|
7
|
-
require 'parts'
|
8
|
-
|
9
|
-
module Faraday
|
10
|
-
# Multipart value used to POST a binary data from a file or
|
11
|
-
#
|
12
|
-
# @example
|
13
|
-
# payload = { file: Faraday::FilePart.new("file_name.ext", "content/type") }
|
14
|
-
# http.post("/upload", payload)
|
15
|
-
#
|
16
|
-
|
17
|
-
# @!method initialize(filename_or_io, content_type, filename = nil, opts = {})
|
18
|
-
#
|
19
|
-
# @param filename_or_io [String, IO] Either a String filename to a local
|
20
|
-
# file or an open IO object.
|
21
|
-
# @param content_type [String] String content type of the file data.
|
22
|
-
# @param filename [String] Optional String filename, usually to add context
|
23
|
-
# to a given IO object.
|
24
|
-
# @param opts [Hash] Optional Hash of String key/value pairs to describethis
|
25
|
-
# this uploaded file. Expected Header keys include:
|
26
|
-
# * Content-Transfer-Encoding - Defaults to "binary"
|
27
|
-
# * Content-Disposition - Defaults to "form-data"
|
28
|
-
# * Content-Type - Defaults to the content_type argument.
|
29
|
-
# * Content-ID - Optional.
|
30
|
-
#
|
31
|
-
# @return [Faraday::FilePart]
|
32
|
-
#
|
33
|
-
# @!attribute [r] content_type
|
34
|
-
# The uploaded binary data's content type.
|
35
|
-
#
|
36
|
-
# @return [String]
|
37
|
-
#
|
38
|
-
# @!attribute [r] original_filename
|
39
|
-
# The base filename, taken either from the filename_or_io or filename
|
40
|
-
# arguments in #initialize.
|
41
|
-
#
|
42
|
-
# @return [String]
|
43
|
-
#
|
44
|
-
# @!attribute [r] opts
|
45
|
-
# Extra String key/value pairs to make up the header for this uploaded file.
|
46
|
-
#
|
47
|
-
# @return [Hash]
|
48
|
-
#
|
49
|
-
# @!attribute [r] io
|
50
|
-
# The open IO object for the uploaded file.
|
51
|
-
#
|
52
|
-
# @return [IO]
|
53
|
-
FilePart = ::UploadIO
|
54
|
-
|
55
|
-
# Multipart value used to POST a file.
|
56
|
-
#
|
57
|
-
# @deprecated Use FilePart instead of this class. It behaves identically, with
|
58
|
-
# a matching name to ParamPart.
|
59
|
-
UploadIO = ::UploadIO
|
60
|
-
|
61
|
-
Parts = ::Parts
|
62
|
-
|
63
|
-
# Similar to, but not compatible with CompositeReadIO provided by the
|
64
|
-
# multipart-post gem.
|
65
|
-
# https://github.com/nicksieger/multipart-post/blob/master/lib/composite_io.rb
|
66
|
-
class CompositeReadIO
|
67
|
-
def initialize(*parts)
|
68
|
-
@parts = parts.flatten
|
69
|
-
@ios = @parts.map(&:to_io)
|
70
|
-
@index = 0
|
71
|
-
end
|
72
|
-
|
73
|
-
# @return [Integer] sum of the lengths of all the parts
|
74
|
-
def length
|
75
|
-
@parts.inject(0) { |sum, part| sum + part.length }
|
76
|
-
end
|
77
|
-
|
78
|
-
# Rewind each of the IOs and reset the index to 0.
|
79
|
-
#
|
80
|
-
# @return [void]
|
81
|
-
def rewind
|
82
|
-
@ios.each(&:rewind)
|
83
|
-
@index = 0
|
84
|
-
end
|
85
|
-
|
86
|
-
# Read from IOs in order until `length` bytes have been received.
|
87
|
-
#
|
88
|
-
# @param length [Integer, nil]
|
89
|
-
# @param outbuf [String, nil]
|
90
|
-
def read(length = nil, outbuf = nil)
|
91
|
-
got_result = false
|
92
|
-
outbuf = outbuf ? (+outbuf).replace('') : +''
|
93
|
-
|
94
|
-
while (io = current_io)
|
95
|
-
if (result = io.read(length))
|
96
|
-
got_result ||= !result.nil?
|
97
|
-
result.force_encoding('BINARY') if result.respond_to?(:force_encoding)
|
98
|
-
outbuf << result
|
99
|
-
length -= result.length if length
|
100
|
-
break if length&.zero?
|
101
|
-
end
|
102
|
-
advance_io
|
103
|
-
end
|
104
|
-
!got_result && length ? nil : outbuf
|
105
|
-
end
|
106
|
-
|
107
|
-
# Close each of the IOs.
|
108
|
-
#
|
109
|
-
# @return [void]
|
110
|
-
def close
|
111
|
-
@ios.each(&:close)
|
112
|
-
end
|
113
|
-
|
114
|
-
def ensure_open_and_readable
|
115
|
-
# Rubinius compatibility
|
116
|
-
end
|
117
|
-
|
118
|
-
private
|
119
|
-
|
120
|
-
def current_io
|
121
|
-
@ios[@index]
|
122
|
-
end
|
123
|
-
|
124
|
-
def advance_io
|
125
|
-
@index += 1
|
126
|
-
end
|
127
|
-
end
|
128
|
-
end
|