faraday 1.7.1 → 2.2.0

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.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +193 -3
  3. data/README.md +11 -9
  4. data/examples/client_spec.rb +19 -19
  5. data/examples/client_test.rb +22 -22
  6. data/lib/faraday/adapter/test.rb +10 -4
  7. data/lib/faraday/adapter.rb +2 -5
  8. data/lib/faraday/connection.rb +17 -93
  9. data/lib/faraday/encoders/nested_params_encoder.rb +2 -2
  10. data/lib/faraday/error.rb +3 -8
  11. data/lib/faraday/logging/formatter.rb +1 -0
  12. data/lib/faraday/middleware.rb +0 -1
  13. data/lib/faraday/middleware_registry.rb +17 -63
  14. data/lib/faraday/options.rb +3 -3
  15. data/lib/faraday/rack_builder.rb +23 -20
  16. data/lib/faraday/request/authorization.rb +32 -38
  17. data/lib/faraday/request/instrumentation.rb +2 -0
  18. data/lib/faraday/request/json.rb +55 -0
  19. data/lib/faraday/request/url_encoded.rb +2 -0
  20. data/lib/faraday/request.rb +11 -31
  21. data/lib/faraday/response/json.rb +54 -0
  22. data/lib/faraday/response/logger.rb +4 -4
  23. data/lib/faraday/response/raise_error.rb +9 -1
  24. data/lib/faraday/response.rb +8 -19
  25. data/lib/faraday/utils/headers.rb +1 -1
  26. data/lib/faraday/utils.rb +10 -5
  27. data/lib/faraday/version.rb +1 -1
  28. data/lib/faraday.rb +11 -39
  29. data/spec/faraday/connection_spec.rb +136 -85
  30. data/spec/faraday/middleware_registry_spec.rb +31 -0
  31. data/spec/faraday/options/env_spec.rb +2 -2
  32. data/spec/faraday/rack_builder_spec.rb +26 -54
  33. data/spec/faraday/request/authorization_spec.rb +19 -24
  34. data/spec/faraday/request/instrumentation_spec.rb +5 -7
  35. data/spec/faraday/request/json_spec.rb +111 -0
  36. data/spec/faraday/request/url_encoded_spec.rb +0 -1
  37. data/spec/faraday/request_spec.rb +4 -15
  38. data/spec/faraday/response/json_spec.rb +117 -0
  39. data/spec/faraday/response/raise_error_spec.rb +7 -4
  40. data/spec/faraday/utils/headers_spec.rb +2 -2
  41. data/spec/faraday/utils_spec.rb +62 -1
  42. data/spec/support/fake_safe_buffer.rb +1 -1
  43. data/spec/support/helper_methods.rb +0 -37
  44. data/spec/support/shared_examples/adapter.rb +0 -1
  45. data/spec/support/shared_examples/request_method.rb +5 -18
  46. metadata +12 -147
  47. data/lib/faraday/adapter/typhoeus.rb +0 -15
  48. data/lib/faraday/autoload.rb +0 -87
  49. data/lib/faraday/dependency_loader.rb +0 -37
  50. data/lib/faraday/file_part.rb +0 -128
  51. data/lib/faraday/param_part.rb +0 -53
  52. data/lib/faraday/request/basic_authentication.rb +0 -20
  53. data/lib/faraday/request/multipart.rb +0 -106
  54. data/lib/faraday/request/retry.rb +0 -239
  55. data/lib/faraday/request/token_authentication.rb +0 -20
  56. data/spec/faraday/adapter/em_http_spec.rb +0 -49
  57. data/spec/faraday/adapter/em_synchrony_spec.rb +0 -18
  58. data/spec/faraday/adapter/excon_spec.rb +0 -49
  59. data/spec/faraday/adapter/httpclient_spec.rb +0 -73
  60. data/spec/faraday/adapter/net_http_spec.rb +0 -64
  61. data/spec/faraday/adapter/patron_spec.rb +0 -18
  62. data/spec/faraday/adapter/rack_spec.rb +0 -8
  63. data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
  64. data/spec/faraday/composite_read_io_spec.rb +0 -80
  65. data/spec/faraday/request/multipart_spec.rb +0 -302
  66. data/spec/faraday/request/retry_spec.rb +0 -242
  67. data/spec/faraday/response/middleware_spec.rb +0 -68
  68. data/spec/support/webmock_rack_app.rb +0 -68
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.7.1
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "@technoweenie"
@@ -10,140 +10,22 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-08-30 00:00:00.000000000 Z
13
+ date: 2022-02-03 00:00:00.000000000 Z
14
14
  dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: faraday-em_http
