faraday 1.7.1 → 1.9.1
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/lib/faraday/connection.rb +6 -1
- data/lib/faraday/error.rb +0 -6
- data/lib/faraday/request/authorization.rb +14 -7
- data/lib/faraday/version.rb +1 -1
- data/lib/faraday.rb +6 -2
- data/spec/faraday/request/authorization_spec.rb +8 -0
- data/spec/faraday/request/instrumentation_spec.rb +5 -7
- data/spec/support/webmock_rack_app.rb +1 -2
- metadata +27 -25
- 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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: efe708e6b5d6c717435558a71f0710aa51b2c9288c79a25dff215eb305a03f21
|
4
|
+
data.tar.gz: 2614e27e8eeb000bd90a97f263778ad758f2b5eaca034606f088eee399af301f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c40f0455e00c5bcb108480221182d512ae3b15c55c2cc10004badf2cd5bb8bab4eb8a2ae40fcb65cdb81193a07a97227673bec44d8b96ea38bdaeb15a511892e
|
7
|
+
data.tar.gz: e8f4df3fe365194c01428e7a9bff4156401704876d0476e7a1fad1dea37c1c2371e945955e8b4132cc74cfc54bcd479e640795da7d563be46881f0c3d6c199f7
|
data/lib/faraday/connection.rb
CHANGED
@@ -433,13 +433,18 @@ module Faraday
|
|
433
433
|
uri.query = nil
|
434
434
|
|
435
435
|
with_uri_credentials(uri) do |user, password|
|
436
|
-
|
436
|
+
set_basic_auth(user, password)
|
437
437
|
uri.user = uri.password = nil
|
438
438
|
end
|
439
439
|
|
440
440
|
@proxy = proxy_from_env(url) unless @manual_proxy
|
441
441
|
end
|
442
442
|
|
443
|
+
def set_basic_auth(user, password)
|
444
|
+
header = Faraday::Request::BasicAuthentication.header(user, password)
|
445
|
+
headers[Faraday::Request::Authorization::KEY] = header
|
446
|
+
end
|
447
|
+
|
443
448
|
# Sets the path prefix and ensures that it always has a leading
|
444
449
|
# slash.
|
445
450
|
#
|
data/lib/faraday/error.rb
CHANGED
@@ -143,10 +143,4 @@ module Faraday
|
|
143
143
|
# Raised by FaradayMiddleware::ResponseMiddleware
|
144
144
|
class ParsingError < Error
|
145
145
|
end
|
146
|
-
|
147
|
-
# Exception used to control the Retry middleware.
|
148
|
-
#
|
149
|
-
# @see Faraday::Request::Retry
|
150
|
-
class RetriableResponse < Error
|
151
|
-
end
|
152
146
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'base64'
|
4
|
+
|
3
5
|
module Faraday
|
4
6
|
class Request
|
5
7
|
# Request middleware for the Authorization HTTP header
|
@@ -13,7 +15,8 @@ module Faraday
|
|
13
15
|
# @return [String] a header value
|
14
16
|
def self.header(type, token)
|
15
17
|
case token
|
16
|
-
when String, Symbol
|
18
|
+
when String, Symbol, Proc
|
19
|
+
token = token.call if token.is_a?(Proc)
|
17
20
|
"#{type} #{token}"
|
18
21
|
when Hash
|
19
22
|
build_hash(type.to_s, token)
|
@@ -32,6 +35,7 @@ module Faraday
|
|
32
35
|
comma = ', '
|
33
36
|
values = []
|
34
37
|
hash.each do |key, value|
|
38
|
+
value = value.call if value.is_a?(Proc)
|
35
39
|
values << "#{key}=#{value.to_s.inspect}"
|
36
40
|
end
|
37
41
|
"#{type} #{values * comma}"
|
@@ -39,16 +43,19 @@ module Faraday
|
|
39
43
|
|
40
44
|
# @param app [#call]
|
41
45
|
# @param type [String, Symbol] Type of Authorization
|
42
|
-
# @param
|
43
|
-
|
44
|
-
|
46
|
+
# @param param [String, Symbol, Hash, Proc] parameter to build the Authorization header.
|
47
|
+
# This value can be a proc, in which case it will be invoked on each request.
|
48
|
+
def initialize(app, type, param)
|
49
|
+
@type = type
|
50
|
+
@param = param
|
45
51
|
super(app)
|
46
52
|
end
|
47
53
|
|
48
54
|
# @param env [Faraday::Env]
|
49
|
-
def
|
50
|
-
|
51
|
-
|
55
|
+
def on_request(env)
|
56
|
+
return if env.request_headers[KEY]
|
57
|
+
|
58
|
+
env.request_headers[KEY] = self.class.header(@type, @param)
|
52
59
|
end
|
53
60
|
end
|
54
61
|
end
|
data/lib/faraday/version.rb
CHANGED
data/lib/faraday.rb
CHANGED
@@ -24,9 +24,13 @@ require 'faraday/adapter'
|
|
24
24
|
require 'faraday/request'
|
25
25
|
require 'faraday/response'
|
26
26
|
require 'faraday/error'
|
27
|
-
require 'faraday/
|
28
|
-
require 'faraday/param_part'
|
27
|
+
require 'faraday/request/url_encoded' # needed by multipart
|
29
28
|
|
29
|
+
# External Middleware gems
|
30
|
+
require 'faraday/multipart'
|
31
|
+
require 'faraday/retry'
|
32
|
+
|
33
|
+
# External Adapters gems
|
30
34
|
unless defined?(JRUBY_VERSION)
|
31
35
|
require 'faraday/em_http'
|
32
36
|
require 'faraday/em_synchrony'
|
@@ -84,5 +84,13 @@ RSpec.describe Faraday::Request::Authorization do
|
|
84
84
|
|
85
85
|
include_examples 'does not interfere with existing authentication'
|
86
86
|
end
|
87
|
+
|
88
|
+
context 'when passed a string and a proc' do
|
89
|
+
let(:auth_config) { ['Bearer', -> { 'custom_from_proc' }] }
|
90
|
+
|
91
|
+
it { expect(response.body).to eq('Bearer custom_from_proc') }
|
92
|
+
|
93
|
+
include_examples 'does not interfere with existing authentication'
|
94
|
+
end
|
87
95
|
end
|
88
96
|
end
|
@@ -30,13 +30,11 @@ RSpec.describe Faraday::Request::Instrumentation do
|
|
30
30
|
|
31
31
|
it { expect(options.name).to eq('request.faraday') }
|
32
32
|
it 'defaults to ActiveSupport::Notifications' do
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
expect(res).to eq(ActiveSupport::Notifications)
|
39
|
-
end
|
33
|
+
res = options.instrumenter
|
34
|
+
rescue NameError => e
|
35
|
+
expect(e.to_s).to match('ActiveSupport')
|
36
|
+
else
|
37
|
+
expect(res).to eq(ActiveSupport::Notifications)
|
40
38
|
end
|
41
39
|
|
42
40
|
it 'instruments with default name' do
|
@@ -41,8 +41,7 @@ class WebmockRackApp
|
|
41
41
|
|
42
42
|
def req_headers(env)
|
43
43
|
http_headers = env.select { |k, _| k.start_with?('HTTP_') }
|
44
|
-
.
|
45
|
-
.to_h
|
44
|
+
.transform_keys { |k| k[5..] }
|
46
45
|
|
47
46
|
special_headers = Faraday::Adapter::Rack::SPECIAL_HEADERS
|
48
47
|
http_headers.merge(env.select { |k, _| special_headers.include?(k) })
|
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.9.1
|
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-01-06 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: faraday-em_http
|
@@ -60,14 +60,28 @@ dependencies:
|
|
60
60
|
requirements:
|
61
61
|
- - "~>"
|
62
62
|
- !ruby/object:Gem::Version
|
63
|
-
version: 1.0
|
63
|
+
version: '1.0'
|
64
64
|
type: :runtime
|
65
65
|
prerelease: false
|
66
66
|
version_requirements: !ruby/object:Gem::Requirement
|
67
67
|
requirements:
|
68
68
|
- - "~>"
|
69
69
|
- !ruby/object:Gem::Version
|
70
|
-
version: 1.0
|
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'
|
71
85
|
- !ruby/object:Gem::Dependency
|
72
86
|
name: faraday-net_http
|
73
87
|
requirement: !ruby/object:Gem::Requirement
|
@@ -88,14 +102,14 @@ dependencies:
|
|
88
102
|
requirements:
|
89
103
|
- - "~>"
|
90
104
|
- !ruby/object:Gem::Version
|
91
|
-
version: '1.
|
105
|
+
version: '1.0'
|
92
106
|
type: :runtime
|
93
107
|
prerelease: false
|
94
108
|
version_requirements: !ruby/object:Gem::Requirement
|
95
109
|
requirements:
|
96
110
|
- - "~>"
|
97
111
|
- !ruby/object:Gem::Version
|
98
|
-
version: '1.
|
112
|
+
version: '1.0'
|
99
113
|
- !ruby/object:Gem::Dependency
|
100
114
|
name: faraday-patron
|
101
115
|
requirement: !ruby/object:Gem::Requirement
|
@@ -125,25 +139,19 @@ dependencies:
|
|
125
139
|
- !ruby/object:Gem::Version
|
126
140
|
version: '1.0'
|
127
141
|
- !ruby/object:Gem::Dependency
|
128
|
-
name:
|
142
|
+
name: faraday-retry
|
129
143
|
requirement: !ruby/object:Gem::Requirement
|
130
144
|
requirements:
|
131
|
-
- - "
|
132
|
-
- !ruby/object:Gem::Version
|
133
|
-
version: '1.2'
|
134
|
-
- - "<"
|
145
|
+
- - "~>"
|
135
146
|
- !ruby/object:Gem::Version
|
136
|
-
version: '
|
147
|
+
version: '1.0'
|
137
148
|
type: :runtime
|
138
149
|
prerelease: false
|
139
150
|
version_requirements: !ruby/object:Gem::Requirement
|
140
151
|
requirements:
|
141
|
-
- - "
|
142
|
-
- !ruby/object:Gem::Version
|
143
|
-
version: '1.2'
|
144
|
-
- - "<"
|
152
|
+
- - "~>"
|
145
153
|
- !ruby/object:Gem::Version
|
146
|
-
version: '
|
154
|
+
version: '1.0'
|
147
155
|
- !ruby/object:Gem::Dependency
|
148
156
|
name: ruby2_keywords
|
149
157
|
requirement: !ruby/object:Gem::Requirement
|
@@ -181,7 +189,6 @@ files:
|
|
181
189
|
- lib/faraday/encoders/flat_params_encoder.rb
|
182
190
|
- lib/faraday/encoders/nested_params_encoder.rb
|
183
191
|
- lib/faraday/error.rb
|
184
|
-
- lib/faraday/file_part.rb
|
185
192
|
- lib/faraday/logging/formatter.rb
|
186
193
|
- lib/faraday/methods.rb
|
187
194
|
- lib/faraday/middleware.rb
|
@@ -192,15 +199,12 @@ files:
|
|
192
199
|
- lib/faraday/options/proxy_options.rb
|
193
200
|
- lib/faraday/options/request_options.rb
|
194
201
|
- lib/faraday/options/ssl_options.rb
|
195
|
-
- lib/faraday/param_part.rb
|
196
202
|
- lib/faraday/parameters.rb
|
197
203
|
- lib/faraday/rack_builder.rb
|
198
204
|
- lib/faraday/request.rb
|
199
205
|
- lib/faraday/request/authorization.rb
|
200
206
|
- lib/faraday/request/basic_authentication.rb
|
201
207
|
- lib/faraday/request/instrumentation.rb
|
202
|
-
- lib/faraday/request/multipart.rb
|
203
|
-
- lib/faraday/request/retry.rb
|
204
208
|
- lib/faraday/request/token_authentication.rb
|
205
209
|
- lib/faraday/request/url_encoded.rb
|
206
210
|
- lib/faraday/response.rb
|
@@ -235,8 +239,6 @@ files:
|
|
235
239
|
- spec/faraday/rack_builder_spec.rb
|
236
240
|
- spec/faraday/request/authorization_spec.rb
|
237
241
|
- spec/faraday/request/instrumentation_spec.rb
|
238
|
-
- spec/faraday/request/multipart_spec.rb
|
239
|
-
- spec/faraday/request/retry_spec.rb
|
240
242
|
- spec/faraday/request/url_encoded_spec.rb
|
241
243
|
- spec/faraday/request_spec.rb
|
242
244
|
- spec/faraday/response/logger_spec.rb
|
@@ -260,7 +262,7 @@ licenses:
|
|
260
262
|
- MIT
|
261
263
|
metadata:
|
262
264
|
homepage_uri: https://lostisland.github.io/faraday
|
263
|
-
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.
|
265
|
+
changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.9.1
|
264
266
|
source_code_uri: https://github.com/lostisland/faraday
|
265
267
|
bug_tracker_uri: https://github.com/lostisland/faraday/issues
|
266
268
|
post_install_message:
|
@@ -272,7 +274,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
272
274
|
requirements:
|
273
275
|
- - ">="
|
274
276
|
- !ruby/object:Gem::Version
|
275
|
-
version: '2.
|
277
|
+
version: '2.6'
|
276
278
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
277
279
|
requirements:
|
278
280
|
- - ">="
|
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
|
data/lib/faraday/param_part.rb
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Faraday
|
4
|
-
# Multipart value used to POST data with a content type.
|
5
|
-
class ParamPart
|
6
|
-
# @param value [String] Uploaded content as a String.
|
7
|
-
# @param content_type [String] String content type of the value.
|
8
|
-
# @param content_id [String] Optional String of this value's Content-ID.
|
9
|
-
#
|
10
|
-
# @return [Faraday::ParamPart]
|
11
|
-
def initialize(value, content_type, content_id = nil)
|
12
|
-
@value = value
|
13
|
-
@content_type = content_type
|
14
|
-
@content_id = content_id
|
15
|
-
end
|
16
|
-
|
17
|
-
# Converts this value to a form part.
|
18
|
-
#
|
19
|
-
# @param boundary [String] String multipart boundary that must not exist in
|
20
|
-
# the content exactly.
|
21
|
-
# @param key [String] String key name for this value.
|
22
|
-
#
|
23
|
-
# @return [Faraday::Parts::Part]
|
24
|
-
def to_part(boundary, key)
|
25
|
-
Faraday::Parts::Part.new(boundary, key, value, headers)
|
26
|
-
end
|
27
|
-
|
28
|
-
# Returns a Hash of String key/value pairs.
|
29
|
-
#
|
30
|
-
# @return [Hash]
|
31
|
-
def headers
|
32
|
-
{
|
33
|
-
'Content-Type' => content_type,
|
34
|
-
'Content-ID' => content_id
|
35
|
-
}
|
36
|
-
end
|
37
|
-
|
38
|
-
# The content to upload.
|
39
|
-
#
|
40
|
-
# @return [String]
|
41
|
-
attr_reader :value
|
42
|
-
|
43
|
-
# The value's content type.
|
44
|
-
#
|
45
|
-
# @return [String]
|
46
|
-
attr_reader :content_type
|
47
|
-
|
48
|
-
# The value's content ID, if given.
|
49
|
-
#
|
50
|
-
# @return [String, nil]
|
51
|
-
attr_reader :content_id
|
52
|
-
end
|
53
|
-
end
|
@@ -1,106 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require File.expand_path('url_encoded', __dir__)
|
4
|
-
require 'securerandom'
|
5
|
-
|
6
|
-
module Faraday
|
7
|
-
class Request
|
8
|
-
# Middleware for supporting multi-part requests.
|
9
|
-
class Multipart < UrlEncoded
|
10
|
-
self.mime_type = 'multipart/form-data'
|
11
|
-
unless defined?(::Faraday::Request::Multipart::DEFAULT_BOUNDARY_PREFIX)
|
12
|
-
DEFAULT_BOUNDARY_PREFIX = '-----------RubyMultipartPost'
|
13
|
-
end
|
14
|
-
|
15
|
-
def initialize(app = nil, options = {})
|
16
|
-
super(app)
|
17
|
-
@options = options
|
18
|
-
end
|
19
|
-
|
20
|
-
# Checks for files in the payload, otherwise leaves everything untouched.
|
21
|
-
#
|
22
|
-
# @param env [Faraday::Env]
|
23
|
-
def call(env)
|
24
|
-
match_content_type(env) do |params|
|
25
|
-
env.request.boundary ||= unique_boundary
|
26
|
-
env.request_headers[CONTENT_TYPE] +=
|
27
|
-
"; boundary=#{env.request.boundary}"
|
28
|
-
env.body = create_multipart(env, params)
|
29
|
-
end
|
30
|
-
@app.call env
|
31
|
-
end
|
32
|
-
|
33
|
-
# @param env [Faraday::Env]
|
34
|
-
def process_request?(env)
|
35
|
-
type = request_type(env)
|
36
|
-
env.body.respond_to?(:each_key) && !env.body.empty? && (
|
37
|
-
(type.empty? && has_multipart?(env.body)) ||
|
38
|
-
(type == self.class.mime_type)
|
39
|
-
)
|
40
|
-
end
|
41
|
-
|
42
|
-
# Returns true if obj is an enumerable with values that are multipart.
|
43
|
-
#
|
44
|
-
# @param obj [Object]
|
45
|
-
# @return [Boolean]
|
46
|
-
def has_multipart?(obj) # rubocop:disable Naming/PredicateName
|
47
|
-
if obj.respond_to?(:each)
|
48
|
-
(obj.respond_to?(:values) ? obj.values : obj).each do |val|
|
49
|
-
return true if val.respond_to?(:content_type) || has_multipart?(val)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
false
|
53
|
-
end
|
54
|
-
|
55
|
-
# @param env [Faraday::Env]
|
56
|
-
# @param params [Hash]
|
57
|
-
def create_multipart(env, params)
|
58
|
-
boundary = env.request.boundary
|
59
|
-
parts = process_params(params) do |key, value|
|
60
|
-
part(boundary, key, value)
|
61
|
-
end
|
62
|
-
parts << Faraday::Parts::EpiloguePart.new(boundary)
|
63
|
-
|
64
|
-
body = Faraday::CompositeReadIO.new(parts)
|
65
|
-
env.request_headers[Faraday::Env::ContentLength] = body.length.to_s
|
66
|
-
body
|
67
|
-
end
|
68
|
-
|
69
|
-
def part(boundary, key, value)
|
70
|
-
if value.respond_to?(:to_part)
|
71
|
-
value.to_part(boundary, key)
|
72
|
-
else
|
73
|
-
Faraday::Parts::Part.new(boundary, key, value)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
# @return [String]
|
78
|
-
def unique_boundary
|
79
|
-
"#{DEFAULT_BOUNDARY_PREFIX}-#{SecureRandom.hex}"
|
80
|
-
end
|
81
|
-
|
82
|
-
# @param params [Hash]
|
83
|
-
# @param prefix [String]
|
84
|
-
# @param pieces [Array]
|
85
|
-
def process_params(params, prefix = nil, pieces = nil, &block)
|
86
|
-
params.inject(pieces || []) do |all, (key, value)|
|
87
|
-
if prefix
|
88
|
-
key = @options[:flat_encode] ? prefix.to_s : "#{prefix}[#{key}]"
|
89
|
-
end
|
90
|
-
|
91
|
-
case value
|
92
|
-
when Array
|
93
|
-
values = value.inject([]) { |a, v| a << [nil, v] }
|
94
|
-
process_params(values, key, all, &block)
|
95
|
-
when Hash
|
96
|
-
process_params(value, key, all, &block)
|
97
|
-
else
|
98
|
-
# rubocop:disable Performance/RedundantBlockCall
|
99
|
-
all << block.call(key, value)
|
100
|
-
# rubocop:enable Performance/RedundantBlockCall
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|