faraday 2.0.0.alpha.pre.4 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f77dd46610919ce71119fc9b51f01a0b8d2e5294c279ca4849088d642c72e1d8
4
- data.tar.gz: 8f6f37724b2f1d730b11f3c9bf2e3440e952e682c7728f9b5adef86a917cd1dc
3
+ metadata.gz: eaa5f5a3ea91b21a40ad7872a8eda558226e1d5f4d8f9875ecf9ece44e0181e4
4
+ data.tar.gz: 51af319bffd649b4b828cfed91b2a419211b1c2b171a3016949388b1a749c1ac
5
5
  SHA512:
6
- metadata.gz: 95ed69ec1d3ba8582309cf314c65f3718ee3feff7d15f7e7b75593e23c992c30df5243547618be0ff1bc17da1a346feb72ae5ce7460f10ec086061426270476d
7
- data.tar.gz: 8d511875d592606eaab5aef9de57c86f083136ab022cb7cc6db56e4676ea9c6fde624378bca18db294f9dd6a0025fd96228baf5c92d93c539fbd7b6a5d2978d9
6
+ metadata.gz: b68ff300b45dbe3a7f85ab40f7a23c86b2c373a1672841a96230179f415da3f3bcf2123251235c4399575826043f7221fe81615171fbfd3af4d33a21410f8670
7
+ data.tar.gz: 41cd332afaf70ea6e306247b3f2ea9d9e38ed248e381aef798f914773b1dd7c7473ab9a6fcb5e03cb8e7163c0f81f26e813877bb5f7412964e55ddb9866ffe20
data/CHANGELOG.md CHANGED
@@ -1,5 +1,79 @@
1
1
  # Faraday Changelog
2
2
 
