faraday 0.13.1 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: e3cf028ad2c8ac0b1a137ace67fe066cf993af87
4
- data.tar.gz: 60250bf4b1c574825c8855351ffb1cc8157bf6f8
2
+ SHA256:
3
+ metadata.gz: ff30e260051828899307875d73f5ccb1fdcdb4bb40fba3df707e2d6966675cb1
4
+ data.tar.gz: 4bc5b5df30090ae639a937d180f0cc2d7b66a163dca438be8049eef0a5b2a483
5
5
  SHA512:
6
- metadata.gz: f41b772f396a8c2a0fb0dac9e613230ee3efaee5954951ecdbe48d4fcccc6ba74ce9a069b222be56901d383fd9d66c223291a852c5f914f34e29432c7d801b41
7
- data.tar.gz: b53cca6785e8ba152235e347505e5f1a0ea4e6eb5e9167582d2943668db2180a0ff31006d868b3c20107528db73576951124fdc9ee2364571917b9a9b28bef33
6
+ metadata.gz: 6edafc07bcf5c647f82ab5e50aa8cd7c319e5198559bd7350394fd3fb918e96f1a450fa4eb175ee3b7db9e03880cc2534c3b52b3a213cdc852fe094953603e13
7
+ data.tar.gz: 9d2951f42b5e8ae17505bc92493ee8ef2814412de9ade880adf20c081a02e6437da7174a064ca68f37483793e48efdf67aeb145e07ca3a90c71ac80014926070
data/README.md CHANGED
@@ -11,7 +11,7 @@ Faraday is an HTTP client lib that provides a common interface over many
11
11
  adapters (such as Net::HTTP) and embraces the concept of Rack middleware when
12
12
  processing the request/response cycle.
13
13
 
14
- Faraday supports these adapters:
14
+ Faraday supports these adapters out of the box:
15
15
 
16
16
  * [Net::HTTP][net_http] _(default)_
17
17
  * [Net::HTTP::Persistent][persistent]
@@ -20,6 +20,10 @@ Faraday supports these adapters:
20
20
  * [EventMachine][]
21
21
  * [HTTPClient][]
22
22
 
23
+ Adapters are slowly being moved into their own gems, or bundled with HTTP clients:
24
+
25
+ * [Typhoeus][]
26
+
23
27
  It also includes a Rack adapter for hitting loaded Rack applications through
24
28
  Rack::Test, and a Test adapter for stubbing requests by hand.
25
29
 
