faraday 0.9.2 → 0.12.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 515b1bc445a69d6ccdbb95b042750cd08962053a
4
+ data.tar.gz: 504c63a2fb5c1bb6eec8b8cf9d930bd966984be2
5
+ SHA512:
6
+ metadata.gz: 5da384774bb02aae523a55d2f383ae4f0804757eabd1a398eca2998f6c6279391cb48c39e99d7948d63b4a71953ef98e31eb7e5dad57af5b615c9af007e8e870
7
+ data.tar.gz: 63f74afdb08b2232af31ceb76309466f30a3fc4e4d9edbb2e67d4ecac94e432591edc0932fb32ad3945af261b49c7fc6663e06bc8fbfef1f24a9ce60418cc76a
data/LICENSE.md CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009-2015 Rick Olson, Zack Hobson
1
+ Copyright (c) 2009-2017 Rick Olson, Zack Hobson
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
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][]
@@ -17,8 +21,30 @@ Faraday supports these adapters:
17
21
  It also includes a Rack adapter for hitting loaded Rack applications through
18
22
  Rack::Test, and a Test adapter for stubbing requests by hand.
19
23
 
24
+ ## API documentation
25
+
26
+ Available at [rubydoc.info](http://www.rubydoc.info/gems/faraday).
27
+
20
28
  ## Usage
21
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
+ Since the default middleware stack uses url\_encoded middleware and default adapter, use them on building your own middleware stack.
47
+
22
48
  ```ruby
23
49
  conn = Faraday.new(:url => 'http://sushi.com') do |faraday|
24
50
  faraday.request :url_encoded # form-encode POST params
@@ -26,6 +52,20 @@ conn = Faraday.new(:url => 'http://sushi.com') do |faraday|
26
52
  faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
27
53
  end
28
54
 
55
+ # Filter sensitive information from logs with a regex matcher
56
+
57
+ conn = Faraday.new(:url => 'http://sushi.com/api_key=s3cr3t') do |faraday|
58
+ faraday.request :url_encoded # form-encode POST params
59
+ faraday.response :logger do | logger |
60
+ logger.filter(/(api_key=)(\w+)/,'\1[REMOVED]')
61
+ end
62
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
63
+ end
64
+ ```
65
+
66
+ Once you have the connection object, use it to make HTTP requests. You can pass paramters to it in a few different ways:
67
+
68
+ ```ruby
29
69
  ## GET ##
30
70
 
31
71
  response = conn.get '/nigiri/sake.json' # GET http://sushi.com/nigiri/sake.json
@@ -33,7 +73,7 @@ response.body
33
73
 
34
74
  conn.get '/nigiri', { :name => 'Maguro' } # GET http://sushi.com/nigiri?name=Maguro
35
75
 
36
- conn.get do |req| # GET http://sushi.com/search?page=2&limit=100
76
+ conn.get do |req| # GET http://sushi.com/search?page=2&limit=100
37
77
  req.url '/search', :page => 2
38
78
  req.params['limit'] = 100
39
79
  end
@@ -41,7 +81,11 @@ end
41
81
  ## POST ##
42
82
 
43
83
  conn.post '/nigiri', { :name => 'Maguro' } # POST "name=maguro" to http://sushi.com/nigiri
84
+ ```
44
85
 
86
+ Some configuration options can be adjusted per request:
87
+
88
+ ```ruby
45
89
  # post payload as JSON instead of "www-form-urlencoded" encoding:
46
90
  conn.post do |req|
47
91
  req.url '/nigiri'
@@ -58,11 +102,18 @@ conn.get do |req|
58
102
  end
59
103
  ```
60
104
 
61
- If you don't need to set up anything, you can roll with just the default middleware
62
- stack and default adapter (see [Faraday::RackBuilder#initialize](https://github.com/lostisland/faraday/blob/master/lib/faraday/rack_builder.rb)):
105
+ And you can inject arbitrary data into the request using the `context` option:
63
106
 
64
107
  ```ruby
65
- response = Faraday.get 'http://sushi.com/nigiri/sake.json'
108
+ # Anything you inject using context option will be available in the env on all middlewares
109
+
110
+ conn.get do |req|
111
+ req.url '/search'
112
+ req.options.context = {
113
+ foo: 'foo',
114
+ bar: 'bar'
115
+ }
116
+ end
66
117
  ```
67
118
 
68
119
  ### Changing how parameters are serialized
@@ -73,7 +124,7 @@ either per-connection or per-request basis.
73
124
 
74
125
  ```ruby
75
126
  # per-connection setting
76
- conn = Faraday.new :params_encoder => Faraday::FlatParamsEncoder
127
+ conn = Faraday.new :request => { :params_encoder => Faraday::FlatParamsEncoder }
77
128
 
78
129
  conn.get do |req|
79
130
  # per-request setting:
@@ -91,6 +142,20 @@ The value of Faraday `params_encoder` can be any object that responds to:
91
142
  The encoder will affect both how query strings are processed and how POST bodies
92
143
  get serialized. The default encoder is Faraday::NestedParamsEncoder.
93
144
 
145
+ ## Authentication
146
+
147
+ Basic and Token authentication are handled by Faraday::Request::BasicAuthentication and Faraday::Request::TokenAuthentication respectively. These can be added as middleware manually or through the helper methods.
148
+
149
+ ```ruby
150
+ Faraday.new(...) do |conn|
151
+ conn.basic_auth('username', 'password')
152
+ end
153
+
154
+ Faraday.new(...) do |conn|
155
+ conn.token_auth('authentication-token')
156
+ end
157
+ ```
158
+
94
159
  ## Advanced middleware usage
95
160
 
96
161
  The order in which middleware is stacked is important. Like with Rack, the
@@ -103,6 +168,7 @@ Faraday.new(...) do |conn|
103
168
  conn.request :multipart
104
169
  conn.request :url_encoded
105
170
 
171
+ # Last middleware must be the adapter:
106
172
  conn.adapter :net_http
107
173
  end
108
174
  ```
@@ -165,6 +231,53 @@ later, response. Some keys are:
165
231
  :response_headers
166
232
  ```
167
233
 
234
+ ## Ad-hoc adapters customization
235
+
236
+ 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.
237
+
238
+ 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.
239
+
240
+ ### NetHttp
241
+ ```ruby
242
+ conn = Faraday.new(...) do |f|
243
+ f.adapter :net_http do |http| # yields Net::HTTP
244
+ http.idle_timeout = 100
245
+ http.verify_callback = lambda do | preverify_ok, cert_store |
246
+ # do something here...
247
+ end
248
+ end
249
+ end
250
+ ```
251
+
252
+ ### NetHttpPersistent
253
+ ```ruby
254
+ conn = Faraday.new(...) do |f|
255
+ f.adapter :net_http_persistent do |http| # yields Net::HTTP::Persistent
256
+ http.idle_timeout = 100
257
+ http.retry_change_requests = true
258
+ end
259
+ end
260
+ ```
261
+
262
+ ### Patron
263
+ ```ruby
264
+ conn = Faraday.new(...) do |f|
265
+ f.adapter :patron do |session| # yields Patron::Session
266
+ session.max_redirects = 10
267
+ end
268
+ end
269
+ ```
270
+
271
+ ### HTTPClient
272
+ ```ruby
273
+ conn = Faraday.new(...) do |f|
274
+ f.adapter :httpclient do |client| # yields HTTPClient
275
+ client.keep_alive_timeout = 20
276
+ client.ssl_config.timeout = 25
277
+ end
278
+ end
279
+ ```
280
+
168
281
  ## Using Faraday for testing
169
282
 
170
283
  ```ruby
@@ -210,7 +323,7 @@ stubs.verify_stubbed_calls
210
323
  This library aims to support and is [tested against][travis] the following Ruby
211
324
  implementations:
212
325
 
213
- * Ruby 1.8.7+
326
+ * Ruby 1.9.3+
214
327
  * [JRuby][] 1.7+
215
328
  * [Rubinius][] 2+
216
329
 
@@ -229,15 +342,14 @@ of a major release, support for that Ruby version may be dropped.
229
342
 
230
343
  ## Copyright
231
344
 
232
- Copyright (c) 2009-2013 [Rick Olson](mailto:technoweenie@gmail.com), Zack Hobson.
345
+ Copyright (c) 2009-2017 [Rick Olson](mailto:technoweenie@gmail.com), Zack Hobson.
233
346
  See [LICENSE][] for details.
234
347
 
235
348
  [net_http]: http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/Net/HTTP.html
236
349
  [persistent]: https://github.com/drbrain/net-http-persistent
237
- [travis]: http://travis-ci.org/lostisland/faraday
238
- [excon]: https://github.com/geemus/excon#readme
239
- [typhoeus]: https://github.com/typhoeus/typhoeus#readme
240
- [patron]: http://toland.github.com/patron/
350
+ [travis]: https://travis-ci.org/lostisland/faraday
351
+ [excon]: https://github.com/excon/excon#readme
352
+ [patron]: http://toland.github.io/patron/
241
353
  [eventmachine]: https://github.com/igrigorik/em-http-request#readme
242
354
  [httpclient]: https://github.com/nahi/httpclient
243
355
  [jruby]: http://jruby.org/
@@ -138,9 +138,11 @@ module Faraday
138
138
 
139
139
  # TODO: reuse the connection to support pipelining
140
140
  def perform_single_request(env)
141
- req = EventMachine::HttpRequest.new(env[:url], connection_config(env))
141
+ req = create_request(env)
142
142
  req.setup_request(env[:method], request_config(env)).callback { |client|
143
- save_response(env, client.response_header.status, client.response) do |resp_headers|
143
+ status = client.response_header.status
144
+ reason = client.response_header.http_reason
145
+ save_response(env, status, client.response, nil, reason) do |resp_headers|
144
146
  client.response_header.each do |name, value|
145
147
  resp_headers[name.to_sym] = value
146
148
  end
@@ -148,6 +150,10 @@ module Faraday
148
150
  }
149
151
  end
150
152
 
153
+ def create_request(env)
154
+ EventMachine::HttpRequest.new(env[:url], connection_config(env).merge(@connection_options))
155
+ end
156
+
151
157
  def error_message(client)
152
158
  client.error or "request failed"
153
159
  end
@@ -39,7 +39,7 @@ module EmHttpSslPatch
39
39
  end
40
40
 
41
41
  def host
42
- parent.connopts.host
42
+ parent.uri.host
43
43
  end
44
44
 
45
45
  def certificate_store
@@ -19,7 +19,7 @@ module Faraday
19
19
 
20
20
  def call(env)
21
21
  super
22
- request = EventMachine::HttpRequest.new(Utils::URI(env[:url].to_s), connection_config(env))
22
+ request = create_request(env)
23
23
 
24
24
  http_method = env[:method].to_s.downcase.to_sym
25
25
 
@@ -54,7 +54,9 @@ module Faraday
54
54
 
55
55
  raise client.error if client.error
56
56
 
57
- save_response(env, client.response_header.status, client.response) do |resp_headers|
57
+ status = client.response_header.status
58
+ reason = client.response_header.http_reason
59
+ save_response(env, status, client.response, nil, reason) do |resp_headers|
58
60
  client.response_header.each do |name, value|
59
61
  resp_headers[name.to_sym] = value
60
62
  end
@@ -85,6 +87,10 @@ module Faraday
85
87
  raise
86
88
  end
87
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
88
94
  end
89
95
  end
90
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,14 +45,14 @@ module Faraday
50
45
  end
51
46
  end
52
47
 
53
- conn = ::Excon.new(env[:url].to_s, opts.merge(@connection_options))
48
+ conn = create_connection(env, opts)
54
49
 
55
50
  resp = conn.request \
56
51
  :method => env[:method].to_s.upcase,
57
52
  :headers => env[:request_headers],
58
53
  :body => read_body(env)
59
54
 
60
- save_response(env, resp.status.to_i, resp.body, resp.headers)
55
+ save_response(env, resp.status.to_i, resp.body, resp.headers, resp.reason_phrase)
61
56
 
62
57
  @app.call env
63
58
  rescue ::Excon::Errors::SocketError => err
@@ -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
@@ -37,7 +39,7 @@ module Faraday
37
39
  :body => env[:body],
38
40
  :header => env[:request_headers]
39
41
 
40
- save_response env, resp.status, resp.body, resp.headers
42
+ save_response env, resp.status, resp.body, resp.headers, resp.reason
41
43
 
42
44
  @app.call env
43
45
  rescue ::HTTPClient::TimeoutError, Errno::ETIMEDOUT
@@ -48,7 +50,7 @@ module Faraday
48
50
  else
49
51
  raise Faraday::Error::ClientError, $!
50
52
  end
51
- rescue Errno::ECONNREFUSED, EOFError
53
+ rescue Errno::ECONNREFUSED, IOError, SocketError
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
- # Use the default cert store by default, i.e. system ca certs
101
- cert_store = OpenSSL::X509::Store.new
102
- cert_store.set_default_paths
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,13 +10,14 @@ module Faraday
10
10
  class Adapter
11
11
  class NetHttp < Faraday::Adapter
12
12
  NET_HTTP_EXCEPTIONS = [
13
- EOFError,
13
+ IOError,
14
14
  Errno::ECONNABORTED,
15
15
  Errno::ECONNREFUSED,
16
16
  Errno::ECONNRESET,
17
17
  Errno::EHOSTUNREACH,
18
18
  Errno::EINVAL,
19
19
  Errno::ENETUNREACH,
20
+ Errno::EPIPE,
20
21
  Net::HTTPBadResponse,
21
22
  Net::HTTPHeaderSyntaxError,
22
23
  Net::ProtocolError,
@@ -31,10 +32,7 @@ module Faraday
31
32
  super
32
33
  with_net_http_connection(env) do |http|
33
34
  configure_ssl(http, env[:ssl]) if env[:url].scheme == 'https' and env[:ssl]
34
-
35
- req = env[:request]
36
- http.read_timeout = http.open_timeout = req[:timeout] if req[:timeout]
37
- http.open_timeout = req[:open_timeout] if req[:open_timeout]
35
+ configure_request(http, env[:request])
38
36
 
39
37
  begin
40
38
  http_response = perform_request(http, env)
@@ -46,7 +44,7 @@ module Faraday
46
44
  end
47
45
  end
48
46
 
49
- save_response(env, http_response.code.to_i, http_response.body || '') do |response_headers|
47
+ save_response(env, http_response.code.to_i, http_response.body || '', nil, http_response.message) do |response_headers|
50
48
  http_response.each_header do |key, value|
51
49
  response_headers[key] = value
52
50
  end
@@ -108,6 +106,13 @@ module Faraday
108
106
  http.ssl_version = ssl[:version] if ssl[:version]
109
107
  end
110
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
+
111
116
  def ssl_cert_store(ssl)
112
117
  return ssl[:cert_store] if ssl[:cert_store]
113
118
  # Use the default cert store by default, i.e. system ca certs