faraday 2.0.0.alpha.pre.1 → 2.0.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: 07fafa8818a9063a9df90d878f7c6b79b0266f330fab9a9a271ac7b3d16e9bd8
4
- data.tar.gz: ef16b014bc9ca58649557382c31817fba19c046029bb0c58c02a4b1a4654561e
3
+ metadata.gz: 367d509be94da297eac6ece76f5561772218a5982696eba047ef8599d0ff05e1
4
+ data.tar.gz: 779c66227b778b17661428c17ab65567e23f13ea2824b92251eba67cd32c6280
5
5
  SHA512:
6
- metadata.gz: ec0843f0b2e4f37732bc6acd0fd2fb5417ce965c597e3385207ae2893bbe359cf037121712d0cbd7ce69371629bef9587b00b33036ae5bd087a23f1f5112cd96
7
- data.tar.gz: 933b84778e644221a00d472ae0e9478672fcc4946a512eb2e03902cd8c082f56c2064fc37fb05ea4e3d904a9e2c7bb192c32b0ff22ce8b790f7b5edf08c490f1
6
+ metadata.gz: 73d405ce87ac4a0917806b16ab11693b68966334656689a95bc06764501c44965832b08fa7bf36beeb71c2144a6661775df4f22b18e8281f664a0605fc2ece49
7
+ data.tar.gz: 22701db3eb1bb1668aaf6429e711acad05099577b39ccc792d32c42a82043f628483973a993c5ce91acd4207a9f0a01f47a51d6bffc4212047e4150fbb63e1e1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
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
+
3
9
  ## [v1.7.0](https://github.com/lostisland/faraday/releases/tag/v1.7.0) (2021-08-09)
4
10
 
5
11
  ### Features
@@ -56,7 +56,7 @@ RSpec.describe Client do
56
56
 
57
57
  it 'handles exception' do
58
58
  stubs.get('/ebi') do
59
- raise Faraday::ConnectionFailed, nil
59
+ raise Faraday::ConnectionFailed
60
60
  end
61
61
 
62
62
  expect { client.sushi('ebi') }.to raise_error(Faraday::ConnectionFailed)
@@ -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, nil
63
+ raise Faraday::ConnectionFailed
64
64
  end
65
65
 
66
66
  cli = client(stubs)
@@ -98,3 +98,5 @@ module Faraday
98
98
  }.freeze
99
99
  end
100
100
  end
101
+
102
+ require 'faraday/adapter/test'
@@ -64,7 +64,7 @@ module Faraday
64
64
  options = ConnectionOptions.from(options)
65
65
 
66
66
  if url.is_a?(Hash) || url.is_a?(ConnectionOptions)
67
- options = options.merge(url)
67
+ options = Utils.deep_merge(options, url)
68
68
  url = options.url
69
69
  end
70
70
 
@@ -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
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
@@ -58,22 +58,21 @@ 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?
75
+ block_given? ? yield(self) : request(:url_encoded)
77
76
  adapter(Faraday.default_adapter) unless @adapter
78
77
  end
79
78
 
@@ -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
@@ -8,10 +8,11 @@ module Faraday
8
8
 
9
9
  # @param app [#call]
10
10
  # @param type [String, Symbol] Type of Authorization
11
- # @param params [Array<String, Proc>] parameters to build the Authorization header.
11
+ # @param params [Array<String, Proc, #call>] parameters to build the Authorization header.
12
12
  # If the type is `:basic`, then these can be a login and password pair.
13
13
  # Otherwise, a single value is expected that will be appended after the type.
14
- # This value can be a proc, in which case it will be invoked on each request.
14
+ # This value can be a proc or an object responding to `.call`, in which case
15
+ # it will be invoked on each request.
15
16
  def initialize(app, type, *params)
16
17
  @type = type
17
18
  @params = params
@@ -37,7 +38,7 @@ module Faraday
37
38
  raise ArgumentError, "Unexpected params received (got #{params.size} instead of 1)"
38
39
  else
39
40
  value = params.first
40
- value = value.call if value.is_a?(Proc)
41
+ value = value.call if value.is_a?(Proc) || value.respond_to?(:call)
41
42
  "#{type} #{value}"
42
43
  end
43
44
  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'
data/lib/faraday/utils.rb CHANGED
@@ -101,7 +101,7 @@ module Faraday
101
101
  # Recursive hash update
102
102
  def deep_merge!(target, hash)
103
103
  hash.each do |key, value|
104
- target[key] = if value.is_a?(Hash) && target[key].is_a?(Hash)
104
+ target[key] = if value.is_a?(Hash) && (target[key].is_a?(Hash) || target[key].is_a?(Options))
105
105
  deep_merge(target[key], value)
