faraday 0.8.11 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.document +6 -0
- data/CHANGELOG.md +15 -0
- data/CONTRIBUTING.md +36 -0
- data/Gemfile +10 -3
- data/LICENSE.md +1 -1
- data/README.md +17 -51
- data/Rakefile +2 -18
- data/faraday.gemspec +34 -0
- data/lib/faraday/adapter/em_http.rb +34 -0
- data/lib/faraday/adapter/em_http_ssl_patch.rb +56 -0
- data/lib/faraday/adapter/em_synchrony.rb +11 -24
- data/lib/faraday/adapter/excon.rb +12 -2
- data/lib/faraday/adapter/httpclient.rb +106 -0
- data/lib/faraday/adapter/net_http.rb +10 -6
- data/lib/faraday/adapter/patron.rb +2 -8
- data/lib/faraday/adapter/rack.rb +0 -2
- data/lib/faraday/adapter/test.rb +39 -39
- data/lib/faraday/adapter/typhoeus.rb +12 -3
- data/lib/faraday/adapter.rb +20 -35
- data/lib/faraday/autoload.rb +85 -0
- data/lib/faraday/connection.rb +232 -125
- data/lib/faraday/error.rb +42 -34
- data/lib/faraday/options.rb +350 -0
- data/lib/faraday/parameters.rb +193 -0
- data/lib/faraday/{builder.rb → rack_builder.rb} +79 -22
- data/lib/faraday/request/authorization.rb +7 -5
- data/lib/faraday/request/basic_authentication.rb +1 -1
- data/lib/faraday/request/instrumentation.rb +36 -0
- data/lib/faraday/request/multipart.rb +10 -9
- data/lib/faraday/request/retry.rb +99 -4
- data/lib/faraday/request/token_authentication.rb +2 -2
- data/lib/faraday/request/url_encoded.rb +7 -6
- data/lib/faraday/request.rb +21 -30
- data/lib/faraday/response/logger.rb +4 -4
- data/lib/faraday/response/raise_error.rb +4 -2
- data/lib/faraday/response.rb +17 -23
- data/lib/faraday/utils.rb +81 -71
- data/lib/faraday.rb +187 -68
- data/script/console +7 -0
- data/script/proxy-server +1 -0
- data/script/release +6 -3
- data/script/test +4 -2
- data/test/adapters/em_http_test.rb +6 -1
- data/test/adapters/em_synchrony_test.rb +7 -1
- data/test/adapters/httpclient_test.rb +21 -0
- data/test/adapters/integration.rb +23 -8
- data/test/adapters/logger_test.rb +1 -1
- data/test/adapters/net_http_persistent_test.rb +10 -1
- data/test/adapters/net_http_test.rb +0 -31
- data/test/adapters/patron_test.rb +4 -1
- data/test/adapters/test_middleware_test.rb +48 -4
- data/test/adapters/typhoeus_test.rb +8 -1
- data/test/authentication_middleware_test.rb +2 -2
- data/test/connection_test.rb +160 -84
- data/test/env_test.rb +51 -24
- data/test/helper.rb +13 -13
- data/test/live_server.rb +8 -0
- data/test/middleware/instrumentation_test.rb +88 -0
- data/test/middleware/retry_test.rb +88 -35
- data/test/middleware_stack_test.rb +13 -12
- data/test/options_test.rb +252 -0
- data/test/request_middleware_test.rb +11 -1
- data/test/response_middleware_test.rb +2 -4
- data/test/strawberry.rb +2 -0
- data/test/utils_test.rb +34 -6
- metadata +71 -11
- data/test/parameters_test.rb +0 -24
data/lib/faraday/connection.rb
CHANGED
@@ -1,24 +1,49 @@
|
|
1
|
-
require 'cgi'
|
2
|
-
require 'set'
|
3
|
-
require 'forwardable'
|
4
|
-
require 'uri'
|
5
|
-
|
6
|
-
Faraday.require_libs 'builder', 'request', 'response', 'utils'
|
7
|
-
|
8
1
|
module Faraday
|
2
|
+
# Public: Connection objects manage the default properties and the middleware
|
3
|
+
# stack for fulfilling an HTTP request.
|
4
|
+
#
|
5
|
+
# Examples
|
6
|
+
#
|
7
|
+
# conn = Faraday::Connection.new 'http://sushi.com'
|
8
|
+
#
|
9
|
+
# # GET http://sushi.com/nigiri
|
10
|
+
# conn.get 'nigiri'
|
11
|
+
# # => #<Faraday::Response>
|
12
|
+
#
|
9
13
|
class Connection
|
14
|
+
# A Set of allowed HTTP verbs.
|
10
15
|
METHODS = Set.new [:get, :post, :put, :delete, :head, :patch, :options]
|
11
|
-
METHODS_WITH_BODIES = Set.new [:post, :put, :patch, :options]
|
12
16
|
|
13
|
-
|
17
|
+
# Public: Returns a Hash of URI query unencoded key/value pairs.
|
18
|
+
attr_reader :params
|
19
|
+
|
20
|
+
# Public: Returns a Hash of unencoded HTTP header key/value pairs.
|
21
|
+
attr_reader :headers
|
22
|
+
|
23
|
+
# Public: Returns a URI with the prefix used for all requests from this
|
24
|
+
# Connection. This includes a default host name, scheme, port, and path.
|
25
|
+
attr_reader :url_prefix
|
26
|
+
|
27
|
+
# Public: Returns the Faraday::Builder for this Connection.
|
28
|
+
attr_reader :builder
|
29
|
+
|
30
|
+
# Public: Returns a Hash of the request options.
|
31
|
+
attr_reader :options
|
32
|
+
|
33
|
+
# Public: Returns a Hash of the SSL options.
|
34
|
+
attr_reader :ssl
|
35
|
+
|
36
|
+
# Public: Returns the parallel manager for this Connection.
|
37
|
+
attr_reader :parallel_manager
|
38
|
+
|
39
|
+
# Public: Sets the default parallel manager for this connection.
|
14
40
|
attr_writer :default_parallel_manager
|
15
41
|
|
16
42
|
# Public: Initializes a new Faraday::Connection.
|
17
43
|
#
|
18
44
|
# url - URI or String base URL to use as a prefix for all
|
19
45
|
# requests (optional).
|
20
|
-
# options - Hash
|
21
|
-
# from this Connection (default: {}).
|
46
|
+
# options - Hash or Faraday::ConnectionOptions.
|
22
47
|
# :url - URI or String base URL (default: "http:/").
|
23
48
|
# :params - Hash of URI query unencoded key/value pairs.
|
24
49
|
# :headers - Hash of unencoded HTTP header key/value pairs.
|
@@ -29,29 +54,30 @@ module Faraday
|
|
29
54
|
# :uri - URI or String
|
30
55
|
# :user - String (optional)
|
31
56
|
# :password - String (optional)
|
32
|
-
def initialize(url = nil, options =
|
57
|
+
def initialize(url = nil, options = nil)
|
33
58
|
if url.is_a?(Hash)
|
34
|
-
options = url
|
35
|
-
url = options
|
59
|
+
options = ConnectionOptions.from(url)
|
60
|
+
url = options.url
|
61
|
+
else
|
62
|
+
options = ConnectionOptions.from(options)
|
36
63
|
end
|
37
|
-
@headers = Utils::Headers.new
|
38
|
-
@params = Utils::ParamsHash.new
|
39
|
-
@options = options[:request] || {}
|
40
|
-
@ssl = options[:ssl] || {}
|
41
64
|
|
42
65
|
@parallel_manager = nil
|
43
|
-
@
|
66
|
+
@headers = Utils::Headers.new
|
67
|
+
@params = Utils::ParamsHash.new
|
68
|
+
@options = options.request
|
69
|
+
@ssl = options.ssl
|
70
|
+
@default_parallel_manager = options.parallel_manager
|
44
71
|
|
45
|
-
@builder = options
|
72
|
+
@builder = options.builder || begin
|
46
73
|
# pass an empty block to Builder so it doesn't assume default middleware
|
47
|
-
|
48
|
-
Builder.new(&block)
|
74
|
+
options.new_builder(block_given? ? Proc.new { |b| } : nil)
|
49
75
|
end
|
50
76
|
|
51
77
|
self.url_prefix = url || 'http:/'
|
52
78
|
|
53
|
-
@params.update
|
54
|
-
@headers.update
|
79
|
+
@params.update(options.params) if options.params
|
80
|
+
@headers.update(options.headers) if options.headers
|
55
81
|
|
56
82
|
@proxy = nil
|
57
83
|
proxy(options.fetch(:proxy) {
|
@@ -62,56 +88,89 @@ module Faraday
|
|
62
88
|
end
|
63
89
|
})
|
64
90
|
|
65
|
-
yield
|
91
|
+
yield(self) if block_given?
|
66
92
|
|
67
93
|
@headers[:user_agent] ||= "Faraday v#{VERSION}"
|
68
94
|
end
|
69
95
|
|
70
|
-
# Public:
|
96
|
+
# Public: Sets the Hash of URI query unencoded key/value pairs.
|
71
97
|
def params=(hash)
|
72
98
|
@params.replace hash
|
73
99
|
end
|
74
100
|
|
75
|
-
# Public:
|
101
|
+
# Public: Sets the Hash of unencoded HTTP header key/value pairs.
|
76
102
|
def headers=(hash)
|
77
103
|
@headers.replace hash
|
78
104
|
end
|
79
105
|
|
80
106
|
extend Forwardable
|
81
|
-
def_delegators :builder, :build, :use, :request, :response, :adapter
|
82
|
-
|
83
|
-
# The "rack app" wrapped in middleware. All requests are sent here.
|
84
|
-
#
|
85
|
-
# The builder is responsible for creating the app object. After this,
|
86
|
-
# the builder gets locked to ensure no further modifications are made
|
87
|
-
# to the middleware stack.
|
88
|
-
#
|
89
|
-
# Returns an object that responds to `call` and returns a Response.
|
90
|
-
def app
|
91
|
-
@app ||= begin
|
92
|
-
builder.lock!
|
93
|
-
builder.to_app(lambda { |env|
|
94
|
-
# the inner app that creates and returns the Response object
|
95
|
-
response = Response.new
|
96
|
-
response.finish(env) unless env[:parallel_manager]
|
97
|
-
env[:response] = response
|
98
|
-
})
|
99
|
-
end
|
100
|
-
end
|
101
107
|
|
102
|
-
|
108
|
+
def_delegators :builder, :build, :use, :request, :response, :adapter, :app
|
109
|
+
|
110
|
+
# Public: Makes an HTTP request without a body.
|
111
|
+
#
|
112
|
+
# url - The optional String base URL to use as a prefix for all
|
113
|
+
# requests. Can also be the options Hash.
|
114
|
+
# params - Hash of URI query unencoded key/value pairs.
|
115
|
+
# headers - Hash of unencoded HTTP header key/value pairs.
|
116
|
+
#
|
117
|
+
# Examples
|
118
|
+
#
|
119
|
+
# conn.get '/items', {:page => 1}, :accept => 'application/json'
|
120
|
+
# conn.head '/items/1'
|
121
|
+
#
|
122
|
+
# # ElasticSearch example sending a body with GET.
|
123
|
+
# conn.get '/twitter/tweet/_search' do |req|
|
124
|
+
# req.headers[:content_type] = 'application/json'
|
125
|
+
# req.params[:routing] = 'kimchy'
|
126
|
+
# req.body = JSON.generate(:query => {...})
|
127
|
+
# end
|
128
|
+
#
|
129
|
+
# Yields a Faraday::Response for further request customizations.
|
130
|
+
# Returns a Faraday::Response.
|
131
|
+
#
|
132
|
+
# Signature
|
133
|
+
#
|
134
|
+
# <verb>(url = nil, params = nil, headers = nil)
|
135
|
+
#
|
136
|
+
# verb - An HTTP verb: get, head, or delete.
|
103
137
|
%w[get head delete].each do |method|
|
104
138
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
105
139
|
def #{method}(url = nil, params = nil, headers = nil)
|
106
140
|
run_request(:#{method}, url, nil, headers) { |request|
|
107
141
|
request.params.update(params) if params
|
108
|
-
yield
|
142
|
+
yield(request) if block_given?
|
109
143
|
}
|
110
144
|
end
|
111
145
|
RUBY
|
112
146
|
end
|
113
147
|
|
114
|
-
#
|
148
|
+
# Public: Makes an HTTP request with a body.
|
149
|
+
#
|
150
|
+
# url - The optional String base URL to use as a prefix for all
|
151
|
+
# requests. Can also be the options Hash.
|
152
|
+
# body - The String body for the request.
|
153
|
+
# headers - Hash of unencoded HTTP header key/value pairs.
|
154
|
+
#
|
155
|
+
# Examples
|
156
|
+
#
|
157
|
+
# conn.post '/items', data, :content_type => 'application/json'
|
158
|
+
#
|
159
|
+
# # Simple ElasticSearch indexing sample.
|
160
|
+
# conn.post '/twitter/tweet' do |req|
|
161
|
+
# req.headers[:content_type] = 'application/json'
|
162
|
+
# req.params[:routing] = 'kimchy'
|
163
|
+
# req.body = JSON.generate(:user => 'kimchy', ...)
|
164
|
+
# end
|
165
|
+
#
|
166
|
+
# Yields a Faraday::Response for further request customizations.
|
167
|
+
# Returns a Faraday::Response.
|
168
|
+
#
|
169
|
+
# Signature
|
170
|
+
#
|
171
|
+
# <verb>(url = nil, body = nil, headers = nil)
|
172
|
+
#
|
173
|
+
# verb - An HTTP verb: post, put, or patch.
|
115
174
|
%w[post put patch].each do |method|
|
116
175
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
117
176
|
def #{method}(url = nil, body = nil, headers = nil, &block)
|
@@ -120,19 +179,60 @@ module Faraday
|
|
120
179
|
RUBY
|
121
180
|
end
|
122
181
|
|
182
|
+
# Public: Sets up the Authorization header with these credentials, encoded
|
183
|
+
# with base64.
|
184
|
+
#
|
185
|
+
# login - The authentication login.
|
186
|
+
# pass - The authentication password.
|
187
|
+
#
|
188
|
+
# Examples
|
189
|
+
#
|
190
|
+
# conn.basic_auth 'Aladdin', 'open sesame'
|
191
|
+
# conn.headers['Authorization']
|
192
|
+
# # => "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
|
193
|
+
#
|
194
|
+
# Returns nothing.
|
123
195
|
def basic_auth(login, pass)
|
124
|
-
|
125
|
-
Faraday::Request::BasicAuthentication.header(login, pass)
|
196
|
+
set_authorization_header(:basic_auth, login, pass)
|
126
197
|
end
|
127
198
|
|
199
|
+
# Public: Sets up the Authorization header with the given token.
|
200
|
+
#
|
201
|
+
# token - The String token.
|
202
|
+
# options - Optional Hash of extra token options.
|
203
|
+
#
|
204
|
+
# Examples
|
205
|
+
#
|
206
|
+
# conn.token_auth 'abcdef', :foo => 'bar'
|
207
|
+
# conn.headers['Authorization']
|
208
|
+
# # => "Token token=\"abcdef\",
|
209
|
+
# foo=\"bar\""
|
210
|
+
#
|
211
|
+
# Returns nothing.
|
128
212
|
def token_auth(token, options = nil)
|
129
|
-
|
130
|
-
Faraday::Request::TokenAuthentication.header(token, options)
|
213
|
+
set_authorization_header(:token_auth, token, options)
|
131
214
|
end
|
132
215
|
|
216
|
+
# Public: Sets up a custom Authorization header.
|
217
|
+
#
|
218
|
+
# type - The String authorization type.
|
219
|
+
# token - The String or Hash token. A String value is taken literally, and
|
220
|
+
# a Hash is encoded into comma separated key/value pairs.
|
221
|
+
#
|
222
|
+
# Examples
|
223
|
+
#
|
224
|
+
# conn.authorization :Bearer, 'mF_9.B5f-4.1JqM'
|
225
|
+
# conn.headers['Authorization']
|
226
|
+
# # => "Bearer mF_9.B5f-4.1JqM"
|
227
|
+
#
|
228
|
+
# conn.authorization :Token, :token => 'abcdef', :foo => 'bar'
|
229
|
+
# conn.headers['Authorization']
|
230
|
+
# # => "Token token=\"abcdef\",
|
231
|
+
# foo=\"bar\""
|
232
|
+
#
|
233
|
+
# Returns nothing.
|
133
234
|
def authorization(type, token)
|
134
|
-
|
135
|
-
Faraday::Request::Authorization.header(type, token)
|
235
|
+
set_authorization_header(:authorization, type, token)
|
136
236
|
end
|
137
237
|
|
138
238
|
# Internal: Traverse the middleware stack in search of a
|
@@ -147,16 +247,27 @@ module Faraday
|
|
147
247
|
h.klass.respond_to?(:supports_parallel?) and h.klass.supports_parallel?
|
148
248
|
end
|
149
249
|
|
150
|
-
if handler
|
151
|
-
|
250
|
+
if handler
|
251
|
+
handler.klass.setup_parallel_manager
|
252
|
+
elsif block_given?
|
253
|
+
yield
|
152
254
|
end
|
153
255
|
end
|
154
256
|
end
|
155
257
|
|
258
|
+
# Public: Determine if this Faraday::Connection can make parallel requests.
|
259
|
+
#
|
260
|
+
# Returns true or false.
|
156
261
|
def in_parallel?
|
157
262
|
!!@parallel_manager
|
158
263
|
end
|
159
264
|
|
265
|
+
# Public: Sets up the parallel manager to make a set of requests.
|
266
|
+
#
|
267
|
+
# manager - The parallel manager that this Connection's Adapter uses.
|
268
|
+
#
|
269
|
+
# Yields a block to execute multiple requests.
|
270
|
+
# Returns nothing.
|
160
271
|
def in_parallel(manager = nil)
|
161
272
|
@parallel_manager = manager || default_parallel_manager {
|
162
273
|
warn "Warning: `in_parallel` called but no parallel-capable adapter on Faraday stack"
|
@@ -169,43 +280,23 @@ module Faraday
|
|
169
280
|
@parallel_manager = nil
|
170
281
|
end
|
171
282
|
|
283
|
+
# Public: Gets or Sets the Hash proxy options.
|
172
284
|
def proxy(arg = nil)
|
173
285
|
return @proxy if arg.nil?
|
174
|
-
|
175
|
-
@proxy = if arg.is_a? Hash
|
176
|
-
uri = self.class.URI arg.fetch(:uri) { raise ArgumentError, "missing :uri" }
|
177
|
-
arg.merge :uri => uri
|
178
|
-
else
|
179
|
-
uri = self.class.URI(arg)
|
180
|
-
{:uri => uri}
|
181
|
-
end
|
182
|
-
|
183
|
-
with_uri_credentials(uri) do |user, password|
|
184
|
-
@proxy[:user] ||= user
|
185
|
-
@proxy[:password] ||= password
|
186
|
-
end
|
187
|
-
|
188
|
-
@proxy
|
189
|
-
end
|
190
|
-
|
191
|
-
# normalize URI() behavior across Ruby versions
|
192
|
-
def self.URI(url)
|
193
|
-
if url.respond_to?(:host)
|
194
|
-
url
|
195
|
-
elsif url.respond_to?(:to_str)
|
196
|
-
Kernel.URI(url)
|
197
|
-
else
|
198
|
-
raise ArgumentError, "bad argument (expected URI object or URI string)"
|
199
|
-
end
|
286
|
+
@proxy = ProxyOptions.from(arg)
|
200
287
|
end
|
201
288
|
|
202
289
|
def_delegators :url_prefix, :scheme, :scheme=, :host, :host=, :port, :port=
|
203
290
|
def_delegator :url_prefix, :path, :path_prefix
|
204
291
|
|
205
|
-
# Parses the giving url with URI and stores the individual
|
292
|
+
# Public: Parses the giving url with URI and stores the individual
|
206
293
|
# components in this connection. These components serve as defaults for
|
207
294
|
# requests made by this connection.
|
208
295
|
#
|
296
|
+
# url - A String or URI.
|
297
|
+
#
|
298
|
+
# Examples
|
299
|
+
#
|
209
300
|
# conn = Faraday::Connection.new { ... }
|
210
301
|
# conn.url_prefix = "https://sushi.com/api"
|
211
302
|
# conn.scheme # => https
|
@@ -213,11 +304,12 @@ module Faraday
|
|
213
304
|
#
|
214
305
|
# conn.get("nigiri?page=2") # accesses https://sushi.com/api/nigiri
|
215
306
|
#
|
216
|
-
|
217
|
-
|
307
|
+
# Returns the parsed URI from teh given input..
|
308
|
+
def url_prefix=(url, encoder = nil)
|
309
|
+
uri = @url_prefix = Utils.URI(url)
|
218
310
|
self.path_prefix = uri.path
|
219
311
|
|
220
|
-
params.merge_query(uri.query)
|
312
|
+
params.merge_query(uri.query, encoder)
|
221
313
|
uri.query = nil
|
222
314
|
|
223
315
|
with_uri_credentials(uri) do |user, password|
|
@@ -228,15 +320,48 @@ module Faraday
|
|
228
320
|
uri
|
229
321
|
end
|
230
322
|
|
231
|
-
#
|
323
|
+
# Public: Sets the path prefix and ensures that it always has a leading
|
324
|
+
# slash.
|
325
|
+
#
|
326
|
+
# value - A String.
|
327
|
+
#
|
328
|
+
# Returns the new String path prefix.
|
232
329
|
def path_prefix=(value)
|
233
330
|
url_prefix.path = if value
|
234
|
-
value = value.chomp '/'
|
235
331
|
value = '/' + value unless value[0,1] == '/'
|
236
332
|
value
|
237
333
|
end
|
238
334
|
end
|
239
335
|
|
336
|
+
# Public: Takes a relative url for a request and combines it with the defaults
|
337
|
+
# set on the connection instance.
|
338
|
+
#
|
339
|
+
# conn = Faraday::Connection.new { ... }
|
340
|
+
# conn.url_prefix = "https://sushi.com/api?token=abc"
|
341
|
+
# conn.scheme # => https
|
342
|
+
# conn.path_prefix # => "/api"
|
343
|
+
#
|
344
|
+
# conn.build_url("nigiri?page=2") # => https://sushi.com/api/nigiri?token=abc&page=2
|
345
|
+
# conn.build_url("nigiri", :page => 2) # => https://sushi.com/api/nigiri?token=abc&page=2
|
346
|
+
#
|
347
|
+
def build_url(url = nil, extra_params = nil)
|
348
|
+
uri = build_exclusive_url(url)
|
349
|
+
|
350
|
+
query_values = params.dup.merge_query(uri.query, options.params_encoder)
|
351
|
+
query_values.update extra_params if extra_params
|
352
|
+
uri.query = query_values.empty? ? nil : query_values.to_query(options.params_encoder)
|
353
|
+
|
354
|
+
uri
|
355
|
+
end
|
356
|
+
|
357
|
+
# Builds and runs the Faraday::Request.
|
358
|
+
#
|
359
|
+
# method - The Symbol HTTP method.
|
360
|
+
# url - The String or URI to access.
|
361
|
+
# body - The String body
|
362
|
+
# headers - Hash of unencoded HTTP header key/value pairs.
|
363
|
+
#
|
364
|
+
# Returns a Faraday::Response.
|
240
365
|
def run_request(method, url, body, headers)
|
241
366
|
if !METHODS.include?(method)
|
242
367
|
raise ArgumentError, "unknown http method: #{method}"
|
@@ -246,14 +371,13 @@ module Faraday
|
|
246
371
|
req.url(url) if url
|
247
372
|
req.headers.update(headers) if headers
|
248
373
|
req.body = body if body
|
249
|
-
yield
|
374
|
+
yield(req) if block_given?
|
250
375
|
end
|
251
376
|
|
252
|
-
|
253
|
-
self.app.call(env)
|
377
|
+
builder.build_response(self, request)
|
254
378
|
end
|
255
379
|
|
256
|
-
#
|
380
|
+
# Creates and configures the request object.
|
257
381
|
#
|
258
382
|
# Returns the new Request.
|
259
383
|
def build_request(method)
|
@@ -261,31 +385,10 @@ module Faraday
|
|
261
385
|
req.params = self.params.dup
|
262
386
|
req.headers = self.headers.dup
|
263
387
|
req.options = self.options.merge(:proxy => self.proxy)
|
264
|
-
yield
|
388
|
+
yield(req) if block_given?
|
265
389
|
end
|
266
390
|
end
|
267
391
|
|
268
|
-
# Takes a relative url for a request and combines it with the defaults
|
269
|
-
# set on the connection instance.
|
270
|
-
#
|
271
|
-
# conn = Faraday::Connection.new { ... }
|
272
|
-
# conn.url_prefix = "https://sushi.com/api?token=abc"
|
273
|
-
# conn.scheme # => https
|
274
|
-
# conn.path_prefix # => "/api"
|
275
|
-
#
|
276
|
-
# conn.build_url("nigiri?page=2") # => https://sushi.com/api/nigiri?token=abc&page=2
|
277
|
-
# conn.build_url("nigiri", :page => 2) # => https://sushi.com/api/nigiri?token=abc&page=2
|
278
|
-
#
|
279
|
-
def build_url(url, extra_params = nil)
|
280
|
-
uri = build_exclusive_url(url)
|
281
|
-
|
282
|
-
query_values = self.params.dup.merge_query(uri.query)
|
283
|
-
query_values.update extra_params if extra_params
|
284
|
-
uri.query = query_values.empty? ? nil : query_values.to_query
|
285
|
-
|
286
|
-
uri
|
287
|
-
end
|
288
|
-
|
289
392
|
# Internal: Build an absolute URL based on url_prefix.
|
290
393
|
#
|
291
394
|
# url - A String or URI-like object
|
@@ -293,7 +396,7 @@ module Faraday
|
|
293
396
|
# of the resulting url (default: nil).
|
294
397
|
#
|
295
398
|
# Returns the resulting URI instance.
|
296
|
-
def build_exclusive_url(url, params = nil)
|
399
|
+
def build_exclusive_url(url = nil, params = nil)
|
297
400
|
url = nil if url.respond_to?(:empty?) and url.empty?
|
298
401
|
base = url_prefix
|
299
402
|
if url and base.path and base.path !~ /\/$/
|
@@ -301,25 +404,29 @@ module Faraday
|
|
301
404
|
base.path = base.path + '/' # ensure trailing slash
|
302
405
|
end
|
303
406
|
uri = url ? base + url : base
|
304
|
-
uri.query = params.to_query if params
|
407
|
+
uri.query = params.to_query(options.params_encoder) if params
|
305
408
|
uri.query = nil if uri.query and uri.query.empty?
|
306
409
|
uri
|
307
410
|
end
|
308
411
|
|
412
|
+
# Internal: Creates a duplicate of this Faraday::Connection.
|
413
|
+
#
|
414
|
+
# Returns a Faraday::Connection.
|
309
415
|
def dup
|
310
|
-
self.class.new(
|
311
|
-
:headers => headers.dup,
|
312
|
-
:params => params.dup,
|
313
|
-
:builder => builder.dup,
|
314
|
-
:ssl => ssl.dup,
|
315
|
-
:request => options.dup)
|
416
|
+
self.class.new(build_exclusive_url, :headers => headers.dup, :params => params.dup, :builder => builder.dup, :ssl => ssl.dup)
|
316
417
|
end
|
317
418
|
|
318
419
|
# Internal: Yields username and password extracted from a URI if they both exist.
|
319
420
|
def with_uri_credentials(uri)
|
320
421
|
if uri.user and uri.password
|
321
|
-
yield
|
422
|
+
yield(Utils.unescape(uri.user), Utils.unescape(uri.password))
|
322
423
|
end
|
323
424
|
end
|
425
|
+
|
426
|
+
def set_authorization_header(header_type, *args)
|
427
|
+
header = Faraday::Request.lookup_middleware(header_type).
|
428
|
+
header(*args)
|
429
|
+
headers[Faraday::Request::Authorization::KEY] = header
|
430
|
+
end
|
324
431
|
end
|
325
432
|
end
|
data/lib/faraday/error.rb
CHANGED
@@ -1,45 +1,53 @@
|
|
1
1
|
module Faraday
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
super(ex.message)
|
12
|
-
@wrapped_exception = ex
|
13
|
-
elsif ex.respond_to?(:each_key)
|
14
|
-
super("the server responded with status #{ex[:status]}")
|
15
|
-
@response = ex
|
16
|
-
else
|
17
|
-
super(ex.to_s)
|
18
|
-
end
|
19
|
-
end
|
2
|
+
class Error < StandardError; end
|
3
|
+
class MissingDependency < Error; end
|
4
|
+
|
5
|
+
class ClientError < Error
|
6
|
+
attr_reader :response
|
7
|
+
|
8
|
+
def initialize(ex, response = nil)
|
9
|
+
@wrapped_exception = nil
|
10
|
+
@response = response
|
20
11
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
12
|
+
if ex.respond_to?(:backtrace)
|
13
|
+
super(ex.message)
|
14
|
+
@wrapped_exception = ex
|
15
|
+
elsif ex.respond_to?(:each_key)
|
16
|
+
super("the server responded with status #{ex[:status]}")
|
17
|
+
@response = ex
|
18
|
+
else
|
19
|
+
super(ex.to_s)
|
27
20
|
end
|
21
|
+
end
|
28
22
|
|
29
|
-
|
30
|
-
|
23
|
+
def backtrace
|
24
|
+
if @wrapped_exception
|
25
|
+
@wrapped_exception.backtrace
|
26
|
+
else
|
27
|
+
super
|
31
28
|
end
|
32
29
|
end
|
33
30
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
31
|
+
def inspect
|
32
|
+
%(#<#{self.class}>)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class ConnectionFailed < ClientError; end
|
37
|
+
class ResourceNotFound < ClientError; end
|
38
|
+
class ParsingError < ClientError; end
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
end
|
40
|
+
class TimeoutError < ClientError
|
41
|
+
def initialize(ex = nil)
|
42
|
+
super(ex || "timeout")
|
43
43
|
end
|
44
44
|
end
|
45
|
+
|
46
|
+
class SSLError < ClientError
|
47
|
+
end
|
48
|
+
|
49
|
+
[:MissingDependency, :ClientError, :ConnectionFailed, :ResourceNotFound,
|
50
|
+
:ParsingError, :TimeoutError, :SSLError].each do |const|
|
51
|
+
Error.const_set(const, Faraday.const_get(const))
|
52
|
+
end
|
45
53
|
end
|