faraday 1.10.2 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +117 -1
- data/README.md +16 -9
- data/examples/client_spec.rb +1 -1
- data/examples/client_test.rb +2 -2
- data/lib/faraday/adapter/test.rb +2 -0
- data/lib/faraday/adapter.rb +2 -5
- data/lib/faraday/connection.rb +5 -86
- data/lib/faraday/encoders/nested_params_encoder.rb +2 -2
- data/lib/faraday/error.rb +3 -2
- data/lib/faraday/logging/formatter.rb +1 -0
- data/lib/faraday/middleware.rb +0 -1
- data/lib/faraday/middleware_registry.rb +15 -79
- data/lib/faraday/options.rb +3 -3
- data/lib/faraday/rack_builder.rb +21 -18
- data/lib/faraday/request/authorization.rb +28 -41
- data/lib/faraday/request/instrumentation.rb +2 -0
- data/lib/faraday/request/url_encoded.rb +2 -0
- data/lib/faraday/request.rb +6 -24
- data/lib/faraday/response/json.rb +4 -4
- data/lib/faraday/response/logger.rb +2 -0
- data/lib/faraday/response/raise_error.rb +9 -1
- data/lib/faraday/response.rb +7 -20
- data/lib/faraday/utils/headers.rb +1 -1
- data/lib/faraday/utils.rb +10 -5
- data/lib/faraday/version.rb +1 -1
- data/lib/faraday.rb +4 -46
- data/spec/faraday/connection_spec.rb +102 -51
- data/spec/faraday/options/env_spec.rb +2 -2
- data/spec/faraday/rack_builder_spec.rb +7 -50
- data/spec/faraday/request/authorization_spec.rb +19 -32
- data/spec/faraday/request/instrumentation_spec.rb +5 -7
- data/spec/faraday/request/url_encoded_spec.rb +0 -1
- data/spec/faraday/request_spec.rb +0 -11
- data/spec/faraday/response/json_spec.rb +4 -6
- data/spec/faraday/response/raise_error_spec.rb +7 -4
- data/spec/faraday/utils_spec.rb +62 -1
- data/spec/spec_helper.rb +0 -2
- data/spec/support/fake_safe_buffer.rb +1 -1
- data/spec/support/helper_methods.rb +0 -37
- data/spec/support/shared_examples/adapter.rb +0 -1
- data/spec/support/shared_examples/request_method.rb +5 -18
- metadata +5 -163
- data/lib/faraday/adapter/typhoeus.rb +0 -15
- data/lib/faraday/autoload.rb +0 -87
- data/lib/faraday/dependency_loader.rb +0 -37
- data/lib/faraday/deprecate.rb +0 -110
- data/lib/faraday/request/basic_authentication.rb +0 -20
- data/lib/faraday/request/token_authentication.rb +0 -20
- data/spec/faraday/adapter/em_http_spec.rb +0 -49
- data/spec/faraday/adapter/em_synchrony_spec.rb +0 -18
- data/spec/faraday/adapter/excon_spec.rb +0 -49
- data/spec/faraday/adapter/httpclient_spec.rb +0 -73
- data/spec/faraday/adapter/net_http_spec.rb +0 -64
- data/spec/faraday/adapter/patron_spec.rb +0 -18
- data/spec/faraday/adapter/rack_spec.rb +0 -8
- data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
- data/spec/faraday/composite_read_io_spec.rb +0 -80
- data/spec/faraday/deprecate_spec.rb +0 -147
- data/spec/faraday/response/middleware_spec.rb +0 -68
- data/spec/support/webmock_rack_app.rb +0 -68
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 367d509be94da297eac6ece76f5561772218a5982696eba047ef8599d0ff05e1
|
4
|
+
data.tar.gz: 779c66227b778b17661428c17ab65567e23f13ea2824b92251eba67cd32c6280
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 73d405ce87ac4a0917806b16ab11693b68966334656689a95bc06764501c44965832b08fa7bf36beeb71c2144a6661775df4f22b18e8281f664a0605fc2ece49
|
7
|
+
data.tar.gz: 22701db3eb1bb1668aaf6429e711acad05099577b39ccc792d32c42a82043f628483973a993c5ce91acd4207a9f0a01f47a51d6bffc4212047e4150fbb63e1e1
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,114 @@
|
|
1
1
|
# Faraday Changelog
|
2
2
|
|
3
|
+
## [1.8.0](https://github.com/lostisland/faraday/releases/tag/v1.8.0) (2021-09-18)
|
4
|
+
|
5
|
+
### Features
|
6
|
+
|
7
|
+
* Backport authorization procs (#1322, @jarl-dk)
|
8
|
+
|
9
|
+
## [v1.7.0](https://github.com/lostisland/faraday/releases/tag/v1.7.0) (2021-08-09)
|
10
|
+
|
11
|
+
### Features
|
12
|
+
|
13
|
+
* Add strict_mode to Test::Stubs (#1298, @yykamei)
|
14
|
+
|
15
|
+
## [v1.6.0](https://github.com/lostisland/faraday/releases/tag/v1.6.0) (2021-08-01)
|
16
|
+
|
17
|
+
### Misc
|
18
|
+
|
19
|
+
* Use external Rack adapter (#1296, @iMacTia)
|
20
|
+
|
21
|
+
## [v1.5.1](https://github.com/lostisland/faraday/releases/tag/v1.5.1) (2021-07-11)
|
22
|
+
|
23
|
+
### Fixes
|
24
|
+
|
25
|
+
* Fix JRuby incompatibility after moving out EM adapters (#1294, @ahorek)
|
26
|
+
|
27
|
+
### Documentation
|
28
|
+
|
29
|
+
* Update YARD to follow RackBuilder (#1292, @kachick)
|
30
|
+
|
31
|
+
## [v1.5.0](https://github.com/lostisland/faraday/releases/tag/v1.5.0) (2021-07-04)
|
32
|
+
|
33
|
+
### Misc
|
34
|
+
|
35
|
+
* Use external httpclient adapter (#1289, @iMacTia)
|
36
|
+
* Use external patron adapter (#1290, @iMacTia)
|
37
|
+
|
38
|
+
## [v1.4.3](https://github.com/lostisland/faraday/releases/tag/v1.4.3) (2021-06-24)
|
39
|
+
|
40
|
+
### Fixes
|
41
|
+
|
42
|
+
* Silence warning (#1286, @gurgeous)
|
43
|
+
* Always dup url_prefix in Connection#build_exclusive_url (#1288, @alexeyds)
|
44
|
+
|
45
|
+
## [v1.4.2](https://github.com/lostisland/faraday/releases/tag/v1.4.2) (2021-05-22)
|
46
|
+
|
47
|
+
### Fixes
|
48
|
+
* Add proxy setting when url_prefix is changed (#1276, @ci)
|
49
|
+
* Default proxy scheme to http:// if necessary, fixes #1282 (#1283, @gurgeous)
|
50
|
+
|
51
|
+
### Documentation
|
52
|
+
* Improve introduction page (#1273, @gurgeous)
|
53
|
+
* Docs: add more middleware examples (#1277, @gurgeous)
|
54
|
+
|
55
|
+
### Misc
|
56
|
+
* Use external `em_http` and `em_synchrony` adapters (#1274, @iMacTia)
|
57
|
+
|
58
|
+
## [v1.4.1](https://github.com/lostisland/faraday/releases/tag/v1.4.1) (2021-04-18)
|
59
|
+
|
60
|
+
### Fixes
|
61
|
+
|
62
|
+
* Fix dependencies from external adapter gems (#1269, @iMacTia)
|
63
|
+
|
64
|
+
## [v1.4.0](https://github.com/lostisland/faraday/releases/tag/v1.4.0) (2021-04-16)
|
65
|
+
|
66
|
+
### Highlights
|
67
|
+
|
68
|
+
With this release, we continue the work of gradually moving out adapters into their own gems 🎉
|
69
|
+
Thanks to @MikeRogers0 for helping the Faraday team in progressing with this quest 👏
|
70
|
+
|
71
|
+
And thanks to @olleolleolle efforts, Faraday is becoming more inclusive than ever 🤗
|
72
|
+
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 ❤️!
|
73
|
+
Checkout the "Misc" section below for more details 🙌 !
|
74
|
+
|
75
|
+
### Fixes
|
76
|
+
|
77
|
+
* Fix NoMethodError undefined method 'coverage' (#1255, @Maroo-b)
|
78
|
+
|
79
|
+
### Documentation
|
80
|
+
|
81
|
+
* Some docs on EventMachine adapters. (#1232, @damau)
|
82
|
+
* CONTRIBUTING: Fix grammar and layout (#1261, @olleolleolle)
|
83
|
+
|
84
|
+
### Misc
|
85
|
+
|
86
|
+
* Replacing Net::HTTP::Persistent with faraday-net_http_persistent (#1250, @MikeRogers0)
|
87
|
+
* CI: Configure the regenerated Coveralls token (#1256, @olleolleolle)
|
88
|
+
* Replace Excon adapter with Faraday::Excon gem, and fix autoloading issue with Faraday::NetHttpPersistent (#1257, @iMacTia)
|
89
|
+
* Drop CodeClimate (#1259, @olleolleolle)
|
90
|
+
* CI: Rename default branch to main (#1263, @olleolleolle)
|
91
|
+
* Drop RDoc support file .document (#1264, @olleolleolle, @iMacTia)
|
92
|
+
* CONTRIBUTING: add a policy on inclusive language (#1262, @olleolleolle)
|
93
|
+
* Add rubocop-inclusivity (#1267, @olleolleolle, @iMacTia)
|
94
|
+
|
95
|
+
## [v1.3.1](https://github.com/lostisland/faraday/releases/tag/v1.3.1) (2021-04-16)
|
96
|
+
|
97
|
+
### Fixes
|
98
|
+
|
99
|
+
* Escape colon in path segment (#1237, @yarafan)
|
100
|
+
* Handle IPv6 address String on Faraday::Connection#proxy_from_env (#1252, @cosmo0920)
|
101
|
+
|
102
|
+
### Documentation
|
103
|
+
|
104
|
+
* Fix broken Rubydoc.info links (#1236, @nickcampbell18)
|
105
|
+
* Add httpx to list of external adapters (#1246, @HoneyryderChuck)
|
106
|
+
|
107
|
+
### Misc
|
108
|
+
|
109
|
+
* Refactor CI to remove duplicated line (#1230, @tricknotes)
|
110
|
+
* Gemspec: Pick a good ruby2_keywords release (#1241, @olleolleolle)
|
111
|
+
|
3
112
|
## [v1.3.0](https://github.com/lostisland/faraday/releases/tag/v1.3.0) (2020-12-31)
|
4
113
|
|
5
114
|
### Highlights
|
@@ -104,7 +213,7 @@ Many thanks to the Faraday Team, @JanDintel and everyone who attended the [ROSS
|
|
104
213
|
* Website: add search bar (#1116)
|
105
214
|
* Fix request/response mix-up in docs text (#1132)
|
106
215
|
|
107
|
-
## v1.0
|
216
|
+
## [v1.0](https://github.com/lostisland/faraday/releases/tag/v1.0.0) (2020-01-22)
|
108
217
|
|
109
218
|
Features:
|
110
219
|
|
@@ -148,6 +257,13 @@ Misc:
|
|
148
257
|
* Describe clearing cached stubs #1045 (@viraptor)
|
149
258
|
* Add project metadata to the gemspec #1046 (@orien)
|
150
259
|
|
260
|
+
## v0.17.4
|
261
|
+
|
262
|
+
Fixes:
|
263
|
+
|
264
|
+
* NetHttp adapter: wrap Errno::EADDRNOTAVAIL (#1114, @embs)
|
265
|
+
* Fix === for subclasses of deprecated classes (#1243, @mervync)
|
266
|
+
|
151
267
|
## v0.17.3
|
152
268
|
|
153
269
|
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
|
-
[![
|
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
|
-
|
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
|
20
|
-
implementations
|
21
|
-
|
22
|
-
|
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 -
|
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
|
data/examples/client_spec.rb
CHANGED
data/examples/client_test.rb
CHANGED
@@ -42,7 +42,7 @@ class ClientTest < Test::Unit::TestCase
|
|
42
42
|
stubs.verify_stubbed_calls
|
43
43
|
end
|
44
44
|
|
45
|
-
def
|
45
|
+
def test_sushi_not_found
|
46
46
|
stubs = Faraday::Adapter::Test::Stubs.new
|
47
47
|
stubs.get('/ebi') do
|
48
48
|
[
|
@@ -60,7 +60,7 @@ class ClientTest < Test::Unit::TestCase
|
|
60
60
|
def test_sushi_exception
|
61
61
|
stubs = Faraday::Adapter::Test::Stubs.new
|
62
62
|
stubs.get('/ebi') do
|
63
|
-
raise Faraday::ConnectionFailed
|
63
|
+
raise Faraday::ConnectionFailed
|
64
64
|
end
|
65
65
|
|
66
66
|
cli = client(stubs)
|
data/lib/faraday/adapter/test.rb
CHANGED
data/lib/faraday/adapter.rb
CHANGED
@@ -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
|
@@ -103,3 +98,5 @@ module Faraday
|
|
103
98
|
}.freeze
|
104
99
|
end
|
105
100
|
end
|
101
|
+
|
102
|
+
require 'faraday/adapter/test'
|
data/lib/faraday/connection.rb
CHANGED
@@ -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.
|
@@ -66,7 +64,7 @@ module Faraday
|
|
66
64
|
options = ConnectionOptions.from(options)
|
67
65
|
|
68
66
|
if url.is_a?(Hash) || url.is_a?(ConnectionOptions)
|
69
|
-
options =
|
67
|
+
options = Utils.deep_merge(options, url)
|
70
68
|
url = options.url
|
71
69
|
end
|
72
70
|
|
@@ -119,7 +117,7 @@ module Faraday
|
|
119
117
|
|
120
118
|
extend Forwardable
|
121
119
|
|
122
|
-
def_delegators :builder, :
|
120
|
+
def_delegators :builder, :use, :request, :response, :adapter, :app
|
123
121
|
|
124
122
|
# Closes the underlying resources and/or connections. In the case of
|
125
123
|
# persistent connections, this closes all currently open connections
|
@@ -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::
|
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
|
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
|
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 =
|
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
|
-
|
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
@@ -6,7 +6,7 @@ module Faraday
|
|
6
6
|
class Error < StandardError
|
7
7
|
attr_reader :response, :wrapped_exception
|
8
8
|
|
9
|
-
def initialize(exc, response = nil)
|
9
|
+
def initialize(exc = nil, response = nil)
|
10
10
|
@wrapped_exception = nil unless defined?(@wrapped_exception)
|
11
11
|
@response = nil unless defined?(@response)
|
12
12
|
super(exc_msg_and_response!(exc, response))
|
@@ -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.
|
@@ -140,7 +141,7 @@ module Faraday
|
|
140
141
|
class SSLError < Error
|
141
142
|
end
|
142
143
|
|
143
|
-
# Raised by
|
144
|
+
# Raised by middlewares that parse the response, like the JSON response middleware.
|
144
145
|
class ParsingError < Error
|
145
146
|
end
|
146
147
|
end
|
data/lib/faraday/middleware.rb
CHANGED
@@ -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
|
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
|
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
|
-
|
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
|
-
|
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
|
48
|
+
# class Whatever < Middleware
|
49
|
+
# register_middleware(foo: Whatever)
|
83
50
|
# end
|
84
51
|
# end
|
85
52
|
#
|
86
|
-
# Faraday::
|
87
|
-
# # => Faraday::Whatever
|
88
|
-
#
|
53
|
+
# Faraday::Middleware.lookup_middleware(:foo)
|
54
|
+
# # => Faraday::Whatever
|
89
55
|
def lookup_middleware(key)
|
90
|
-
|
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
|
data/lib/faraday/options.rb
CHANGED
@@ -104,7 +104,7 @@ module Faraday
|
|
104
104
|
|
105
105
|
# Public
|
106
106
|
def each_key(&block)
|
107
|
-
return to_enum(:each_key) unless
|
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
|
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
|
171
|
+
unless block
|
172
172
|
raise ArgumentError, '#memoized must be called with a block'
|
173
173
|
end
|
174
174
|
|