faraday 1.10.6 → 2.0.0.alpha.pre.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.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +111 -1
  3. data/README.md +16 -9
  4. data/examples/client_test.rb +1 -1
  5. data/lib/faraday/adapter/test.rb +2 -0
  6. data/lib/faraday/adapter.rb +0 -5
  7. data/lib/faraday/connection.rb +3 -86
  8. data/lib/faraday/encoders/nested_params_encoder.rb +3 -12
  9. data/lib/faraday/error.rb +7 -0
  10. data/lib/faraday/file_part.rb +122 -0
  11. data/lib/faraday/logging/formatter.rb +1 -0
  12. data/lib/faraday/middleware.rb +0 -1
  13. data/lib/faraday/middleware_registry.rb +15 -79
  14. data/lib/faraday/options.rb +3 -3
  15. data/lib/faraday/param_part.rb +53 -0
  16. data/lib/faraday/rack_builder.rb +1 -1
  17. data/lib/faraday/request/authorization.rb +26 -40
  18. data/lib/faraday/request/instrumentation.rb +2 -0
  19. data/lib/faraday/request/multipart.rb +108 -0
  20. data/lib/faraday/request/retry.rb +241 -0
  21. data/lib/faraday/request/url_encoded.rb +2 -0
  22. data/lib/faraday/request.rb +8 -24
  23. data/lib/faraday/response/json.rb +4 -4
  24. data/lib/faraday/response/logger.rb +2 -0
  25. data/lib/faraday/response/raise_error.rb +9 -1
  26. data/lib/faraday/response.rb +7 -20
  27. data/lib/faraday/utils/headers.rb +1 -1
  28. data/lib/faraday/utils.rb +9 -4
  29. data/lib/faraday/version.rb +1 -1
  30. data/lib/faraday.rb +6 -45
  31. data/spec/faraday/connection_spec.rb +78 -97
  32. data/spec/faraday/options/env_spec.rb +2 -2
  33. data/spec/faraday/params_encoders/nested_spec.rb +0 -30
  34. data/spec/faraday/rack_builder_spec.rb +5 -43
  35. data/spec/faraday/request/authorization_spec.rb +14 -36
  36. data/spec/faraday/request/instrumentation_spec.rb +5 -7
  37. data/spec/faraday/request/multipart_spec.rb +302 -0
  38. data/spec/faraday/request/retry_spec.rb +254 -0
  39. data/spec/faraday/request_spec.rb +0 -11
  40. data/spec/faraday/response/json_spec.rb +4 -6
  41. data/spec/faraday/response/raise_error_spec.rb +7 -4
  42. data/spec/faraday/utils_spec.rb +1 -1
  43. data/spec/faraday_spec.rb +4 -8
  44. data/spec/spec_helper.rb +0 -2
  45. data/spec/support/fake_safe_buffer.rb +1 -1
  46. data/spec/support/shared_examples/request_method.rb +5 -5
  47. metadata +25 -156
  48. data/lib/faraday/adapter/typhoeus.rb +0 -15
  49. data/lib/faraday/autoload.rb +0 -89
  50. data/lib/faraday/dependency_loader.rb +0 -39
  51. data/lib/faraday/deprecate.rb +0 -110
  52. data/lib/faraday/request/basic_authentication.rb +0 -20
  53. data/lib/faraday/request/token_authentication.rb +0 -20
  54. data/spec/faraday/adapter/em_http_spec.rb +0 -49
  55. data/spec/faraday/adapter/em_synchrony_spec.rb +0 -18
  56. data/spec/faraday/adapter/excon_spec.rb +0 -49
  57. data/spec/faraday/adapter/httpclient_spec.rb +0 -73
  58. data/spec/faraday/adapter/net_http_spec.rb +0 -64
  59. data/spec/faraday/adapter/patron_spec.rb +0 -18
  60. data/spec/faraday/adapter/rack_spec.rb +0 -8
  61. data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
  62. data/spec/faraday/deprecate_spec.rb +0 -147
  63. data/spec/faraday/response/middleware_spec.rb +0 -68
  64. data/spec/support/webmock_rack_app.rb +0 -68
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6c99ff059f5fdb0926b754a976809c5fe7db068c288a1ac5e0c3d70c26986f6f
4
- data.tar.gz: 1ca17845876218b7d70805f8898da7fa1c520dc37030ccadbc3753d0f92866a0
3
+ metadata.gz: 07fafa8818a9063a9df90d878f7c6b79b0266f330fab9a9a271ac7b3d16e9bd8
4
+ data.tar.gz: ef16b014bc9ca58649557382c31817fba19c046029bb0c58c02a4b1a4654561e
5
5
  SHA512:
