faraday 0.17.6 → 1.0.0.pre.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.md +1 -1
- data/README.md +18 -358
- data/lib/faraday/adapter/em_http.rb +142 -99
- data/lib/faraday/adapter/em_http_ssl_patch.rb +23 -17
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
- data/lib/faraday/adapter/em_synchrony.rb +104 -60
- data/lib/faraday/adapter/excon.rb +97 -57
- data/lib/faraday/adapter/httpclient.rb +61 -39
- data/lib/faraday/adapter/net_http.rb +103 -51
- data/lib/faraday/adapter/net_http_persistent.rb +49 -28
- data/lib/faraday/adapter/patron.rb +54 -35
- data/lib/faraday/adapter/rack.rb +28 -12
- data/lib/faraday/adapter/test.rb +86 -53
- data/lib/faraday/adapter/typhoeus.rb +4 -1
- data/lib/faraday/adapter.rb +36 -22
- data/lib/faraday/adapter_registry.rb +28 -0
- data/lib/faraday/autoload.rb +47 -36
- data/lib/faraday/connection.rb +321 -179
- data/lib/faraday/dependency_loader.rb +37 -0
- data/lib/faraday/encoders/flat_params_encoder.rb +94 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +171 -0
- data/lib/faraday/error.rb +21 -79
- data/lib/faraday/logging/formatter.rb +92 -0
- data/lib/faraday/middleware.rb +4 -28
- data/lib/faraday/middleware_registry.rb +129 -0
- 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 +21 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/options.rb +33 -184
- data/lib/faraday/parameters.rb +4 -197
- data/lib/faraday/rack_builder.rb +66 -55
- data/lib/faraday/request/authorization.rb +42 -30
- data/lib/faraday/request/basic_authentication.rb +14 -7
- data/lib/faraday/request/instrumentation.rb +45 -27
- data/lib/faraday/request/multipart.rb +72 -49
- data/lib/faraday/request/retry.rb +197 -171
- data/lib/faraday/request/token_authentication.rb +15 -10
- data/lib/faraday/request/url_encoded.rb +41 -23
- data/lib/faraday/request.rb +68 -38
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +36 -18
- data/lib/faraday/response.rb +22 -15
- data/lib/faraday/upload_io.rb +31 -30
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +28 -245
- data/lib/faraday.rb +93 -174
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- metadata +25 -51
- data/CHANGELOG.md +0 -232
- data/Rakefile +0 -13
- data/lib/faraday/deprecate.rb +0 -109
- data/spec/faraday/deprecate_spec.rb +0 -147
- data/spec/faraday/error_spec.rb +0 -102
- data/spec/faraday/response/raise_error_spec.rb +0 -106
- data/spec/spec_helper.rb +0 -105
- 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
data/lib/faraday/parameters.rb
CHANGED
@@ -1,198 +1,5 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
extend Forwardable
|
7
|
-
def_delegators :'Faraday::Utils', :escape, :unescape
|
8
|
-
end
|
9
|
-
|
10
|
-
def self.encode(params)
|
11
|
-
return nil if params == nil
|
12
|
-
|
13
|
-
if !params.is_a?(Array)
|
14
|
-
if !params.respond_to?(:to_hash)
|
15
|
-
raise TypeError,
|
16
|
-
"Can't convert #{params.class} into Hash."
|
17
|
-
end
|
18
|
-
params = params.to_hash
|
19
|
-
params = params.map do |key, value|
|
20
|
-
key = key.to_s if key.kind_of?(Symbol)
|
21
|
-
[key, value]
|
22
|
-
end
|
23
|
-
# Useful default for OAuth and caching.
|
24
|
-
# Only to be used for non-Array inputs. Arrays should preserve order.
|
25
|
-
params.sort!
|
26
|
-
end
|
27
|
-
|
28
|
-
# Helper lambda
|
29
|
-
to_query = lambda do |parent, value|
|
30
|
-
if value.is_a?(Hash)
|
31
|
-
value = value.map do |key, val|
|
32
|
-
key = escape(key)
|
33
|
-
[key, val]
|
34
|
-
end
|
35
|
-
value.sort!
|
36
|
-
buffer = ""
|
37
|
-
value.each do |key, val|
|
38
|
-
new_parent = "#{parent}%5B#{key}%5D"
|
39
|
-
buffer << "#{to_query.call(new_parent, val)}&"
|
40
|
-
end
|
41
|
-
return buffer.chop
|
42
|
-
elsif value.is_a?(Array)
|
43
|
-
new_parent = "#{parent}%5B%5D"
|
44
|
-
return new_parent if value.empty?
|
45
|
-
buffer = ""
|
46
|
-
value.each_with_index do |val, i|
|
47
|
-
buffer << "#{to_query.call(new_parent, val)}&"
|
48
|
-
end
|
49
|
-
return buffer.chop
|
50
|
-
elsif value.nil?
|
51
|
-
return parent
|
52
|
-
else
|
53
|
-
encoded_value = escape(value)
|
54
|
-
return "#{parent}=#{encoded_value}"
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
# The params have form [['key1', 'value1'], ['key2', 'value2']].
|
59
|
-
buffer = ''
|
60
|
-
params.each do |parent, value|
|
61
|
-
encoded_parent = escape(parent)
|
62
|
-
buffer << "#{to_query.call(encoded_parent, value)}&"
|
63
|
-
end
|
64
|
-
return buffer.chop
|
65
|
-
end
|
66
|
-
|
67
|
-
def self.decode(query)
|
68
|
-
return nil if query == nil
|
69
|
-
|
70
|
-
params = {}
|
71
|
-
query.split("&").each do |pair|
|
72
|
-
next if pair.empty?
|
73
|
-
key, value = pair.split("=", 2)
|
74
|
-
key = unescape(key)
|
75
|
-
value = unescape(value.gsub(/\+/, ' ')) if value
|
76
|
-
|
77
|
-
subkeys = key.scan(/[^\[\]]+(?:\]?\[\])?/)
|
78
|
-
context = params
|
79
|
-
subkeys.each_with_index do |subkey, i|
|
80
|
-
is_array = subkey =~ /[\[\]]+\Z/
|
81
|
-
subkey = $` if is_array
|
82
|
-
last_subkey = i == subkeys.length - 1
|
83
|
-
|
84
|
-
if !last_subkey || is_array
|
85
|
-
value_type = is_array ? Array : Hash
|
86
|
-
if context[subkey] && !context[subkey].is_a?(value_type)
|
87
|
-
raise TypeError, "expected %s (got %s) for param `%s'" % [
|
88
|
-
value_type.name,
|
89
|
-
context[subkey].class.name,
|
90
|
-
subkey
|
91
|
-
]
|
92
|
-
end
|
93
|
-
context = (context[subkey] ||= value_type.new)
|
94
|
-
end
|
95
|
-
|
96
|
-
if context.is_a?(Array) && !is_array
|
97
|
-
if !context.last.is_a?(Hash) || context.last.has_key?(subkey)
|
98
|
-
context << {}
|
99
|
-
end
|
100
|
-
context = context.last
|
101
|
-
end
|
102
|
-
|
103
|
-
if last_subkey
|
104
|
-
if is_array
|
105
|
-
context << value
|
106
|
-
else
|
107
|
-
context[subkey] = value
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
dehash(params, 0)
|
114
|
-
end
|
115
|
-
|
116
|
-
# Internal: convert a nested hash with purely numeric keys into an array.
|
117
|
-
# FIXME: this is not compatible with Rack::Utils.parse_nested_query
|
118
|
-
def self.dehash(hash, depth)
|
119
|
-
hash.each do |key, value|
|
120
|
-
hash[key] = dehash(value, depth + 1) if value.kind_of?(Hash)
|
121
|
-
end
|
122
|
-
|
123
|
-
if depth > 0 && !hash.empty? && hash.keys.all? { |k| k =~ /^\d+$/ }
|
124
|
-
hash.keys.sort.inject([]) { |all, key| all << hash[key] }
|
125
|
-
else
|
126
|
-
hash
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
module FlatParamsEncoder
|
132
|
-
class << self
|
133
|
-
extend Forwardable
|
134
|
-
def_delegators :'Faraday::Utils', :escape, :unescape
|
135
|
-
end
|
136
|
-
|
137
|
-
def self.encode(params)
|
138
|
-
return nil if params == nil
|
139
|
-
|
140
|
-
if !params.is_a?(Array)
|
141
|
-
if !params.respond_to?(:to_hash)
|
142
|
-
raise TypeError,
|
143
|
-
"Can't convert #{params.class} into Hash."
|
144
|
-
end
|
145
|
-
params = params.to_hash
|
146
|
-
params = params.map do |key, value|
|
147
|
-
key = key.to_s if key.kind_of?(Symbol)
|
148
|
-
[key, value]
|
149
|
-
end
|
150
|
-
# Useful default for OAuth and caching.
|
151
|
-
# Only to be used for non-Array inputs. Arrays should preserve order.
|
152
|
-
params.sort!
|
153
|
-
end
|
154
|
-
|
155
|
-
# The params have form [['key1', 'value1'], ['key2', 'value2']].
|
156
|
-
buffer = ''
|
157
|
-
params.each do |key, value|
|
158
|
-
encoded_key = escape(key)
|
159
|
-
value = value.to_s if value == true || value == false
|
160
|
-
if value == nil
|
161
|
-
buffer << "#{encoded_key}&"
|
162
|
-
elsif value.kind_of?(Array)
|
163
|
-
value.each do |sub_value|
|
164
|
-
encoded_value = escape(sub_value)
|
165
|
-
buffer << "#{encoded_key}=#{encoded_value}&"
|
166
|
-
end
|
167
|
-
else
|
168
|
-
encoded_value = escape(value)
|
169
|
-
buffer << "#{encoded_key}=#{encoded_value}&"
|
170
|
-
end
|
171
|
-
end
|
172
|
-
return buffer.chop
|
173
|
-
end
|
174
|
-
|
175
|
-
def self.decode(query)
|
176
|
-
empty_accumulator = {}
|
177
|
-
return nil if query == nil
|
178
|
-
split_query = (query.split('&').map do |pair|
|
179
|
-
pair.split('=', 2) if pair && !pair.empty?
|
180
|
-
end).compact
|
181
|
-
return split_query.inject(empty_accumulator.dup) do |accu, pair|
|
182
|
-
pair[0] = unescape(pair[0])
|
183
|
-
pair[1] = true if pair[1].nil?
|
184
|
-
if pair[1].respond_to?(:to_str)
|
185
|
-
pair[1] = unescape(pair[1].to_str.gsub(/\+/, " "))
|
186
|
-
end
|
187
|
-
if accu[pair[0]].kind_of?(Array)
|
188
|
-
accu[pair[0]] << pair[1]
|
189
|
-
elsif accu[pair[0]]
|
190
|
-
accu[pair[0]] = [accu[pair[0]], pair[1]]
|
191
|
-
else
|
192
|
-
accu[pair[0]] = pair[1]
|
193
|
-
end
|
194
|
-
accu
|
195
|
-
end
|
196
|
-
end
|
197
|
-
end
|
198
|
-
end
|
3
|
+
require 'forwardable'
|
4
|
+
require 'faraday/encoders/nested_params_encoder'
|
5
|
+
require 'faraday/encoders/flat_params_encoder'
|
data/lib/faraday/rack_builder.rb
CHANGED
@@ -1,12 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'faraday/adapter_registry'
|
4
|
+
|
1
5
|
module Faraday
|
2
6
|
# A Builder that processes requests into responses by passing through an inner
|
3
7
|
# middleware stack (heavily inspired by Rack).
|
4
8
|
#
|
5
|
-
#
|
9
|
+
# @example
|
10
|
+
# Faraday::Connection.new(url: 'http://sushi.com') do |builder|
|
6
11
|
# builder.request :url_encoded # Faraday::Request::UrlEncoded
|
7
12
|
# builder.adapter :net_http # Faraday::Adapter::NetHttp
|
8
13
|
# end
|
9
14
|
class RackBuilder
|
15
|
+
# Used to detect missing arguments
|
16
|
+
NO_ARGUMENT = Object.new
|
17
|
+
|
10
18
|
attr_accessor :handlers
|
11
19
|
|
12
20
|
# Error raised when trying to modify the stack after calling `lock!`
|
@@ -15,28 +23,28 @@ module Faraday
|
|
15
23
|
# borrowed from ActiveSupport::Dependencies::Reference &
|
16
24
|
# ActionDispatch::MiddlewareStack::Middleware
|
17
25
|
class Handler
|
18
|
-
|
19
|
-
@@constants = Hash.new { |h, k|
|
20
|
-
value = k.respond_to?(:constantize) ? k.constantize : Object.const_get(k)
|
21
|
-
@@constants_mutex.synchronize { h[k] = value }
|
22
|
-
}
|
26
|
+
REGISTRY = Faraday::AdapterRegistry.new
|
23
27
|
|
24
28
|
attr_reader :name
|
25
29
|
|
26
30
|
def initialize(klass, *args, &block)
|
27
31
|
@name = klass.to_s
|
28
|
-
if klass.respond_to?(:name)
|
29
|
-
|
30
|
-
|
31
|
-
@args, @block = args, block
|
32
|
+
REGISTRY.set(klass) if klass.respond_to?(:name)
|
33
|
+
@args = args
|
34
|
+
@block = block
|
32
35
|
end
|
33
36
|
|
34
|
-
def klass
|
35
|
-
|
37
|
+
def klass
|
38
|
+
REGISTRY.get(@name)
|
39
|
+
end
|
40
|
+
|
41
|
+
def inspect
|
42
|
+
@name
|
43
|
+
end
|
36
44
|
|
37
45
|
def ==(other)
|
38
46
|
if other.is_a? Handler
|
39
|
-
|
47
|
+
name == other.name
|
40
48
|
elsif other.respond_to? :name
|
41
49
|
klass == other
|
42
50
|
else
|
@@ -44,18 +52,19 @@ module Faraday
|
|
44
52
|
end
|
45
53
|
end
|
46
54
|
|
47
|
-
def build(app)
|
55
|
+
def build(app = nil)
|
48
56
|
klass.new(app, *@args, &@block)
|
49
57
|
end
|
50
58
|
end
|
51
59
|
|
52
|
-
def initialize(handlers = [], &block)
|
60
|
+
def initialize(handlers = [], adapter = nil, &block)
|
61
|
+
@adapter = adapter
|
53
62
|
@handlers = handlers
|
54
63
|
if block_given?
|
55
64
|
build(&block)
|
56
65
|
elsif @handlers.empty?
|
57
66
|
# default stack, if nothing else is configured
|
58
|
-
|
67
|
+
request :url_encoded
|
59
68
|
self.adapter Faraday.default_adapter
|
60
69
|
end
|
61
70
|
end
|
@@ -64,13 +73,14 @@ module Faraday
|
|
64
73
|
raise_if_locked
|
65
74
|
@handlers.clear unless options[:keep]
|
66
75
|
yield(self) if block_given?
|
76
|
+
adapter(Faraday.default_adapter) unless @adapter
|
67
77
|
end
|
68
78
|
|
69
79
|
def [](idx)
|
70
80
|
@handlers[idx]
|
71
81
|
end
|
72
82
|
|
73
|
-
# Locks the middleware stack to ensure no further modifications are
|
83
|
+
# Locks the middleware stack to ensure no further modifications are made.
|
74
84
|
def lock!
|
75
85
|
@handlers.freeze
|
76
86
|
end
|
@@ -84,7 +94,7 @@ module Faraday
|
|
84
94
|
use_symbol(Faraday::Middleware, klass, *args, &block)
|
85
95
|
else
|
86
96
|
raise_if_locked
|
87
|
-
|
97
|
+
raise_if_adapter(klass)
|
88
98
|
@handlers << self.class::Handler.new(klass, *args, &block)
|
89
99
|
end
|
90
100
|
end
|
@@ -97,8 +107,11 @@ module Faraday
|
|
97
107
|
use_symbol(Faraday::Response, key, *args, &block)
|
98
108
|
end
|
99
109
|
|
100
|
-
def adapter(
|
101
|
-
|
110
|
+
def adapter(klass = NO_ARGUMENT, *args, &block)
|
111
|
+
return @adapter if klass == NO_ARGUMENT
|
112
|
+
|
113
|
+
klass = Faraday::Adapter.lookup_middleware(klass) if klass.is_a?(Symbol)
|
114
|
+
@adapter = self.class::Handler.new(klass, *args, &block)
|
102
115
|
end
|
103
116
|
|
104
117
|
## methods to push onto the various positions in the stack:
|
@@ -106,12 +119,11 @@ module Faraday
|
|
106
119
|
def insert(index, *args, &block)
|
107
120
|
raise_if_locked
|
108
121
|
index = assert_index(index)
|
109
|
-
warn_middleware_after_adapter if inserting_after_adapter?(index)
|
110
122
|
handler = self.class::Handler.new(*args, &block)
|
111
123
|
@handlers.insert(index, handler)
|
112
124
|
end
|
113
125
|
|
114
|
-
|
126
|
+
alias insert_before insert
|
115
127
|
|
116
128
|
def insert_after(index, *args, &block)
|
117
129
|
index = assert_index(index)
|
@@ -133,13 +145,11 @@ module Faraday
|
|
133
145
|
# Processes a Request into a Response by passing it through this Builder's
|
134
146
|
# middleware stack.
|
135
147
|
#
|
136
|
-
# connection
|
137
|
-
# request
|
148
|
+
# @param connection [Faraday::Connection]
|
149
|
+
# @param request [Faraday::Request]
|
138
150
|
#
|
139
|
-
#
|
151
|
+
# @return [Faraday::Response]
|
140
152
|
def build_response(connection, request)
|
141
|
-
warn 'WARNING: No adapter was configured for this request' unless adapter_set?
|
142
|
-
|
143
153
|
app.call(build_env(connection, request))
|
144
154
|
end
|
145
155
|
|
@@ -153,26 +163,26 @@ module Faraday
|
|
153
163
|
def app
|
154
164
|
@app ||= begin
|
155
165
|
lock!
|
156
|
-
to_app
|
157
|
-
response = Response.new
|
158
|
-
env.response = response
|
159
|
-
response.finish(env) unless env.parallel?
|
160
|
-
response
|
161
|
-
})
|
166
|
+
to_app
|
162
167
|
end
|
163
168
|
end
|
164
169
|
|
165
|
-
def to_app
|
170
|
+
def to_app
|
166
171
|
# last added handler is the deepest and thus closest to the inner app
|
167
|
-
|
172
|
+
# adapter is always the last one
|
173
|
+
@handlers.reverse.inject(@adapter.build) do |app, handler|
|
174
|
+
handler.build(app)
|
175
|
+
end
|
168
176
|
end
|
169
177
|
|
170
178
|
def ==(other)
|
171
|
-
other.is_a?(self.class) &&
|
179
|
+
other.is_a?(self.class) &&
|
180
|
+
@handlers == other.handlers &&
|
181
|
+
@adapter == other.adapter
|
172
182
|
end
|
173
183
|
|
174
184
|
def dup
|
175
|
-
self.class.new(@handlers.dup)
|
185
|
+
self.class.new(@handlers.dup, @adapter.dup)
|
176
186
|
end
|
177
187
|
|
178
188
|
# ENV Keys
|
@@ -192,36 +202,36 @@ module Faraday
|
|
192
202
|
# :password - Proxy server password
|
193
203
|
# :ssl - Hash of options for configuring SSL requests.
|
194
204
|
def build_env(connection, request)
|
195
|
-
|
196
|
-
|
197
|
-
request.options
|
198
|
-
|
205
|
+
exclusive_url = connection.build_exclusive_url(
|
206
|
+
request.path, request.params,
|
207
|
+
request.options.params_encoder
|
208
|
+
)
|
209
|
+
|
210
|
+
Env.new(request.method, request.body, exclusive_url,
|
211
|
+
request.options, request.headers, connection.ssl,
|
212
|
+
connection.parallel_manager)
|
199
213
|
end
|
200
214
|
|
201
215
|
private
|
202
216
|
|
217
|
+
LOCK_ERR = "can't modify middleware stack after making a request"
|
218
|
+
|
203
219
|
def raise_if_locked
|
204
|
-
raise StackLocked,
|
220
|
+
raise StackLocked, LOCK_ERR if locked?
|
205
221
|
end
|
206
222
|
|
207
|
-
def
|
208
|
-
|
209
|
-
"This won't be supported from Faraday 1.0."
|
210
|
-
end
|
223
|
+
def raise_if_adapter(klass)
|
224
|
+
return unless is_adapter?(klass)
|
211
225
|
|
212
|
-
|
213
|
-
@handlers.any? { |handler| is_adapter?(handler) }
|
226
|
+
raise 'Adapter should be set using the `adapter` method, not `use`'
|
214
227
|
end
|
215
228
|
|
216
|
-
def
|
217
|
-
|
218
|
-
return false if adapter_index.nil?
|
219
|
-
|
220
|
-
index > adapter_index
|
229
|
+
def adapter_set?
|
230
|
+
!@adapter.nil?
|
221
231
|
end
|
222
232
|
|
223
|
-
def is_adapter?(
|
224
|
-
|
233
|
+
def is_adapter?(klass) # rubocop:disable Naming/PredicateName
|
234
|
+
klass.ancestors.include?(Faraday::Adapter)
|
225
235
|
end
|
226
236
|
|
227
237
|
def use_symbol(mod, key, *args, &block)
|
@@ -231,6 +241,7 @@ module Faraday
|
|
231
241
|
def assert_index(index)
|
232
242
|
idx = index.is_a?(Integer) ? index : @handlers.index(index)
|
233
243
|
raise "No such handler: #{index.inspect}" unless idx
|
244
|
+
|
234
245
|
idx
|
235
246
|
end
|
236
247
|
end
|
@@ -1,41 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Faraday
|
2
|
-
class Request
|
3
|
-
|
4
|
+
class Request
|
5
|
+
# Request middleware for the Authorization HTTP header
|
6
|
+
class Authorization < Faraday::Middleware
|
7
|
+
KEY = 'Authorization' unless defined? KEY
|
4
8
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
# @param type [String, Symbol]
|
10
|
+
# @param token [String, Symbol, Hash]
|
11
|
+
# @return [String] a header value
|
12
|
+
def self.header(type, token)
|
13
|
+
case token
|
14
|
+
when String, Symbol
|
15
|
+
"#{type} #{token}"
|
16
|
+
when Hash
|
17
|
+
build_hash(type.to_s, token)
|
18
|
+
else
|
19
|
+
raise ArgumentError,
|
20
|
+
"Can't build an Authorization #{type}" \
|
21
|
+
"header from #{token.inspect}"
|
22
|
+
end
|
14
23
|
end
|
15
|
-
end
|
16
24
|
|
17
|
-
|
18
|
-
|
19
|
-
comma =
|
20
|
-
|
21
|
-
|
22
|
-
|
25
|
+
# @param type [String]
|
26
|
+
# @param hash [Hash]
|
27
|
+
# @return [String] type followed by comma-separated key=value pairs
|
28
|
+
# @api private
|
29
|
+
def self.build_hash(type, hash)
|
30
|
+
comma = ', '
|
31
|
+
values = []
|
32
|
+
hash.each do |key, value|
|
33
|
+
values << "#{key}=#{value.to_s.inspect}"
|
34
|
+
end
|
35
|
+
"#{type} #{values * comma}"
|
23
36
|
end
|
24
|
-
"#{type} #{values * comma}"
|
25
|
-
end
|
26
37
|
|
27
|
-
|
28
|
-
@
|
29
|
-
|
30
|
-
|
38
|
+
# @param app [#call]
|
39
|
+
# @param type [String, Symbol] Type of Authorization
|
40
|
+
# @param token [String, Symbol, Hash] Token value for the Authorization
|
41
|
+
def initialize(app, type, token)
|
42
|
+
@header_value = self.class.header(type, token)
|
43
|
+
super(app)
|
44
|
+
end
|
31
45
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
env
|
46
|
+
# @param env [Faraday::Env]
|
47
|
+
def call(env)
|
48
|
+
env.request_headers[KEY] = @header_value unless env.request_headers[KEY]
|
49
|
+
@app.call(env)
|
36
50
|
end
|
37
|
-
@app.call(env)
|
38
51
|
end
|
39
52
|
end
|
40
53
|
end
|
41
|
-
|
@@ -1,13 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'base64'
|
2
4
|
|
3
5
|
module Faraday
|
4
|
-
class Request
|
5
|
-
#
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
class Request
|
7
|
+
# Authorization middleware for Basic Authentication.
|
8
|
+
class BasicAuthentication < load_middleware(:authorization)
|
9
|
+
# @param login [String]
|
10
|
+
# @param pass [String]
|
11
|
+
#
|
12
|
+
# @return [String] a Basic Authentication header line
|
13
|
+
def self.header(login, pass)
|
14
|
+
value = Base64.encode64([login, pass].join(':'))
|
15
|
+
value.delete!("\n")
|
16
|
+
super(:Basic, value)
|
17
|
+
end
|
10
18
|
end
|
11
19
|
end
|
12
20
|
end
|
13
|
-
|
@@ -1,35 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Faraday
|
2
|
-
class Request
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
class Request
|
5
|
+
# Middleware for instrumenting Requests.
|
6
|
+
class Instrumentation < Faraday::Middleware
|
7
|
+
# Options class used in Request::Instrumentation class.
|
8
|
+
class Options < Faraday::Options.new(:name, :instrumenter)
|
9
|
+
# @return [String]
|
10
|
+
def name
|
11
|
+
self[:name] ||= 'request.faraday'
|
12
|
+
end
|
7
13
|
|
8
|
-
|
9
|
-
|
14
|
+
# @return [Class]
|
15
|
+
def instrumenter
|
16
|
+
self[:instrumenter] ||= ActiveSupport::Notifications
|
17
|
+
end
|
10
18
|
end
|
11
|
-
end
|
12
19
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
20
|
+
# Instruments requests using Active Support.
|
21
|
+
#
|
22
|
+
# Measures time spent only for synchronous requests.
|
23
|
+
#
|
24
|
+
# @example Using ActiveSupport::Notifications to measure time spent
|
25
|
+
# for Faraday requests.
|
26
|
+
# ActiveSupport::Notifications
|
27
|
+
# .subscribe('request.faraday') do |name, starts, ends, _, env|
|
28
|
+
# url = env[:url]
|
29
|
+
# http_method = env[:method].to_s.upcase
|
30
|
+
# duration = ends - starts
|
31
|
+
# $stderr.puts '[%s] %s %s (%.3f s)' %
|
32
|
+
# [url.host, http_method, url.request_uri, duration]
|
33
|
+
# end
|
34
|
+
# @param app [#call]
|
35
|
+
# @param options [nil, Hash] Options hash
|
36
|
+
# @option options [String] :name ('request.faraday')
|
37
|
+
# Name of the instrumenter
|
38
|
+
# @option options [Class] :instrumenter (ActiveSupport::Notifications)
|
39
|
+
# Active Support instrumenter class.
|
40
|
+
def initialize(app, options = nil)
|
41
|
+
super(app)
|
42
|
+
@name, @instrumenter = Options.from(options)
|
43
|
+
.values_at(:name, :instrumenter)
|
44
|
+
end
|
29
45
|
|
30
|
-
|
31
|
-
|
32
|
-
@
|
46
|
+
# @param env [Faraday::Env]
|
47
|
+
def call(env)
|
48
|
+
@instrumenter.instrument(@name, env) do
|
49
|
+
@app.call(env)
|
50
|
+
end
|
33
51
|
end
|
34
52
|
end
|
35
53
|
end
|