3
+ ## The changelog has moved!
4
+
5
+ This file is not being updated anymore. Instead, please check the [Releases](https://github.com/lostisland/faraday/releases) page.
6
+
7
+ ## [2.1.0](https://github.com/lostisland/faraday/compare/v2.0.1...v2.1.0) (2022-01-15)
8
+
9
+ * Fix test adapter thread safety by @iMacTia in [#1380](https://github.com/lostisland/faraday/pull/1380)
10
+ * Add default adapter options by @hirasawayuki in [#1382](https://github.com/lostisland/faraday/pull/1382)
11
+ * CI: Add Ruby 3.1 to matrix by @petergoldstein in [#1374](https://github.com/lostisland/faraday/pull/1374)
12
+ * docs: fix regex pattern in logger.md examples by @hirasawayuki in [#1378](https://github.com/lostisland/faraday/pull/1378)
13
+
14
+ ## [2.0.1](https://github.com/lostisland/faraday/compare/v2.0.0...v2.0.1) (2022-01-05)
15
+
16
+ * Re-add `faraday-net_http` as default adapter by @iMacTia in [#1366](https://github.com/lostisland/faraday/pull/1366)
17
+ * Updated sample format in UPGRADING.md by @vimutter in [#1361](https://github.com/lostisland/faraday/pull/1361)
18
+ * docs: Make UPGRADING examples more copyable by @olleolleolle in [#1363](https://github.com/lostisland/faraday/pull/1363)
19
+
20
+ ## [2.0.0](https://github.com/lostisland/faraday/compare/v1.8.0...v2.0.0) (2022-01-04)
21
+
22
+ The next major release is here, and it comes almost 2 years after the release of v1.0!
23
+
24
+ This release changes the way you use Faraday and embraces a new paradigm of Faraday as an ecosystem, rather than a library.
25
+
26
+ What does that mean? It means that Faraday is less of a bundled tool and more of a framework for the community to build on top of.
27
+
28
+ As a result, all adapters and some middleware have moved out and are now shipped as standalone gems 🙌!
29
+
30
+ But this doesn't mean that upgrading from Faraday 1.x to Faraday 2.0 should be hard, in fact we've listed everything you need to do in the [UPGRADING.md](https://github.com/lostisland/faraday/blob/main/UPGRADING.md) doc.
31
+
32
+ Moreover, we've setup a new [awesome-faraday](https://github.com/lostisland/awesome-faraday) repository that will showcase a curated list of adapters and middleware 😎.
33
+
34
+ This release was the result of the efforts of the core team and all the contributors, new and old, that have helped achieve this milestone 👏.
35
+
36
+ ## What's Changed
37
+
38
+ * Autoloading, dependency loading and middleware registry cleanup by @iMacTia in [#1301](https://github.com/lostisland/faraday/pull/1301)
39
+ * Move JSON middleware (request and response) from faraday_middleware by @iMacTia in [#1300](https://github.com/lostisland/faraday/pull/1300)
40
+ * Remove deprecated `Faraday::Request#method` by @olleolleolle in [#1303](https://github.com/lostisland/faraday/pull/1303)
41
+ * Remove deprecated `Faraday::UploadIO` by @iMacTia in [#1307](https://github.com/lostisland/faraday/pull/1307)
42
+ * [1.x] Deprecate Authorization helpers in `Faraday::Connection` by @iMacTia in [#1306](https://github.com/lostisland/faraday/pull/1306)
43
+ * Drop deprecated auth helpers from Connection and refactor auth middleware by @iMacTia in [#1308](https://github.com/lostisland/faraday/pull/1308)
44
+ * Add Faraday 1.x examples in authentication.md docs by @iMacTia in [#1320](https://github.com/lostisland/faraday/pull/1320)
45
+ * Fix passing a URL with embedded basic auth by @iMacTia in [#1324](https://github.com/lostisland/faraday/pull/1324)
46
+ * Register JSON middleware by @mollerhoj in [#1331](https://github.com/lostisland/faraday/pull/1331)
47
+ * Retry middleware should handle string exception class name consistently by @jrochkind in [#1334](https://github.com/lostisland/faraday/pull/1334)
48
+ * Improve request info in exceptions raised by RaiseError Middleware by @willianzocolau in [#1335](https://github.com/lostisland/faraday/pull/1335)
49
+ * Remove net-http adapter and update docs by @iMacTia in [#1336](https://github.com/lostisland/faraday/pull/1336)
50
+ * Explain plan for faraday_middleware in UPGRADING.md by @iMacTia in [#1339](https://github.com/lostisland/faraday/pull/1339)
51
+ * Scripts folder cleanup by @iMacTia in [#1340](https://github.com/lostisland/faraday/pull/1340)
52
+ * Replace `Hash#merge` with `Utils#deep_merge` for connection options by @xkwd in [#1343](https://github.com/lostisland/faraday/pull/1343)
53
+ * Callable authorizers by @sled in [#1345](https://github.com/lostisland/faraday/pull/1345)
54
+ * Default value for exc error by @DariuszMusielak in [#1351](https://github.com/lostisland/faraday/pull/1351)
55
+ * Don't call `retry_block` unless a retry is going to happen by @jrochkind in [#1350](https://github.com/lostisland/faraday/pull/1350)
56
+ * Improve documentation for v2 by @iMacTia in [#1353](https://github.com/lostisland/faraday/pull/1353)
57
+ * Remove default `default_adapter` (yes, you read that right) by @iMacTia in [#1354](https://github.com/lostisland/faraday/pull/1354)
58
+ * Remove retry middleware by @iMacTia in [#1356](https://github.com/lostisland/faraday/pull/1356)
59
+ * Remove multipart middleware and all its documentation and tests by @iMacTia in [#1357](https://github.com/lostisland/faraday/pull/1357)
60
+
61
+ ## [1.9.3](https://github.com/lostisland/faraday/compare/v1.9.2...v1.9.3) (2022-01-06)
62
+
63
+ * Re-add support for Ruby 2.4+ by @iMacTia in [#1371](https://github.com/lostisland/faraday/pull/1371)
64
+
65
+ ## [1.9.2](https://github.com/lostisland/faraday/compare/v1.9.1...v1.9.2) (2022-01-06)
66
+
67
+ * Add alias with legacy name to gemified middleware by @iMacTia in [#1372](https://github.com/lostisland/faraday/pull/1372)
68
+
69
+ ## [1.9.1](https://github.com/lostisland/faraday/compare/v1.9.0...v1.9.1) (2022-01-06)
70
+
71
+ * Update adapter dependencies in Gemspec by @iMacTia in [#1370](https://github.com/lostisland/faraday/pull/1370)
72
+
73
+ ## [1.9.0](https://github.com/lostisland/faraday/compare/v1.8.0...v1.9.0) (2022-01-06)
74
+
75
+ * Use external multipart and retry middleware by @iMacTia in [#1367](https://github.com/lostisland/faraday/pull/1367)
76
+
3
77
  ## [1.8.0](https://github.com/lostisland/faraday/releases/tag/v1.8.0) (2021-09-18)
4
78
 
5
79
  ### Features
@@ -65,7 +139,7 @@
65
139
 
66
140
  ### Highlights
67
141
 
68
- With this release, we continue the work of gradually moving out adapters into their own gems 🎉
142
+ With this release, we continue the work of gradually moving out adapters into their own gems 🎉
69
143
  Thanks to @MikeRogers0 for helping the Faraday team in progressing with this quest 👏
70
144
 
71
145
  And thanks to @olleolleolle efforts, Faraday is becoming more inclusive than ever 🤗
@@ -112,7 +186,7 @@ Checkout the "Misc" section below for more details 🙌 !
112
186
  ## [v1.3.0](https://github.com/lostisland/faraday/releases/tag/v1.3.0) (2020-12-31)
113
187
 
114
188
  ### Highlights
115
- Faraday v1.3.0 is the first release to officially support Ruby 3.0 in the CI pipeline 🎉 🍾!
189
+ Faraday v1.3.0 is the first release to officially support Ruby 3.0 in the CI pipeline 🎉 🍾!
116
190
 
117
191
  This is also the first release with a previously "included" adapter (Net::HTTP) being isolated into a [separate gem](https://github.com/lostisland/faraday-net_http) 🎊!
118
192
  The new adapter is added to Faraday as a dependency for now, so that means full backwards-compatibility, but just to be safe be careful when upgrading!
@@ -203,7 +277,7 @@ Many thanks to the Faraday Team, @JanDintel and everyone who attended the [ROSS
203
277
  * Allows `parse` method to be private/protected in response middleware (#1123)
204
278
  * Encode Spaces in Query Strings as '%20' Instead of '+' (#1125)
205
279
  * Limits rack to v2.0.x (#1127)
206
- * Adapter Registry reads also use mutex (#1136)
280
+ * Adapter Registry reads also use mutex (#1136)
207
281
 
208
282
  ### Documentation
209
283
 
data/README.md CHANGED
@@ -10,11 +10,6 @@ adapters (such as Net::HTTP) and embraces the concept of Rack middleware when pr
10
10
  You probably don't want to use Faraday directly in your project, as it will lack an actual client library to perform
11
11
  requests. Instead, you probably want to have a look at [Awesome Faraday][awesome] for a list of available adapters.
12
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).
17
-
18
13
  ## Getting Started
19
14
 
20
15
  The best starting point is the [Faraday Website][website], with its introduction and explanation.
@@ -12,10 +12,10 @@ class Client
12
12
  @conn = conn
13
13
  end
14
14
 
15
- def sushi(jname, params: {})
15
+ def httpbingo(jname, params: {})
16
16
  res = @conn.get("/#{jname}", params)
17
17
  data = JSON.parse(res.body)
18
- data['name']
18
+ data['origin']
19
19
  end
20
20
  end
21
21
 
@@ -24,42 +24,42 @@ RSpec.describe Client do
24
24
  let(:conn) { Faraday.new { |b| b.adapter(:test, stubs) } }
25
25
  let(:client) { Client.new(conn) }
26
26
 
27
- it 'parses name' do
28
- stubs.get('/ebi') do |env|
27
+ it 'parses origin' do
28
+ stubs.get('/ip') do |env|
29
29
  # optional: you can inspect the Faraday::Env
30
- expect(env.url.path).to eq('/ebi')
30
+ expect(env.url.path).to eq('/ip')
31
31
  [
32
32
  200,
33
33
  { 'Content-Type': 'application/javascript' },
34
- '{"name": "shrimp"}'
34
+ '{"origin": "127.0.0.1"}'
35
35
  ]
36
36
  end
37
37
 
38
38
  # uncomment to trigger stubs.verify_stubbed_calls failure
39
39
  # stubs.get('/unused') { [404, {}, ''] }
40
40
 
41
- expect(client.sushi('ebi')).to eq('shrimp')
41
+ expect(client.httpbingo('ip')).to eq('127.0.0.1')
42
42
  stubs.verify_stubbed_calls
43
43
  end
44
44
 
45
45
  it 'handles 404' do
46
- stubs.get('/ebi') do
46
+ stubs.get('/api') do
47
47
  [
48
48
  404,
49
49
  { 'Content-Type': 'application/javascript' },
50
50
  '{}'
51
51
  ]
52
52
  end
53
- expect(client.sushi('ebi')).to be_nil
53
+ expect(client.httpbingo('api')).to be_nil
54
54
  stubs.verify_stubbed_calls
55
55
  end
56
56
 
57
57
  it 'handles exception' do
58
- stubs.get('/ebi') do
59
- raise Faraday::ConnectionFailed, nil
58
+ stubs.get('/api') do
59
+ raise Faraday::ConnectionFailed
60
60
  end
61
61
 
62
- expect { client.sushi('ebi') }.to raise_error(Faraday::ConnectionFailed)
62
+ expect { client.httpbingo('api') }.to raise_error(Faraday::ConnectionFailed)
63
63
  stubs.verify_stubbed_calls
64
64
  end
65
65
 
@@ -67,17 +67,17 @@ RSpec.describe Client do
67
67
  let(:stubs) { Faraday::Adapter::Test::Stubs.new(strict_mode: true) }
68
68
 
69
69
  it 'verifies the all parameter values are identical' do
70
- stubs.get('/ebi?abc=123') do
70
+ stubs.get('/api?abc=123') do
71
71
  [
72
72
  200,
73
73
  { 'Content-Type': 'application/javascript' },
74
- '{"name": "shrimp"}'
74
+ '{"origin": "127.0.0.1"}'
75
75
  ]
76
76
  end
77
77
 
78
78
  # uncomment to raise Stubs::NotFound
79
- # expect(client.sushi('ebi', params: { abc: 123, foo: 'Kappa' })).to eq('shrimp')
80
- expect(client.sushi('ebi', params: { abc: 123 })).to eq('shrimp')
79
+ # expect(client.httpbingo('api', params: { abc: 123, foo: 'Kappa' })).to eq('127.0.0.1')
80
+ expect(client.httpbingo('api', params: { abc: 123 })).to eq('127.0.0.1')
81
81
  stubs.verify_stubbed_calls
82
82
  end
83
83
  end
@@ -86,11 +86,11 @@ RSpec.describe Client do
86
86
  let(:conn) { Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) { |b| b.adapter(:test, stubs) } }
87
87
 
88
88
  it 'handles the same multiple URL parameters' do
89
- stubs.get('/ebi?a=x&a=y&a=z') { [200, { 'Content-Type' => 'application/json' }, '{"name": "shrimp"}'] }
89
+ stubs.get('/api?a=x&a=y&a=z') { [200, { 'Content-Type' => 'application/json' }, '{"origin": "127.0.0.1"}'] }
90
90
 
91
91
  # uncomment to raise Stubs::NotFound
92
- # expect(client.sushi('ebi', params: { a: %w[x y] })).to eq('shrimp')
93
- expect(client.sushi('ebi', params: { a: %w[x y z] })).to eq('shrimp')
92
+ # expect(client.httpbingo('api', params: { a: %w[x y] })).to eq('127.0.0.1')
93
+ expect(client.httpbingo('api', params: { a: %w[x y z] })).to eq('127.0.0.1')
94
94
  stubs.verify_stubbed_calls
95
95
  end
96
96
  end
@@ -13,24 +13,24 @@ class Client
13
13
  @conn = conn
14
14
  end
15
15
 
16
- def sushi(jname, params: {})
16
+ def httpbingo(jname, params: {})
17
17
  res = @conn.get("/#{jname}", params)
18
18
  data = JSON.parse(res.body)
19
- data['name']
19
+ data['origin']
20
20
  end
21
21
  end
22
22
 
23
23
  # Example API client test
24
24
  class ClientTest < Test::Unit::TestCase
25
- def test_sushi_name
25
+ def test_httpbingo_name
26
26
  stubs = Faraday::Adapter::Test::Stubs.new
27
- stubs.get('/ebi') do |env|
27
+ stubs.get('/api') do |env|
28
28
  # optional: you can inspect the Faraday::Env
29
- assert_equal '/ebi', env.url.path
29
+ assert_equal '/api', env.url.path
30
30
  [
31
31
  200,
32
32
  { 'Content-Type': 'application/javascript' },
33
- '{"name": "shrimp"}'
33
+ '{"origin": "127.0.0.1"}'
34
34
  ]
35
35
  end
36
36
 
@@ -38,13 +38,13 @@ class ClientTest < Test::Unit::TestCase
38
38
  # stubs.get('/unused') { [404, {}, ''] }
39
39
 
40
40
  cli = client(stubs)
41
- assert_equal 'shrimp', cli.sushi('ebi')
41
+ assert_equal '127.0.0.1', cli.httpbingo('api')
42
42
  stubs.verify_stubbed_calls
43
43
  end
44
44
 
45
- def test_sushi_not_found
45
+ def test_httpbingo_not_found
46
46
  stubs = Faraday::Adapter::Test::Stubs.new
47
- stubs.get('/ebi') do
47
+ stubs.get('/api') do
48
48
  [
49
49
  404,
50
50
  { 'Content-Type': 'application/javascript' },
@@ -53,48 +53,48 @@ class ClientTest < Test::Unit::TestCase
53
53
  end
54
54
 
55
55
  cli = client(stubs)
56
- assert_nil cli.sushi('ebi')
56
+ assert_nil cli.httpbingo('api')
57
57
  stubs.verify_stubbed_calls
58
58
  end
59
59
 
60
- def test_sushi_exception
60
+ def test_httpbingo_exception
61
61
  stubs = Faraday::Adapter::Test::Stubs.new
62
- stubs.get('/ebi') do
63
- raise Faraday::ConnectionFailed, nil
62
+ stubs.get('/api') do
63
+ raise Faraday::ConnectionFailed
64
64
  end
65
65
 
66
66
  cli = client(stubs)
67
67
  assert_raise Faraday::ConnectionFailed do
68
- cli.sushi('ebi')
68
+ cli.httpbingo('api')
69
69
  end
70
70
  stubs.verify_stubbed_calls
71
71
  end
72
72
 
73
73
  def test_strict_mode
74
74
  stubs = Faraday::Adapter::Test::Stubs.new(strict_mode: true)
75
- stubs.get('/ebi?abc=123') do
75
+ stubs.get('/api?abc=123') do
76
76
  [
77
77
  200,
78
78
  { 'Content-Type': 'application/javascript' },
79
- '{"name": "shrimp"}'
79
+ '{"origin": "127.0.0.1"}'
80
80
  ]
81
81
  end
82
82
 
83
83
  cli = client(stubs)
84
- assert_equal 'shrimp', cli.sushi('ebi', params: { abc: 123 })
84
+ assert_equal '127.0.0.1', cli.httpbingo('api', params: { abc: 123 })
85
85
 
86
86
  # uncomment to raise Stubs::NotFound
87
- # assert_equal 'shrimp', cli.sushi('ebi', params: { abc: 123, foo: 'Kappa' })
87
+ # assert_equal '127.0.0.1', cli.httpbingo('api', params: { abc: 123, foo: 'Kappa' })
88
88
  stubs.verify_stubbed_calls
89
89
  end
90
90
 
91
91
  def test_non_default_params_encoder
92
92
  stubs = Faraday::Adapter::Test::Stubs.new(strict_mode: true)
93
- stubs.get('/ebi?a=x&a=y&a=z') do
93
+ stubs.get('/api?a=x&a=y&a=z') do
94
94
  [
95
95
  200,
96
96
  { 'Content-Type': 'application/javascript' },
97
- '{"name": "shrimp"}'
97
+ '{"origin": "127.0.0.1"}'
98
98
  ]
99
99
  end
100
100
  conn = Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) do |builder|
@@ -102,10 +102,10 @@ class ClientTest < Test::Unit::TestCase
102
102
  end
103
103
 
104
104
  cli = Client.new(conn)
105
- assert_equal 'shrimp', cli.sushi('ebi', params: { a: %w[x y z] })
105
+ assert_equal '127.0.0.1', cli.httpbingo('api', params: { a: %w[x y z] })
106
106
 
107
107
  # uncomment to raise Stubs::NotFound
108
- # assert_equal 'shrimp', cli.sushi('ebi', params: { a: %w[x y] })
108
+ # assert_equal '127.0.0.1', cli.httpbingo('api', params: { a: %w[x y] })
109
109
  stubs.verify_stubbed_calls
110
110
  end
111
111
 
@@ -55,6 +55,7 @@ module Faraday
55
55
  @stack = {}
56
56
  @consumed = {}
57
57
  @strict_mode = strict_mode
58
+ @stubs_mutex = Monitor.new
58
59
  yield(self) if block_given?
59
60
  end
60
61
 
@@ -70,10 +71,13 @@ module Faraday
70
71
  stack = @stack[request_method]
71
72
  consumed = (@consumed[request_method] ||= [])
72
73
 
73
- stub, meta = matches?(stack, env)
74
- if stub
75
- consumed << stack.delete(stub)
76
- return stub, meta
74
+ @stubs_mutex.synchronize do
75
+ stub, meta = matches?(stack, env)
76
+ if stub
77
+ removed = stack.delete(stub)
78
+ consumed << removed unless removed.nil?
79
+ return stub, meta
80
+ end
77
81
  end
78
82
  matches?(consumed, env)
79
83
  end
@@ -6,9 +6,9 @@ module Faraday
6
6
  #
7
7
  # @example
8
8
  #
9
- # conn = Faraday::Connection.new 'http://sushi.com'
9
+ # conn = Faraday::Connection.new 'http://httpbingo.org'
10
10
  #
11
- # # GET http://sushi.com/nigiri
11
+ # # GET http://httpbingo.org/nigiri
12
12
  # conn.get 'nigiri'
13
13
  # # => #<Faraday::Response>
14
14
  #
@@ -117,7 +117,7 @@ module Faraday
117
117
 
118
118
  extend Forwardable
119
119
 
120
- def_delegators :builder, :build, :use, :request, :response, :adapter, :app
120
+ def_delegators :builder, :use, :request, :response, :adapter, :app
121
121
 
122
122
  # Closes the underlying resources and/or connections. In the case of
123
123
  # persistent connections, this closes all currently open connections
@@ -349,11 +349,11 @@ module Faraday
349
349
  # @example
350
350
  #
351
351
  # conn = Faraday::Connection.new { ... }
352
- # conn.url_prefix = "https://sushi.com/api"
352
+ # conn.url_prefix = "https://httpbingo.org/api"
353
353
  # conn.scheme # => https
354
354
  # conn.path_prefix # => "/api"
355
355
  #
356
- # conn.get("nigiri?page=2") # accesses https://sushi.com/api/nigiri
356
+ # conn.get("nigiri?page=2") # accesses https://httpbingo.org/api/nigiri
357
357
  def url_prefix=(url, encoder = nil)
358
358
  uri = @url_prefix = Utils.URI(url)
359
359
  self.path_prefix = uri.path
@@ -395,15 +395,15 @@ module Faraday
395
395
  #
396
396
  # @example
397
397
  # conn = Faraday::Connection.new { ... }
398
- # conn.url_prefix = "https://sushi.com/api?token=abc"
398
+ # conn.url_prefix = "https://httpbingo.org/api?token=abc"
399
399
  # conn.scheme # => https
400
400
  # conn.path_prefix # => "/api"
401
401
  #
402
402
  # conn.build_url("nigiri?page=2")
403
- # # => https://sushi.com/api/nigiri?token=abc&page=2
403
+ # # => https://httpbingo.org/api/nigiri?token=abc&page=2
404
404
  #
405
405
  # conn.build_url("nigiri", page: 2)
406
- # # => https://sushi.com/api/nigiri?token=abc&page=2
406
+ # # => https://httpbingo.org/api/nigiri?token=abc&page=2
407
407
  #
408
408
  def build_url(url = nil, extra_params = nil)
409
409
  uri = build_exclusive_url(url)
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))
@@ -141,13 +141,7 @@ module Faraday
141
141
  class SSLError < Error
142
142
  end
143
143
 
144
- # Raised by FaradayMiddleware::ResponseMiddleware
144
+ # Raised by middlewares that parse the response, like the JSON response middleware.
145
145
  class ParsingError < Error
146
146
  end
147
-
148
- # Exception used to control the Retry middleware.
149
- #
150
- # @see Faraday::Request::Retry
151
- class RetriableResponse < Error
152
- end
153
147
  end
@@ -53,13 +53,31 @@ module Faraday
53
53
  # Faraday::Middleware.lookup_middleware(:foo)
54
54
  # # => Faraday::Whatever
55
55
  def lookup_middleware(key)
56
- registered_middleware[key] ||
56
+ load_middleware(key) ||
57
57
  raise(Faraday::Error, "#{key.inspect} is not registered on #{self}")
58
58
  end
59
59
 
60
+ private
61
+
60
62
  def middleware_mutex(&block)
61
63
  @middleware_mutex ||= Monitor.new
62
64
  @middleware_mutex.synchronize(&block)
63
65
  end
66
+
67
+ def load_middleware(key)
68
+ value = registered_middleware[key]
69
+ case value
70
+ when Module
71
+ value
72
+ when Symbol, String
73
+ middleware_mutex do
74
+ @registered_middleware[key] = const_get(value)
75
+ end
76
+ when Proc
77
+ middleware_mutex do
78
+ @registered_middleware[key] = value.call
79
+ end
80
+ end
81
+ end
64
82
  end
65
83
  end
@@ -8,7 +8,7 @@ module Faraday
8
8
  # middleware stack (heavily inspired by Rack).
9
9
  #
10
10
  # @example
11
- # Faraday::Connection.new(url: 'http://sushi.com') do |builder|
11
+ # Faraday::Connection.new(url: 'http://httpbingo.org') do |builder|
12
12
  # builder.request :url_encoded # Faraday::Request::UrlEncoded
13
13
  # builder.adapter :net_http # Faraday::Adapter::NetHttp
14
14
  # end
@@ -58,23 +58,22 @@ module Faraday
58
58
  end
59
59
  end
60
60
 
61
- def initialize(handlers = [], adapter = nil, &block)
62
- @adapter = adapter
63
- @handlers = handlers
64
- if block
65
- build(&block)
66
- elsif @handlers.empty?
67
- # default stack, if nothing else is configured
68
- request :url_encoded
69
- self.adapter Faraday.default_adapter
70
- end
61
+ def initialize(&block)
62
+ @adapter = nil
63
+ @handlers = []
64
+ build(&block)
65
+ end
66
+
67
+ def initialize_dup(original)
68
+ super
69
+ @adapter = original.adapter
70
+ @handlers = original.handlers.dup
71
71
  end
72
72
 
73
- def build(options = {})
73
+ def build
74
74
  raise_if_locked
75
- @handlers.clear unless options[:keep]
76
- yield(self) if block_given?
77
- adapter(Faraday.default_adapter) unless @adapter
75
+ block_given? ? yield(self) : request(:url_encoded)
76
+ adapter(Faraday.default_adapter, **Faraday.default_adapter_options) unless @adapter
78
77
  end
79
78
 
80
79
  def [](idx)
@@ -109,7 +108,7 @@ module Faraday
109
108
  end
110
109
 
111
110
  ruby2_keywords def adapter(klass = NO_ARGUMENT, *args, &block)
112
- return @adapter if klass == NO_ARGUMENT
111
+ return @adapter if klass == NO_ARGUMENT || klass.nil?
113
112
 
114
113
  klass = Faraday::Adapter.lookup_middleware(klass) if klass.is_a?(Symbol)
115
114
  @adapter = self.class::Handler.new(klass, *args, &block)
@@ -164,6 +163,7 @@ module Faraday
164
163
  def app
165
164
  @app ||= begin
166
165
  lock!
166
+ ensure_adapter!
167
167
  to_app
168
168
  end
169
169
  end
@@ -182,10 +182,6 @@ module Faraday
182
182
  @adapter == other.adapter
183
183
  end
184
184
 
185
- def dup
186
- self.class.new(@handlers.dup, @adapter.dup)
187
- end
188
-
189
185
  # ENV Keys
190
186
  # :http_method - a symbolized request HTTP method (:get, :post)
191
187
  # :body - the request body that will eventually be converted to a string.
@@ -216,6 +212,9 @@ module Faraday
216
212
  private
217
213
 
218
214
  LOCK_ERR = "can't modify middleware stack after making a request"
215
+ MISSING_ADAPTER_ERROR = "An attempt to run a request with a Faraday::Connection without adapter has been made.\n" \
216
+ "Please set Faraday.default_adapter or provide one when initializing the connection.\n" \
217
+ 'For more info, check https://lostisland.github.io/faraday/usage/.'
219
218
 
220
219
  def raise_if_locked
221
220
  raise StackLocked, LOCK_ERR if locked?
@@ -227,6 +226,10 @@ module Faraday
227
226
  raise 'Adapter should be set using the `adapter` method, not `use`'
228
227
  end
229
228
 
229
+ def ensure_adapter!
230
+ raise MISSING_ADAPTER_ERROR unless @adapter
231
+ end
232
+
230
233
  def adapter_set?
231
234
  !@adapter.nil?
232
235
  end
@@ -133,6 +133,4 @@ end
133
133
  require 'faraday/request/authorization'
134
134
  require 'faraday/request/instrumentation'
135
135
  require 'faraday/request/json'
136
- require 'faraday/request/multipart'
137
- require 'faraday/request/retry'
138
136
  require 'faraday/request/url_encoded'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Faraday
4
- VERSION = '2.0.0.alpha-4'
4
+ VERSION = '2.2.0'
5
5
  end
data/lib/faraday.rb CHANGED
@@ -17,9 +17,7 @@ require 'faraday/middleware'
17
17
  require 'faraday/adapter'
18
18
  require 'faraday/request'
19
19
  require 'faraday/response'
20
- require 'faraday/file_part'
21
- require 'faraday/param_part'
22
-
20
+ require 'faraday/net_http'
23
21
  # This is the main namespace for Faraday.
24
22
  #
25
23
  # It provides methods to create {Connection} objects, and HTTP-related
@@ -56,6 +54,10 @@ module Faraday
56
54
  # @return [Symbol] the new default_adapter.
57
55
  attr_reader :default_adapter
58
56
 
57
+ # Option for the default_adapter
58
+ # @return [Hash] default_adapter options
59
+ attr_accessor :default_adapter_options
60
+
59
61
  # Documented below, see default_connection
60
62
  attr_writer :default_connection
61
63
 
@@ -150,5 +152,6 @@ module Faraday
150
152
  self.ignore_env_proxy = false
151
153
  self.root_path = File.expand_path __dir__
152
154
  self.lib_path = File.expand_path 'faraday', __dir__
153
- self.default_adapter = :test
155
+ self.default_adapter = :net_http
156
+ self.default_adapter_options = {}
154
157
  end