17
- requirement: !ruby/object:Gem::Requirement
18
- requirements:
19
- - - "~>"
20
- - !ruby/object:Gem::Version
21
- version: '1.0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- requirements:
26
- - - "~>"
27
- - !ruby/object:Gem::Version
28
- version: '1.0'
29
- - !ruby/object:Gem::Dependency
30
- name: faraday-em_synchrony
31
- requirement: !ruby/object:Gem::Requirement
32
- requirements:
33
- - - "~>"
34
- - !ruby/object:Gem::Version
35
- version: '1.0'
36
- type: :runtime
37
- prerelease: false
38
- version_requirements: !ruby/object:Gem::Requirement
39
- requirements:
40
- - - "~>"
41
- - !ruby/object:Gem::Version
42
- version: '1.0'
43
- - !ruby/object:Gem::Dependency
44
- name: faraday-excon
45
- requirement: !ruby/object:Gem::Requirement
46
- requirements:
47
- - - "~>"
48
- - !ruby/object:Gem::Version
49
- version: '1.1'
50
- type: :runtime
51
- prerelease: false
52
- version_requirements: !ruby/object:Gem::Requirement
53
- requirements:
54
- - - "~>"
55
- - !ruby/object:Gem::Version
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.1
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.1
71
15
  - !ruby/object:Gem::Dependency
72
16
  name: faraday-net_http
73
17
  requirement: !ruby/object:Gem::Requirement
74
18
  requirements:
75
19
  - - "~>"
76
20
  - !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'
85
- - !ruby/object:Gem::Dependency
86
- name: faraday-net_http_persistent
87
- requirement: !ruby/object:Gem::Requirement
88
- requirements:
89
- - - "~>"
90
- - !ruby/object:Gem::Version
91
- version: '1.1'
21
+ version: '2.0'
92
22
  type: :runtime
93
23
  prerelease: false
94
24
  version_requirements: !ruby/object:Gem::Requirement
95
25
  requirements:
96
26
  - - "~>"
97
27
  - !ruby/object:Gem::Version
98
- version: '1.1'
99
- - !ruby/object:Gem::Dependency
100
- name: faraday-patron
101
- requirement: !ruby/object:Gem::Requirement
102
- requirements:
103
- - - "~>"
104
- - !ruby/object:Gem::Version
105
- version: '1.0'
106
- type: :runtime
107
- prerelease: false
108
- version_requirements: !ruby/object:Gem::Requirement
109
- requirements:
110
- - - "~>"
111
- - !ruby/object:Gem::Version
112
- version: '1.0'
113
- - !ruby/object:Gem::Dependency
114
- name: faraday-rack
115
- requirement: !ruby/object:Gem::Requirement
116
- requirements:
117
- - - "~>"
118
- - !ruby/object:Gem::Version
119
- version: '1.0'
120
- type: :runtime
121
- prerelease: false
122
- version_requirements: !ruby/object:Gem::Requirement
123
- requirements:
124
- - - "~>"
125
- - !ruby/object:Gem::Version
126
- version: '1.0'
127
- - !ruby/object:Gem::Dependency
128
- name: multipart-post
129
- requirement: !ruby/object:Gem::Requirement
130
- requirements:
131
- - - ">="
132
- - !ruby/object:Gem::Version
133
- version: '1.2'
134
- - - "<"
135
- - !ruby/object:Gem::Version
136
- version: '3'
137
- type: :runtime
138
- prerelease: false
139
- version_requirements: !ruby/object:Gem::Requirement
140
- requirements:
141
- - - ">="
142
- - !ruby/object:Gem::Version
143
- version: '1.2'
144
- - - "<"
145
- - !ruby/object:Gem::Version
146
- version: '3'
28
+ version: '2.0'
147
29
  - !ruby/object:Gem::Dependency
148
30
  name: ruby2_keywords
149
31
  requirement: !ruby/object:Gem::Requirement
@@ -173,15 +55,11 @@ files:
173
55
  - lib/faraday.rb
174
56
  - lib/faraday/adapter.rb
175
57
  - lib/faraday/adapter/test.rb
176
- - lib/faraday/adapter/typhoeus.rb
177
58
  - lib/faraday/adapter_registry.rb
178
- - lib/faraday/autoload.rb
179
59
  - lib/faraday/connection.rb
180
- - lib/faraday/dependency_loader.rb
181
60
  - lib/faraday/encoders/flat_params_encoder.rb
182
61
  - lib/faraday/encoders/nested_params_encoder.rb
183
62
  - lib/faraday/error.rb
184
- - lib/faraday/file_part.rb
185
63
  - lib/faraday/logging/formatter.rb
186
64
  - lib/faraday/methods.rb
187
65
  - lib/faraday/middleware.rb
@@ -192,18 +70,15 @@ files:
192
70
  - lib/faraday/options/proxy_options.rb