106
106
  else
107
107
  value
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Faraday
4
- VERSION = '2.0.0.alpha-1'
4
+ VERSION = '2.0.0'
5
5
  end
data/lib/faraday.rb CHANGED
@@ -17,8 +17,6 @@ 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
 
23
21
  # This is the main namespace for Faraday.
24
22
  #
@@ -92,7 +90,7 @@ module Faraday
92
90
  # params: { page: 1 }
93
91
  # # => Faraday::Connection to http://faraday.com?page=1
94
92
  def new(url = nil, options = {}, &block)
95
- options = default_connection_options.merge(options)
93
+ options = Utils.deep_merge(default_connection_options, options)
96
94
  Faraday::Connection.new(url, options, &block)
97
95
  end
98
96
 
@@ -150,5 +148,4 @@ module Faraday
150
148
  self.ignore_env_proxy = false
151
149
  self.root_path = File.expand_path __dir__
152
150
  self.lib_path = File.expand_path 'faraday', __dir__
153
- self.default_adapter = :test
154
151
  end
@@ -151,6 +151,9 @@ RSpec.describe Faraday::Connection do
151
151
  end
152
152
 
153
153
  describe '#close' do
154
+ before { Faraday.default_adapter = :test }
155
+ after { Faraday.default_adapter = nil }
156
+
154
157
  it 'can close underlying app' do
155
158
  expect(conn.app).to receive(:close)
156
159
  conn.close
@@ -639,6 +642,27 @@ RSpec.describe Faraday::Connection do
639
642
 
640
643
  it_behaves_like 'default connection options'
641
644
  end
645
+
646
+ context 'preserving a user_agent assigned via default_conncetion_options' do
647
+ around do |example|
648
+ old = Faraday.default_connection_options
649
+ Faraday.default_connection_options = { headers: { user_agent: 'My Agent 1.2' } }
650
+ example.run
651
+ Faraday.default_connection_options = old
652
+ end
653
+
654
+ context 'when url is a Hash' do
655
+ let(:conn) { Faraday.new(url: 'http://example.co', headers: { 'CustomHeader' => 'CustomValue' }) }
656
+
657
+ it { expect(conn.headers).to eq('CustomHeader' => 'CustomValue', 'User-Agent' => 'My Agent 1.2') }
658
+ end
659
+
660
+ context 'when url is a String' do
661
+ let(:conn) { Faraday.new('http://example.co', headers: { 'CustomHeader' => 'CustomValue' }) }
662
+
663
+ it { expect(conn.headers).to eq('CustomHeader' => 'CustomValue', 'User-Agent' => 'My Agent 1.2') }
664
+ end
665
+ end
642
666
  end
643
667
 
644
668
  describe 'request params' do
@@ -20,6 +20,8 @@ RSpec.describe Faraday::RackBuilder do
20
20
  end
21
21
 
22
22
  subject { conn.builder }
23
+ before { Faraday.default_adapter = :test }
24
+ after { Faraday.default_adapter = nil }
23
25
 
24
26
  context 'with default stack' do
25
27
  let(:conn) { Faraday::Connection.new }
@@ -86,13 +88,6 @@ RSpec.describe Faraday::RackBuilder do
86
88
 
87
89
  it { expect(subject.handlers).to eq([Apple]) }
88
90
 
89
- it 'allows rebuilding' do
90
- subject.build do |builder|
91
- builder.use(Orange)
92
- end
93
- expect(subject.handlers).to eq([Orange])
94
- end
95
-
96
91
  it 'allows use' do
97
92
  subject.use(Orange)
98
93
  expect(subject.handlers).to eq([Apple, Orange])
@@ -63,6 +63,15 @@ RSpec.describe Faraday::Request::Authorization do
63
63
  include_examples 'does not interfere with existing authentication'
64
64
  end
65
65
 
66
+ context 'when passed a callable' do
67
+ let(:callable) { double('Callable Authorizer', call: 'custom_from_callable') }
68
+ let(:auth_config) { [callable] }
69
+
70
+ it { expect(response.body).to eq('Bearer custom_from_callable') }
71
+
72
+ include_examples 'does not interfere with existing authentication'
73
+ end
74
+
66
75
  context 'when passed too many arguments' do
67
76
  let(:auth_config) { %w[baz foo] }
68
77
 
@@ -3,7 +3,6 @@
3
3
  RSpec.describe Faraday::Request::UrlEncoded do
4
4
  let(:conn) do
5
5
  Faraday.new do |b|
6
- b.request :multipart
7
6
  b.request :url_encoded
8
7
  b.adapter :test do |stub|
9
8
  stub.post('/echo') do |env|
