faraday 1.1.0 → 1.10.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +104 -0
  3. data/README.md +1 -2
  4. data/examples/client_spec.rb +34 -2
  5. data/examples/client_test.rb +41 -2
  6. data/lib/faraday/adapter/test.rb +59 -43
  7. data/lib/faraday/adapter.rb +1 -12
  8. data/lib/faraday/autoload.rb +1 -9
  9. data/lib/faraday/connection.rb +34 -6
  10. data/lib/faraday/deprecate.rb +110 -0
  11. data/lib/faraday/error.rb +12 -6
  12. data/lib/faraday/methods.rb +6 -0
  13. data/lib/faraday/middleware.rb +14 -4
  14. data/lib/faraday/options/proxy_options.rb +4 -0
  15. data/lib/faraday/request/authorization.rb +14 -7
  16. data/lib/faraday/request/json.rb +55 -0
  17. data/lib/faraday/request.rb +10 -12
  18. data/lib/faraday/response/json.rb +54 -0
  19. data/lib/faraday/response/logger.rb +2 -4
  20. data/lib/faraday/response.rb +3 -7
  21. data/lib/faraday/version.rb +5 -0
  22. data/lib/faraday.rb +67 -40
  23. data/spec/faraday/adapter/em_http_spec.rb +39 -37
  24. data/spec/faraday/adapter/em_synchrony_spec.rb +11 -9
  25. data/spec/faraday/adapter/test_spec.rb +117 -0
  26. data/spec/faraday/connection_spec.rb +45 -0
  27. data/spec/faraday/deprecate_spec.rb +147 -0
  28. data/spec/faraday/error_spec.rb +15 -0
  29. data/spec/faraday/middleware_spec.rb +32 -6
  30. data/spec/faraday/options/proxy_options_spec.rb +7 -0
  31. data/spec/faraday/request/authorization_spec.rb +8 -0
  32. data/spec/faraday/request/json_spec.rb +111 -0
  33. data/spec/faraday/request_spec.rb +1 -1
  34. data/spec/faraday/response/json_spec.rb +119 -0
  35. data/spec/faraday/response/raise_error_spec.rb +30 -0
  36. data/spec/spec_helper.rb +2 -0
  37. data/spec/support/shared_examples/adapter.rb +2 -1
  38. data/spec/support/shared_examples/request_method.rb +36 -8
  39. metadata +143 -32
  40. data/lib/faraday/adapter/em_http.rb +0 -286
  41. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -62
  42. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -69
  43. data/lib/faraday/adapter/em_synchrony.rb +0 -150
  44. data/lib/faraday/adapter/excon.rb +0 -124
  45. data/lib/faraday/adapter/httpclient.rb +0 -152
  46. data/lib/faraday/adapter/net_http.rb +0 -219
  47. data/lib/faraday/adapter/net_http_persistent.rb +0 -91
  48. data/lib/faraday/adapter/patron.rb +0 -132
  49. data/lib/faraday/adapter/rack.rb +0 -75
  50. data/lib/faraday/file_part.rb +0 -128
  51. data/lib/faraday/param_part.rb +0 -53
  52. data/lib/faraday/request/multipart.rb +0 -106
  53. data/lib/faraday/request/retry.rb +0 -239
  54. data/spec/faraday/adapter/net_http_persistent_spec.rb +0 -57
  55. data/spec/faraday/request/multipart_spec.rb +0 -302
  56. data/spec/faraday/request/retry_spec.rb +0 -242
