faraday 0.17.1 → 1.1.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 +69 -9
- data/LICENSE.md +1 -1
- data/README.md +17 -347
- data/Rakefile +1 -7
- data/examples/client_spec.rb +65 -0
- data/examples/client_test.rb +79 -0
- data/lib/faraday.rb +94 -176
- data/lib/faraday/adapter.rb +83 -22
- data/lib/faraday/adapter/em_http.rb +143 -100
- data/lib/faraday/adapter/em_http_ssl_patch.rb +24 -18
- data/lib/faraday/adapter/em_synchrony.rb +104 -60
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
- data/lib/faraday/adapter/excon.rb +98 -56
- data/lib/faraday/adapter/httpclient.rb +83 -59
- data/lib/faraday/adapter/net_http.rb +130 -63
- data/lib/faraday/adapter/net_http_persistent.rb +51 -28
- data/lib/faraday/adapter/patron.rb +80 -43
- data/lib/faraday/adapter/rack.rb +30 -13
- data/lib/faraday/adapter/test.rb +86 -53
- data/lib/faraday/adapter/typhoeus.rb +4 -1
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/autoload.rb +47 -36
- data/lib/faraday/connection.rb +312 -182
- data/lib/faraday/dependency_loader.rb +39 -0
- data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
- data/lib/faraday/error.rb +46 -25
- data/lib/faraday/file_part.rb +128 -0
- data/lib/faraday/logging/formatter.rb +105 -0
- data/lib/faraday/middleware.rb +12 -28
- data/lib/faraday/middleware_registry.rb +129 -0
- data/lib/faraday/options.rb +38 -193
- data/lib/faraday/options/connection_options.rb +22 -0
- data/lib/faraday/options/env.rb +181 -0
- data/lib/faraday/options/proxy_options.rb +28 -0
- data/lib/faraday/options/request_options.rb +22 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/param_part.rb +53 -0
- data/lib/faraday/parameters.rb +4 -197
- data/lib/faraday/rack_builder.rb +77 -65
- data/lib/faraday/request.rb +86 -44
- data/lib/faraday/request/authorization.rb +44 -30
- data/lib/faraday/request/basic_authentication.rb +14 -7
- data/lib/faraday/request/instrumentation.rb +45 -27
- data/lib/faraday/request/multipart.rb +86 -48
- data/lib/faraday/request/retry.rb +197 -171
- data/lib/faraday/request/token_authentication.rb +15 -10
- data/lib/faraday/request/url_encoded.rb +43 -23
- data/lib/faraday/response.rb +24 -14
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +49 -18
- data/lib/faraday/utils.rb +38 -247
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/em_http_spec.rb +47 -0
- data/spec/faraday/adapter/em_synchrony_spec.rb +16 -0
- data/spec/faraday/adapter/excon_spec.rb +49 -0
- data/spec/faraday/adapter/httpclient_spec.rb +73 -0
- data/spec/faraday/adapter/net_http_persistent_spec.rb +57 -0
- data/spec/faraday/adapter/net_http_spec.rb +64 -0
- data/spec/faraday/adapter/patron_spec.rb +18 -0
- data/spec/faraday/adapter/rack_spec.rb +8 -0
- data/spec/faraday/adapter/test_spec.rb +260 -0
- data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
- data/spec/faraday/adapter_registry_spec.rb +28 -0
- data/spec/faraday/adapter_spec.rb +55 -0
- data/spec/faraday/composite_read_io_spec.rb +80 -0
- data/spec/faraday/connection_spec.rb +691 -0
- data/spec/faraday/error_spec.rb +0 -57
- data/spec/faraday/middleware_spec.rb +26 -0
- data/spec/faraday/options/env_spec.rb +70 -0
- data/spec/faraday/options/options_spec.rb +297 -0
- data/spec/faraday/options/proxy_options_spec.rb +37 -0
- data/spec/faraday/options/request_options_spec.rb +19 -0
- data/spec/faraday/params_encoders/flat_spec.rb +42 -0
- data/spec/faraday/params_encoders/nested_spec.rb +142 -0
- data/spec/faraday/rack_builder_spec.rb +345 -0
- data/spec/faraday/request/authorization_spec.rb +88 -0
- data/spec/faraday/request/instrumentation_spec.rb +76 -0
- data/spec/faraday/request/multipart_spec.rb +302 -0
- data/spec/faraday/request/retry_spec.rb +242 -0
- data/spec/faraday/request/url_encoded_spec.rb +83 -0
- data/spec/faraday/request_spec.rb +120 -0
- data/spec/faraday/response/logger_spec.rb +220 -0
- data/spec/faraday/response/middleware_spec.rb +68 -0
- data/spec/faraday/response/raise_error_spec.rb +60 -16
- data/spec/faraday/response_spec.rb +75 -0
- data/spec/faraday/utils/headers_spec.rb +82 -0
- data/spec/faraday/utils_spec.rb +56 -0
- data/spec/faraday_spec.rb +37 -0
- data/spec/spec_helper.rb +63 -35
- data/spec/support/disabling_stub.rb +14 -0
- data/spec/support/fake_safe_buffer.rb +15 -0
- data/spec/support/helper_methods.rb +133 -0
- data/spec/support/shared_examples/adapter.rb +104 -0
- data/spec/support/shared_examples/params_encoder.rb +18 -0
- data/spec/support/shared_examples/request_method.rb +234 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- data/spec/support/webmock_rack_app.rb +68 -0
- metadata +80 -37
- data/lib/faraday/deprecate.rb +0 -101
- data/lib/faraday/upload_io.rb +0 -67
- data/spec/faraday/deprecate_spec.rb +0 -69
- data/test/adapters/default_test.rb +0 -14
- data/test/adapters/em_http_test.rb +0 -30
- data/test/adapters/em_synchrony_test.rb +0 -32
- data/test/adapters/excon_test.rb +0 -30
- data/test/adapters/httpclient_test.rb +0 -34
- data/test/adapters/integration.rb +0 -263
- data/test/adapters/logger_test.rb +0 -136
- data/test/adapters/net_http_persistent_test.rb +0 -114
- data/test/adapters/net_http_test.rb +0 -79
- data/test/adapters/patron_test.rb +0 -40
- data/test/adapters/rack_test.rb +0 -38
- data/test/adapters/test_middleware_test.rb +0 -157
- data/test/adapters/typhoeus_test.rb +0 -38
- data/test/authentication_middleware_test.rb +0 -65
- data/test/composite_read_io_test.rb +0 -109
- data/test/connection_test.rb +0 -738
- data/test/env_test.rb +0 -268
- data/test/helper.rb +0 -75
- data/test/live_server.rb +0 -67
- data/test/middleware/instrumentation_test.rb +0 -88
- data/test/middleware/retry_test.rb +0 -282
- data/test/middleware_stack_test.rb +0 -260
- data/test/multibyte.txt +0 -1
- data/test/options_test.rb +0 -333
- data/test/parameters_test.rb +0 -157
- data/test/request_middleware_test.rb +0 -126
- data/test/response_middleware_test.rb +0 -72
- data/test/strawberry.rb +0 -2
- data/test/utils_test.rb +0 -98
@@ -0,0 +1,105 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'pp'
|
4
|
+
module Faraday
|
5
|
+
module Logging
|
6
|
+
# Serves as an integration point to customize logging
|
7
|
+
class Formatter
|
8
|
+
extend Forwardable
|
9
|
+
|
10
|
+
DEFAULT_OPTIONS = { headers: true, bodies: false,
|
11
|
+
log_level: :info }.freeze
|
12
|
+
|
13
|
+
def initialize(logger:, options:)
|
14
|
+
@logger = logger
|
15
|
+
@filter = []
|
16
|
+
@options = DEFAULT_OPTIONS.merge(options)
|
17
|
+
end
|
18
|
+
|
19
|
+
def_delegators :@logger, :debug, :info, :warn, :error, :fatal
|
20
|
+
|
21
|
+
def request(env)
|
22
|
+
request_log = proc do
|
23
|
+
"#{env.method.upcase} #{apply_filters(env.url.to_s)}"
|
24
|
+
end
|
25
|
+
public_send(log_level, 'request', &request_log)
|
26
|
+
|
27
|
+
log_headers('request', env.request_headers) if log_headers?(:request)
|
28
|
+
log_body('request', env[:body]) if env[:body] && log_body?(:request)
|
29
|
+
end
|
30
|
+
|
31
|
+
def response(env)
|
32
|
+
status = proc { "Status #{env.status}" }
|
33
|
+
public_send(log_level, 'response', &status)
|
34
|
+
|
35
|
+
log_headers('response', env.response_headers) if log_headers?(:response)
|
36
|
+
log_body('response', env[:body]) if env[:body] && log_body?(:response)
|
37
|
+
end
|
38
|
+
|
39
|
+
def filter(filter_word, filter_replacement)
|
40
|
+
@filter.push([filter_word, filter_replacement])
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def dump_headers(headers)
|
46
|
+
headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n")
|
47
|
+
end
|
48
|
+
|
49
|
+
def dump_body(body)
|
50
|
+
if body.respond_to?(:to_str)
|
51
|
+
body.to_str
|
52
|
+
else
|
53
|
+
pretty_inspect(body)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def pretty_inspect(body)
|
58
|
+
body.pretty_inspect
|
59
|
+
end
|
60
|
+
|
61
|
+
def log_headers?(type)
|
62
|
+
case @options[:headers]
|
63
|
+
when Hash
|
64
|
+
@options[:headers][type]
|
65
|
+
else
|
66
|
+
@options[:headers]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def log_body?(type)
|
71
|
+
case @options[:bodies]
|
72
|
+
when Hash
|
73
|
+
@options[:bodies][type]
|
74
|
+
else
|
75
|
+
@options[:bodies]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def apply_filters(output)
|
80
|
+
@filter.each do |pattern, replacement|
|
81
|
+
output = output.to_s.gsub(pattern, replacement)
|
82
|
+
end
|
83
|
+
output
|
84
|
+
end
|
85
|
+
|
86
|
+
def log_level
|
87
|
+
unless %i[debug info warn error fatal].include?(@options[:log_level])
|
88
|
+
return :info
|
89
|
+
end
|
90
|
+
|
91
|
+
@options[:log_level]
|
92
|
+
end
|
93
|
+
|
94
|
+
def log_headers(type, headers)
|
95
|
+
headers_log = proc { apply_filters(dump_headers(headers)) }
|
96
|
+
public_send(log_level, type, &headers_log)
|
97
|
+
end
|
98
|
+
|
99
|
+
def log_body(type, body)
|
100
|
+
body_log = proc { apply_filters(dump_body(body)) }
|
101
|
+
public_send(log_level, type, &body_log)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
data/lib/faraday/middleware.rb
CHANGED
@@ -1,37 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Faraday
|
4
|
+
# Middleware is the basic base class of any Faraday middleware.
|
2
5
|
class Middleware
|
3
6
|
extend MiddlewareRegistry
|
4
|
-
|
5
|
-
class << self
|
6
|
-
attr_accessor :load_error
|
7
|
-
private :load_error=
|
8
|
-
end
|
9
|
-
|
10
|
-
self.load_error = nil
|
11
|
-
|
12
|
-
# Executes a block which should try to require and reference dependent libraries
|
13
|
-
def self.dependency(lib = nil)
|
14
|
-
lib ? require(lib) : yield
|
15
|
-
rescue LoadError, NameError => error
|
16
|
-
self.load_error = error
|
17
|
-
end
|
18
|
-
|
19
|
-
def self.new(*)
|
20
|
-
raise "missing dependency for #{self}: #{load_error.message}" unless loaded?
|
21
|
-
super
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.loaded?
|
25
|
-
load_error.nil?
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.inherited(subclass)
|
29
|
-
super
|
30
|
-
subclass.send(:load_error=, self.load_error)
|
31
|
-
end
|
7
|
+
extend DependencyLoader
|
32
8
|
|
33
9
|
def initialize(app = nil)
|
34
10
|
@app = app
|
35
11
|
end
|
12
|
+
|
13
|
+
def close
|
14
|
+
if @app.respond_to?(:close)
|
15
|
+
@app.close
|
16
|
+
else
|
17
|
+
warn "#{@app} does not implement \#close!"
|
18
|
+
end
|
19
|
+
end
|
36
20
|
end
|
37
21
|
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'monitor'
|
4
|
+
|
5
|
+
module Faraday
|
6
|
+
# Adds the ability for other modules to register and lookup
|
7
|
+
# middleware classes.
|
8
|
+
module MiddlewareRegistry
|
9
|
+
# Register middleware class(es) on the current module.
|
10
|
+
#
|
11
|
+
# @param autoload_path [String] Middleware autoload path
|
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`.
|
23
|
+
# @return [void]
|
24
|
+
#
|
25
|
+
# @example Lookup by a constant
|
26
|
+
#
|
27
|
+
# module Faraday
|
28
|
+
# class Whatever
|
29
|
+
# # Middleware looked up by :foo returns Faraday::Whatever::Foo.
|
30
|
+
# register_middleware foo: Foo
|
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']
|
51
|
+
# end
|
52
|
+
# 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
|
59
|
+
middleware_mutex do
|
60
|
+
@middleware_autoload_path = autoload_path if autoload_path
|
61
|
+
(@registered_middleware ||= {}).update(mapping)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Unregister a previously registered middleware class.
|
66
|
+
#
|
67
|
+
# @param key [Symbol] key for the registered middleware.
|
68
|
+
def unregister_middleware(key)
|
69
|
+
@registered_middleware.delete(key)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Lookup middleware class with a registered Symbol shortcut.
|
73
|
+
#
|
74
|
+
# @param key [Symbol] key for the registered middleware.
|
75
|
+
# @return [Class] a middleware Class.
|
76
|
+
# @raise [Faraday::Error] if given key is not registered
|
77
|
+
#
|
78
|
+
# @example
|
79
|
+
#
|
80
|
+
# module Faraday
|
81
|
+
# class Whatever
|
82
|
+
# register_middleware foo: Foo
|
83
|
+
# end
|
84
|
+
# end
|
85
|
+
#
|
86
|
+
# Faraday::Whatever.lookup_middleware(:foo)
|
87
|
+
# # => Faraday::Whatever::Foo
|
88
|
+
#
|
89
|
+
def lookup_middleware(key)
|
90
|
+
load_middleware(key) ||
|
91
|
+
raise(Faraday::Error, "#{key.inspect} is not registered on #{self}")
|
92
|
+
end
|
93
|
+
|
94
|
+
def middleware_mutex(&block)
|
95
|
+
@middleware_mutex ||= Monitor.new
|
96
|
+
@middleware_mutex.synchronize(&block)
|
97
|
+
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
|
+
end
|
129
|
+
end
|
data/lib/faraday/options.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Faraday
|
2
4
|
# Subclasses Struct with some special helpers for converting from a Hash to
|
3
5
|
# a Struct.
|
@@ -10,6 +12,7 @@ module Faraday
|
|
10
12
|
# Public
|
11
13
|
def each
|
12
14
|
return to_enum(:each) unless block_given?
|
15
|
+
|
13
16
|
members.each do |key|
|
14
17
|
yield(key.to_sym, send(key))
|
15
18
|
end
|
@@ -27,7 +30,7 @@ module Faraday
|
|
27
30
|
new_value = value
|
28
31
|
end
|
29
32
|
|
30
|
-
|
33
|
+
send("#{key}=", new_value) unless new_value.nil?
|
31
34
|
end
|
32
35
|
self
|
33
36
|
end
|
@@ -47,10 +50,14 @@ module Faraday
|
|
47
50
|
# Public
|
48
51
|
def merge!(other)
|
49
52
|
other.each do |key, other_value|
|
50
|
-
self_value =
|
53
|
+
self_value = send(key)
|
51
54
|
sub_options = self.class.options_for(key)
|
52
|
-
new_value =
|
53
|
-
|
55
|
+
new_value = if self_value && sub_options && other_value
|
56
|
+
self_value.merge(other_value)
|
57
|
+
else
|
58
|
+
other_value
|
59
|
+
end
|
60
|
+
send("#{key}=", new_value) unless new_value.nil?
|
54
61
|
end
|
55
62
|
self
|
56
63
|
end
|
@@ -69,7 +76,7 @@ module Faraday
|
|
69
76
|
def fetch(key, *args)
|
70
77
|
unless symbolized_key_set.include?(key.to_sym)
|
71
78
|
key_setter = "#{key}="
|
72
|
-
if args.
|
79
|
+
if !args.empty?
|
73
80
|
send(key_setter, args.first)
|
74
81
|
elsif block_given?
|
75
82
|
send(key_setter, yield(key))
|
@@ -96,11 +103,10 @@ module Faraday
|
|
96
103
|
end
|
97
104
|
|
98
105
|
# Public
|
99
|
-
def each_key
|
106
|
+
def each_key(&block)
|
100
107
|
return to_enum(:each_key) unless block_given?
|
101
|
-
|
102
|
-
|
103
|
-
end
|
108
|
+
|
109
|
+
keys.each(&block)
|
104
110
|
end
|
105
111
|
|
106
112
|
# Public
|
@@ -111,11 +117,10 @@ module Faraday
|
|
111
117
|
alias has_key? key?
|
112
118
|
|
113
119
|
# Public
|
114
|
-
def each_value
|
120
|
+
def each_value(&block)
|
115
121
|
return to_enum(:each_value) unless block_given?
|
116
|
-
|
117
|
-
|
118
|
-
end
|
122
|
+
|
123
|
+
values.each(&block)
|
119
124
|
end
|
120
125
|
|
121
126
|
# Public
|
@@ -142,9 +147,9 @@ module Faraday
|
|
142
147
|
value = send(member)
|
143
148
|
values << "#{member}=#{value.inspect}" if value
|
144
149
|
end
|
145
|
-
values = values.empty? ? '
|
150
|
+
values = values.empty? ? '(empty)' : values.join(', ')
|
146
151
|
|
147
|
-
%(#<#{self.class}#{values}>)
|
152
|
+
%(#<#{self.class} #{values}>)
|
148
153
|
end
|
149
154
|
|
150
155
|
# Internal
|
@@ -162,8 +167,12 @@ module Faraday
|
|
162
167
|
@attribute_options ||= {}
|
163
168
|
end
|
164
169
|
|
165
|
-
def self.memoized(key)
|
166
|
-
|
170
|
+
def self.memoized(key, &block)
|
171
|
+
unless block_given?
|
172
|
+
raise ArgumentError, '#memoized must be called with a block'
|
173
|
+
end
|
174
|
+
|
175
|
+
memoized_attributes[key.to_sym] = block
|
167
176
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
168
177
|
def #{key}() self[:#{key}]; end
|
169
178
|
RUBY
|
@@ -175,7 +184,7 @@ module Faraday
|
|
175
184
|
|
176
185
|
def [](key)
|
177
186
|
key = key.to_sym
|
178
|
-
if method = self.class.memoized_attributes[key]
|
187
|
+
if (method = self.class.memoized_attributes[key])
|
179
188
|
super(key) || (self[key] = instance_eval(&method))
|
180
189
|
else
|
181
190
|
super
|
@@ -183,7 +192,7 @@ module Faraday
|
|
183
192
|
end
|
184
193
|
|
185
194
|
def symbolized_key_set
|
186
|
-
@symbolized_key_set ||= Set.new(keys.map
|
195
|
+
@symbolized_key_set ||= Set.new(keys.map(&:to_sym))
|
187
196
|
end
|
188
197
|
|
189
198
|
def self.inherited(subclass)
|
@@ -194,180 +203,16 @@ module Faraday
|
|
194
203
|
|
195
204
|
def self.fetch_error_class
|
196
205
|
@fetch_error_class ||= if Object.const_defined?(:KeyError)
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
class RequestOptions < Options.new(:params_encoder, :proxy, :bind,
|
205
|
-
:timeout, :open_timeout, :write_timeout, :boundary, :oauth, :context)
|
206
|
-
|
207
|
-
def []=(key, value)
|
208
|
-
if key && key.to_sym == :proxy
|
209
|
-
super(key, value ? ProxyOptions.from(value) : nil)
|
210
|
-
else
|
211
|
-
super(key, value)
|
212
|
-
end
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
|
-
class SSLOptions < Options.new(:verify, :ca_file, :ca_path, :verify_mode,
|
217
|
-
:cert_store, :client_cert, :client_key, :certificate, :private_key, :verify_depth,
|
218
|
-
:version, :min_version, :max_version)
|
219
|
-
|
220
|
-
def verify?
|
221
|
-
verify != false
|
222
|
-
end
|
223
|
-
|
224
|
-
def disable?
|
225
|
-
!verify?
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
class ProxyOptions < Options.new(:uri, :user, :password)
|
230
|
-
extend Forwardable
|
231
|
-
def_delegators :uri, :scheme, :scheme=, :host, :host=, :port, :port=, :path, :path=
|
232
|
-
|
233
|
-
def self.from(value)
|
234
|
-
case value
|
235
|
-
when String
|
236
|
-
value = {:uri => Utils.URI(value)}
|
237
|
-
when URI
|
238
|
-
value = {:uri => value}
|
239
|
-
when Hash, Options
|
240
|
-
if uri = value.delete(:uri)
|
241
|
-
value[:uri] = Utils.URI(uri)
|
242
|
-
end
|
243
|
-
end
|
244
|
-
super(value)
|
245
|
-
end
|
246
|
-
|
247
|
-
memoized(:user) { uri && uri.user && Utils.unescape(uri.user) }
|
248
|
-
memoized(:password) { uri && uri.password && Utils.unescape(uri.password) }
|
249
|
-
end
|
250
|
-
|
251
|
-
class ConnectionOptions < Options.new(:request, :proxy, :ssl, :builder, :url,
|
252
|
-
:parallel_manager, :params, :headers, :builder_class)
|
253
|
-
|
254
|
-
options :request => RequestOptions, :ssl => SSLOptions
|
255
|
-
|
256
|
-
memoized(:request) { self.class.options_for(:request).new }
|
257
|
-
|
258
|
-
memoized(:ssl) { self.class.options_for(:ssl).new }
|
259
|
-
|
260
|
-
memoized(:builder_class) { RackBuilder }
|
261
|
-
|
262
|
-
def new_builder(block)
|
263
|
-
builder_class.new(&block)
|
264
|
-
end
|
265
|
-
end
|
266
|
-
|
267
|
-
class Env < Options.new(:method, :body, :url, :request, :request_headers,
|
268
|
-
:ssl, :parallel_manager, :params, :response, :response_headers, :status,
|
269
|
-
:reason_phrase)
|
270
|
-
|
271
|
-
ContentLength = 'Content-Length'.freeze
|
272
|
-
StatusesWithoutBody = Set.new [204, 304]
|
273
|
-
SuccessfulStatuses = 200..299
|
274
|
-
|
275
|
-
# A Set of HTTP verbs that typically send a body. If no body is set for
|
276
|
-
# these requests, the Content-Length header is set to 0.
|
277
|
-
MethodsWithBodies = Set.new [:post, :put, :patch, :options]
|
278
|
-
|
279
|
-
options :request => RequestOptions,
|
280
|
-
:request_headers => Utils::Headers, :response_headers => Utils::Headers
|
281
|
-
|
282
|
-
extend Forwardable
|
283
|
-
|
284
|
-
def_delegators :request, :params_encoder
|
285
|
-
|
286
|
-
# Public
|
287
|
-
def self.from(value)
|
288
|
-
env = super(value)
|
289
|
-
if value.respond_to?(:custom_members)
|
290
|
-
env.custom_members.update(value.custom_members)
|
291
|
-
end
|
292
|
-
env
|
293
|
-
end
|
294
|
-
|
295
|
-
# Public
|
296
|
-
def [](key)
|
297
|
-
if in_member_set?(key)
|
298
|
-
super(key)
|
299
|
-
else
|
300
|
-
custom_members[key]
|
301
|
-
end
|
302
|
-
end
|
303
|
-
|
304
|
-
# Public
|
305
|
-
def []=(key, value)
|
306
|
-
if in_member_set?(key)
|
307
|
-
super(key, value)
|
308
|
-
else
|
309
|
-
custom_members[key] = value
|
310
|
-
end
|
311
|
-
end
|
312
|
-
|
313
|
-
# Public
|
314
|
-
def success?
|
315
|
-
SuccessfulStatuses.include?(status)
|
316
|
-
end
|
317
|
-
|
318
|
-
# Public
|
319
|
-
def needs_body?
|
320
|
-
!body && MethodsWithBodies.include?(method)
|
321
|
-
end
|
322
|
-
|
323
|
-
# Public
|
324
|
-
def clear_body
|
325
|
-
request_headers[ContentLength] = '0'
|
326
|
-
self.body = ''
|
327
|
-
end
|
328
|
-
|
329
|
-
# Public
|
330
|
-
def parse_body?
|
331
|
-
!StatusesWithoutBody.include?(status)
|
332
|
-
end
|
333
|
-
|
334
|
-
# Public
|
335
|
-
def parallel?
|
336
|
-
!!parallel_manager
|
337
|
-
end
|
338
|
-
|
339
|
-
def inspect
|
340
|
-
attrs = [nil]
|
341
|
-
members.each do |mem|
|
342
|
-
if value = send(mem)
|
343
|
-
attrs << "@#{mem}=#{value.inspect}"
|
344
|
-
end
|
345
|
-
end
|
346
|
-
if !custom_members.empty?
|
347
|
-
attrs << "@custom=#{custom_members.inspect}"
|
348
|
-
end
|
349
|
-
%(#<#{self.class}#{attrs.join(" ")}>)
|
350
|
-
end
|
351
|
-
|
352
|
-
# Internal
|
353
|
-
def custom_members
|
354
|
-
@custom_members ||= {}
|
355
|
-
end
|
356
|
-
|
357
|
-
# Internal
|
358
|
-
if members.first.is_a?(Symbol)
|
359
|
-
def in_member_set?(key)
|
360
|
-
self.class.member_set.include?(key.to_sym)
|
361
|
-
end
|
362
|
-
else
|
363
|
-
def in_member_set?(key)
|
364
|
-
self.class.member_set.include?(key.to_s)
|
365
|
-
end
|
366
|
-
end
|
367
|
-
|
368
|
-
# Internal
|
369
|
-
def self.member_set
|
370
|
-
@member_set ||= Set.new(members)
|
206
|
+
::KeyError
|
207
|
+
else
|
208
|
+
::IndexError
|
209
|
+
end
|
371
210
|
end
|
372
211
|
end
|
373
212
|
end
|
213
|
+
|
214
|
+
require 'faraday/options/request_options'
|
215
|
+
require 'faraday/options/ssl_options'
|
216
|
+
require 'faraday/options/proxy_options'
|
217
|
+
require 'faraday/options/connection_options'
|
218
|
+
require 'faraday/options/env'
|