193
71
  - lib/faraday/options/request_options.rb
194
72
  - lib/faraday/options/ssl_options.rb
195
- - lib/faraday/param_part.rb
196
73
  - lib/faraday/parameters.rb
197
74
  - lib/faraday/rack_builder.rb
198
75
  - lib/faraday/request.rb
199
76
  - lib/faraday/request/authorization.rb
200
- - lib/faraday/request/basic_authentication.rb
201
77
  - lib/faraday/request/instrumentation.rb
202
- - lib/faraday/request/multipart.rb
203
- - lib/faraday/request/retry.rb
204
- - lib/faraday/request/token_authentication.rb
78
+ - lib/faraday/request/json.rb
205
79
  - lib/faraday/request/url_encoded.rb
206
80
  - lib/faraday/response.rb
81
+ - lib/faraday/response/json.rb
207
82
  - lib/faraday/response/logger.rb
208
83
  - lib/faraday/response/raise_error.rb
209
84
  - lib/faraday/utils.rb
@@ -211,20 +86,12 @@ files:
211
86
  - lib/faraday/utils/params_hash.rb
212
87
  - lib/faraday/version.rb
213
88
  - spec/external_adapters/faraday_specs_setup.rb
214
- - spec/faraday/adapter/em_http_spec.rb
215
- - spec/faraday/adapter/em_synchrony_spec.rb
216
- - spec/faraday/adapter/excon_spec.rb
217
- - spec/faraday/adapter/httpclient_spec.rb
218
- - spec/faraday/adapter/net_http_spec.rb
219
- - spec/faraday/adapter/patron_spec.rb
220
- - spec/faraday/adapter/rack_spec.rb
221
89
  - spec/faraday/adapter/test_spec.rb
222
- - spec/faraday/adapter/typhoeus_spec.rb
223
90
  - spec/faraday/adapter_registry_spec.rb
224
91
  - spec/faraday/adapter_spec.rb
225
- - spec/faraday/composite_read_io_spec.rb
226
92
  - spec/faraday/connection_spec.rb
227
93
  - spec/faraday/error_spec.rb
94
+ - spec/faraday/middleware_registry_spec.rb
228
95
  - spec/faraday/middleware_spec.rb
229
96
  - spec/faraday/options/env_spec.rb
230
97
  - spec/faraday/options/options_spec.rb
@@ -235,12 +102,11 @@ files:
235
102
  - spec/faraday/rack_builder_spec.rb
236
103
  - spec/faraday/request/authorization_spec.rb
237
104
  - spec/faraday/request/instrumentation_spec.rb
238
- - spec/faraday/request/multipart_spec.rb
239
- - spec/faraday/request/retry_spec.rb
105
+ - spec/faraday/request/json_spec.rb
240
106
  - spec/faraday/request/url_encoded_spec.rb
241
107
  - spec/faraday/request_spec.rb
108
+ - spec/faraday/response/json_spec.rb
242
109
  - spec/faraday/response/logger_spec.rb
243
- - spec/faraday/response/middleware_spec.rb
244
110
  - spec/faraday/response/raise_error_spec.rb
245
111
  - spec/faraday/response_spec.rb
246
112
  - spec/faraday/utils/headers_spec.rb
@@ -254,13 +120,12 @@ files:
254
120
  - spec/support/shared_examples/params_encoder.rb
255
121
  - spec/support/shared_examples/request_method.rb
256
122
  - spec/support/streaming_response_checker.rb
257
- - spec/support/webmock_rack_app.rb
258
123
  homepage: https://lostisland.github.io/faraday
259
124
  licenses:
260
125
  - MIT
261
126
  metadata:
262
127
  homepage_uri: https://lostisland.github.io/faraday
263
- changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.7.1
128
+ changelog_uri: https://github.com/lostisland/faraday/releases/tag/v2.2.0
264
129
  source_code_uri: https://github.com/lostisland/faraday
265
130
  bug_tracker_uri: https://github.com/lostisland/faraday/issues
266
131
  post_install_message:
@@ -272,14 +137,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
272
137
  requirements:
273
138
  - - ">="
274
139
  - !ruby/object:Gem::Version
275
- version: '2.4'
140
+ version: '2.6'
276
141
  required_rubygems_version: !ruby/object:Gem::Requirement
277
142
  requirements:
278
143
  - - ">="
279
144
  - !ruby/object:Gem::Version
280
145
  version: '0'
281
146
  requirements: []
282
- rubygems_version: 3.0.3.1
147
+ rubygems_version: 3.1.6
283
148
  signing_key:
284
149
  specification_version: 4