@@ -53,4 +53,65 @@ RSpec.describe Faraday::Utils do
53
53
  expect(headers).not_to have_key('authorization')
54
54
  end
55
55
  end
56
+
57
+ describe '.deep_merge!' do
58
+ let(:connection_options) { Faraday::ConnectionOptions.new }
59
+ let(:url) do
60
+ {
61
+ url: 'http://example.com/abc',
62
+ headers: { 'Mime-Version' => '1.0' },
63
+ request: { oauth: { consumer_key: 'anonymous' } },
64
+ ssl: { version: '2' }
65
+ }
66
+ end
67
+
68
+ it 'recursively merges the headers' do
69
+ connection_options.headers = { user_agent: 'My Agent 1.0' }
70
+ deep_merge = Faraday::Utils.deep_merge!(connection_options, url)
71
+
72
+ expect(deep_merge.headers).to eq('Mime-Version' => '1.0', user_agent: 'My Agent 1.0')
73
+ end
74
+
75
+ context 'when a target hash has an Options Struct value' do
76
+ let(:request) do
77
+ {
78
+ params_encoder: nil,
79
+ proxy: nil,
80
+ bind: nil,
81
+ timeout: nil,
82
+ open_timeout: nil,
83
+ read_timeout: nil,
84
+ write_timeout: nil,
85
+ boundary: nil,
86
+ oauth: { consumer_key: 'anonymous' },
87
+ context: nil,
88
+ on_data: nil
89
+ }
90
+ end
91
+ let(:ssl) do
92
+ {
93
+ verify: nil,
94
+ ca_file: nil,
95
+ ca_path: nil,
96
+ verify_mode: nil,
97
+ cert_store: nil,
98
+ client_cert: nil,
99
+ client_key: nil,
100
+ certificate: nil,
101
+ private_key: nil,
102
+ verify_depth: nil,
103
+ version: '2',
104
+ min_version: nil,
105
+ max_version: nil
106
+ }
107
+ end
108
+
109
+ it 'does not overwrite an Options Struct value' do
110
+ deep_merge = Faraday::Utils.deep_merge!(connection_options, url)
111
+
112
+ expect(deep_merge.request.to_h).to eq(request)
113
+ expect(deep_merge.ssl.to_h).to eq(ssl)
114
+ end
115
+ end
116
+ end
56
117
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'multipart_parser/reader'
4
-
5
3
  module Faraday
6
4
  module HelperMethods
7
5
  def self.included(base)
@@ -86,41 +84,6 @@ module Faraday
86
84
  end
87
85
  end
88
86
 
89
- def multipart_file
90
- Faraday::FilePart.new(__FILE__, 'text/x-ruby')
91
- end
92
-
93
- # parse boundary out of a Content-Type header like:
94
- # Content-Type: multipart/form-data; boundary=gc0p4Jq0M2Yt08jU534c0p
95
- def parse_multipart_boundary(ctype)
96
- MultipartParser::Reader.extract_boundary_value(ctype)
97
- end
98
-
99
- # parse a multipart MIME message, returning a hash of any multipart errors
100
- def parse_multipart(boundary, body)
101
- reader = MultipartParser::Reader.new(boundary)
102
- result = { errors: [], parts: [] }
103
- def result.part(name)
104
- hash = self[:parts].detect { |h| h[:part].name == name }
105
- [hash[:part], hash[:body].join]
106
- end
107
-
108
- reader.on_part do |part|
109
- result[:parts] << thispart = {
110
- part: part,
111
- body: []
112
- }
113
- part.on_data do |chunk|
114
- thispart[:body] << chunk
115
- end
116
- end
117
- reader.on_error do |msg|
118
- result[:errors] << msg
119
- end
120
- reader.write(body)
121
- result
122
- end
123
-
124
87
  def method_with_body?(method)
125
88
  self.class.method_with_body?(method)
126
89
  end
@@ -40,7 +40,6 @@ shared_examples 'adapter examples' do |**options|
40
40
  conn_options[:ssl][:ca_file] ||= ENV['SSL_FILE']
41
41
 
42
42
  Faraday.new(remote, conn_options) do |conn|
43
- conn.request :multipart
44
43
  conn.request :url_encoded
45
44
  conn.response :raise_error
46
45
  conn.adapter described_class, *adapter_options
@@ -126,19 +126,6 @@ shared_examples 'a request method' do |http_method|
126
126
  expect { conn.public_send(http_method, '/') }.to raise_error(exc)
127
127
  end
128
128
 