6
- metadata.gz: 7b5206b7e57f8ab2755de596c244807ed990067c240e23d18cf9a613a948edd0d83d131aa95eb79ec55a83787b0a360615ed0abf2cbc7a9ef27ddb45ae458150
7
- data.tar.gz: d86797dcf2c0f154dbf8f4ad557665d8cd4f3b86940e15480cc107151fa2d7db8a8efd081dc1a9ae3e56303d7ecdba6fc373c380e5b5e0e32d6cb7c453a6e437
6
+ metadata.gz: ec0843f0b2e4f37732bc6acd0fd2fb5417ce965c597e3385207ae2893bbe359cf037121712d0cbd7ce69371629bef9587b00b33036ae5bd087a23f1f5112cd96
7
+ data.tar.gz: 933b84778e644221a00d472ae0e9478672fcc4946a512eb2e03902cd8c082f56c2064fc37fb05ea4e3d904a9e2c7bb192c32b0ff22ce8b790f7b5edf08c490f1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,108 @@
1
1
  # Faraday Changelog
2
2
 
3
+ ## [v1.7.0](https://github.com/lostisland/faraday/releases/tag/v1.7.0) (2021-08-09)
4
+
5
+ ### Features
6
+
7
+ * Add strict_mode to Test::Stubs (#1298, @yykamei)
8
+
9
+ ## [v1.6.0](https://github.com/lostisland/faraday/releases/tag/v1.6.0) (2021-08-01)
10
+
11
+ ### Misc
12
+
13
+ * Use external Rack adapter (#1296, @iMacTia)
14
+
15
+ ## [v1.5.1](https://github.com/lostisland/faraday/releases/tag/v1.5.1) (2021-07-11)
16
+
17
+ ### Fixes
18
+
19
+ * Fix JRuby incompatibility after moving out EM adapters (#1294, @ahorek)
20
+
21
+ ### Documentation
22
+
23
+ * Update YARD to follow RackBuilder (#1292, @kachick)
24
+
25
+ ## [v1.5.0](https://github.com/lostisland/faraday/releases/tag/v1.5.0) (2021-07-04)
26
+
27
+ ### Misc
28
+
29
+ * Use external httpclient adapter (#1289, @iMacTia)
30
+ * Use external patron adapter (#1290, @iMacTia)
31
+
32
+ ## [v1.4.3](https://github.com/lostisland/faraday/releases/tag/v1.4.3) (2021-06-24)
33
+
34
+ ### Fixes
35
+
36
+ * Silence warning (#1286, @gurgeous)
37
+ * Always dup url_prefix in Connection#build_exclusive_url (#1288, @alexeyds)
38
+
39
+ ## [v1.4.2](https://github.com/lostisland/faraday/releases/tag/v1.4.2) (2021-05-22)
40
+
41
+ ### Fixes
42
+ * Add proxy setting when url_prefix is changed (#1276, @ci)
43
+ * Default proxy scheme to http:// if necessary, fixes #1282 (#1283, @gurgeous)
44
+
45
+ ### Documentation
46
+ * Improve introduction page (#1273, @gurgeous)
47
+ * Docs: add more middleware examples (#1277, @gurgeous)
48
+
49
+ ### Misc
50
+ * Use external `em_http` and `em_synchrony` adapters (#1274, @iMacTia)
51
+
52
+ ## [v1.4.1](https://github.com/lostisland/faraday/releases/tag/v1.4.1) (2021-04-18)
53
+
54
+ ### Fixes
55
+
56
+ * Fix dependencies from external adapter gems (#1269, @iMacTia)
57
+
58
+ ## [v1.4.0](https://github.com/lostisland/faraday/releases/tag/v1.4.0) (2021-04-16)
59
+
60
+ ### Highlights
61
+
62
+ With this release, we continue the work of gradually moving out adapters into their own gems 🎉
63
+ Thanks to @MikeRogers0 for helping the Faraday team in progressing with this quest 👏
64
+
65
+ And thanks to @olleolleolle efforts, Faraday is becoming more inclusive than ever 🤗
66
+ Faraday's `master` branch has been renamed into `main`, we have an official policy on inclusive language and even a rubocop plugin to check for non-inclusive words ❤️!
67
+ Checkout the "Misc" section below for more details 🙌 !
68
+
69
+ ### Fixes
70
+
71
+ * Fix NoMethodError undefined method 'coverage' (#1255, @Maroo-b)
72
+
73
+ ### Documentation
74
+
75
+ * Some docs on EventMachine adapters. (#1232, @damau)
76
+ * CONTRIBUTING: Fix grammar and layout (#1261, @olleolleolle)
77
+
78
+ ### Misc
79
+
80
+ * Replacing Net::HTTP::Persistent with faraday-net_http_persistent (#1250, @MikeRogers0)
81
+ * CI: Configure the regenerated Coveralls token (#1256, @olleolleolle)
82
+ * Replace Excon adapter with Faraday::Excon gem, and fix autoloading issue with Faraday::NetHttpPersistent (#1257, @iMacTia)
83
+ * Drop CodeClimate (#1259, @olleolleolle)
84
+ * CI: Rename default branch to main (#1263, @olleolleolle)
85
+ * Drop RDoc support file .document (#1264, @olleolleolle, @iMacTia)
86
+ * CONTRIBUTING: add a policy on inclusive language (#1262, @olleolleolle)
87
+ * Add rubocop-inclusivity (#1267, @olleolleolle, @iMacTia)
88
+
89
+ ## [v1.3.1](https://github.com/lostisland/faraday/releases/tag/v1.3.1) (2021-04-16)
90
+
91
+ ### Fixes
92
+
93
+ * Escape colon in path segment (#1237, @yarafan)
94
+ * Handle IPv6 address String on Faraday::Connection#proxy_from_env (#1252, @cosmo0920)
95
+
96
+ ### Documentation
97
+
98
+ * Fix broken Rubydoc.info links (#1236, @nickcampbell18)
99
+ * Add httpx to list of external adapters (#1246, @HoneyryderChuck)
100
+
101
+ ### Misc
102
+
103
+ * Refactor CI to remove duplicated line (#1230, @tricknotes)
104
+ * Gemspec: Pick a good ruby2_keywords release (#1241, @olleolleolle)
105
+
3
106
  ## [v1.3.0](https://github.com/lostisland/faraday/releases/tag/v1.3.0) (2020-12-31)
4
107
 
5
108
  ### Highlights
@@ -104,7 +207,7 @@ Many thanks to the Faraday Team, @JanDintel and everyone who attended the [ROSS
104
207
  * Website: add search bar (#1116)
105
208
  * Fix request/response mix-up in docs text (#1132)
106
209
 
107
- ## v1.0
210
+ ## [v1.0](https://github.com/lostisland/faraday/releases/tag/v1.0.0) (2020-01-22)
108
211
 
109
212
  Features:
110
213
 
@@ -148,6 +251,13 @@ Misc:
148
251
  * Describe clearing cached stubs #1045 (@viraptor)
149
252
  * Add project metadata to the gemspec #1046 (@orien)
150
253
 
254
+ ## v0.17.4
255
+
256
+ Fixes:
257
+
258
+ * NetHttp adapter: wrap Errno::EADDRNOTAVAIL (#1114, @embs)
259
+ * Fix === for subclasses of deprecated classes (#1243, @mervync)
260
+
151
261
  ## v0.17.3
152
262
 
153
263
  Fixes:
data/README.md CHANGED
@@ -2,12 +2,18 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/faraday.svg)](https://rubygems.org/gems/faraday)
4
4
  [![GitHub Actions CI](https://github.com/lostisland/faraday/workflows/CI/badge.svg)](https://github.com/lostisland/faraday/actions?query=workflow%3ACI)
5
- [![Gitter](https://badges.gitter.im/lostisland/faraday.svg)](https://gitter.im/lostisland/faraday?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
5
+ [![GitHub Discussions](https://img.shields.io/github/discussions/lostisland/faraday?logo=github)](https://github.com/lostisland/faraday/discussions)
6
6
 
7
7
 
8
- Faraday is an HTTP client library that provides a common interface over many
9
- adapters (such as Net::HTTP) and embraces the concept of Rack middleware when
10
- processing the request/response cycle.
8
+ Faraday is an HTTP client library abstraction layer that provides a common interface over many
9
+ adapters (such as Net::HTTP) and embraces the concept of Rack middleware when processing the request/response cycle.
10
+ You probably don't want to use Faraday directly in your project, as it will lack an actual client library to perform
11
+ requests. Instead, you probably want to have a look at [Awesome Faraday][awesome] for a list of available adapters.
12
+
13
+ ## FARADAY 2.0
14
+
15
+ You're reading the README and looking at the code of our upcoming v2.0 release (the `main` branch, currently in alpha).
16
+ If you're here to read about our latest v1.x release, then please head over to the [1.x branch](https://github.com/lostisland/faraday/tree/1.x).
11
17
 
12
18
  ## Getting Started
13
19
 
@@ -16,10 +22,10 @@ Need more details? See the [Faraday API Documentation][apidoc] to see how it wor
16
22
 
17
23
  ## Supported Ruby versions
18
24
 
19
- This library aims to support and is [tested against][actions] the following Ruby
20
- implementations:
21
-
22
- * Ruby 2.4+
25
+ This library aims to support and is [tested against][actions] the currently officially supported Ruby
26
+ implementations. This means that, even without a major release, we could add or drop support for Ruby versions,
27
+ following their [EOL](https://endoflife.date/ruby).
28
+ Currently that means we support Ruby 2.6+
23
29
 
24
30
  If something doesn't work on one of these Ruby versions, it's a bug.
25
31
 
@@ -41,8 +47,9 @@ Open the issues page and check for the `help wanted` label!
41
47
  But before you start coding, please read our [Contributing Guide][contributing]
42
48
 
43
49
  ## Copyright
44
- © 2009 - 2020, the [Faraday Team][faraday_team]. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
50
+ © 2009 - 2021, the [Faraday Team][faraday_team]. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
45
51
 
52
+ [awesome]: https://github.com/lostisland/awesome-faraday/#adapters
46
53
  [website]: https://lostisland.github.io/faraday
47
54
  [faraday_team]: https://lostisland.github.io/faraday/team
48
55
  [contributing]: https://github.com/lostisland/faraday/blob/master/.github/CONTRIBUTING.md
@@ -42,7 +42,7 @@ class ClientTest < Test::Unit::TestCase
42
42
  stubs.verify_stubbed_calls
43
43
  end
44
44
 
45
- def test_sushi_404
45
+ def test_sushi_not_found
46
46
  stubs = Faraday::Adapter::Test::Stubs.new
47
47
  stubs.get('/ebi') do
48
48
  [
@@ -260,3 +260,5 @@ module Faraday
260
260
  end
261
261
  end
262
262
  end
263
+
264
+ Faraday::Adapter.register_middleware(test: Faraday::Adapter::Test)
@@ -5,14 +5,9 @@ module Faraday
5
5
  # responsible for fulfilling a Faraday request.
6
6
  class Adapter
7
7
  extend MiddlewareRegistry
8
- extend DependencyLoader
9
8
 
10
9
  CONTENT_LENGTH = 'Content-Length'
11
10
 
12
- register_middleware File.expand_path('adapter', __dir__),
13
- test: [:Test, 'test'],
14
- typhoeus: [:Typhoeus, 'typhoeus']
15
-
16
11
  # This module marks an Adapter as supporting parallel requests.
17
12
  module Parallelism
18
13
  attr_writer :supports_parallel
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'faraday/deprecate'
4
-
5
3
  module Faraday
6
4
  # Connection objects manage the default properties and the middleware
7
5
  # stack for fulfilling an HTTP request.
@@ -285,75 +283,6 @@ module Faraday
285
283
  RUBY
286
284
  end
287
285
 
288
- # Sets up the Authorization header with these credentials, encoded
289
- # with base64.
290
- #
291
- # @param login [String] The authentication login.
292
- # @param pass [String] The authentication password.
293
- #
294
- # @example
295
- #
296
- # conn.basic_auth 'Aladdin', 'open sesame'
297
- # conn.headers['Authorization']
298
- # # => "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
299
- #
300
- # @return [void]
301
- def basic_auth(login, pass)
302
- set_authorization_header(:basic_auth, login, pass)
303
- end
304
-
305
- extend Faraday::Deprecate
306
- deprecate :basic_auth, '#request(:basic_auth, ...)', '2.0'
307
-
308
- # Sets up the Authorization header with the given token.
309
- #
310
- # @param token [String]
311
- # @param options [Hash] extra token options.
312
- #
313
- # @example
314
- #
315
- # conn.token_auth 'abcdef', foo: 'bar'
316
- # conn.headers['Authorization']
317
- # # => "Token token=\"abcdef\",
318
- # foo=\"bar\""
319
- #
320
- # @return [void]
321
- def token_auth(token, options = nil)
322
- set_authorization_header(:token_auth, token, options)
323
- end
324
-
325
- deprecate :token_auth,
326
- '#request(:token_auth, ...)',
327
- '2.0',
328
- 'See https://lostisland.github.io/faraday/middleware/authentication for more usage info.'
329
-
330
- # Sets up a custom Authorization header.
331
- #
332
- # @param type [String] authorization type
333
- # @param token [String, Hash] token. A String value is taken literally, and
334
- # a Hash is encoded into comma-separated key/value pairs.
335
- #
336
- # @example
337
- #
338
- # conn.authorization :Bearer, 'mF_9.B5f-4.1JqM'
339
- # conn.headers['Authorization']
340
- # # => "Bearer mF_9.B5f-4.1JqM"
341
- #
342
- # conn.authorization :Token, token: 'abcdef', foo: 'bar'
343
- # conn.headers['Authorization']
344
- # # => "Token token=\"abcdef\",
345
- # foo=\"bar\""
346
- #
347
- # @return [void]
348
- def authorization(type, token)
349
- set_authorization_header(:authorization, type, token)
350
- end
351
-
352
- deprecate :authorization,
353
- '#request(:authorization, ...)',
354
- '2.0',
355
- 'See https://lostisland.github.io/faraday/middleware/authentication for more usage info.'
356
-
357
286
  # Check if the adapter is parallel-capable.
358
287
  #
359
288
  # @yield if the adapter isn't parallel-capable, or if no adapter is set yet.
@@ -441,7 +370,7 @@ module Faraday
441
370
  end
442
371
 
443
372
  def set_basic_auth(user, password)
444
- header = Faraday::Request::BasicAuthentication.header(user, password)
373
+ header = Faraday::Utils.basic_header_from(user, password)
445
374
  headers[Faraday::Request::Authorization::KEY] = header
446
375
  end
447
376
 
@@ -545,16 +474,12 @@ module Faraday
545
474
  if url && base.path && base.path !~ %r{/$}
546
475
  base.path = "#{base.path}/" # ensure trailing slash
547
476
  end
548
- # Ensure protocol-relative URLs are handled correctly (CVE-2026-25765)
549
- url = "./#{url}" if url.respond_to?(:start_with?) && url.start_with?('//')
550
- url = url && URI.parse(url.to_s).opaque ? url.to_s.gsub(':', '%3A') : url
477
+ url = url.to_s.gsub(':', '%3A') if url && URI.parse(url.to_s).opaque
551
478
  uri = url ? base + url : base
552
479
  if params
553
480
  uri.query = params.to_query(params_encoder || options.params_encoder)
554
481
  end
555
- # rubocop:disable Style/SafeNavigation
556
482
  uri.query = nil if uri.query && uri.query.empty?
557
- # rubocop:enable Style/SafeNavigation
558
483
  uri
559
484
  end
560
485
 
@@ -586,14 +511,6 @@ module Faraday
586
511
  yield(Utils.unescape(uri.user), Utils.unescape(uri.password))
587
512
  end
588
513
 
589
- def set_authorization_header(header_type, *args)
590
- header = Faraday::Request
591
- .lookup_middleware(header_type)
592
- .header(*args)
593
-
594
- headers[Faraday::Request::Authorization::KEY] = header
595
- end
596
-
597
514
  def proxy_from_env(url)
598
515
  return if Faraday.ignore_env_proxy
599
516
 
@@ -638,7 +555,7 @@ module Faraday
638
555
  end
639
556
 
640
557
  def support_parallel?(adapter)
641
- adapter&.respond_to?(:supports_parallel?) && adapter&.supports_parallel?
558
+ adapter.respond_to?(:supports_parallel?) && adapter&.supports_parallel?
642
559
  end
643
560
  end
644
561
  end
@@ -100,11 +100,9 @@ module Faraday
100
100
 
101
101
  def decode_pair(key, value, context)
102
102
  subkeys = key.scan(SUBKEYS_REGEX)
103
- validate_params_depth!(subkeys.length)
104
-
105
103
  subkeys.each_with_index do |subkey, i|
106
104
  is_array = subkey =~ /[\[\]]+\Z/
107
- subkey = $` if is_array
105
+ subkey = Regexp.last_match.pre_match if is_array
108
106
  last_subkey = i == subkeys.length - 1
109
107
 
110
108
  context = prepare_context(context, subkey, is_array, last_subkey)
@@ -126,7 +124,7 @@ module Faraday
126
124
  value_type = is_array ? Array : Hash
127
125
  if context[subkey] && !context[subkey].is_a?(value_type)
128
126
  raise TypeError, "expected #{value_type.name} " \
129
- "(got #{context[subkey].class.name}) for param `#{subkey}'"
127
+ "(got #{context[subkey].class.name}) for param `#{subkey}'"
130
128
  end
131
129
 
132
130
  context[subkey] ||= value_type.new
@@ -141,12 +139,6 @@ module Faraday
141
139
  is_array ? context << value : context[subkey] = value
142
140
  end
143
141
 
144
- def validate_params_depth!(depth)
145
- return unless @param_depth_limit && depth > @param_depth_limit
146
-
147
- raise Faraday::Error, "exceeded nested parameter depth limit of #{@param_depth_limit}"
148
- end
149
-
150
142
  # Internal: convert a nested hash with purely numeric keys into an array.
151
143
  # FIXME: this is not compatible with Rack::Utils.parse_nested_query
152
144
  # @!visibility private
@@ -169,7 +161,7 @@ module Faraday
169
161
  # for your requests.
170
162
  module NestedParamsEncoder
171
163
  class << self
172
- attr_accessor :sort_params, :param_depth_limit
164
+ attr_accessor :sort_params
173
165
 
174
166
  extend Forwardable
175
167
  def_delegators :'Faraday::Utils', :escape, :unescape
@@ -177,7 +169,6 @@ module Faraday
177
169
 
178
170
  # Useful default for OAuth and caching.
179
171
  @sort_params = true
180
- @param_depth_limit = 100
181
172
 
182
173
  extend EncodeMethods
183
174
  extend DecodeMethods
data/lib/faraday/error.rb CHANGED
@@ -52,6 +52,7 @@ module Faraday
52
52
  # :body - Optional string HTTP response body.
53
53
  # :request - Hash
54
54
  # :method - Symbol with the request HTTP method.
55
+ # :url - URI object with the url requested.
55
56
  # :url_path - String with the url path requested.
56
57
  # :params - String key/value hash of query params
57
58
  # present in the request.
@@ -143,4 +144,10 @@ module Faraday
143
144
  # Raised by FaradayMiddleware::ResponseMiddleware
144
145
  class ParsingError < Error
145
146
  end
147
+
148
+ # Exception used to control the Retry middleware.
149
+ #
150
+ # @see Faraday::Request::Retry
151
+ class RetriableResponse < Error
152
+ end
146
153
  end
@@ -0,0 +1,122 @@
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
+ Parts = ::Parts
56
+
57
+ # Similar to, but not compatible with CompositeReadIO provided by the
58
+ # multipart-post gem.
59
+ # https://github.com/nicksieger/multipart-post/blob/master/lib/composite_io.rb
60
+ class CompositeReadIO
61
+ def initialize(*parts)
62
+ @parts = parts.flatten
63
+ @ios = @parts.map(&:to_io)
64
+ @index = 0
65
+ end
66
+
67
+ # @return [Integer] sum of the lengths of all the parts
68
+ def length
69
+ @parts.inject(0) { |sum, part| sum + part.length }
70
+ end
71
+
72
+ # Rewind each of the IOs and reset the index to 0.
73
+ #
74
+ # @return [void]
75
+ def rewind
76
+ @ios.each(&:rewind)
77
+ @index = 0
78
+ end
79
+
80
+ # Read from IOs in order until `length` bytes have been received.
81
+ #
82
+ # @param length [Integer, nil]
83
+ # @param outbuf [String, nil]
84
+ def read(length = nil, outbuf = nil)
85
+ got_result = false
86
+ outbuf = outbuf ? (+outbuf).replace('') : +''
87
+
88
+ while (io = current_io)
89
+ if (result = io.read(length))
90
+ got_result ||= !result.nil?
91
+ result.force_encoding('BINARY') if result.respond_to?(:force_encoding)
92
+ outbuf << result
93
+ length -= result.length if length
94
+ break if length&.zero?
95
+ end
96
+ advance_io
97
+ end
98
+ !got_result && length ? nil : outbuf
99
+ end
100
+
101
+ # Close each of the IOs.
102
+ #
103
+ # @return [void]
104
+ def close
105
+ @ios.each(&:close)
106
+ end
107
+
108
+ def ensure_open_and_readable
109
+ # Rubinius compatibility
110
+ end
111
+
112
+ private
113
+
114
+ def current_io
115
+ @ios[@index]
116
+ end
117
+
118
+ def advance_io
119
+ @index += 1
120
+ end
121
+ end
122
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'pp'
4
+
4
5
  module Faraday
5
6
  module Logging
6
7
  # Serves as an integration point to customize logging
@@ -4,7 +4,6 @@ module Faraday
4
4
  # Middleware is the basic base class of any Faraday middleware.
5
5
  class Middleware
6
6
  extend MiddlewareRegistry
7
- extend DependencyLoader
8
7
 
9
8
  attr_reader :app, :options
10
9