faraday 1.10.3 → 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 (62) 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 -84
  8. data/lib/faraday/encoders/nested_params_encoder.rb +2 -2
  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 -51
  32. data/spec/faraday/options/env_spec.rb +2 -2
  33. data/spec/faraday/rack_builder_spec.rb +5 -43
  34. data/spec/faraday/request/authorization_spec.rb +14 -36
  35. data/spec/faraday/request/instrumentation_spec.rb +5 -7
  36. data/spec/faraday/request/multipart_spec.rb +302 -0
  37. data/spec/faraday/request/retry_spec.rb +254 -0
  38. data/spec/faraday/request_spec.rb +0 -11
  39. data/spec/faraday/response/json_spec.rb +4 -6
  40. data/spec/faraday/response/raise_error_spec.rb +7 -4
  41. data/spec/faraday/utils_spec.rb +1 -1
  42. data/spec/spec_helper.rb +0 -2
  43. data/spec/support/fake_safe_buffer.rb +1 -1
  44. data/spec/support/shared_examples/request_method.rb +5 -5
  45. metadata +21 -152
  46. data/lib/faraday/adapter/typhoeus.rb +0 -15
  47. data/lib/faraday/autoload.rb +0 -87
  48. data/lib/faraday/dependency_loader.rb +0 -39
  49. data/lib/faraday/deprecate.rb +0 -110
  50. data/lib/faraday/request/basic_authentication.rb +0 -20
  51. data/lib/faraday/request/token_authentication.rb +0 -20
  52. data/spec/faraday/adapter/em_http_spec.rb +0 -49
  53. data/spec/faraday/adapter/em_synchrony_spec.rb +0 -18
  54. data/spec/faraday/adapter/excon_spec.rb +0 -49
  55. data/spec/faraday/adapter/httpclient_spec.rb +0 -73
  56. data/spec/faraday/adapter/net_http_spec.rb +0 -64
  57. data/spec/faraday/adapter/patron_spec.rb +0 -18
  58. data/spec/faraday/adapter/rack_spec.rb +0 -8
  59. data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
  60. data/spec/faraday/deprecate_spec.rb +0 -147
  61. data/spec/faraday/response/middleware_spec.rb +0 -68
  62. data/spec/support/webmock_rack_app.rb +0 -68
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a35b8ef069416d74f46cbe1f9b14b9d28e8b141bed4143d2c97ac137b766f18
4
- data.tar.gz: cdbb3768e4aa6d316e2216ec7fdf2a93b5141abe83dd82b3ffa522002b01523c
3
+ metadata.gz: 07fafa8818a9063a9df90d878f7c6b79b0266f330fab9a9a271ac7b3d16e9bd8
4
+ data.tar.gz: ef16b014bc9ca58649557382c31817fba19c046029bb0c58c02a4b1a4654561e
5
5
  SHA512:
6
- metadata.gz: 743727fca5013eb1bd1f005c334d171fc59a5cb56687395b3d14a48911ee4b7fef5da00e64ac6dbe15b75cc76edcb26d400b4d748e9676d9cdb12e8d2d8ee7b7
7
- data.tar.gz: 203527c8adc2828334b74e97a33041ed9127548b91905d15331ff627c5f20d679c3d2ccd0b3102c0de35e011c5f15f2bac50b8aa4eccaeafaf1551611ca020e1
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,14 +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
- 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
549
478
  uri = url ? base + url : base
550
479
  if params
551
480
  uri.query = params.to_query(params_encoder || options.params_encoder)
552
481
  end
553
- # rubocop:disable Style/SafeNavigation
554
482
  uri.query = nil if uri.query && uri.query.empty?
555
- # rubocop:enable Style/SafeNavigation
556
483
  uri
557
484
  end
558
485
 
@@ -584,14 +511,6 @@ module Faraday
584
511
  yield(Utils.unescape(uri.user), Utils.unescape(uri.password))
585
512
  end
586
513
 
587
- def set_authorization_header(header_type, *args)
588
- header = Faraday::Request
589
- .lookup_middleware(header_type)
590
- .header(*args)
591
-
592
- headers[Faraday::Request::Authorization::KEY] = header
593
- end
594
-
595
514
  def proxy_from_env(url)
596
515
  return if Faraday.ignore_env_proxy
597
516
 
@@ -636,7 +555,7 @@ module Faraday
636
555
  end
637
556
 
638
557
  def support_parallel?(adapter)
639
- adapter&.respond_to?(:supports_parallel?) && adapter&.supports_parallel?
558
+ adapter.respond_to?(:supports_parallel?) && adapter&.supports_parallel?
640
559
  end
641
560
  end
642
561
  end
@@ -102,7 +102,7 @@ module Faraday
102
102
  subkeys = key.scan(SUBKEYS_REGEX)
103
103
  subkeys.each_with_index do |subkey, i|
104
104
  is_array = subkey =~ /[\[\]]+\Z/