129
- # Can't send files on get, head and delete methods
130
- if method_with_body?(http_method)
131
- it 'sends files' do
132
- payload = { uploaded_file: multipart_file }
133
- request_stub.with(headers: { 'Content-Type' => %r{\Amultipart/form-data} }) do |request|
134
- # WebMock does not support matching body for multipart/form-data requests yet :(
135
- # https://github.com/bblimke/webmock/issues/623
136
- request.body.include?('RubyMultipartPost')
137
- end
138
- conn.public_send(http_method, '/', payload)
139
- end
140
- end
141
-
142
129
  on_feature :reason_phrase_parse do
143
130
  it 'parses the reason phrase' do
144
131
  request_stub.to_return(status: [200, 'OK'])
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.alpha.pre.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "@technoweenie"
@@ -10,28 +10,8 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-10-25 00:00:00.000000000 Z
13
+ date: 2022-01-04 00:00:00.000000000 Z
14
14
  dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: multipart-post
17
- requirement: !ruby/object:Gem::Requirement
18
- requirements:
19
- - - ">="
20
- - !ruby/object:Gem::Version
21
- version: '1.2'
22
- - - "<"
23
- - !ruby/object:Gem::Version
24
- version: '3'
25
- type: :runtime
26
- prerelease: false
27
- version_requirements: !ruby/object:Gem::Requirement
28
- requirements:
29
- - - ">="
30
- - !ruby/object:Gem::Version
31
- version: '1.2'
32
- - - "<"
33
- - !ruby/object:Gem::Version
34
- version: '3'
35
15
  - !ruby/object:Gem::Dependency
36
16
  name: ruby2_keywords
37
17
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +46,6 @@ files:
66
46
  - lib/faraday/encoders/flat_params_encoder.rb
67
47
  - lib/faraday/encoders/nested_params_encoder.rb
68
48
  - lib/faraday/error.rb
69
- - lib/faraday/file_part.rb
70
49
  - lib/faraday/logging/formatter.rb
71
50
  - lib/faraday/methods.rb
72
51
  - lib/faraday/middleware.rb
@@ -77,15 +56,12 @@ files:
77
56
  - lib/faraday/options/proxy_options.rb
78
57
  - lib/faraday/options/request_options.rb
79
58
  - lib/faraday/options/ssl_options.rb
80
- - lib/faraday/param_part.rb
81
59
  - lib/faraday/parameters.rb
82
60
  - lib/faraday/rack_builder.rb
83
61
  - lib/faraday/request.rb
84
62
  - lib/faraday/request/authorization.rb
85
63
  - lib/faraday/request/instrumentation.rb
86
64
  - lib/faraday/request/json.rb
87
- - lib/faraday/request/multipart.rb
88
- - lib/faraday/request/retry.rb
89
65
  - lib/faraday/request/url_encoded.rb
90
66
  - lib/faraday/response.rb
91
67
  - lib/faraday/response/json.rb
@@ -99,7 +75,6 @@ files:
99
75
  - spec/faraday/adapter/test_spec.rb
100
76
  - spec/faraday/adapter_registry_spec.rb
101
77
  - spec/faraday/adapter_spec.rb
102
- - spec/faraday/composite_read_io_spec.rb
103
78
  - spec/faraday/connection_spec.rb
104
79
  - spec/faraday/error_spec.rb
105
80
  - spec/faraday/middleware_spec.rb
@@ -113,8 +88,6 @@ files:
113
88
  - spec/faraday/request/authorization_spec.rb
114
89
  - spec/faraday/request/instrumentation_spec.rb
115
90
  - spec/faraday/request/json_spec.rb
116
- - spec/faraday/request/multipart_spec.rb
117
- - spec/faraday/request/retry_spec.rb
118
91
  - spec/faraday/request/url_encoded_spec.rb
119
92
  - spec/faraday/request_spec.rb
120
93
  - spec/faraday/response/json_spec.rb
@@ -137,7 +110,7 @@ licenses:
137
110
  - MIT
138
111
  metadata:
139
112
  homepage_uri: https://lostisland.github.io/faraday
140
- changelog_uri: https://github.com/lostisland/faraday/releases/tag/v2.0.0.alpha.pre.1
113
+ changelog_uri: https://github.com/lostisland/faraday/releases/tag/v2.0.0
141
114
  source_code_uri: https://github.com/lostisland/faraday
142
115
  bug_tracker_uri: https://github.com/lostisland/faraday/issues
143
116
  post_install_message:
@@ -152,9 +125,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
152
125
  version: '2.6'
153
126
  required_rubygems_version: !ruby/object:Gem::Requirement
154
127
  requirements:
155
- - - ">"
128
+ - - ">="
156
129
  - !ruby/object:Gem::Version
157
- version: 1.3.1
130
+ version: '0'
158
131
  requirements: []
159
132
  rubygems_version: 3.1.6
160
133
  signing_key: