faraday 1.8.0 → 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 (60) 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 +1 -0
  10. data/lib/faraday/file_part.rb +0 -6
  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/rack_builder.rb +1 -1
  16. data/lib/faraday/request/authorization.rb +26 -40
  17. data/lib/faraday/request/instrumentation.rb +2 -0
  18. data/lib/faraday/request/json.rb +55 -0
  19. data/lib/faraday/request/multipart.rb +2 -0
  20. data/lib/faraday/request/retry.rb +3 -1
  21. data/lib/faraday/request/url_encoded.rb +2 -0
  22. data/lib/faraday/request.rb +13 -31
  23. data/lib/faraday/response/json.rb +54 -0
  24. data/lib/faraday/response/logger.rb +4 -4
  25. data/lib/faraday/response/raise_error.rb +9 -1
  26. data/lib/faraday/response.rb +8 -19
  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 -37
  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/json_spec.rb +111 -0
  37. data/spec/faraday/request/multipart_spec.rb +5 -5
  38. data/spec/faraday/request/retry_spec.rb +13 -1
  39. data/spec/faraday/request_spec.rb +0 -11
  40. data/spec/faraday/response/json_spec.rb +117 -0
  41. data/spec/faraday/response/raise_error_spec.rb +7 -4
  42. data/spec/faraday/utils_spec.rb +1 -1
  43. data/spec/support/fake_safe_buffer.rb +1 -1
  44. data/spec/support/shared_examples/request_method.rb +5 -5
  45. metadata +11 -134
  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 -37
  49. data/lib/faraday/request/basic_authentication.rb +0 -20
  50. data/lib/faraday/request/token_authentication.rb +0 -20
  51. data/spec/faraday/adapter/em_http_spec.rb +0 -49
  52. data/spec/faraday/adapter/em_synchrony_spec.rb +0 -18
  53. data/spec/faraday/adapter/excon_spec.rb +0 -49
  54. data/spec/faraday/adapter/httpclient_spec.rb +0 -73
  55. data/spec/faraday/adapter/net_http_spec.rb +0 -64
  56. data/spec/faraday/adapter/patron_spec.rb +0 -18
  57. data/spec/faraday/adapter/rack_spec.rb +0 -8
  58. data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
  59. data/spec/faraday/response/middleware_spec.rb +0 -68
  60. data/spec/support/webmock_rack_app.rb +0 -68
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f6441da03048f7371bd0cb898dd3e27c7be6382c67fc4901883e9dbe9841b56f
4
- data.tar.gz: 0beb3f2da75282655fc46cd5540de96d5fef259dd35193ec9eb0d3163cc959d2
3
+ metadata.gz: 07fafa8818a9063a9df90d878f7c6b79b0266f330fab9a9a271ac7b3d16e9bd8
4
+ data.tar.gz: ef16b014bc9ca58649557382c31817fba19c046029bb0c58c02a4b1a4654561e
5
5
  SHA512:
6
- metadata.gz: 1ab1aa0e0c68c9348d9e180a903f01f2517bd38672fe1bdf056ceab4729e841944173e47dc26aa1b4fe16b2779f1a8102914ed594ea92d08feff6dc944412f0b
7
- data.tar.gz: bc43fbac427062861e79d5328b60886a5b9c8783f707faa1fcfad064b9c6945f6a4e2a97dbad9ecadc1c1620b5327d651a764962764b7fc86e6d2fbd84ad0b4b
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
@@ -283,77 +283,6 @@ module Faraday
283
283
  RUBY
284
284
  end
285
285
 
286
- # Sets up the Authorization header with these credentials, encoded
287
- # with base64.
288
- #
289
- # @param login [String] The authentication login.
290
- # @param pass [String] The authentication password.
291
- #
292
- # @example
293
- #
294
- # conn.basic_auth 'Aladdin', 'open sesame'
295
- # conn.headers['Authorization']
296
- # # => "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
297
- #
298
- # @return [void]
299
- def basic_auth(login, pass)
300
- warn <<~TEXT
301
- WARNING: `Faraday::Connection#basic_auth` is deprecated; it will be removed in version 2.0.
302
- While initializing your connection, use `#request(:basic_auth, ...)` instead.
303
- See https://lostisland.github.io/faraday/middleware/authentication for more usage info.
304
- TEXT
305
- set_authorization_header(:basic_auth, login, pass)
306
- end
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
- warn <<~TEXT
323
- WARNING: `Faraday::Connection#token_auth` is deprecated; it will be removed in version 2.0.
324
- While initializing your connection, use `#request(:token_auth, ...)` instead.
325
- See https://lostisland.github.io/faraday/middleware/authentication for more usage info.
326
- TEXT
327
- set_authorization_header(:token_auth, token, options)
328
- end
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
- warn <<~TEXT
350
- WARNING: `Faraday::Connection#authorization` is deprecated; it will be removed in version 2.0.
351
- While initializing your connection, use `#request(:authorization, ...)` instead.
352
- See https://lostisland.github.io/faraday/middleware/authentication for more usage info.
353
- TEXT
354
- set_authorization_header(:authorization, type, token)
355
- end
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.
@@ -52,12 +52,6 @@ module Faraday
52
52
  # @return [IO]