105
- subkey = $` if is_array
105
+ subkey = Regexp.last_match.pre_match if is_array
106
106
  last_subkey = i == subkeys.length - 1
107
107
 
108
108
  context = prepare_context(context, subkey, is_array, last_subkey)
@@ -124,7 +124,7 @@ module Faraday
124
124
  value_type = is_array ? Array : Hash
125
125
  if context[subkey] && !context[subkey].is_a?(value_type)
126
126
  raise TypeError, "expected #{value_type.name} " \
127
- "(got #{context[subkey].class.name}) for param `#{subkey}'"
127
+ "(got #{context[subkey].class.name}) for param `#{subkey}'"
128
128
  end
129
129
 
130
130
  context[subkey] ||= value_type.new
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
 
@@ -6,59 +6,26 @@ module Faraday
6
6
  # Adds the ability for other modules to register and lookup
7
7
  # middleware classes.
8
8
  module MiddlewareRegistry
9
+ def registered_middleware
10
+ @registered_middleware ||= {}
11
+ end
12
+
9
13
  # Register middleware class(es) on the current module.
10
14
  #
11
- # @param autoload_path [String] Middleware autoload path
12
- # @param mapping [Hash{
13
- # Symbol => Module,
14
- # Symbol => Array<Module, Symbol, String>,
15
- # }] Middleware mapping from a lookup symbol to a reference to the
16
- # middleware.
17
- # Classes can be expressed as:
18
- # - a fully qualified constant
19
- # - a Symbol
20
- # - a Proc that will be lazily called to return the former
21
- # - an array is given, its first element is the constant or symbol,
22
- # and its second is a file to `require`.
15
+ # @param mappings [Hash] Middleware mappings from a lookup symbol to a middleware class.
23
16
  # @return [void]
24
17
  #
25
18
  # @example Lookup by a constant
26
19
  #
27
20
  # module Faraday
28
- # class Whatever
21
+ # class Whatever < Middleware
29
22
  # # Middleware looked up by :foo returns Faraday::Whatever::Foo.
30
- # register_middleware foo: Foo
31
- # end
32
- # end
33
- #
34
- # @example Lookup by a symbol
35
- #
36
- # module Faraday
37
- # class Whatever
38
- # # Middleware looked up by :bar returns
39
- # # Faraday::Whatever.const_get(:Bar)
40
- # register_middleware bar: :Bar
41
- # end
42
- # end
43
- #
44
- # @example Lookup by a symbol and string in an array
45
- #
46
- # module Faraday
47
- # class Whatever
48
- # # Middleware looked up by :baz requires 'baz' and returns
49
- # # Faraday::Whatever.const_get(:Baz)
50
- # register_middleware baz: [:Baz, 'baz']
23
+ # register_middleware(foo: Whatever)
51
24
  # end
52
25
  # end
53
- #
54
- def register_middleware(autoload_path = nil, mapping = nil)
55
- if mapping.nil?
56
- mapping = autoload_path
57
- autoload_path = nil
58
- end
26
+ def register_middleware(**mappings)
59
27
  middleware_mutex do
60
- @middleware_autoload_path = autoload_path if autoload_path
61
- (@registered_middleware ||= {}).update(mapping)
28
+ registered_middleware.update(mappings)
62
29
  end
63
30
  end
64
31
 
@@ -66,7 +33,7 @@ module Faraday
66
33
  #
67
34
  # @param key [Symbol] key for the registered middleware.
68
35
  def unregister_middleware(key)
69
- @registered_middleware.delete(key)
36
+ registered_middleware.delete(key)
70
37
  end
71
38
 
72
39
  # Lookup middleware class with a registered Symbol shortcut.
@@ -78,16 +45,15 @@ module Faraday
78
45
  # @example
79
46
  #
80
47
  # module Faraday
81
- # class Whatever
82
- # register_middleware foo: Foo
48
+ # class Whatever < Middleware
49
+ # register_middleware(foo: Whatever)
83
50
  # end
84
51
  # end
85
52
  #
86
- # Faraday::Whatever.lookup_middleware(:foo)
87
- # # => Faraday::Whatever::Foo
88
- #
53
+ # Faraday::Middleware.lookup_middleware(:foo)
54
+ # # => Faraday::Whatever
89
55
  def lookup_middleware(key)
90
- load_middleware(key) ||
56
+ registered_middleware[key] ||
91
57
  raise(Faraday::Error, "#{key.inspect} is not registered on #{self}")
92
58
  end
93
59
 
@@ -95,35 +61,5 @@ module Faraday
95
61
  @middleware_mutex ||= Monitor.new
96
62
  @middleware_mutex.synchronize(&block)
97
63
  end
98
-
99
- def fetch_middleware(key)
100
- defined?(@registered_middleware) && @registered_middleware[key]
101
- end
102
-
103
- def load_middleware(key)
104
- value = fetch_middleware(key)
105
- case value
106
- when Module
107
- value
108
- when Symbol, String
109
- middleware_mutex do
110
- @registered_middleware[key] = const_get(value)
111
- end
112
- when Proc
113
- middleware_mutex do
114
- @registered_middleware[key] = value.call
115
- end
116
- when Array
117
- middleware_mutex do
118
- const, path = value
119
- if (root = @middleware_autoload_path)
120
- path = "#{root}/#{path}"
121
- end
122
- require(path)
123
- @registered_middleware[key] = const
124
- end
125
- load_middleware(key)
126
- end
127
- end
128
64
  end
129
65
  end