@@ -1,91 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Faraday
4
- class Adapter
5
- # Net::HTTP::Persistent adapter.
6
- class NetHttpPersistent < NetHttp
7
- dependency 'net/http/persistent'
8
-
9
- private
10
-
11
- def net_http_connection(env)
12
- @cached_connection ||=
13
- if Net::HTTP::Persistent.instance_method(:initialize)
14
- .parameters.first == %i[key name]
15
- options = { name: 'Faraday' }
16
- if @connection_options.key?(:pool_size)
17
- options[:pool_size] = @connection_options[:pool_size]
18
- end
19
- Net::HTTP::Persistent.new(**options)
20
- else
21
- Net::HTTP::Persistent.new('Faraday')
22
- end
23
-
24
- proxy_uri = proxy_uri(env)
25
- if @cached_connection.proxy_uri != proxy_uri
26
- @cached_connection.proxy = proxy_uri
27
- end
28
- @cached_connection
29
- end
30
-
31
- def proxy_uri(env)
32
- proxy_uri = nil
33
- if (proxy = env[:request][:proxy])
34
- proxy_uri = if proxy[:uri].is_a?(::URI::HTTP)
35
- proxy[:uri].dup
36
- else
37
- ::URI.parse(proxy[:uri].to_s)
38
- end
39
- proxy_uri.user = proxy_uri.password = nil
40
- # awful patch for net-http-persistent 2.8
41
- # not unescaping user/password
42
- if proxy[:user]
43
- (class << proxy_uri; self; end).class_eval do
44
- define_method(:user) { proxy[:user] }
45
- define_method(:password) { proxy[:password] }
46
- end
47
- end
48
- end
49
- proxy_uri
50
- end
51
-
52
- def perform_request(http, env)
53
- http.request env[:url], create_request(env)
54
- rescue Errno::ETIMEDOUT => e
55
- raise Faraday::TimeoutError, e
56
- rescue Net::HTTP::Persistent::Error => e
57
- raise Faraday::TimeoutError, e if e.message.include? 'Timeout'
58
-
59
- if e.message.include? 'connection refused'
60
- raise Faraday::ConnectionFailed, e
61
- end
62
-
63
- raise
64
- end
65
-
66
- SSL_CONFIGURATIONS = {
67
- certificate: :client_cert,
68
- private_key: :client_key,
69
- ca_file: :ca_file,
70
- ssl_version: :version,
71
- min_version: :min_version,
72
- max_version: :max_version
73
- }.freeze
74
-
75
- def configure_ssl(http, ssl)
76
- return unless ssl
77
-
78
- http_set(http, :verify_mode, ssl_verify_mode(ssl))
79
- http_set(http, :cert_store, ssl_cert_store(ssl))
80
-
81
- SSL_CONFIGURATIONS
82
- .select { |_, key| ssl[key] }
83
- .each { |target, key| http_set(http, target, ssl[key]) }
84
- end
85
-
86
- def http_set(http, attr, value)
87
- http.send("#{attr}=", value) if http.send(attr) != value
88
- end
89
- end
90
- end
91
- end
@@ -1,132 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Faraday
4
- class Adapter
5
- # Patron adapter.
6
- class Patron < Faraday::Adapter
7
- dependency 'patron'
8
-
9
- def build_connection(env)
10
- session = ::Patron::Session.new
11
- @config_block&.call(session)
12
- if (env[:url].scheme == 'https') && env[:ssl]
13
- configure_ssl(session, env[:ssl])
14
- end
15
-
16
- if (req = env[:request])
17
- configure_timeouts(session, req)
18
- configure_proxy(session, req[:proxy])
19
- end
20
-
21
- session
22
- end
23
-
24
- def call(env)
25
- super
26
- # TODO: support streaming requests
27
- env[:body] = env[:body].read if env[:body].respond_to? :read
28
-
29
- response = connection(env) do |session|
30
- begin
31
- data = env[:body] ? env[:body].to_s : nil
32
- session.request(env[:method], env[:url].to_s,
33
- env[:request_headers], data: data)
34
- rescue Errno::ECONNREFUSED, ::Patron::ConnectionFailed
35
- raise Faraday::ConnectionFailed, $ERROR_INFO
36
- end
37
- end
38
-
39
- if (req = env[:request]).stream_response?
40
- warn "Streaming downloads for #{self.class.name} " \
41
- 'are not yet implemented.'
42
- req.on_data.call(response.body, response.body.bytesize)
43
- end
44
- # Remove the "HTTP/1.1 200", leaving just the reason phrase
45
- reason_phrase = response.status_line.gsub(/^.* \d{3} /, '')
46
-
47
- save_response(env, response.status, response.body,
48
- response.headers, reason_phrase)
49
-
50
- @app.call env
51
- rescue ::Patron::TimeoutError => e
52
- if connection_timed_out_message?(e.message)
53
- raise Faraday::ConnectionFailed, e
54
- end
55
-
56
- raise Faraday::TimeoutError, e
57
- rescue ::Patron::Error => e
58
- if e.message.include?('code 407')
59
- raise Faraday::ConnectionFailed,
60
- %(407 "Proxy Authentication Required ")
61
- end
62
-
63
- raise Faraday::ConnectionFailed, e
64
- end
65
-
66
- if loaded? && defined?(::Patron::Request::VALID_ACTIONS)
67
- # HAX: helps but doesn't work completely
68
- # https://github.com/toland/patron/issues/34
69
- ::Patron::Request::VALID_ACTIONS.tap do |actions|
70
- if actions[0].is_a?(Symbol)
71
- actions << :patch unless actions.include? :patch
72
- actions << :options unless actions.include? :options
73
- else
74
- # Patron 0.4.20 and up
75
- actions << 'PATCH' unless actions.include? 'PATCH'
76
- actions << 'OPTIONS' unless actions.include? 'OPTIONS'
77
- end
78
- end
79
- end
80
-
81
- def configure_ssl(session, ssl)
82
- if ssl.fetch(:verify, true)
83
- session.cacert = ssl[:ca_file]
84
- else
85
- session.insecure = true
86
- end
87
- end
88
-
89
- def configure_timeouts(session, req)
90
- return unless req
91
-
92
- if (sec = request_timeout(:read, req))
93
- session.timeout = sec
94
- end
95
-
96
- return unless (sec = request_timeout(:open, req))
97
-
98
- session.connect_timeout = sec
99
- end
100
-
101
- def configure_proxy(session, proxy)
102
- return unless proxy
103
-
104
- proxy_uri = proxy[:uri].dup
105
- proxy_uri.user = proxy[:user] &&
106
- Utils.escape(proxy[:user]).gsub('+', '%20')
107
- proxy_uri.password = proxy[:password] &&
108
- Utils.escape(proxy[:password]).gsub('+', '%20')
109
- session.proxy = proxy_uri.to_s
110
- end
111
-
112
- private
113
-
114
- CURL_TIMEOUT_MESSAGES = [
115
- 'Connection time-out',
116
- 'Connection timed out',
117
- 'Timed out before name resolve',
118
- 'server connect has timed out',
119
- 'Resolving timed out',
120
- 'name lookup timed out',
121
- 'timed out before SSL',
122
- 'connect() timed out'
123
- ].freeze
124
-
125
- def connection_timed_out_message?(message)
126
- CURL_TIMEOUT_MESSAGES.any? do |curl_message|
127
- message.include?(curl_message)
128
- end
129
- end
130
- end
131
- end
132
- end
@@ -1,75 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Faraday
4
- class Adapter
5
- # Sends requests to a Rack app.
6
- #
7
- # @example
8
- #
9
- # class MyRackApp
10
- # def call(env)
11
- # [200, {'Content-Type' => 'text/html'}, ["hello world"]]
12
- # end
13
- # end
14
- #
15
- # Faraday.new do |conn|
16
- # conn.adapter :rack, MyRackApp.new
17
- # end
18
- class Rack < Faraday::Adapter
19
- dependency 'rack/test'
20
-
21
- # not prefixed with "HTTP_"
22
- SPECIAL_HEADERS = %w[CONTENT_LENGTH CONTENT_TYPE].freeze
23
-
24
- def initialize(faraday_app, rack_app)
25
- super(faraday_app)
26
- mock_session = ::Rack::MockSession.new(rack_app)
27
- @session = ::Rack::Test::Session.new(mock_session)
28
- end
29
-
30
- def call(env)
31
- super
32
- rack_env = build_rack_env(env)
33
-
34
- env[:request_headers]&.each do |name, value|
35
- name = name.upcase.tr('-', '_')
36
- name = "HTTP_#{name}" unless SPECIAL_HEADERS.include? name
37
- rack_env[name] = value
38
- end
39
-
40
- timeout = request_timeout(:open, env[:request])
41
- timeout ||= request_timeout(:read, env[:request])
42
- response = if timeout
43
- Timer.timeout(timeout, Faraday::TimeoutError) do
44
- execute_request(env, rack_env)
45
- end
46
- else
47
- execute_request(env, rack_env)
48
- end
49
-
50
- if (req = env[:request]).stream_response?
51
- warn "Streaming downloads for #{self.class.name} " \
52
- 'are not yet implemented.'
53
- req.on_data.call(response.body, response.body.bytesize)
54
- end
55
-
56
- save_response(env, response.status, response.body, response.headers)
57
- @app.call env
58
- end
59
-
60
- private
61
-
62
- def execute_request(env, rack_env)
63
- @session.request(env[:url].to_s, rack_env)
64
- end
65
-
66
- def build_rack_env(env)
67
- {
68
- method: env[:method],
69
- input: env[:body].respond_to?(:read) ? env[:body].read : env[:body],
70
- 'rack.url_scheme' => env[:url].scheme
71
- }
72
- end
73
- end
74
- end
75
- end
@@ -1,128 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'stringio'
4
-
5
- # multipart-post gem
6
- require 'composite_io'
7
- require 'parts'
8
-
9
- module Faraday
10
- # Multipart value used to POST a binary data from a file or
11
- #
12
- # @example
13
- # payload = { file: Faraday::FilePart.new("file_name.ext", "content/type") }
14
- # http.post("/upload", payload)
15
- #
16
-
17
- # @!method initialize(filename_or_io, content_type, filename = nil, opts = {})
18
- #
19
- # @param filename_or_io [String, IO] Either a String filename to a local
20
- # file or an open IO object.
21
- # @param content_type [String] String content type of the file data.
22
- # @param filename [String] Optional String filename, usually to add context
23
- # to a given IO object.
24
- # @param opts [Hash] Optional Hash of String key/value pairs to describethis
25
- # this uploaded file. Expected Header keys include:
26
- # * Content-Transfer-Encoding - Defaults to "binary"
27
- # * Content-Disposition - Defaults to "form-data"
28
- # * Content-Type - Defaults to the content_type argument.
29
- # * Content-ID - Optional.
30
- #
31
- # @return [Faraday::FilePart]
32
- #
33
- # @!attribute [r] content_type
34
- # The uploaded binary data's content type.
35
- #
36
- # @return [String]
37
- #
38
- # @!attribute [r] original_filename
39
- # The base filename, taken either from the filename_or_io or filename
40
- # arguments in #initialize.
41
- #
42
- # @return [String]
43
- #
44
- # @!attribute [r] opts
45
- # Extra String key/value pairs to make up the header for this uploaded file.
46
- #
47
- # @return [Hash]
48
- #
49
- # @!attribute [r] io
50
- # The open IO object for the uploaded file.
51
- #
52
- # @return [IO]
53
- FilePart = ::UploadIO
54
-
55
- # 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
- Parts = ::Parts
62
-
63
- # Similar to, but not compatible with CompositeReadIO provided by the
64
- # multipart-post gem.
65
- # https://github.com/nicksieger/multipart-post/blob/master/lib/composite_io.rb
66
- class CompositeReadIO
67
- def initialize(*parts)
68
- @parts = parts.flatten
69
- @ios = @parts.map(&:to_io)
70
- @index = 0
71
- end
72
-
73
- # @return [Integer] sum of the lengths of all the parts
74
- def length
75
- @parts.inject(0) { |sum, part| sum + part.length }
76
- end
77
-
78
- # Rewind each of the IOs and reset the index to 0.
79
- #
80
- # @return [void]
81
- def rewind
82
- @ios.each(&:rewind)
83
- @index = 0
84
- end
85
-
86
- # Read from IOs in order until `length` bytes have been received.
87
- #
88
- # @param length [Integer, nil]
89
- # @param outbuf [String, nil]
90
- def read(length = nil, outbuf = nil)
91
- got_result = false
92
- outbuf = outbuf ? (+outbuf).replace('') : +''
93
-
94
- while (io = current_io)
95
- if (result = io.read(length))
96
- got_result ||= !result.nil?
97
- result.force_encoding('BINARY') if result.respond_to?(:force_encoding)
98
- outbuf << result
99
- length -= result.length if length
100
- break if length&.zero?
101
- end
102
- advance_io
103
- end
104
- !got_result && length ? nil : outbuf
105
- end
106
-
107
- # Close each of the IOs.
108
- #
109
- # @return [void]
110
- def close
111
- @ios.each(&:close)
112
- end
113
-
114
- def ensure_open_and_readable
115
- # Rubinius compatibility
116
- end
117
-
118
- private
119
-
120
- def current_io
121
- @ios[@index]
122
- end
123
-
124
- def advance_io
125
- @index += 1
126
- end
127
- end
128
- end
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Faraday
4
- # Multipart value used to POST data with a content type.
5
- class ParamPart
6
- # @param value [String] Uploaded content as a String.
7
- # @param content_type [String] String content type of the value.
8
- # @param content_id [String] Optional String of this value's Content-ID.
9
- #
10
- # @return [Faraday::ParamPart]
11
- def initialize(value, content_type, content_id = nil)
12
- @value = value
13
- @content_type = content_type
14
- @content_id = content_id
15
- end
16
-
17
- # Converts this value to a form part.
18
- #
19
- # @param boundary [String] String multipart boundary that must not exist in
20
- # the content exactly.
21
- # @param key [String] String key name for this value.
22
- #
23
- # @return [Faraday::Parts::Part]
24
- def to_part(boundary, key)
25
- Faraday::Parts::Part.new(boundary, key, value, headers)
26
- end
27
-
28
- # Returns a Hash of String key/value pairs.
29
- #
30
- # @return [Hash]
31
- def headers
32
- {
33
- 'Content-Type' => content_type,
34
- 'Content-ID' => content_id
35
- }
36
- end
37
-
38
- # The content to upload.
39
- #
40
- # @return [String]
41
- attr_reader :value
42
-
43
- # The value's content type.
44
- #
45
- # @return [String]
46
- attr_reader :content_type
47
-
48
- # The value's content ID, if given.
49
- #
50
- # @return [String, nil]
51
- attr_reader :content_id
52
- end
53
- end
@@ -1,106 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require File.expand_path('url_encoded', __dir__)
4
- require 'securerandom'
5
-
6
- module Faraday
7
- class Request
8
- # Middleware for supporting multi-part requests.
9
- class Multipart < UrlEncoded
10
- self.mime_type = 'multipart/form-data'
11
- unless defined?(::Faraday::Request::Multipart::DEFAULT_BOUNDARY_PREFIX)
12
- DEFAULT_BOUNDARY_PREFIX = '-----------RubyMultipartPost'
13
- end
14
-
15
- def initialize(app = nil, options = {})
16
- super(app)
17
- @options = options
18
- end
19
-
20
- # Checks for files in the payload, otherwise leaves everything untouched.
21
- #
22
- # @param env [Faraday::Env]
23
- def call(env)
24
- match_content_type(env) do |params|
25
- env.request.boundary ||= unique_boundary
26
- env.request_headers[CONTENT_TYPE] +=
27
- "; boundary=#{env.request.boundary}"
28
- env.body = create_multipart(env, params)
29
- end
30
- @app.call env
31
- end
32
-
33
- # @param env [Faraday::Env]
34
- def process_request?(env)
35
- type = request_type(env)
36
- env.body.respond_to?(:each_key) && !env.body.empty? && (
37
- (type.empty? && has_multipart?(env.body)) ||
38
- (type == self.class.mime_type)
39
- )
40
- end
41
-
42
- # Returns true if obj is an enumerable with values that are multipart.
43
- #
44
- # @param obj [Object]
45
- # @return [Boolean]
46
- def has_multipart?(obj) # rubocop:disable Naming/PredicateName
47
- if obj.respond_to?(:each)
48
- (obj.respond_to?(:values) ? obj.values : obj).each do |val|
49
- return true if val.respond_to?(:content_type) || has_multipart?(val)
50
- end
51
- end
52
- false
53
- end
54
-
55
- # @param env [Faraday::Env]
56
- # @param params [Hash]
57
- def create_multipart(env, params)
58
- boundary = env.request.boundary
59
- parts = process_params(params) do |key, value|
60
- part(boundary, key, value)
61
- end
62
- parts << Faraday::Parts::EpiloguePart.new(boundary)
63
-
64
- body = Faraday::CompositeReadIO.new(parts)
65
- env.request_headers[Faraday::Env::ContentLength] = body.length.to_s
66
- body
67
- end
68
-
69
- def part(boundary, key, value)
70
- if value.respond_to?(:to_part)
71
- value.to_part(boundary, key)
72
- else
73
- Faraday::Parts::Part.new(boundary, key, value)
74
- end
75
- end
76
-
77
- # @return [String]
78
- def unique_boundary
79
- "#{DEFAULT_BOUNDARY_PREFIX}-#{SecureRandom.hex}"
80
- end
81
-
82
- # @param params [Hash]
83
- # @param prefix [String]
84
- # @param pieces [Array]
85
- def process_params(params, prefix = nil, pieces = nil, &block)
86
- params.inject(pieces || []) do |all, (key, value)|
87
- if prefix
88
- key = @options[:flat_encode] ? prefix.to_s : "#{prefix}[#{key}]"
89
- end
90
-
91
- case value
92
- when Array
93
- values = value.inject([]) { |a, v| a << [nil, v] }
94
- process_params(values, key, all, &block)
95
- when Hash
96
- process_params(value, key, all, &block)
97
- else
98
- # rubocop:disable Performance/RedundantBlockCall
99
- all << block.call(key, value)
100
- # rubocop:enable Performance/RedundantBlockCall
101
- end
102
- end
103
- end
104
- end
105
- end
106
- end