faraday 0.10.0 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +97 -6
- data/lib/faraday/adapter/em_http.rb +5 -1
- data/lib/faraday/adapter/em_synchrony.rb +5 -1
- data/lib/faraday/adapter/excon.rb +5 -6
- data/lib/faraday/adapter/httpclient.rb +16 -5
- data/lib/faraday/adapter/net_http.rb +9 -5
- data/lib/faraday/adapter/net_http_persistent.rb +4 -3
- data/lib/faraday/adapter/patron.rb +1 -6
- data/lib/faraday/adapter.rb +6 -0
- data/lib/faraday/connection.rb +25 -7
- data/lib/faraday/options.rb +27 -15
- data/lib/faraday/request/authorization.rb +1 -2
- data/lib/faraday/request/multipart.rb +7 -2
- data/lib/faraday/request/retry.rb +11 -2
- data/lib/faraday/response/logger.rb +19 -5
- data/lib/faraday/utils.rb +8 -0
- data/lib/faraday.rb +7 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d6ed59b880f0d605e4efc8ce513ced0b4688c4ef
|
4
|
+
data.tar.gz: 21d3a0326416db1e19add553b8d5c0ef1c5a3b50
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e882a5072cc36a986e8fc2f23f0563755bae2d0c4e6fed4adc51a6fd46b449ea6be3dd189662ea0aa4f427c5402969bfb0ad626c837e9bad68d06aa0a3cc6d3
|
7
|
+
data.tar.gz: 63856c35cbcceda6abeb3dc076574d7e880343e8ee7af5e80971c08637a647a079433b1220161c24871d3718c268d018ebdce009ca46d89c4da7c059fdaeaf72
|
data/README.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# Faraday
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/faraday.svg)](https://rubygems.org/gems/faraday)
|
4
|
+
[![Build Status](https://travis-ci.org/lostisland/faraday.svg)](https://travis-ci.org/lostisland/faraday)
|
5
|
+
[![Gitter](https://badges.gitter.im/lostisland/faraday.svg)](https://gitter.im/lostisland/faraday?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
6
|
+
|
7
|
+
|
3
8
|
Faraday is an HTTP client lib that provides a common interface over many
|
4
9
|
adapters (such as Net::HTTP) and embraces the concept of Rack middleware when
|
5
10
|
processing the request/response cycle.
|
@@ -9,7 +14,6 @@ Faraday supports these adapters:
|
|
9
14
|
* [Net::HTTP][net_http] _(default)_
|
10
15
|
* [Net::HTTP::Persistent][persistent]
|
11
16
|
* [Excon][]
|
12
|
-
* [Typhoeus][]
|
13
17
|
* [Patron][]
|
14
18
|
* [EventMachine][]
|
15
19
|
* [HTTPClient][]
|
@@ -23,6 +27,23 @@ Available at [rubydoc.info](http://www.rubydoc.info/gems/faraday).
|
|
23
27
|
|
24
28
|
## Usage
|
25
29
|
|
30
|
+
### Basic Use
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
response = Faraday.get 'http://sushi.com/nigiri/sake.json'
|
34
|
+
```
|
35
|
+
A simple `get` request can be performed by using the syntax described above. This works if you don't need to set up anything; you can roll with just the default middleware
|
36
|
+
stack and default adapter (see [Faraday::RackBuilder#initialize](https://github.com/lostisland/faraday/blob/master/lib/faraday/rack_builder.rb)).
|
37
|
+
|
38
|
+
A more flexible way to use Faraday is to start with a Connection object. If you want to keep the same defaults, you can use this syntax:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
conn = Faraday.new(:url => 'http://www.example.com')
|
42
|
+
response = conn.get '/users' # GET http://www.example.com/users'
|
43
|
+
```
|
44
|
+
|
45
|
+
Connections can also take an options hash as a parameter or be configured by using a block. Checkout the section called [Advanced middleware usage](#advanced-middleware-usage) for more details about how to use this block for configurations.
|
46
|
+
|
26
47
|
```ruby
|
27
48
|
conn = Faraday.new(:url => 'http://sushi.com') do |faraday|
|
28
49
|
faraday.request :url_encoded # form-encode POST params
|
@@ -30,6 +51,19 @@ conn = Faraday.new(:url => 'http://sushi.com') do |faraday|
|
|
30
51
|
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
|
31
52
|
end
|
32
53
|
|
54
|
+
# Filter sensitive information from logs with a regex matcher
|
55
|
+
|
56
|
+
conn = Faraday.new(:url => 'http://sushi.com/api_key=s3cr3t') do |faraday|
|
57
|
+
faraday.response :logger do | logger |
|
58
|
+
logger.filter(/(api_key=)(\w+)/,'\1[REMOVED]')
|
59
|
+
end
|
60
|
+
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
Once you have the connection object, use it to make HTTP requests. You can pass paramters to it in a few different ways:
|
65
|
+
|
66
|
+
```ruby
|
33
67
|
## GET ##
|
34
68
|
|
35
69
|
response = conn.get '/nigiri/sake.json' # GET http://sushi.com/nigiri/sake.json
|
@@ -37,7 +71,7 @@ response.body
|
|
37
71
|
|
38
72
|
conn.get '/nigiri', { :name => 'Maguro' } # GET http://sushi.com/nigiri?name=Maguro
|
39
73
|
|
40
|
-
conn.get do |req| # GET http://sushi.com/search?page=2&limit=100
|
74
|
+
conn.get do |req| # GET http://sushi.com/search?page=2&limit=100
|
41
75
|
req.url '/search', :page => 2
|
42
76
|
req.params['limit'] = 100
|
43
77
|
end
|
@@ -45,7 +79,11 @@ end
|
|
45
79
|
## POST ##
|
46
80
|
|
47
81
|
conn.post '/nigiri', { :name => 'Maguro' } # POST "name=maguro" to http://sushi.com/nigiri
|
82
|
+
```
|
83
|
+
|
84
|
+
Some configuration options can be adjusted per request:
|
48
85
|
|
86
|
+
```ruby
|
49
87
|
# post payload as JSON instead of "www-form-urlencoded" encoding:
|
50
88
|
conn.post do |req|
|
51
89
|
req.url '/nigiri'
|
@@ -62,11 +100,18 @@ conn.get do |req|
|
|
62
100
|
end
|
63
101
|
```
|
64
102
|
|
65
|
-
|
66
|
-
stack and default adapter (see [Faraday::RackBuilder#initialize](https://github.com/lostisland/faraday/blob/master/lib/faraday/rack_builder.rb)):
|
103
|
+
And you can inject arbitrary data into the request using the `context` option:
|
67
104
|
|
68
105
|
```ruby
|
69
|
-
|
106
|
+
# Anything you inject using context option will be available in the env on all middlewares
|
107
|
+
|
108
|
+
conn.get do |req|
|
109
|
+
req.url '/search'
|
110
|
+
req.options.context = {
|
111
|
+
foo: 'foo',
|
112
|
+
bar: 'bar'
|
113
|
+
}
|
114
|
+
end
|
70
115
|
```
|
71
116
|
|
72
117
|
### Changing how parameters are serialized
|
@@ -169,6 +214,53 @@ later, response. Some keys are:
|
|
169
214
|
:response_headers
|
170
215
|
```
|
171
216
|
|
217
|
+
## Ad-hoc adapters customization
|
218
|
+
|
219
|
+
Faraday is intended to be a generic interface between your code and the adapter. However, sometimes you need to access a feature specific to one of the adapters that is not covered in Faraday's interface.
|
220
|
+
|
221
|
+
When that happens, you can pass a block when specifying the adapter to customize it. The block parameter will change based on the adapter you're using. See below for some examples.
|
222
|
+
|
223
|
+
### NetHttp
|
224
|
+
```ruby
|
225
|
+
conn = Faraday.new(...) do |f|
|
226
|
+
f.adapter :net_http do |http| # yields Net::HTTP
|
227
|
+
http.idle_timeout = 100
|
228
|
+
http.verify_callback = lambda do | preverify_ok, cert_store |
|
229
|
+
# do something here...
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
```
|
234
|
+
|
235
|
+
### NetHttpPersistent
|
236
|
+
```ruby
|
237
|
+
conn = Faraday.new(...) do |f|
|
238
|
+
f.adapter :net_http_persistent do |http| # yields Net::HTTP::Persistent
|
239
|
+
http.idle_timeout = 100
|
240
|
+
http.retry_change_requests = true
|
241
|
+
end
|
242
|
+
end
|
243
|
+
```
|
244
|
+
|
245
|
+
### Patron
|
246
|
+
```ruby
|
247
|
+
conn = Faraday.new(...) do |f|
|
248
|
+
f.adapter :patron do |session| # yields Patron::Session
|
249
|
+
session.max_redirects = 10
|
250
|
+
end
|
251
|
+
end
|
252
|
+
```
|
253
|
+
|
254
|
+
### HTTPClient
|
255
|
+
```ruby
|
256
|
+
conn = Faraday.new(...) do |f|
|
257
|
+
f.adapter :httpclient do |client| # yields HTTPClient
|
258
|
+
client.keep_alive_timeout = 20
|
259
|
+
client.ssl_config.timeout = 25
|
260
|
+
end
|
261
|
+
end
|
262
|
+
```
|
263
|
+
|
172
264
|
## Using Faraday for testing
|
173
265
|
|
174
266
|
```ruby
|
@@ -240,7 +332,6 @@ See [LICENSE][] for details.
|
|
240
332
|
[persistent]: https://github.com/drbrain/net-http-persistent
|
241
333
|
[travis]: https://travis-ci.org/lostisland/faraday
|
242
334
|
[excon]: https://github.com/excon/excon#readme
|
243
|
-
[typhoeus]: https://github.com/typhoeus/typhoeus#readme
|
244
335
|
[patron]: http://toland.github.io/patron/
|
245
336
|
[eventmachine]: https://github.com/igrigorik/em-http-request#readme
|
246
337
|
[httpclient]: https://github.com/nahi/httpclient
|
@@ -138,7 +138,7 @@ module Faraday
|
|
138
138
|
|
139
139
|
# TODO: reuse the connection to support pipelining
|
140
140
|
def perform_single_request(env)
|
141
|
-
req =
|
141
|
+
req = create_request(env)
|
142
142
|
req.setup_request(env[:method], request_config(env)).callback { |client|
|
143
143
|
status = client.response_header.status
|
144
144
|
reason = client.response_header.http_reason
|
@@ -150,6 +150,10 @@ module Faraday
|
|
150
150
|
}
|
151
151
|
end
|
152
152
|
|
153
|
+
def create_request(env)
|
154
|
+
EventMachine::HttpRequest.new(env[:url], connection_config(env).merge(@connection_options))
|
155
|
+
end
|
156
|
+
|
153
157
|
def error_message(client)
|
154
158
|
client.error or "request failed"
|
155
159
|
end
|
@@ -19,7 +19,7 @@ module Faraday
|
|
19
19
|
|
20
20
|
def call(env)
|
21
21
|
super
|
22
|
-
request =
|
22
|
+
request = create_request(env)
|
23
23
|
|
24
24
|
http_method = env[:method].to_s.downcase.to_sym
|
25
25
|
|
@@ -87,6 +87,10 @@ module Faraday
|
|
87
87
|
raise
|
88
88
|
end
|
89
89
|
end
|
90
|
+
|
91
|
+
def create_request(env)
|
92
|
+
EventMachine::HttpRequest.new(Utils::URI(env[:url].to_s), connection_config(env).merge(@connection_options))
|
93
|
+
end
|
90
94
|
end
|
91
95
|
end
|
92
96
|
end
|
@@ -3,11 +3,6 @@ module Faraday
|
|
3
3
|
class Excon < Faraday::Adapter
|
4
4
|
dependency 'excon'
|
5
5
|
|
6
|
-
def initialize(app, connection_options = {})
|
7
|
-
@connection_options = connection_options
|
8
|
-
super(app)
|
9
|
-
end
|
10
|
-
|
11
6
|
def call(env)
|
12
7
|
super
|
13
8
|
|
@@ -50,7 +45,7 @@ module Faraday
|
|
50
45
|
end
|
51
46
|
end
|
52
47
|
|
53
|
-
conn =
|
48
|
+
conn = create_connection(env, opts)
|
54
49
|
|
55
50
|
resp = conn.request \
|
56
51
|
:method => env[:method].to_s.upcase,
|
@@ -72,6 +67,10 @@ module Faraday
|
|
72
67
|
raise Error::TimeoutError, err
|
73
68
|
end
|
74
69
|
|
70
|
+
def create_connection(env, opts)
|
71
|
+
::Excon.new(env[:url].to_s, opts.merge(@connection_options))
|
72
|
+
end
|
73
|
+
|
75
74
|
# TODO: support streaming requests
|
76
75
|
def read_body(env)
|
77
76
|
env[:body].respond_to?(:read) ? env[:body].read : env[:body]
|
@@ -29,6 +29,8 @@ module Faraday
|
|
29
29
|
configure_ssl ssl
|
30
30
|
end
|
31
31
|
|
32
|
+
configure_client
|
33
|
+
|
32
34
|
# TODO Don't stream yet.
|
33
35
|
# https://github.com/nahi/httpclient/pull/90
|
34
36
|
env[:body] = env[:body].read if env[:body].respond_to? :read
|
@@ -48,7 +50,7 @@ module Faraday
|
|
48
50
|
else
|
49
51
|
raise Faraday::Error::ClientError, $!
|
50
52
|
end
|
51
|
-
rescue Errno::ECONNREFUSED,
|
53
|
+
rescue Errno::ECONNREFUSED, IOError
|
52
54
|
raise Faraday::Error::ConnectionFailed, $!
|
53
55
|
rescue => err
|
54
56
|
if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
|
@@ -95,12 +97,21 @@ module Faraday
|
|
95
97
|
end
|
96
98
|
end
|
97
99
|
|
100
|
+
def configure_client
|
101
|
+
@config_block.call(client) if @config_block
|
102
|
+
end
|
103
|
+
|
98
104
|
def ssl_cert_store(ssl)
|
99
105
|
return ssl[:cert_store] if ssl[:cert_store]
|
100
|
-
#
|
101
|
-
|
102
|
-
|
103
|
-
cert_store
|
106
|
+
# Memoize the cert store so that the same one is passed to
|
107
|
+
# HTTPClient each time, to avoid resyncing SSL sesions when
|
108
|
+
# it's changed
|
109
|
+
@cert_store ||= begin
|
110
|
+
# Use the default cert store by default, i.e. system ca certs
|
111
|
+
cert_store = OpenSSL::X509::Store.new
|
112
|
+
cert_store.set_default_paths
|
113
|
+
cert_store
|
114
|
+
end
|
104
115
|
end
|
105
116
|
|
106
117
|
def ssl_verify_mode(ssl)
|
@@ -10,7 +10,7 @@ module Faraday
|
|
10
10
|
class Adapter
|
11
11
|
class NetHttp < Faraday::Adapter
|
12
12
|
NET_HTTP_EXCEPTIONS = [
|
13
|
-
|
13
|
+
IOError,
|
14
14
|
Errno::ECONNABORTED,
|
15
15
|
Errno::ECONNREFUSED,
|
16
16
|
Errno::ECONNRESET,
|
@@ -32,10 +32,7 @@ module Faraday
|
|
32
32
|
super
|
33
33
|
with_net_http_connection(env) do |http|
|
34
34
|
configure_ssl(http, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
|
35
|
-
|
36
|
-
req = env[:request]
|
37
|
-
http.read_timeout = http.open_timeout = req[:timeout] if req[:timeout]
|
38
|
-
http.open_timeout = req[:open_timeout] if req[:open_timeout]
|
35
|
+
configure_request(http, env[:request])
|
39
36
|
|
40
37
|
begin
|
41
38
|
http_response = perform_request(http, env)
|
@@ -109,6 +106,13 @@ module Faraday
|
|
109
106
|
http.ssl_version = ssl[:version] if ssl[:version]
|
110
107
|
end
|
111
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
|
+
|
112
116
|
def ssl_cert_store(ssl)
|
113
117
|
return ssl[:cert_store] if ssl[:cert_store]
|
114
118
|
# Use the default cert store by default, i.e. system ca certs
|
@@ -7,8 +7,8 @@ module Faraday
|
|
7
7
|
class NetHttpPersistent < NetHttp
|
8
8
|
dependency 'net/http/persistent'
|
9
9
|
|
10
|
-
def
|
11
|
-
if proxy = env[:request][:proxy]
|
10
|
+
def net_http_connection(env)
|
11
|
+
if (proxy = env[:request][:proxy])
|
12
12
|
proxy_uri = ::URI::HTTP === proxy[:uri] ? proxy[:uri].dup : ::URI.parse(proxy[:uri].to_s)
|
13
13
|
proxy_uri.user = proxy_uri.password = nil
|
14
14
|
# awful patch for net-http-persistent 2.8 not unescaping user/password
|
@@ -16,9 +16,10 @@ module Faraday
|
|
16
16
|
define_method(:user) { proxy[:user] }
|
17
17
|
define_method(:password) { proxy[:password] }
|
18
18
|
end if proxy[:user]
|
19
|
+
return Net::HTTP::Persistent.new 'Faraday', proxy_uri
|
19
20
|
end
|
20
21
|
|
21
|
-
|
22
|
+
Net::HTTP::Persistent.new 'Faraday'
|
22
23
|
end
|
23
24
|
|
24
25
|
def perform_request(http, env)
|
@@ -3,11 +3,6 @@ module Faraday
|
|
3
3
|
class Patron < Faraday::Adapter
|
4
4
|
dependency 'patron'
|
5
5
|
|
6
|
-
def initialize(app, &block)
|
7
|
-
super(app)
|
8
|
-
@block = block
|
9
|
-
end
|
10
|
-
|
11
6
|
def call(env)
|
12
7
|
super
|
13
8
|
|
@@ -73,7 +68,7 @@ module Faraday
|
|
73
68
|
def create_session
|
74
69
|
session = ::Patron::Session.new
|
75
70
|
session.insecure = true
|
76
|
-
@
|
71
|
+
@config_block.call(session) if @config_block
|
77
72
|
session
|
78
73
|
end
|
79
74
|
end
|
data/lib/faraday/adapter.rb
CHANGED
@@ -30,6 +30,12 @@ module Faraday
|
|
30
30
|
extend Parallelism
|
31
31
|
self.supports_parallel = false
|
32
32
|
|
33
|
+
def initialize(app = nil, opts = {}, &block)
|
34
|
+
super(app)
|
35
|
+
@connection_options = opts
|
36
|
+
@config_block = block
|
37
|
+
end
|
38
|
+
|
33
39
|
def call(env)
|
34
40
|
env.clear_body if env.needs_body?
|
35
41
|
end
|
data/lib/faraday/connection.rb
CHANGED
@@ -55,11 +55,11 @@ module Faraday
|
|
55
55
|
# :user - String (optional)
|
56
56
|
# :password - String (optional)
|
57
57
|
def initialize(url = nil, options = nil)
|
58
|
+
options = ConnectionOptions.from(options)
|
59
|
+
|
58
60
|
if url.is_a?(Hash)
|
59
|
-
options = options
|
61
|
+
options = options.merge(url)
|
60
62
|
url = options.url
|
61
|
-
else
|
62
|
-
options = ConnectionOptions.from(options)
|
63
63
|
end
|
64
64
|
|
65
65
|
@parallel_manager = nil
|
@@ -81,11 +81,20 @@ module Faraday
|
|
81
81
|
|
82
82
|
@proxy = nil
|
83
83
|
proxy(options.fetch(:proxy) {
|
84
|
-
uri =
|
85
|
-
if
|
86
|
-
|
87
|
-
|
84
|
+
uri = nil
|
85
|
+
if URI.parse("").respond_to?(:find_proxy)
|
86
|
+
case url
|
87
|
+
when String
|
88
|
+
uri = URI.parse(url).find_proxy
|
89
|
+
when URI
|
90
|
+
uri = url.find_proxy
|
91
|
+
when nil
|
92
|
+
uri = find_default_proxy
|
93
|
+
end
|
94
|
+
else
|
95
|
+
uri = find_default_proxy
|
88
96
|
end
|
97
|
+
uri
|
89
98
|
})
|
90
99
|
|
91
100
|
yield(self) if block_given?
|
@@ -433,5 +442,14 @@ module Faraday
|
|
433
442
|
header(*args)
|
434
443
|
headers[Faraday::Request::Authorization::KEY] = header
|
435
444
|
end
|
445
|
+
|
446
|
+
def find_default_proxy
|
447
|
+
warn 'no_proxy is unsupported' if ENV['no_proxy'] || ENV['NO_PROXY']
|
448
|
+
uri = ENV['http_proxy']
|
449
|
+
if uri && !uri.empty?
|
450
|
+
uri = 'http://' + uri if uri !~ /^http/i
|
451
|
+
uri
|
452
|
+
end
|
453
|
+
end
|
436
454
|
end
|
437
455
|
end
|
data/lib/faraday/options.rb
CHANGED
@@ -18,23 +18,20 @@ module Faraday
|
|
18
18
|
# Public
|
19
19
|
def update(obj)
|
20
20
|
obj.each do |key, value|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
value.
|
26
|
-
|
27
|
-
|
28
|
-
value = hash
|
21
|
+
sub_options = self.class.options_for(key)
|
22
|
+
if sub_options
|
23
|
+
new_value = sub_options.from(value) if value
|
24
|
+
elsif value.is_a?(Hash)
|
25
|
+
new_value = value.dup
|
26
|
+
else
|
27
|
+
new_value = value
|
29
28
|
end
|
30
29
|
|
31
|
-
self.send("#{key}=",
|
30
|
+
self.send("#{key}=", new_value) unless new_value.nil?
|
32
31
|
end
|
33
32
|
self
|
34
33
|
end
|
35
34
|
|
36
|
-
alias merge! update
|
37
|
-
|
38
35
|
# Public
|
39
36
|
def delete(key)
|
40
37
|
value = send(key)
|
@@ -48,8 +45,24 @@ module Faraday
|
|
48
45
|
end
|
49
46
|
|
50
47
|
# Public
|
51
|
-
def merge(
|
52
|
-
|
48
|
+
def merge!(other)
|
49
|
+
other.each do |key, other_value|
|
50
|
+
self_value = self.send(key)
|
51
|
+
sub_options = self.class.options_for(key)
|
52
|
+
new_value = sub_options ? self_value.merge(other_value) : other_value
|
53
|
+
self.send("#{key}=", new_value) unless new_value.nil?
|
54
|
+
end
|
55
|
+
self
|
56
|
+
end
|
57
|
+
|
58
|
+
# Public
|
59
|
+
def merge(other)
|
60
|
+
dup.merge!(other)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Public
|
64
|
+
def deep_dup
|
65
|
+
self.class.from(self)
|
53
66
|
end
|
54
67
|
|
55
68
|
# Public
|
@@ -189,8 +202,7 @@ module Faraday
|
|
189
202
|
end
|
190
203
|
|
191
204
|
class RequestOptions < Options.new(:params_encoder, :proxy, :bind,
|
192
|
-
:timeout, :open_timeout, :boundary,
|
193
|
-
:oauth)
|
205
|
+
:timeout, :open_timeout, :boundary, :oauth, :context)
|
194
206
|
|
195
207
|
def []=(key, value)
|
196
208
|
if key && key.to_sym == :proxy
|
@@ -1,13 +1,14 @@
|
|
1
1
|
require File.expand_path("../url_encoded", __FILE__)
|
2
|
+
require 'securerandom'
|
2
3
|
|
3
4
|
module Faraday
|
4
5
|
class Request::Multipart < Request::UrlEncoded
|
5
6
|
self.mime_type = 'multipart/form-data'.freeze
|
6
|
-
|
7
|
+
DEFAULT_BOUNDARY_PREFIX = "-----------RubyMultipartPost".freeze unless defined? DEFAULT_BOUNDARY_PREFIX
|
7
8
|
|
8
9
|
def call(env)
|
9
10
|
match_content_type(env) do |params|
|
10
|
-
env.request.boundary ||=
|
11
|
+
env.request.boundary ||= unique_boundary
|
11
12
|
env.request_headers[CONTENT_TYPE] += "; boundary=#{env.request.boundary}"
|
12
13
|
env.body = create_multipart(env, params)
|
13
14
|
end
|
@@ -44,6 +45,10 @@ module Faraday
|
|
44
45
|
return body
|
45
46
|
end
|
46
47
|
|
48
|
+
def unique_boundary
|
49
|
+
"#{DEFAULT_BOUNDARY_PREFIX}-#{SecureRandom.hex}"
|
50
|
+
end
|
51
|
+
|
47
52
|
def process_params(params, prefix = nil, pieces = nil, &block)
|
48
53
|
params.inject(pieces || []) do |all, (key, value)|
|
49
54
|
key = "#{prefix}[#{key}]" if prefix
|
@@ -10,7 +10,7 @@ module Faraday
|
|
10
10
|
#
|
11
11
|
# Faraday.new do |conn|
|
12
12
|
# conn.request :retry, max: 2, interval: 0.05,
|
13
|
-
# interval_randomness: 0.5, backoff_factor: 2
|
13
|
+
# interval_randomness: 0.5, backoff_factor: 2,
|
14
14
|
# exceptions: [CustomException, 'Timeout::Error']
|
15
15
|
# conn.adapter ...
|
16
16
|
# end
|
@@ -27,7 +27,7 @@ module Faraday
|
|
27
27
|
DEFAULT_CHECK = lambda { |env,exception| false }
|
28
28
|
|
29
29
|
def self.from(value)
|
30
|
-
if
|
30
|
+
if Integer === value
|
31
31
|
new(value)
|
32
32
|
else
|
33
33
|
super(value)
|
@@ -117,6 +117,7 @@ module Faraday
|
|
117
117
|
rescue @errmatch => exception
|
118
118
|
if retries > 0 && retry_request?(env, exception)
|
119
119
|
retries -= 1
|
120
|
+
rewind_files(request_body)
|
120
121
|
sleep sleep_amount(retries + 1)
|
121
122
|
retry
|
122
123
|
end
|
@@ -150,5 +151,13 @@ module Faraday
|
|
150
151
|
@options.methods.include?(env[:method]) || @options.retry_if.call(env, exception)
|
151
152
|
end
|
152
153
|
|
154
|
+
def rewind_files(env)
|
155
|
+
env && env.each do |_, value|
|
156
|
+
if value.is_a? UploadIO
|
157
|
+
value.rewind
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
153
162
|
end
|
154
163
|
end
|
@@ -12,22 +12,28 @@ module Faraday
|
|
12
12
|
require 'logger'
|
13
13
|
::Logger.new(STDOUT)
|
14
14
|
end
|
15
|
+
@filter = []
|
15
16
|
@options = DEFAULT_OPTIONS.merge(options)
|
17
|
+
yield self if block_given?
|
16
18
|
end
|
17
19
|
|
18
20
|
def_delegators :@logger, :debug, :info, :warn, :error, :fatal
|
19
21
|
|
20
22
|
def call(env)
|
21
|
-
info "#{env.method} #{env.url.to_s}"
|
22
|
-
debug('request') { dump_headers env.request_headers } if log_headers?(:request)
|
23
|
-
debug('request') { dump_body(env[:body]) } if env[:body] && log_body?(:request)
|
23
|
+
info "#{env.method} #{apply_filters(env.url.to_s)}"
|
24
|
+
debug('request') { apply_filters( dump_headers env.request_headers ) } if log_headers?(:request)
|
25
|
+
debug('request') { apply_filters( dump_body(env[:body]) ) } if env[:body] && log_body?(:request)
|
24
26
|
super
|
25
27
|
end
|
26
28
|
|
27
29
|
def on_complete(env)
|
28
30
|
info('Status') { env.status.to_s }
|
29
|
-
debug('response') { dump_headers env.response_headers } if log_headers?(:response)
|
30
|
-
debug('response') { dump_body env[:body] } if env[:body] && log_body?(:response)
|
31
|
+
debug('response') { apply_filters( dump_headers env.response_headers ) } if log_headers?(:response)
|
32
|
+
debug('response') { apply_filters( dump_body env[:body] ) } if env[:body] && log_body?(:response)
|
33
|
+
end
|
34
|
+
|
35
|
+
def filter(filter_word, filter_replacement)
|
36
|
+
@filter.push([ filter_word, filter_replacement ])
|
31
37
|
end
|
32
38
|
|
33
39
|
private
|
@@ -62,5 +68,13 @@ module Faraday
|
|
62
68
|
else @options[:bodies]
|
63
69
|
end
|
64
70
|
end
|
71
|
+
|
72
|
+
def apply_filters(output)
|
73
|
+
@filter.each do |pattern, replacement|
|
74
|
+
output = output.to_s.gsub(pattern, replacement)
|
75
|
+
end
|
76
|
+
output
|
77
|
+
end
|
78
|
+
|
65
79
|
end
|
66
80
|
end
|
data/lib/faraday/utils.rb
CHANGED
data/lib/faraday.rb
CHANGED
@@ -14,7 +14,7 @@ require 'forwardable'
|
|
14
14
|
# conn.get '/'
|
15
15
|
#
|
16
16
|
module Faraday
|
17
|
-
VERSION = "0.
|
17
|
+
VERSION = "0.12.0"
|
18
18
|
|
19
19
|
class << self
|
20
20
|
# Public: Gets or sets the root path that Faraday is being loaded from.
|
@@ -34,9 +34,6 @@ module Faraday
|
|
34
34
|
# Faraday.get "https://faraday.com"
|
35
35
|
attr_writer :default_connection
|
36
36
|
|
37
|
-
# Public: Sets the default options used when calling Faraday#new.
|
38
|
-
attr_writer :default_connection_options
|
39
|
-
|
40
37
|
# Public: Initializes a new Faraday::Connection.
|
41
38
|
#
|
42
39
|
# url - The optional String base URL to use as a prefix for all
|
@@ -66,7 +63,7 @@ module Faraday
|
|
66
63
|
# Returns a Faraday::Connection.
|
67
64
|
def new(url = nil, options = nil)
|
68
65
|
block = block_given? ? Proc.new : nil
|
69
|
-
options = options ? default_connection_options.merge(options) : default_connection_options
|
66
|
+
options = options ? default_connection_options.merge(options) : default_connection_options
|
70
67
|
Faraday::Connection.new(url, options, &block)
|
71
68
|
end
|
72
69
|
|
@@ -122,6 +119,11 @@ module Faraday
|
|
122
119
|
@default_connection_options ||= ConnectionOptions.new
|
123
120
|
end
|
124
121
|
|
122
|
+
# Public: Sets the default options used when calling Faraday#new.
|
123
|
+
def self.default_connection_options=(options)
|
124
|
+
@default_connection_options = ConnectionOptions.from(options)
|
125
|
+
end
|
126
|
+
|
125
127
|
unless const_defined? :Timer
|
126
128
|
require 'timeout'
|
127
129
|
Timer = Timeout
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rick Olson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: multipart-post
|