faraday 0.13.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +496 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +28 -328
  5. data/Rakefile +7 -0
  6. data/examples/client_spec.rb +97 -0
  7. data/examples/client_test.rb +118 -0
  8. data/lib/faraday/adapter/test.rb +127 -68
  9. data/lib/faraday/adapter.rb +71 -22
  10. data/lib/faraday/adapter_registry.rb +30 -0
  11. data/lib/faraday/connection.rb +314 -226
  12. data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
  13. data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
  14. data/lib/faraday/error.rb +121 -37
  15. data/lib/faraday/logging/formatter.rb +106 -0
  16. data/lib/faraday/methods.rb +6 -0
  17. data/lib/faraday/middleware.rb +18 -25
  18. data/lib/faraday/middleware_registry.rb +65 -0
  19. data/lib/faraday/options/connection_options.rb +22 -0
  20. data/lib/faraday/options/env.rb +181 -0
  21. data/lib/faraday/options/proxy_options.rb +32 -0
  22. data/lib/faraday/options/request_options.rb +22 -0
  23. data/lib/faraday/options/ssl_options.rb +59 -0
  24. data/lib/faraday/options.rb +41 -195
  25. data/lib/faraday/parameters.rb +4 -196
  26. data/lib/faraday/rack_builder.rb +91 -74
  27. data/lib/faraday/request/authorization.rb +37 -29
  28. data/lib/faraday/request/instrumentation.rb +47 -27
  29. data/lib/faraday/request/json.rb +55 -0
  30. data/lib/faraday/request/url_encoded.rb +45 -23
  31. data/lib/faraday/request.rb +74 -32
  32. data/lib/faraday/response/json.rb +54 -0
  33. data/lib/faraday/response/logger.rb +22 -69
  34. data/lib/faraday/response/raise_error.rb +57 -14
  35. data/lib/faraday/response.rb +26 -33
  36. data/lib/faraday/utils/headers.rb +139 -0
  37. data/lib/faraday/utils/params_hash.rb +61 -0
  38. data/lib/faraday/utils.rb +47 -251
  39. data/lib/faraday/version.rb +5 -0
  40. data/lib/faraday.rb +104 -197
  41. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  42. data/spec/faraday/adapter/test_spec.rb +377 -0
  43. data/spec/faraday/adapter_registry_spec.rb +28 -0
  44. data/spec/faraday/adapter_spec.rb +55 -0
  45. data/spec/faraday/connection_spec.rb +787 -0
  46. data/spec/faraday/error_spec.rb +60 -0
  47. data/spec/faraday/middleware_spec.rb +52 -0
  48. data/spec/faraday/options/env_spec.rb +70 -0
  49. data/spec/faraday/options/options_spec.rb +297 -0
  50. data/spec/faraday/options/proxy_options_spec.rb +44 -0
  51. data/spec/faraday/options/request_options_spec.rb +19 -0
  52. data/spec/faraday/params_encoders/flat_spec.rb +42 -0
  53. data/spec/faraday/params_encoders/nested_spec.rb +142 -0
  54. data/spec/faraday/rack_builder_spec.rb +302 -0
  55. data/spec/faraday/request/authorization_spec.rb +83 -0
  56. data/spec/faraday/request/instrumentation_spec.rb +74 -0
  57. data/spec/faraday/request/json_spec.rb +111 -0
  58. data/spec/faraday/request/url_encoded_spec.rb +82 -0
  59. data/spec/faraday/request_spec.rb +109 -0
  60. data/spec/faraday/response/json_spec.rb +117 -0
  61. data/spec/faraday/response/logger_spec.rb +220 -0
  62. data/spec/faraday/response/raise_error_spec.rb +172 -0
  63. data/spec/faraday/response_spec.rb +75 -0
  64. data/spec/faraday/utils/headers_spec.rb +82 -0
  65. data/spec/faraday/utils_spec.rb +117 -0
  66. data/spec/faraday_spec.rb +37 -0
  67. data/spec/spec_helper.rb +132 -0
  68. data/spec/support/disabling_stub.rb +14 -0
  69. data/spec/support/fake_safe_buffer.rb +15 -0
  70. data/spec/support/helper_methods.rb +96 -0
  71. data/spec/support/shared_examples/adapter.rb +104 -0
  72. data/spec/support/shared_examples/params_encoder.rb +18 -0
  73. data/spec/support/shared_examples/request_method.rb +249 -0
  74. data/spec/support/streaming_response_checker.rb +35 -0
  75. metadata +71 -34
  76. data/lib/faraday/adapter/em_http.rb +0 -243
  77. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
  78. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
  79. data/lib/faraday/adapter/em_synchrony.rb +0 -106
  80. data/lib/faraday/adapter/excon.rb +0 -80
  81. data/lib/faraday/adapter/httpclient.rb +0 -128
  82. data/lib/faraday/adapter/net_http.rb +0 -135
  83. data/lib/faraday/adapter/net_http_persistent.rb +0 -54
  84. data/lib/faraday/adapter/patron.rb +0 -83
  85. data/lib/faraday/adapter/rack.rb +0 -58
  86. data/lib/faraday/adapter/typhoeus.rb +0 -123
  87. data/lib/faraday/autoload.rb +0 -84
  88. data/lib/faraday/request/basic_authentication.rb +0 -13
  89. data/lib/faraday/request/multipart.rb +0 -68
  90. data/lib/faraday/request/retry.rb +0 -164
  91. data/lib/faraday/request/token_authentication.rb +0 -15
  92. data/lib/faraday/upload_io.rb +0 -67