285
150
  summary: HTTP/REST API client library.
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Faraday
4
- class Adapter
5
- # Typhoeus adapter. This class is just a stub, the real adapter is in
6
- # https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/adapters/faraday.rb
7
- class Typhoeus < Faraday::Adapter
8
- # Needs to define this method in order to support Typhoeus <= 1.3.0
9
- def call; end
10
-
11
- dependency 'typhoeus'
12
- dependency 'typhoeus/adapters/faraday'
13
- end
14
- end
15
- end
@@ -1,87 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Faraday
4
- # Adds the ability for other modules to manage autoloadable
5
- # constants.
6
- #
7
- # @api private
8
- module AutoloadHelper
9
- # Registers the constants to be auto loaded.
10
- #
11
- # @param prefix [String] The require prefix. If the path is inside Faraday,
12
- # then it will be prefixed with the root path of this loaded
13
- # Faraday version.
14
- # @param options [{ Symbol => String }] library names.
15
- #
16
- # @example
17
- #
18
- # Faraday.autoload_all 'faraday/foo',
19
- # Bar: 'bar'
20
- #
21
- # # requires faraday/foo/bar to load Faraday::Bar.
22
- # Faraday::Bar
23
- #
24
- # @return [void]
25
- def autoload_all(prefix, options)
26
- if prefix.match? %r{^faraday(/|$)}i
27
- prefix = File.join(Faraday.root_path, prefix)
28
- end
29
-
30
- options.each do |const_name, path|
31
- autoload const_name, File.join(prefix, path)
32
- end
33
- end
34
-
35
- # Loads each autoloaded constant. If thread safety is a concern,
36
- # wrap this in a Mutex.
37
- #
38
- # @return [void]
39
- def load_autoloaded_constants
40
- constants.each do |const|
41
- const_get(const) if autoload?(const)
42
- end
43
- end
44
-
45
- # Filters the module's contents with those that have been already
46
- # autoloaded.
47
- #
48
- # @return [Array<Class, Module>]
49
- def all_loaded_constants
50
- constants
51
- .map { |c| const_get(c) }
52
- .select { |a| a.respond_to?(:loaded?) && a.loaded? }
53
- end
54
- end
55
-
56
- # Adapter is the base class for all Faraday adapters.
57
- # @see lib/faraday/adapter.rb Original class location
58
- class Adapter
59
- extend AutoloadHelper
60
- autoload_all 'faraday/adapter',
61
- Typhoeus: 'typhoeus',
62
- Test: 'test'
63
- end
64
-
65
- # Request represents a single HTTP request for a Faraday adapter to make.
66
- # @see lib/faraday/request.rb Original class location
67
- class Request
68
- extend AutoloadHelper
69
- autoload_all 'faraday/request',
70
- UrlEncoded: 'url_encoded',
71
- Multipart: 'multipart',
72
- Retry: 'retry',
73
- Authorization: 'authorization',
74
- BasicAuthentication: 'basic_authentication',
75
- TokenAuthentication: 'token_authentication',
76
- Instrumentation: 'instrumentation'
77
- end
78
-
79
- # Response represents the returned value of a sent Faraday request.
80
- # @see lib/faraday/response.rb Original class location
81
- class Response
82
- extend AutoloadHelper
83
- autoload_all 'faraday/response',
84
- RaiseError: 'raise_error',
85
- Logger: 'logger'
86
- end
87
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Faraday
4
- # DependencyLoader helps Faraday adapters and middleware load dependencies.
5
- module DependencyLoader
6
- attr_reader :load_error
7
-
8
- # Executes a block which should try to require and reference dependent
9
- # libraries
10
- def dependency(lib = nil)
11
- lib ? require(lib) : yield
12
- rescue LoadError, NameError => e
13
- self.load_error = e
14
- end
15
-
16
- def new(*)
17
- unless loaded?
18
- raise "missing dependency for #{self}: #{load_error.message}"
19
- end
20
-
21
- super
22
- end
23
-
24
- def loaded?
25
- load_error.nil?
26
- end
27
-
28
- def inherited(subclass)
29
- super
30
- subclass.send(:load_error=, load_error)
31
- end
32
-
33
- private
34
-
35
- attr_writer :load_error
36
- end
37
- end
@@ -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
@@ -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,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'base64'
4
-
5
- module Faraday
6
- class Request
7
- # Authorization middleware for Basic Authentication.
8
- class BasicAuthentication < load_middleware(:authorization)
9
- # @param login [String]
10
- # @param pass [String]
11
- #
12
- # @return [String] a Basic Authentication header line
13
- def self.header(login, pass)
14
- value = Base64.encode64([login, pass].join(':'))
15
- value.delete!("\n")
16
- super(:Basic, value)
17
- end
18
- end
19
- end
20
- 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