53
53
  FilePart = ::UploadIO
54
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
55
  Parts = ::Parts
62
56
 
63
57
  # Similar to, but not compatible with CompositeReadIO provided by the
@@ -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
@@ -104,7 +104,7 @@ module Faraday
104
104
 
105
105
  # Public
106
106
  def each_key(&block)
107
- return to_enum(:each_key) unless block_given?
107
+ return to_enum(:each_key) unless block
108
108
 
109
109
  keys.each(&block)
110
110
  end
@@ -118,7 +118,7 @@ module Faraday
118
118
 
119
119
  # Public
120
120
  def each_value(&block)
121
- return to_enum(:each_value) unless block_given?
121
+ return to_enum(:each_value) unless block
122
122
 
123
123
  values.each(&block)
124
124
  end
@@ -168,7 +168,7 @@ module Faraday
168
168
  end
169
169
 
170
170
  def self.memoized(key, &block)
171
- unless block_given?
171
+ unless block
172
172
  raise ArgumentError, '#memoized must be called with a block'
173
173
  end
174
174
 
@@ -61,7 +61,7 @@ module Faraday
61
61
  def initialize(handlers = [], adapter = nil, &block)
62
62
  @adapter = adapter
63
63
  @handlers = handlers
64
- if block_given?
64
+ if block
65
65
  build(&block)
66
66
  elsif @handlers.empty?
67
67
  # default stack, if nothing else is configured
@@ -1,53 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'base64'
4
-
5
3
  module Faraday
6
4
  class Request
7
5
  # Request middleware for the Authorization HTTP header
8
6
  class Authorization < Faraday::Middleware
9
- unless defined?(::Faraday::Request::Authorization::KEY)
10
- KEY = 'Authorization'
11
- end
12
-
13
- # @param type [String, Symbol]
14
- # @param token [String, Symbol, Hash]
15
- # @return [String] a header value
16
- def self.header(type, token)
17
- case token
18
- when String, Symbol, Proc
19
- token = token.call if token.is_a?(Proc)
20
- "#{type} #{token}"
21
- when Hash
22
- build_hash(type.to_s, token)
23
- else
24
- raise ArgumentError,
25
- "Can't build an Authorization #{type}" \
26
- "header from #{token.inspect}"
27
- end
28
- end
29
-
30
- # @param type [String]
31
- # @param hash [Hash]
32
- # @return [String] type followed by comma-separated key=value pairs
33
- # @api private
34
- def self.build_hash(type, hash)
35
- comma = ', '
36
- values = []
37
- hash.each do |key, value|
38
- value = value.call if value.is_a?(Proc)
39
- values << "#{key}=#{value.to_s.inspect}"
40
- end
41
- "#{type} #{values * comma}"
42
- end
7
+ KEY = 'Authorization'
43
8
 
44
9
  # @param app [#call]
45
10
  # @param type [String, Symbol] Type of Authorization
46
- # @param param [String, Symbol, Hash, Proc] parameter to build the Authorization header.
11
+ # @param params [Array<String, Proc>] parameters to build the Authorization header.
12
+ # If the type is `:basic`, then these can be a login and password pair.
13
+ # Otherwise, a single value is expected that will be appended after the type.
47
14
  # This value can be a proc, in which case it will be invoked on each request.
48
- def initialize(app, type, param)
15
+ def initialize(app, type, *params)
49
16
  @type = type
50
- @param = param
17
+ @params = params
51
18
  super(app)
52
19
  end
53
20
 
@@ -55,8 +22,27 @@ module Faraday
55
22
  def on_request(env)
56
23
  return if env.request_headers[KEY]
57
24
 
58
- env.request_headers[KEY] = self.class.header(@type, @param)
25
+ env.request_headers[KEY] = header_from(@type, *@params)
26
+ end
27
+
28
+ private
29
+
30
+ # @param type [String, Symbol]
31
+ # @param params [Array]
32
+ # @return [String] a header value
33
+ def header_from(type, *params)
34
+ if type.to_s.casecmp('basic').zero? && params.size == 2
35
+ Utils.basic_header_from(*params)
36
+ elsif params.size != 1
37
+ raise ArgumentError, "Unexpected params received (got #{params.size} instead of 1)"
38
+ else
39
+ value = params.first
40
+ value = value.call if value.is_a?(Proc)
41
+ "#{type} #{value}"
42
+ end
59
43
  end
60
44
  end
61
45
  end
62
46
  end
47
+
48
+ Faraday::Request.register_middleware(authorization: Faraday::Request::Authorization)
@@ -52,3 +52,5 @@ module Faraday
52
52
  end
53
53
  end
54
54
  end
55
+
56
+ Faraday::Request.register_middleware(instrumentation: Faraday::Request::Instrumentation)