@@ -1,135 +0,0 @@
1
- begin
2
- require 'net/https'
3
- rescue LoadError
4
- warn "Warning: no such file to load -- net/https. Make sure openssl is installed if you want ssl support"
5
- require 'net/http'
6
- end
7
- require 'zlib'
8
-
9
- module Faraday
10
- class Adapter
11
- class NetHttp < Faraday::Adapter
12
- NET_HTTP_EXCEPTIONS = [
13
- IOError,
14
- Errno::ECONNABORTED,
15
- Errno::ECONNREFUSED,
16
- Errno::ECONNRESET,
17
- Errno::EHOSTUNREACH,
18
- Errno::EINVAL,
19
- Errno::ENETUNREACH,
20
- Errno::EPIPE,
21
- Net::HTTPBadResponse,
22
- Net::HTTPHeaderSyntaxError,
23
- Net::ProtocolError,
24
- SocketError,
25
- Zlib::GzipFile::Error,
26
- ]
27
-
28
- NET_HTTP_EXCEPTIONS << OpenSSL::SSL::SSLError if defined?(OpenSSL)
29
- NET_HTTP_EXCEPTIONS << Net::OpenTimeout if defined?(Net::OpenTimeout)
30
-
31
- def call(env)
32
- super
33
- with_net_http_connection(env) do |http|
34
- configure_ssl(http, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
35
- configure_request(http, env[:request])
36
-
37
- begin
38
- http_response = perform_request(http, env)
39
- rescue *NET_HTTP_EXCEPTIONS => err
40
- if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
41
- raise Faraday::SSLError, err
42
- else
43
- raise Error::ConnectionFailed, err
44
- end
45
- end
46
-
47
- save_response(env, http_response.code.to_i, http_response.body || '', nil, http_response.message) do |response_headers|
48
- http_response.each_header do |key, value|
49
- response_headers[key] = value
50
- end
51
- end
52
- end
53
-
54
- @app.call env
55
- rescue Timeout::Error, Errno::ETIMEDOUT => err
56
- raise Faraday::Error::TimeoutError, err
57
- end
58
-
59
- def create_request(env)
60
- request = Net::HTTPGenericRequest.new \
61
- env[:method].to_s.upcase, # request method
62
- !!env[:body], # is there request body
63
- :head != env[:method], # is there response body
64
- env[:url].request_uri, # request uri path
65
- env[:request_headers] # request headers
66
-
67
- if env[:body].respond_to?(:read)
68
- request.body_stream = env[:body]
69
- else
70
- request.body = env[:body]
71
- end
72
- request
73
- end
74
-
75
- def perform_request(http, env)
76
- if :get == env[:method] and !env[:body]
77
- # prefer `get` to `request` because the former handles gzip (ruby 1.9)
78
- http.get env[:url].request_uri, env[:request_headers]
79
- else
80
- http.request create_request(env)
81
- end
82
- end
83
-
84
- def with_net_http_connection(env)
85
- yield net_http_connection(env)
86
- end
87
-
88
- def net_http_connection(env)
89
- if proxy = env[:request][:proxy]
90
- Net::HTTP::Proxy(proxy[:uri].hostname, proxy[:uri].port, proxy[:user], proxy[:password])
91
- else
92
- Net::HTTP
93
- end.new(env[:url].hostname, env[:url].port || (env[:url].scheme == 'https' ? 443 : 80))
94
- end
95
-
96
- def configure_ssl(http, ssl)
97
- http.use_ssl = true
98
- http.verify_mode = ssl_verify_mode(ssl)
99
- http.cert_store = ssl_cert_store(ssl)
100
-
101
- http.cert = ssl[:client_cert] if ssl[:client_cert]
102
- http.key = ssl[:client_key] if ssl[:client_key]
103
- http.ca_file = ssl[:ca_file] if ssl[:ca_file]
104
- http.ca_path = ssl[:ca_path] if ssl[:ca_path]
105
- http.verify_depth = ssl[:verify_depth] if ssl[:verify_depth]
106
- http.ssl_version = ssl[:version] if ssl[:version]
107
- end
108
-
109
- def configure_request(http, req)
110
- http.read_timeout = http.open_timeout = req[:timeout] if req[:timeout]
111
- http.open_timeout = req[:open_timeout] if req[:open_timeout]
112
-
113
- @config_block.call(http) if @config_block
114
- end
115
-
116
- def ssl_cert_store(ssl)
117
- return ssl[:cert_store] if ssl[:cert_store]
118
- # Use the default cert store by default, i.e. system ca certs
119
- cert_store = OpenSSL::X509::Store.new
120
- cert_store.set_default_paths
121
- cert_store
122
- end
123
-
124
- def ssl_verify_mode(ssl)
125
- ssl[:verify_mode] || begin
126
- if ssl.fetch(:verify, true)
127
- OpenSSL::SSL::VERIFY_PEER
128
- else
129
- OpenSSL::SSL::VERIFY_NONE
130
- end
131
- end
132
- end
133
- end
134
- end
135
- end
@@ -1,54 +0,0 @@
1
- # Rely on autoloading instead of explicit require; helps avoid the "already
2
- # initialized constant" warning on Ruby 1.8.7 when NetHttp is refereced below.
3
- # require 'faraday/adapter/net_http'
4
-
5
- module Faraday
6
- class Adapter
7
- class NetHttpPersistent < NetHttp
8
- dependency 'net/http/persistent'
9
-
10
- def net_http_connection(env)
11
- proxy_uri = nil
12
- if (proxy = env[:request][:proxy])
13
- proxy_uri = ::URI::HTTP === proxy[:uri] ? proxy[:uri].dup : ::URI.parse(proxy[:uri].to_s)
14
- proxy_uri.user = proxy_uri.password = nil
15
- # awful patch for net-http-persistent 2.8 not unescaping user/password
16
- (class << proxy_uri; self; end).class_eval do
17
- define_method(:user) { proxy[:user] }
18
- define_method(:password) { proxy[:password] }
19
- end if proxy[:user]
20
- end
21
-
22
- if Net::HTTP::Persistent.instance_method(:initialize).parameters.first == [:key, :name]
23
- Net::HTTP::Persistent.new(name: 'Faraday', proxy: proxy_uri)
24
- else
25
- Net::HTTP::Persistent.new('Faraday', proxy_uri)
26
- end
27
- end
28
-
29
- def perform_request(http, env)
30
- http.request env[:url], create_request(env)
31
- rescue Errno::ETIMEDOUT => error
32
- raise Faraday::Error::TimeoutError, error
33
- rescue Net::HTTP::Persistent::Error => error
34
- if error.message.include? 'Timeout'
35
- raise Faraday::Error::TimeoutError, error
36
- elsif error.message.include? 'connection refused'
37
- raise Faraday::Error::ConnectionFailed, error
38
- else
39
- raise
40
- end
41
- end
42
-
43
- def configure_ssl(http, ssl)
44
- http.verify_mode = ssl_verify_mode(ssl)
45
- http.cert_store = ssl_cert_store(ssl)
46
-
47
- http.certificate = ssl[:client_cert] if ssl[:client_cert]
48
- http.private_key = ssl[:client_key] if ssl[:client_key]
49
- http.ca_file = ssl[:ca_file] if ssl[:ca_file]
50
- http.ssl_version = ssl[:version] if ssl[:version]
51
- end
52
- end
53
- end
54
- end
@@ -1,83 +0,0 @@
1
- module Faraday
2
- class Adapter
3
- class Patron < Faraday::Adapter
4
- dependency 'patron'
5
-
6
- def call(env)
7
- super
8
- # TODO: support streaming requests
9
- env[:body] = env[:body].read if env[:body].respond_to? :read
10
-
11
- session = @session ||= create_session
12
- configure_ssl(session, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
13
-
14
- if req = env[:request]
15
- session.timeout = session.connect_timeout = req[:timeout] if req[:timeout]
16
- session.connect_timeout = req[:open_timeout] if req[:open_timeout]
17
-
18
- if proxy = req[:proxy]
19
- proxy_uri = proxy[:uri].dup
20
- proxy_uri.user = proxy[:user] && Utils.escape(proxy[:user]).gsub('+', '%20')
21
- proxy_uri.password = proxy[:password] && Utils.escape(proxy[:password]).gsub('+', '%20')
22
- session.proxy = proxy_uri.to_s
23
- end
24
- end
25
-
26
- response = begin
27
- data = env[:body] ? env[:body].to_s : nil
28
- session.request(env[:method], env[:url].to_s, env[:request_headers], :data => data)
29
- rescue Errno::ECONNREFUSED, ::Patron::ConnectionFailed
30
- raise Error::ConnectionFailed, $!
31
- end
32
-
33
- # Remove the "HTTP/1.1 200", leaving just the reason phrase
34
- reason_phrase = response.status_line.gsub(/^.* \d{3} /, '')
35
-
36
- save_response(env, response.status, response.body, response.headers, reason_phrase)
37
-
38
- @app.call env
39
- rescue ::Patron::TimeoutError => err
40
- if err.message == "Connection time-out"
41
- raise Faraday::Error::ConnectionFailed, err
42
- else
43
- raise Faraday::Error::TimeoutError, err
44
- end
45
- rescue ::Patron::Error => err
46
- if err.message.include?("code 407")
47
- raise Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
48
- else
49
- raise Error::ConnectionFailed, err
50
- end
51
- end
52
-
53
- if loaded? && defined?(::Patron::Request::VALID_ACTIONS)
54
- # HAX: helps but doesn't work completely
55
- # https://github.com/toland/patron/issues/34
56
- ::Patron::Request::VALID_ACTIONS.tap do |actions|
57
- if actions[0].is_a?(Symbol)
58
- actions << :patch unless actions.include? :patch
59
- actions << :options unless actions.include? :options
60
- else
61
- # Patron 0.4.20 and up
62
- actions << "PATCH" unless actions.include? "PATCH"
63
- actions << "OPTIONS" unless actions.include? "OPTIONS"
64
- end
65
- end
66
- end
67
-
68
- def create_session
69
- session = ::Patron::Session.new
70
- @config_block.call(session) if @config_block
71
- session
72
- end
73
-
74
- def configure_ssl(session, ssl)
75
- if ssl.fetch(:verify, true)
76
- session.cacert = ssl[:ca_file]
77
- else
78
- session.insecure = true
79
- end
80
- end
81
- end
82
- end
83
- end
@@ -1,58 +0,0 @@
1
- module Faraday
2
- class Adapter
3
- # Sends requests to a Rack app.
4
- #
5
- # Examples
6
- #
7
- # class MyRackApp
8
- # def call(env)
9
- # [200, {'Content-Type' => 'text/html'}, ["hello world"]]
10
- # end
11
- # end
12
- #
13
- # Faraday.new do |conn|
14
- # conn.adapter :rack, MyRackApp.new
15
- # end
16
- class Rack < Faraday::Adapter
17
- dependency 'rack/test'
18
-
19
- # not prefixed with "HTTP_"
20
- SPECIAL_HEADERS = %w[ CONTENT_LENGTH CONTENT_TYPE ]
21
-
22
- def initialize(faraday_app, rack_app)
23
- super(faraday_app)
24
- mock_session = ::Rack::MockSession.new(rack_app)
25
- @session = ::Rack::Test::Session.new(mock_session)
26
- end
27
-
28
- def call(env)
29
- super
30
- rack_env = {
31
- :method => env[:method],
32
- :input => env[:body].respond_to?(:read) ? env[:body].read : env[:body],
33
- 'rack.url_scheme' => env[:url].scheme
34
- }
35
-
36
- env[:request_headers].each do |name, value|
37
- name = name.upcase.tr('-', '_')
38
- name = "HTTP_#{name}" unless SPECIAL_HEADERS.include? name
39
- rack_env[name] = value
40
- end if env[:request_headers]
41
-
42
- timeout = env[:request][:timeout] || env[:request][:open_timeout]
43
- response = if timeout
44
- Timer.timeout(timeout, Faraday::Error::TimeoutError) { execute_request(env, rack_env) }
45
- else
46
- execute_request(env, rack_env)
47
- end
48
-
49
- save_response(env, response.status, response.body, response.headers)
50
- @app.call env
51
- end
52
-
53
- def execute_request(env, rack_env)
54
- @session.request(env[:url].to_s, rack_env)
55
- end
56
- end
57
- end
58
- end
@@ -1,123 +0,0 @@
1
- module Faraday
2
- class Adapter
3
- class Typhoeus < Faraday::Adapter
4
- self.supports_parallel = true
5
-
6
- def self.setup_parallel_manager(options = {})
7
- options.empty? ? ::Typhoeus::Hydra.hydra : ::Typhoeus::Hydra.new(options)
8
- end
9
-
10
- dependency 'typhoeus'
11
-
12
- def call(env)
13
- super
14
- perform_request env
15
- @app.call env
16
- end
17
-
18
- def perform_request(env)
19
- read_body env
20
-
21
- hydra = env[:parallel_manager] || self.class.setup_parallel_manager
22
- hydra.queue request(env)
23
- hydra.run unless parallel?(env)
24
- rescue Errno::ECONNREFUSED
25
- raise Error::ConnectionFailed, $!
26
- end
27
-
28
- # TODO: support streaming requests
29
- def read_body(env)
30
- env[:body] = env[:body].read if env[:body].respond_to? :read
31
- end
32
-
33
- def request(env)
34
- method = env[:method]
35
- # For some reason, prevents Typhoeus from using "100-continue".
36
- # We want this because Webrick 1.3.1 can't seem to handle it w/ PUT.
37
- method = method.to_s.upcase if method == :put
38
-
39
- req = ::Typhoeus::Request.new env[:url].to_s,
40
- :method => method,
41
- :body => env[:body],
42
- :headers => env[:request_headers],
43
- :disable_ssl_peer_verification => (env[:ssl] && env[:ssl].disable?)
44
-
45
- configure_ssl req, env
46
- configure_proxy req, env
47
- configure_timeout req, env
48
- configure_socket req, env
49
-
50
- req.on_complete do |resp|
51
- if resp.timed_out?
52
- if parallel?(env)
53
- # TODO: error callback in async mode
54
- else
55
- raise Faraday::Error::TimeoutError, "request timed out"
56
- end
57
- end
58
-
59
- case resp.curl_return_code
60
- when 0
61
- # everything OK
62
- when 7
63
- raise Error::ConnectionFailed, resp.curl_error_message
64
- when 60
65
- raise Faraday::SSLError, resp.curl_error_message
66
- else
67
- raise Error::ClientError, resp.curl_error_message
68
- end
69
-
70
- save_response(env, resp.code, resp.body) do |response_headers|
71
- response_headers.parse resp.headers
72
- end
73
- # in async mode, :response is initialized at this point
74
- env[:response].finish(env) if parallel?(env)
75
- end
76
-
77
- req
78
- end
79
-
80
- def configure_ssl(req, env)
81
- ssl = env[:ssl]
82
-
83
- req.ssl_version = ssl[:version] if ssl[:version]
84
- req.ssl_cert = ssl[:client_cert] if ssl[:client_cert]
85
- req.ssl_key = ssl[:client_key] if ssl[:client_key]
86
- req.ssl_cacert = ssl[:ca_file] if ssl[:ca_file]
87
- req.ssl_capath = ssl[:ca_path] if ssl[:ca_path]
88
- end
89
-
90
- def configure_proxy(req, env)
91
- proxy = request_options(env)[:proxy]
92
- return unless proxy
93
-
94
- req.proxy = "#{proxy[:uri].host}:#{proxy[:uri].port}"
95
-
96
- if proxy[:user] && proxy[:password]
97
- req.proxy_username = proxy[:user]
98
- req.proxy_password = proxy[:password]
99
- end
100
- end
101
-
102
- def configure_timeout(req, env)
103
- env_req = request_options(env)
104
- req.timeout = req.connect_timeout = (env_req[:timeout] * 1000) if env_req[:timeout]
105
- req.connect_timeout = (env_req[:open_timeout] * 1000) if env_req[:open_timeout]
106
- end
107
-
108
- def configure_socket(req, env)
109
- if bind = request_options(env)[:bind]
110
- req.interface = bind[:host]
111
- end
112
- end
113
-
114
- def request_options(env)
115
- env[:request]
116
- end
117
-
118
- def parallel?(env)
119
- !!env[:parallel_manager]
120
- end
121
- end
122
- end
123
- end
@@ -1,84 +0,0 @@
1
- module Faraday
2
- # Internal: Adds the ability for other modules to manage autoloadable
3
- # constants.
4
- module AutoloadHelper
5
- # Internal: Registers the constants to be auto loaded.
6
- #
7
- # prefix - The String require prefix. If the path is inside Faraday, then
8
- # it will be prefixed with the root path of this loaded Faraday
9
- # version.
10
- # options - Hash of Symbol => String library names.
11
- #
12
- # Examples.
13
- #
14
- # Faraday.autoload_all 'faraday/foo',
15
- # :Bar => 'bar'
16
- #
17
- # # requires faraday/foo/bar to load Faraday::Bar.
18
- # Faraday::Bar
19
- #
20
- #
21
- # Returns nothing.
22
- def autoload_all(prefix, options)
23
- if prefix =~ /^faraday(\/|$)/i
24
- prefix = File.join(Faraday.root_path, prefix)
25
- end
26
- options.each do |const_name, path|
27
- autoload const_name, File.join(prefix, path)
28
- end
29
- end
30
-
31
- # Internal: Loads each autoloaded constant. If thread safety is a concern,
32
- # wrap this in a Mutex.
33
- #
34
- # Returns nothing.
35
- def load_autoloaded_constants
36
- constants.each do |const|
37
- const_get(const) if autoload?(const)
38
- end
39
- end
40
-
41
- # Internal: Filters the module's contents with those that have been already
42
- # autoloaded.
43
- #
44
- # Returns an Array of Class/Module objects.
45
- def all_loaded_constants
46
- constants.map { |c| const_get(c) }.
47
- select { |a| a.respond_to?(:loaded?) && a.loaded? }
48
- end
49
- end
50
-
51
- class Adapter
52
- extend AutoloadHelper
53
- autoload_all 'faraday/adapter',
54
- :NetHttp => 'net_http',
55
- :NetHttpPersistent => 'net_http_persistent',
56
- :Typhoeus => 'typhoeus',
57
- :EMSynchrony => 'em_synchrony',
58
- :EMHttp => 'em_http',
59
- :Patron => 'patron',
60
- :Excon => 'excon',
61
- :Test => 'test',
62
- :Rack => 'rack',
63
- :HTTPClient => 'httpclient'
64
- end
65
-
66
- class Request
67
- extend AutoloadHelper
68
- autoload_all 'faraday/request',
69
- :UrlEncoded => 'url_encoded',
70
- :Multipart => 'multipart',
71
- :Retry => 'retry',
72
- :Authorization => 'authorization',
73
- :BasicAuthentication => 'basic_authentication',
74
- :TokenAuthentication => 'token_authentication',
75
- :Instrumentation => 'instrumentation'
76
- end
77
-
78
- class Response
79
- extend AutoloadHelper
80
- autoload_all 'faraday/response',
81
- :RaiseError => 'raise_error',
82
- :Logger => 'logger'
83
- end
84
- end
@@ -1,13 +0,0 @@
1
- require 'base64'
2
-
3
- module Faraday
4
- class Request::BasicAuthentication < Request.load_middleware(:authorization)
5
- # Public
6
- def self.header(login, pass)
7
- value = Base64.encode64([login, pass].join(':'))
8
- value.gsub!("\n", '')
9
- super(:Basic, value)
10
- end
11
- end
12
- end
13
-
@@ -1,68 +0,0 @@
1
- require File.expand_path("../url_encoded", __FILE__)
2
- require 'securerandom'
3
-
4
- module Faraday
5
- class Request::Multipart < Request::UrlEncoded
6
- self.mime_type = 'multipart/form-data'.freeze
7
- DEFAULT_BOUNDARY_PREFIX = "-----------RubyMultipartPost".freeze unless defined? DEFAULT_BOUNDARY_PREFIX
8
-
9
- def call(env)
10
- match_content_type(env) do |params|
11
- env.request.boundary ||= unique_boundary
12
- env.request_headers[CONTENT_TYPE] += "; boundary=#{env.request.boundary}"
13
- env.body = create_multipart(env, params)
14
- end
15
- @app.call env
16
- end
17
-
18
- def process_request?(env)
19
- type = request_type(env)
20
- env.body.respond_to?(:each_key) and !env.body.empty? and (
21
- (type.empty? and has_multipart?(env.body)) or
22
- type == self.class.mime_type
23
- )
24
- end
25
-
26
- def has_multipart?(obj)
27
- # string is an enum in 1.8, returning list of itself
28
- if obj.respond_to?(:each) && !obj.is_a?(String)
29
- (obj.respond_to?(:values) ? obj.values : obj).each do |val|
30
- return true if (val.respond_to?(:content_type) || has_multipart?(val))
31
- end
32
- end
33
- false
34
- end
35
-
36
- def create_multipart(env, params)
37
- boundary = env.request.boundary
38
- parts = process_params(params) do |key, value|
39
- Faraday::Parts::Part.new(boundary, key, value)
40
- end
41
- parts << Faraday::Parts::EpiloguePart.new(boundary)
42
-
43
- body = Faraday::CompositeReadIO.new(parts)
44
- env.request_headers[Faraday::Env::ContentLength] = body.length.to_s
45
- return body
46
- end
47
-
48
- def unique_boundary
49
- "#{DEFAULT_BOUNDARY_PREFIX}-#{SecureRandom.hex}"
50
- end
51
-
52
- def process_params(params, prefix = nil, pieces = nil, &block)
53
- params.inject(pieces || []) do |all, (key, value)|
54
- key = "#{prefix}[#{key}]" if prefix
55
-
56
- case value
57
- when Array
58
- values = value.inject([]) { |a,v| a << [nil, v] }
59
- process_params(values, key, all, &block)
60
- when Hash
61
- process_params(value, key, all, &block)
62
- else
63
- all << block.call(key, value)
64
- end
65
- end
66
- end
67
- end
68
- end