@@ -40,8 +44,8 @@ stack and default adapter (see [Faraday::RackBuilder#initialize](https://github.
40
44
  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:
41
45
 
42
46
  ```ruby
43
- conn = Faraday.new(:url => 'http://www.example.com')
44
- response = conn.get '/users' # GET http://www.example.com/users'
47
+ conn = Faraday.new(:url => 'http://www.example.com')
48
+ response = conn.get '/users' # GET http://www.example.com/users'
45
49
  ```
46
50
 
47
51
  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.
@@ -145,7 +149,7 @@ The encoder will affect both how query strings are processed and how POST bodies
145
149
  get serialized. The default encoder is Faraday::NestedParamsEncoder.
146
150
 
147
151
  ## Authentication
148
-
152
+
149
153
  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.
150
154
 
151
155
  ```ruby
@@ -154,10 +158,26 @@ Faraday.new(...) do |conn|
154
158
  end
155
159
 
156
160
  Faraday.new(...) do |conn|
157
- conn.token_auth('authentication-token')
161
+ conn.token_auth('authentication-token')
158
162
  end
159
163
  ```
160
164
 
165
+ ## Proxy
166
+
167
+ Faraday will try to automatically infer the proxy settings from your system using `URI#find_proxy`.
168
+ This will retrieve them from environment variables such as http_proxy, ftp_proxy, no_proxy, etc.
169
+ If for any reason you want to disable this behaviour, you can do so by setting the global varibale `ignore_env_proxy`:
170
+
171
+ ```ruby
172
+ Faraday.ignore_env_proxy = true
173
+ ```
174
+
175
+ You can also specify a custom proxy when initializing the connection
176
+
177
+ ```ruby
178
+ Faraday.new('http://www.example.com', :proxy => 'http://proxy.com')
179
+ ```
180
+
161
181
  ## Advanced middleware usage
162
182
 
163
183
  The order in which middleware is stacked is important. Like with Rack, the
@@ -235,7 +255,7 @@ later, response. Some keys are:
235
255
 
236
256
  ## Ad-hoc adapters customization
237
257
 
238
- 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.
258
+ 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.
239
259
 
240
260
  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.
241
261
 
@@ -341,7 +361,7 @@ of a major release, support for that Ruby version may be dropped.
341
361
 
342
362
  Do you want to contribute to Faraday?
343
363
  Open the issues page and check for the `any volunteer?` label!
344
- But before you start coding, please read our [Contributing Guide](https://github.com/lostisland/faraday/blob/master/CONTRIBUTING.md)
364
+ But before you start coding, please read our [Contributing Guide](https://github.com/lostisland/faraday/blob/master/.github/CONTRIBUTING.md)
345
365
 
346
366
  ## Copyright
347
367
 
@@ -355,6 +375,7 @@ See [LICENSE][] for details.
355
375
  [patron]: http://toland.github.io/patron/
356
376
  [eventmachine]: https://github.com/igrigorik/em-http-request#readme
357
377
  [httpclient]: https://github.com/nahi/httpclient
378
+ [typhoeus]: https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/adapters/faraday.rb
358
379
  [jruby]: http://jruby.org/
359
380
  [rubinius]: http://rubini.us/
360
381
  [license]: LICENSE.md
@@ -14,7 +14,7 @@ require 'forwardable'
14
14
  # conn.get '/'
15
15
  #
16
16
  module Faraday
17
- VERSION = "0.13.1"
17
+ VERSION = "0.14.0"
18
18
 
19
19
  class << self
20
20
  # Public: Gets or sets the root path that Faraday is being loaded from.
@@ -34,6 +34,9 @@ module Faraday
34
34
  # Faraday.get "https://faraday.com"
35
35
  attr_writer :default_connection
36
36
 
37
+ # Public: Tells faraday to ignore the environment proxy (http_proxy).
38
+ attr_accessor :ignore_env_proxy
39
+
37
40
  # Public: Initializes a new Faraday::Connection.
38
41
  #
39
42
  # url - The optional String base URL to use as a prefix for all
@@ -101,6 +104,7 @@ module Faraday
101
104
  end
102
105
  end
103
106
 
107
+ self.ignore_env_proxy = false
104
108
  self.root_path = File.expand_path "..", __FILE__
105
109
  self.lib_path = File.expand_path "../faraday", __FILE__
106
110
  self.default_adapter = :net_http
@@ -30,7 +30,6 @@ module Faraday
30
30
 
31
31
  if req[:open_timeout]
32
32
  opts[:connect_timeout] = req[:open_timeout]
33
- opts[:write_timeout] = req[:open_timeout]
34
33
  end
35
34
 
36
35
  if req[:proxy]
@@ -37,7 +37,7 @@ module Faraday
37
37
 
38
38
  @app.call env
39
39
  rescue ::Patron::TimeoutError => err
40
- if err.message == "Connection time-out"
40
+ if connection_timed_out_message?(err.message)
41
41
  raise Faraday::Error::ConnectionFailed, err
42
42
  else
43
43
  raise Faraday::Error::TimeoutError, err
@@ -78,6 +78,23 @@ module Faraday
78
78
  session.insecure = true
79
79
  end
80
80
  end
81
+
82
+ private
83
+
84
+ CURL_TIMEOUT_MESSAGES = [ "Connection time-out",
85
+ "Connection timed out",
86
+ "Timed out before name resolve",
87
+ "server connect has timed out",
88
+ "Resolving timed out",
89
+ "name lookup timed out",
90
+ "timed out before SSL",
91
+ "connect() timed out"
92
+ ].freeze
93
+
94
+ def connection_timed_out_message?(message)
95
+ CURL_TIMEOUT_MESSAGES.any? { |curl_message| message.include?(curl_message) }
96
+ end
97
+
81
98
  end
82
99
  end
83
100
  end
@@ -53,17 +53,17 @@ module Faraday
53
53
  @stack.empty?
54
54
  end
55
55
 
56
- def match(request_method, path, headers, body)
56
+ def match(request_method, host, path, headers, body)
57
57
  return false if !@stack.key?(request_method)
58
58
  stack = @stack[request_method]
59
59
  consumed = (@consumed[request_method] ||= [])
60
60
 
61
- stub, meta = matches?(stack, path, headers, body)
61
+ stub, meta = matches?(stack, host, path, headers, body)
62
62
  if stub
63
63
  consumed << stack.delete(stub)
64
64
  return stub, meta
65
65
  end
66
- matches?(consumed, path, headers, body)
66
+ matches?(consumed, host, path, headers, body)
67
67
  end
68
68
 
69
69
  def get(path, headers = {}, &block)
@@ -110,29 +110,35 @@ module Faraday
110
110
  protected
111
111
 
112
112
  def new_stub(request_method, path, headers = {}, body=nil, &block)
113
- normalized_path = path.is_a?(Regexp) ? path : Faraday::Utils.normalize_path(path)
114
- (@stack[request_method] ||= []) << Stub.new(normalized_path, headers, body, block)
113
+ normalized_path, host =
114
+ if path.is_a?(Regexp)
115
+ path
116
+ else
117
+ [Faraday::Utils.normalize_path(path), Faraday::Utils.URI(path).host]
118
+ end
119
+
120
+ (@stack[request_method] ||= []) << Stub.new(host, normalized_path, headers, body, block)
115
121
  end
116
122
 
117
- def matches?(stack, path, headers, body)
123
+ def matches?(stack, host, path, headers, body)
118
124
  stack.each do |stub|
119
- match_result, meta = stub.matches?(path, headers, body)
125
+ match_result, meta = stub.matches?(host, path, headers, body)
120
126
  return stub, meta if match_result
121
127
  end
122
128
  nil
123
129
  end
124
130
  end
125
131
 
126
- class Stub < Struct.new(:path, :params, :headers, :body, :block)
127
- def initialize(full, headers, body, block)
132
+ class Stub < Struct.new(:host, :path, :params, :headers, :body, :block)
133
+ def initialize(host, full, headers, body, block)
128
134
  path, query = full.respond_to?(:split) ? full.split("?") : full
129
135
  params = query ?
130
136
  Faraday::Utils.parse_nested_query(query) :
131
137
  {}
132
- super(path, params, headers, body, block)
138
+ super(host, path, params, headers, body, block)
133
139
  end
134
140
 
135
- def matches?(request_uri, request_headers, request_body)
141
+ def matches?(request_host, request_uri, request_headers, request_body)
136
142
  request_path, request_query = request_uri.split('?')
137
143
  request_params = request_query ?
138
144
  Faraday::Utils.parse_nested_query(request_query) :
@@ -140,7 +146,8 @@ module Faraday
140
146
  # meta is a hash use as carrier
141
147
  # that will be yielded to consumer block
142
148
  meta = {}
143
- return path_match?(request_path, meta) &&
149
+ return (host.nil? || host == request_host) &&
150
+ path_match?(request_path, meta) &&
144
151
  params_match?(request_params) &&
145
152
  (body.to_s.size.zero? || request_body == body) &&
146
153
  headers_match?(request_headers), meta
@@ -183,10 +190,11 @@ module Faraday
183
190
 
184
191
  def call(env)
185
192
  super
193
+ host = env[:url].host
186
194
  normalized_path = Faraday::Utils.normalize_path(env[:url])
187
195
  params_encoder = env.request.params_encoder || Faraday::Utils.default_params_encoder
188
196
 
189
- stub, meta = stubs.match(env[:method], normalized_path, env.request_headers, env[:body])
197
+ stub, meta = stubs.match(env[:method], host, normalized_path, env.request_headers, env[:body])
190
198
  if stub
191
199
  env[:params] = (query = env[:url].query) ?
192
200
  params_encoder.decode(query) : {}
@@ -1,123 +1,12 @@
1
1
  module Faraday
2
2
  class Adapter
3
+ # This class is just a stub, the real adapter is in https://github.com/philsturgeon/typhoeus/blob/master/lib/typhoeus/adapters/faraday.rb
3
4
  class Typhoeus < Faraday::Adapter
4
- self.supports_parallel = true
5
-
6
- def self.setup_parallel_manager(options = {})
7
- options.empty? ? ::Typhoeus::Hydra.hydra : ::Typhoeus::Hydra.new(options)
8
- end
5
+ # Needs to define this method in order to support Typhoeus <= 1.3.0
6
+ def call; end
9
7
 
10
8
  dependency 'typhoeus'
11
-
12
- def call(env)
13
- super
14
- perform_request env
15
- @app.call env
16
- end
17
-
18
- def perform_request(env)
19
- read_body env
20
-
21
- hydra = env[:parallel_manager] || self.class.setup_parallel_manager
22
- hydra.queue request(env)
23
- hydra.run unless parallel?(env)
24
- rescue Errno::ECONNREFUSED
25
- raise Error::ConnectionFailed, $!
26
- end
27
-
28
- # TODO: support streaming requests
29
- def read_body(env)
30
- env[:body] = env[:body].read if env[:body].respond_to? :read
31
- end
32
-
33
- def request(env)
34
- method = env[:method]
35
- # For some reason, prevents Typhoeus from using "100-continue".
36
- # We want this because Webrick 1.3.1 can't seem to handle it w/ PUT.
37
- method = method.to_s.upcase if method == :put
38
-
39
- req = ::Typhoeus::Request.new env[:url].to_s,
40
- :method => method,
41
- :body => env[:body],
42
- :headers => env[:request_headers],
43
- :disable_ssl_peer_verification => (env[:ssl] && env[:ssl].disable?)
44
-
45
- configure_ssl req, env
46
- configure_proxy req, env
47
- configure_timeout req, env
48
- configure_socket req, env
49
-
50
- req.on_complete do |resp|
51
- if resp.timed_out?
52
- if parallel?(env)
53
- # TODO: error callback in async mode
54
- else
55
- raise Faraday::Error::TimeoutError, "request timed out"
56
- end
57
- end
58
-
59
- case resp.curl_return_code
60
- when 0
61
- # everything OK
62
- when 7
63
- raise Error::ConnectionFailed, resp.curl_error_message
64
- when 60
65
- raise Faraday::SSLError, resp.curl_error_message
66
- else
67
- raise Error::ClientError, resp.curl_error_message
68
- end
69
-
70
- save_response(env, resp.code, resp.body) do |response_headers|
71
- response_headers.parse resp.headers
72
- end
73
- # in async mode, :response is initialized at this point
74
- env[:response].finish(env) if parallel?(env)
75
- end
76
-
77
- req
78
- end
79
-
80
- def configure_ssl(req, env)
81
- ssl = env[:ssl]
82
-
83
- req.ssl_version = ssl[:version] if ssl[:version]
84
- req.ssl_cert = ssl[:client_cert] if ssl[:client_cert]
85
- req.ssl_key = ssl[:client_key] if ssl[:client_key]
86
- req.ssl_cacert = ssl[:ca_file] if ssl[:ca_file]
87
- req.ssl_capath = ssl[:ca_path] if ssl[:ca_path]
88
- end
89
-
90
- def configure_proxy(req, env)
91
- proxy = request_options(env)[:proxy]
92
- return unless proxy
93
-
94
- req.proxy = "#{proxy[:uri].host}:#{proxy[:uri].port}"
95
-
96
- if proxy[:user] && proxy[:password]
97
- req.proxy_username = proxy[:user]
98
- req.proxy_password = proxy[:password]
99
- end
100
- end
101
-
102
- def configure_timeout(req, env)
103
- env_req = request_options(env)
104
- req.timeout = req.connect_timeout = (env_req[:timeout] * 1000) if env_req[:timeout]
105
- req.connect_timeout = (env_req[:open_timeout] * 1000) if env_req[:open_timeout]
106
- end
107
-
108
- def configure_socket(req, env)
109
- if bind = request_options(env)[:bind]
110
- req.interface = bind[:host]
111
- end
112
- end
113
-
114
- def request_options(env)
115
- env[:request]
116
- end
117
-
118
- def parallel?(env)
119
- !!env[:parallel_manager]
120
- end
9
+ dependency 'typhoeus/adapters/faraday'
121
10
  end
122
11
  end
123
12
  end
@@ -53,9 +53,9 @@ module Faraday
53
53
  autoload_all 'faraday/adapter',
54
54
  :NetHttp => 'net_http',
55
55
  :NetHttpPersistent => 'net_http_persistent',
56
- :Typhoeus => 'typhoeus',
57
56
  :EMSynchrony => 'em_synchrony',
58
57
  :EMHttp => 'em_http',
58
+ :Typhoeus => 'typhoeus',
59
59
  :Patron => 'patron',
60
60
  :Excon => 'excon',
61
61
  :Test => 'test',
@@ -82,6 +82,7 @@ module Faraday
82
82
  @params.update(options.params) if options.params
83
83
  @headers.update(options.headers) if options.headers
84
84
 
85
+ @manual_proxy = !!options.proxy
85
86
  @proxy = options.proxy ? ProxyOptions.from(options.proxy) : proxy_from_env(url)
86
87
  @temp_proxy = @proxy
87
88
 
@@ -281,12 +282,14 @@ module Faraday
281
282
  def proxy(arg = nil)
282
283
  return @proxy if arg.nil?
283
284
  warn 'Warning: use of proxy(new_value) to set connection proxy have been DEPRECATED and will be removed in Faraday 1.0'
285
+ @manual_proxy = true
284
286
  @proxy = ProxyOptions.from(arg)
285
287
  end
286
288
 
287
289
  # Public: Sets the Hash proxy options.
288
290
  def proxy=(new_value)
289
- @proxy = ProxyOptions.from(new_value)
291
+ @manual_proxy = true
292
+ @proxy = new_value ? ProxyOptions.from(new_value) : nil
290
293
  end
291
294
 
292
295
  def_delegators :url_prefix, :scheme, :scheme=, :host, :host=, :port, :port=
@@ -307,7 +310,7 @@ module Faraday
307
310
  #
308
311
  # conn.get("nigiri?page=2") # accesses https://sushi.com/api/nigiri
309
312
  #
310
- # Returns the parsed URI from teh given input..
313
+ # Returns the parsed URI from the given input..
311
314
  def url_prefix=(url, encoder = nil)
312
315
  uri = @url_prefix = Utils.URI(url)
313
316
  self.path_prefix = uri.path
@@ -371,10 +374,7 @@ module Faraday
371
374
  end
372
375
 
373
376
  # Resets temp_proxy
374
- @temp_proxy = self.proxy
375
-
376
- # Set temporary proxy if request url is absolute
377
- @temp_proxy = proxy_from_env(url) if url && Utils.URI(url).absolute?
377
+ @temp_proxy = proxy_for_request(url)
378
378
 
379
379
  request = build_request(method) do |req|
380
380
  req.options = req.options.merge(:proxy => @temp_proxy)
@@ -445,6 +445,7 @@ module Faraday
445
445
  end
446
446
 
447
447
  def proxy_from_env(url)
448
+ return if Faraday.ignore_env_proxy
448
449
  uri = nil
449
450
  if URI.parse('').respond_to?(:find_proxy)
450
451
  case url
@@ -470,5 +471,14 @@ module Faraday
470
471
  uri
471
472
  end
472
473
  end
474
+
475
+ def proxy_for_request(url)
476
+ return self.proxy if @manual_proxy
477
+ if url && Utils.URI(url).absolute?
478
+ proxy_from_env(url)
479
+ else
480
+ self.proxy
481
+ end
482
+ end
473
483
  end
474
484
  end
@@ -243,8 +243,8 @@ module Faraday
243
243
  super(value)
244
244
  end
245
245
 
246
- memoized(:user) { uri.user && Utils.unescape(uri.user) }
247
- memoized(:password) { uri.password && Utils.unescape(uri.password) }
246
+ memoized(:user) { uri && uri.user && Utils.unescape(uri.user) }
247
+ memoized(:password) { uri && uri.password && Utils.unescape(uri.password) }
248
248
  end
249
249
 
250
250
  class ConnectionOptions < Options.new(:request, :proxy, :ssl, :builder, :url,
@@ -138,6 +138,8 @@ module Faraday
138
138
  #
139
139
  # Returns a Faraday::Response.
140
140
  def build_response(connection, request)
141
+ warn 'WARNING: No adapter was configured for this request' unless adapter_set?
142
+
141
143
  app.call(build_env(connection, request))
142
144
  end
143
145
 
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.13.1
4
+ version: 0.14.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: 2017-08-18 00:00:00.000000000 Z
11
+ date: 2018-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multipart-post
@@ -92,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
92
  version: '0'
93
93
  requirements: []
94
94
  rubyforge_project:
95
- rubygems_version: 2.4.5
95
+ rubygems_version: 2.7.4
96
96
  signing_key:
97
97
  specification_version: 4
98
98
  summary: HTTP/REST API client library.