faraday 0.9.1 → 2.5.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 +5 -5
- data/CHANGELOG.md +554 -0
- data/LICENSE.md +1 -1
- data/README.md +32 -197
- data/Rakefile +4 -68
- data/examples/client_spec.rb +119 -0
- data/examples/client_test.rb +144 -0
- data/lib/faraday/adapter/test.rb +194 -58
- data/lib/faraday/adapter.rb +76 -20
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/connection.rb +341 -212
- data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +183 -0
- data/lib/faraday/error.rb +123 -29
- data/lib/faraday/logging/formatter.rb +106 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +18 -25
- data/lib/faraday/middleware_registry.rb +83 -0
- data/lib/faraday/options/connection_options.rb +22 -0
- data/lib/faraday/options/env.rb +199 -0
- data/lib/faraday/options/proxy_options.rb +32 -0
- data/lib/faraday/options/request_options.rb +22 -0
- data/lib/faraday/options/ssl_options.rb +69 -0
- data/lib/faraday/options.rb +63 -195
- data/lib/faraday/parameters.rb +4 -180
- data/lib/faraday/rack_builder.rb +99 -59
- data/lib/faraday/request/authorization.rb +37 -30
- data/lib/faraday/request/instrumentation.rb +47 -27
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request/url_encoded.rb +48 -24
- data/lib/faraday/request.rb +76 -32
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +22 -48
- data/lib/faraday/response/raise_error.rb +57 -14
- data/lib/faraday/response.rb +32 -35
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +47 -222
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +111 -222
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/test_spec.rb +413 -0
- data/spec/faraday/adapter_registry_spec.rb +28 -0
- data/spec/faraday/adapter_spec.rb +55 -0
- data/spec/faraday/connection_spec.rb +793 -0
- data/spec/faraday/error_spec.rb +60 -0
- data/spec/faraday/middleware_registry_spec.rb +31 -0
- data/spec/faraday/middleware_spec.rb +52 -0
- data/spec/faraday/options/env_spec.rb +76 -0
- data/spec/faraday/options/options_spec.rb +297 -0
- data/spec/faraday/options/proxy_options_spec.rb +44 -0
- data/spec/faraday/options/request_options_spec.rb +19 -0
- data/spec/faraday/params_encoders/flat_spec.rb +42 -0
- data/spec/faraday/params_encoders/nested_spec.rb +150 -0
- data/spec/faraday/rack_builder_spec.rb +317 -0
- data/spec/faraday/request/authorization_spec.rb +83 -0
- data/spec/faraday/request/instrumentation_spec.rb +74 -0
- data/spec/faraday/request/json_spec.rb +111 -0
- data/spec/faraday/request/url_encoded_spec.rb +93 -0
- data/spec/faraday/request_spec.rb +110 -0
- data/spec/faraday/response/json_spec.rb +117 -0
- data/spec/faraday/response/logger_spec.rb +220 -0
- data/spec/faraday/response/raise_error_spec.rb +172 -0
- data/spec/faraday/response_spec.rb +75 -0
- data/spec/faraday/utils/headers_spec.rb +82 -0
- data/spec/faraday/utils_spec.rb +118 -0
- data/spec/faraday_spec.rb +37 -0
- data/spec/spec_helper.rb +132 -0
- data/spec/support/disabling_stub.rb +14 -0
- data/spec/support/fake_safe_buffer.rb +15 -0
- data/spec/support/helper_methods.rb +96 -0
- data/spec/support/shared_examples/adapter.rb +105 -0
- data/spec/support/shared_examples/params_encoder.rb +18 -0
- data/spec/support/shared_examples/request_method.rb +263 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- metadata +81 -109
- data/.document +0 -6
- data/CONTRIBUTING.md +0 -36
- data/Gemfile +0 -25
- data/faraday.gemspec +0 -34
- data/lib/faraday/adapter/em_http.rb +0 -237
- data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
- data/lib/faraday/adapter/em_synchrony.rb +0 -92
- data/lib/faraday/adapter/excon.rb +0 -80
- data/lib/faraday/adapter/httpclient.rb +0 -106
- data/lib/faraday/adapter/net_http.rb +0 -130
- data/lib/faraday/adapter/net_http_persistent.rb +0 -48
- data/lib/faraday/adapter/patron.rb +0 -72
- data/lib/faraday/adapter/rack.rb +0 -58
- data/lib/faraday/adapter/typhoeus.rb +0 -123
- data/lib/faraday/autoload.rb +0 -84
- data/lib/faraday/request/basic_authentication.rb +0 -13
- data/lib/faraday/request/multipart.rb +0 -63
- data/lib/faraday/request/retry.rb +0 -148
- data/lib/faraday/request/token_authentication.rb +0 -15
- data/lib/faraday/upload_io.rb +0 -67
- data/script/cached-bundle +0 -46
- data/script/console +0 -7
- data/script/generate_certs +0 -42
- data/script/package +0 -7
- data/script/proxy-server +0 -42
- data/script/release +0 -17
- data/script/s3-put +0 -71
- data/script/server +0 -36
- data/script/test +0 -172
- data/test/adapters/default_test.rb +0 -14
- data/test/adapters/em_http_test.rb +0 -20
- data/test/adapters/em_synchrony_test.rb +0 -20
- data/test/adapters/excon_test.rb +0 -20
- data/test/adapters/httpclient_test.rb +0 -21
- data/test/adapters/integration.rb +0 -254
- data/test/adapters/logger_test.rb +0 -82
- data/test/adapters/net_http_persistent_test.rb +0 -20
- data/test/adapters/net_http_test.rb +0 -14
- data/test/adapters/patron_test.rb +0 -20
- data/test/adapters/rack_test.rb +0 -31
- data/test/adapters/test_middleware_test.rb +0 -114
- data/test/adapters/typhoeus_test.rb +0 -28
- data/test/authentication_middleware_test.rb +0 -65
- data/test/composite_read_io_test.rb +0 -111
- data/test/connection_test.rb +0 -522
- data/test/env_test.rb +0 -218
- data/test/helper.rb +0 -81
- data/test/live_server.rb +0 -67
- data/test/middleware/instrumentation_test.rb +0 -88
- data/test/middleware/retry_test.rb +0 -177
- data/test/middleware_stack_test.rb +0 -173
- data/test/multibyte.txt +0 -1
- data/test/options_test.rb +0 -252
- data/test/parameters_test.rb +0 -64
- data/test/request_middleware_test.rb +0 -142
- data/test/response_middleware_test.rb +0 -72
- data/test/strawberry.rb +0 -2
- data/test/utils_test.rb +0 -58
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
module Faraday
|
|
2
|
-
class Adapter
|
|
3
|
-
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
|
|
9
|
-
|
|
10
|
-
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
|
|
121
|
-
end
|
|
122
|
-
end
|
|
123
|
-
end
|
data/lib/faraday/autoload.rb
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
module Faraday
|
|
2
|
-
# Internal: Adds the ability for other modules to manage autoloadable
|
|
3
|
-
# constants.
|
|
4
|
-
module AutoloadHelper
|
|
5
|
-
# Internal: Registers the constants to be auto loaded.
|
|
6
|
-
#
|
|
7
|
-
# prefix - The String require prefix. If the path is inside Faraday, then
|
|
8
|
-
# it will be prefixed with the root path of this loaded Faraday
|
|
9
|
-
# version.
|
|
10
|
-
# options - Hash of Symbol => String library names.
|
|
11
|
-
#
|
|
12
|
-
# Examples.
|
|
13
|
-
#
|
|
14
|
-
# Faraday.autoload_all 'faraday/foo',
|
|
15
|
-
# :Bar => 'bar'
|
|
16
|
-
#
|
|
17
|
-
# # requires faraday/foo/bar to load Faraday::Bar.
|
|
18
|
-
# Faraday::Bar
|
|
19
|
-
#
|
|
20
|
-
#
|
|
21
|
-
# Returns nothing.
|
|
22
|
-
def autoload_all(prefix, options)
|
|
23
|
-
if prefix =~ /^faraday(\/|$)/i
|
|
24
|
-
prefix = File.join(Faraday.root_path, prefix)
|
|
25
|
-
end
|
|
26
|
-
options.each do |const_name, path|
|
|
27
|
-
autoload const_name, File.join(prefix, path)
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
# Internal: Loads each autoloaded constant. If thread safety is a concern,
|
|
32
|
-
# wrap this in a Mutex.
|
|
33
|
-
#
|
|
34
|
-
# Returns nothing.
|
|
35
|
-
def load_autoloaded_constants
|
|
36
|
-
constants.each do |const|
|
|
37
|
-
const_get(const) if autoload?(const)
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
# Internal: Filters the module's contents with those that have been already
|
|
42
|
-
# autoloaded.
|
|
43
|
-
#
|
|
44
|
-
# Returns an Array of Class/Module objects.
|
|
45
|
-
def all_loaded_constants
|
|
46
|
-
constants.map { |c| const_get(c) }.
|
|
47
|
-
select { |a| a.respond_to?(:loaded?) && a.loaded? }
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
class Adapter
|
|
52
|
-
extend AutoloadHelper
|
|
53
|
-
autoload_all 'faraday/adapter',
|
|
54
|
-
:NetHttp => 'net_http',
|
|
55
|
-
:NetHttpPersistent => 'net_http_persistent',
|
|
56
|
-
:Typhoeus => 'typhoeus',
|
|
57
|
-
:EMSynchrony => 'em_synchrony',
|
|
58
|
-
:EMHttp => 'em_http',
|
|
59
|
-
:Patron => 'patron',
|
|
60
|
-
:Excon => 'excon',
|
|
61
|
-
:Test => 'test',
|
|
62
|
-
:Rack => 'rack',
|
|
63
|
-
:HTTPClient => 'httpclient'
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
class Request
|
|
67
|
-
extend AutoloadHelper
|
|
68
|
-
autoload_all 'faraday/request',
|
|
69
|
-
:UrlEncoded => 'url_encoded',
|
|
70
|
-
:Multipart => 'multipart',
|
|
71
|
-
:Retry => 'retry',
|
|
72
|
-
:Authorization => 'authorization',
|
|
73
|
-
:BasicAuthentication => 'basic_authentication',
|
|
74
|
-
:TokenAuthentication => 'token_authentication',
|
|
75
|
-
:Instrumentation => 'instrumentation'
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
class Response
|
|
79
|
-
extend AutoloadHelper
|
|
80
|
-
autoload_all 'faraday/response',
|
|
81
|
-
:RaiseError => 'raise_error',
|
|
82
|
-
:Logger => 'logger'
|
|
83
|
-
end
|
|
84
|
-
end
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
require 'base64'
|
|
2
|
-
|
|
3
|
-
module Faraday
|
|
4
|
-
class Request::BasicAuthentication < Request.load_middleware(:authorization)
|
|
5
|
-
# Public
|
|
6
|
-
def self.header(login, pass)
|
|
7
|
-
value = Base64.encode64([login, pass].join(':'))
|
|
8
|
-
value.gsub!("\n", '')
|
|
9
|
-
super(:Basic, value)
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
require File.expand_path("../url_encoded", __FILE__)
|
|
2
|
-
|
|
3
|
-
module Faraday
|
|
4
|
-
class Request::Multipart < Request::UrlEncoded
|
|
5
|
-
self.mime_type = 'multipart/form-data'.freeze
|
|
6
|
-
DEFAULT_BOUNDARY = "-----------RubyMultipartPost".freeze unless defined? DEFAULT_BOUNDARY
|
|
7
|
-
|
|
8
|
-
def call(env)
|
|
9
|
-
match_content_type(env) do |params|
|
|
10
|
-
env.request.boundary ||= DEFAULT_BOUNDARY
|
|
11
|
-
env.request_headers[CONTENT_TYPE] += "; boundary=#{env.request.boundary}"
|
|
12
|
-
env.body = create_multipart(env, params)
|
|
13
|
-
end
|
|
14
|
-
@app.call env
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def process_request?(env)
|
|
18
|
-
type = request_type(env)
|
|
19
|
-
env.body.respond_to?(:each_key) and !env.body.empty? and (
|
|
20
|
-
(type.empty? and has_multipart?(env.body)) or
|
|
21
|
-
type == self.class.mime_type
|
|
22
|
-
)
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def has_multipart?(obj)
|
|
26
|
-
# string is an enum in 1.8, returning list of itself
|
|
27
|
-
if obj.respond_to?(:each) && !obj.is_a?(String)
|
|
28
|
-
(obj.respond_to?(:values) ? obj.values : obj).each do |val|
|
|
29
|
-
return true if (val.respond_to?(:content_type) || has_multipart?(val))
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
false
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
def create_multipart(env, params)
|
|
36
|
-
boundary = env.request.boundary
|
|
37
|
-
parts = process_params(params) do |key, value|
|
|
38
|
-
Faraday::Parts::Part.new(boundary, key, value)
|
|
39
|
-
end
|
|
40
|
-
parts << Faraday::Parts::EpiloguePart.new(boundary)
|
|
41
|
-
|
|
42
|
-
body = Faraday::CompositeReadIO.new(parts)
|
|
43
|
-
env.request_headers[Faraday::Env::ContentLength] = body.length.to_s
|
|
44
|
-
return body
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
def process_params(params, prefix = nil, pieces = nil, &block)
|
|
48
|
-
params.inject(pieces || []) do |all, (key, value)|
|
|
49
|
-
key = "#{prefix}[#{key}]" if prefix
|
|
50
|
-
|
|
51
|
-
case value
|
|
52
|
-
when Array
|
|
53
|
-
values = value.inject([]) { |a,v| a << [nil, v] }
|
|
54
|
-
process_params(values, key, all, &block)
|
|
55
|
-
when Hash
|
|
56
|
-
process_params(value, key, all, &block)
|
|
57
|
-
else
|
|
58
|
-
all << block.call(key, value)
|
|
59
|
-
end
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
end
|
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
module Faraday
|
|
2
|
-
# Catches exceptions and retries each request a limited number of times.
|
|
3
|
-
#
|
|
4
|
-
# By default, it retries 2 times and handles only timeout exceptions. It can
|
|
5
|
-
# be configured with an arbitrary number of retries, a list of exceptions to
|
|
6
|
-
# handle, a retry interval, a percentage of randomness to add to the retry
|
|
7
|
-
# interval, and a backoff factor.
|
|
8
|
-
#
|
|
9
|
-
# Examples
|
|
10
|
-
#
|
|
11
|
-
# Faraday.new do |conn|
|
|
12
|
-
# conn.request :retry, max: 2, interval: 0.05,
|
|
13
|
-
# interval_randomness: 0.5, backoff_factor: 2
|
|
14
|
-
# exceptions: [CustomException, 'Timeout::Error']
|
|
15
|
-
# conn.adapter ...
|
|
16
|
-
# end
|
|
17
|
-
#
|
|
18
|
-
# This example will result in a first interval that is random between 0.05 and 0.075 and a second
|
|
19
|
-
# interval that is random between 0.1 and 0.15
|
|
20
|
-
#
|
|
21
|
-
class Request::Retry < Faraday::Middleware
|
|
22
|
-
|
|
23
|
-
IDEMPOTENT_METHODS = [:delete, :get, :head, :options, :put]
|
|
24
|
-
|
|
25
|
-
class Options < Faraday::Options.new(:max, :interval, :interval_randomness, :backoff_factor,
|
|
26
|
-
:exceptions, :methods, :retry_if)
|
|
27
|
-
DEFAULT_CHECK = lambda { |env,exception| false }
|
|
28
|
-
|
|
29
|
-
def self.from(value)
|
|
30
|
-
if Fixnum === value
|
|
31
|
-
new(value)
|
|
32
|
-
else
|
|
33
|
-
super(value)
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
def max
|
|
38
|
-
(self[:max] ||= 2).to_i
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def interval
|
|
42
|
-
(self[:interval] ||= 0).to_f
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
def interval_randomness
|
|
46
|
-
(self[:interval_randomness] ||= 0).to_i
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def backoff_factor
|
|
50
|
-
(self[:backoff_factor] ||= 1).to_f
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
def exceptions
|
|
54
|
-
Array(self[:exceptions] ||= [Errno::ETIMEDOUT, 'Timeout::Error',
|
|
55
|
-
Error::TimeoutError])
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
def methods
|
|
59
|
-
Array(self[:methods] ||= IDEMPOTENT_METHODS)
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
def retry_if
|
|
63
|
-
self[:retry_if] ||= DEFAULT_CHECK
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
# Public: Initialize middleware
|
|
69
|
-
#
|
|
70
|
-
# Options:
|
|
71
|
-
# max - Maximum number of retries (default: 2)
|
|
72
|
-
# interval - Pause in seconds between retries (default: 0)
|
|
73
|
-
# interval_randomness - The maximum random interval amount expressed
|
|
74
|
-
# as a float between 0 and 1 to use in addition to the
|
|
75
|
-
# interval. (default: 0)
|
|
76
|
-
# backoff_factor - The amount to multiple each successive retry's
|
|
77
|
-
# interval amount by in order to provide backoff
|
|
78
|
-
# (default: 1)
|
|
79
|
-
# exceptions - The list of exceptions to handle. Exceptions can be
|
|
80
|
-
# given as Class, Module, or String. (default:
|
|
81
|
-
# [Errno::ETIMEDOUT, Timeout::Error,
|
|
82
|
-
# Error::TimeoutError])
|
|
83
|
-
# methods - A list of HTTP methods to retry without calling retry_if. Pass
|
|
84
|
-
# an empty Array to call retry_if for all exceptions.
|
|
85
|
-
# (defaults to the idempotent HTTP methods in IDEMPOTENT_METHODS)
|
|
86
|
-
# retry_if - block that will receive the env object and the exception raised
|
|
87
|
-
# and should decide if the code should retry still the action or
|
|
88
|
-
# not independent of the retry count. This would be useful
|
|
89
|
-
# if the exception produced is non-recoverable or if the
|
|
90
|
-
# the HTTP method called is not idempotent.
|
|
91
|
-
# (defaults to return false)
|
|
92
|
-
def initialize(app, options = nil)
|
|
93
|
-
super(app)
|
|
94
|
-
@options = Options.from(options)
|
|
95
|
-
@errmatch = build_exception_matcher(@options.exceptions)
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
def sleep_amount(retries)
|
|
99
|
-
retry_index = @options.max - retries
|
|
100
|
-
current_interval = @options.interval * (@options.backoff_factor ** retry_index)
|
|
101
|
-
random_interval = rand * @options.interval_randomness.to_f * @options.interval
|
|
102
|
-
current_interval + random_interval
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
def call(env)
|
|
106
|
-
retries = @options.max
|
|
107
|
-
request_body = env[:body]
|
|
108
|
-
begin
|
|
109
|
-
env[:body] = request_body # after failure env[:body] is set to the response body
|
|
110
|
-
@app.call(env)
|
|
111
|
-
rescue @errmatch => exception
|
|
112
|
-
if retries > 0 && retry_request?(env, exception)
|
|
113
|
-
retries -= 1
|
|
114
|
-
sleep sleep_amount(retries + 1)
|
|
115
|
-
retry
|
|
116
|
-
end
|
|
117
|
-
raise
|
|
118
|
-
end
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
# Private: construct an exception matcher object.
|
|
122
|
-
#
|
|
123
|
-
# An exception matcher for the rescue clause can usually be any object that
|
|
124
|
-
# responds to `===`, but for Ruby 1.8 it has to be a Class or Module.
|
|
125
|
-
def build_exception_matcher(exceptions)
|
|
126
|
-
matcher = Module.new
|
|
127
|
-
(class << matcher; self; end).class_eval do
|
|
128
|
-
define_method(:===) do |error|
|
|
129
|
-
exceptions.any? do |ex|
|
|
130
|
-
if ex.is_a? Module
|
|
131
|
-
error.is_a? ex
|
|
132
|
-
else
|
|
133
|
-
error.class.to_s == ex.to_s
|
|
134
|
-
end
|
|
135
|
-
end
|
|
136
|
-
end
|
|
137
|
-
end
|
|
138
|
-
matcher
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
private
|
|
142
|
-
|
|
143
|
-
def retry_request?(env, exception)
|
|
144
|
-
@options.methods.include?(env[:method]) || @options.retry_if.call(env, exception)
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
end
|
|
148
|
-
end
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
module Faraday
|
|
2
|
-
class Request::TokenAuthentication < Request.load_middleware(:authorization)
|
|
3
|
-
# Public
|
|
4
|
-
def self.header(token, options = nil)
|
|
5
|
-
options ||= {}
|
|
6
|
-
options[:token] = token
|
|
7
|
-
super(:Token, options)
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
def initialize(app, token, options = nil)
|
|
11
|
-
super(app, token, options)
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
|
data/lib/faraday/upload_io.rb
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
begin
|
|
2
|
-
require 'composite_io'
|
|
3
|
-
require 'parts'
|
|
4
|
-
require 'stringio'
|
|
5
|
-
rescue LoadError
|
|
6
|
-
$stderr.puts "Install the multipart-post gem."
|
|
7
|
-
raise
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
module Faraday
|
|
11
|
-
# Similar but not compatible with ::CompositeReadIO provided by multipart-post.
|
|
12
|
-
class CompositeReadIO
|
|
13
|
-
def initialize(*parts)
|
|
14
|
-
@parts = parts.flatten
|
|
15
|
-
@ios = @parts.map { |part| part.to_io }
|
|
16
|
-
@index = 0
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
def length
|
|
20
|
-
@parts.inject(0) { |sum, part| sum + part.length }
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
def rewind
|
|
24
|
-
@ios.each { |io| io.rewind }
|
|
25
|
-
@index = 0
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
# Read from IOs in order until `length` bytes have been received.
|
|
29
|
-
def read(length = nil, outbuf = nil)
|
|
30
|
-
got_result = false
|
|
31
|
-
outbuf = outbuf ? outbuf.replace("") : ""
|
|
32
|
-
|
|
33
|
-
while io = current_io
|
|
34
|
-
if result = io.read(length)
|
|
35
|
-
got_result ||= !result.nil?
|
|
36
|
-
result.force_encoding("BINARY") if result.respond_to?(:force_encoding)
|
|
37
|
-
outbuf << result
|
|
38
|
-
length -= result.length if length
|
|
39
|
-
break if length == 0
|
|
40
|
-
end
|
|
41
|
-
advance_io
|
|
42
|
-
end
|
|
43
|
-
(!got_result && length) ? nil : outbuf
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
def close
|
|
47
|
-
@ios.each { |io| io.close }
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
def ensure_open_and_readable
|
|
51
|
-
# Rubinius compatibility
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
private
|
|
55
|
-
|
|
56
|
-
def current_io
|
|
57
|
-
@ios[@index]
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
def advance_io
|
|
61
|
-
@index += 1
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
UploadIO = ::UploadIO
|
|
66
|
-
Parts = ::Parts
|
|
67
|
-
end
|
data/script/cached-bundle
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bash
|
|
2
|
-
# Usage: cached-bundle install --deployment
|
|
3
|
-
#
|
|
4
|
-
# After running `bundle`, caches the `./bundle` directory to S3.
|
|
5
|
-
# On the next run, restores the cached directory before running `bundle`.
|
|
6
|
-
# When `Gemfile` changes, the cache gets rebuilt.
|
|
7
|
-
#
|
|
8
|
-
# Requirements:
|
|
9
|
-
# - Gemfile
|
|
10
|
-
# - TRAVIS_REPO_SLUG
|
|
11
|
-
# - TRAVIS_RUBY_VERSION
|
|
12
|
-
# - AMAZON_S3_BUCKET
|
|
13
|
-
# - script/s3-put
|
|
14
|
-
# - bundle
|
|
15
|
-
# - curl
|
|
16
|
-
#
|
|
17
|
-
# Author: Mislav Marohnić
|
|
18
|
-
|
|
19
|
-
set -e
|
|
20
|
-
|
|
21
|
-
compute_md5() {
|
|
22
|
-
local output="$(openssl md5)"
|
|
23
|
-
echo "${output##* }"
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
download() {
|
|
27
|
-
curl --tcp-nodelay -qsfL "$1" -o "$2"
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
bundle_path="bundle"
|
|
31
|
-
gemfile_hash="$(compute_md5 <"${BUNDLE_GEMFILE:-Gemfile}")"
|
|
32
|
-
cache_name="${TRAVIS_RUBY_VERSION}-${gemfile_hash}.tgz"
|
|
33
|
-
fetch_url="http://${AMAZON_S3_BUCKET}.s3.amazonaws.com/${TRAVIS_REPO_SLUG}/${cache_name}"
|
|
34
|
-
|
|
35
|
-
if download "$fetch_url" "$cache_name"; then
|
|
36
|
-
echo "Reusing cached bundle ${cache_name}"
|
|
37
|
-
tar xzf "$cache_name"
|
|
38
|
-
fi
|
|
39
|
-
|
|
40
|
-
bundle "$@"
|
|
41
|
-
|
|
42
|
-
if [ ! -f "$cache_name" ] && [ -n "$AMAZON_SECRET_ACCESS_KEY" ]; then
|
|
43
|
-
echo "Caching \`${bundle_path}' to S3"
|
|
44
|
-
tar czf "$cache_name" "$bundle_path"
|
|
45
|
-
script/s3-put "$cache_name" "${AMAZON_S3_BUCKET}:${TRAVIS_REPO_SLUG}/${cache_name}"
|
|
46
|
-
fi
|
data/script/console
DELETED
data/script/generate_certs
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# Usage: generate_certs
|
|
3
|
-
# Generate test certs for testing Faraday with SSL
|
|
4
|
-
|
|
5
|
-
require 'openssl'
|
|
6
|
-
require 'fileutils'
|
|
7
|
-
|
|
8
|
-
$shell = ARGV.include? '-s'
|
|
9
|
-
|
|
10
|
-
# Adapted from WEBrick::Utils. Skips cert extensions so it
|
|
11
|
-
# can be used as a CA bundle
|
|
12
|
-
def create_self_signed_cert(bits, cn, comment)
|
|
13
|
-
rsa = OpenSSL::PKey::RSA.new(bits)
|
|
14
|
-
cert = OpenSSL::X509::Certificate.new
|
|
15
|
-
cert.version = 2
|
|
16
|
-
cert.serial = 1
|
|
17
|
-
name = OpenSSL::X509::Name.new(cn)
|
|
18
|
-
cert.subject = name
|
|
19
|
-
cert.issuer = name
|
|
20
|
-
cert.not_before = Time.now
|
|
21
|
-
cert.not_after = Time.now + (365*24*60*60)
|
|
22
|
-
cert.public_key = rsa.public_key
|
|
23
|
-
cert.sign(rsa, OpenSSL::Digest::SHA1.new)
|
|
24
|
-
return [cert, rsa]
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def write(file, contents, env_var)
|
|
28
|
-
FileUtils.mkdir_p(File.dirname(file))
|
|
29
|
-
File.open(file, 'w') {|f| f.puts(contents) }
|
|
30
|
-
puts %(export #{env_var}="#{file}") if $shell
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
# One cert / CA for ease of testing when ignoring verification
|
|
35
|
-
cert, key = create_self_signed_cert(1024, [['CN', 'localhost']], 'Faraday Test CA')
|
|
36
|
-
write 'tmp/faraday-cert.key', key, 'SSL_KEY'
|
|
37
|
-
write 'tmp/faraday-cert.crt', cert, 'SSL_FILE'
|
|
38
|
-
|
|
39
|
-
# And a second CA to prove that verification can fail
|
|
40
|
-
cert, key = create_self_signed_cert(1024, [['CN', 'real-ca.com']], 'A different CA')
|
|
41
|
-
write 'tmp/faraday-different-ca-cert.key', key, 'SSL_KEY_ALT'
|
|
42
|
-
write 'tmp/faraday-different-ca-cert.crt', cert, 'SSL_FILE_ALT'
|
data/script/package
DELETED
data/script/proxy-server
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# Usage: script/proxy-server [-p PORT] [-u USER:PASSWORD]
|
|
3
|
-
require 'webrick'
|
|
4
|
-
require 'webrick/httpproxy'
|
|
5
|
-
|
|
6
|
-
port = 4001
|
|
7
|
-
|
|
8
|
-
if found = ARGV.index('-p')
|
|
9
|
-
port = ARGV[found + 1].to_i
|
|
10
|
-
end
|
|
11
|
-
if found = ARGV.index('-u')
|
|
12
|
-
username, password = ARGV[found + 1].split(':', 2)
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
match_credentials = lambda { |credentials|
|
|
16
|
-
got_username, got_password = credentials.to_s.unpack("m*")[0].split(":", 2)
|
|
17
|
-
got_username == username && got_password == password
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
log_io = $stdout
|
|
21
|
-
log_io.sync = true
|
|
22
|
-
|
|
23
|
-
webrick_opts = {
|
|
24
|
-
:Port => port, :Logger => WEBrick::Log::new(log_io),
|
|
25
|
-
:AccessLog => [[log_io, "[%{X-Faraday-Adapter}i] %m %U -> %s %b"]],
|
|
26
|
-
:ProxyAuthProc => lambda { |req, res|
|
|
27
|
-
if username
|
|
28
|
-
type, credentials = req.header['proxy-authorization'].first.to_s.split(/\s+/, 2)
|
|
29
|
-
unless "Basic" == type && match_credentials.call(credentials)
|
|
30
|
-
res['proxy-authenticate'] = %{Basic realm="testing"}
|
|
31
|
-
raise WEBrick::HTTPStatus::ProxyAuthenticationRequired
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
proxy = WEBrick::HTTPProxyServer.new(webrick_opts)
|
|
38
|
-
|
|
39
|
-
trap(:TERM) { proxy.shutdown }
|
|
40
|
-
trap(:INT) { proxy.shutdown }
|
|
41
|
-
|
|
42
|
-
proxy.start
|
data/script/release
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bash
|
|
2
|
-
# Usage: script/release
|
|
3
|
-
# Build the package, tag a commit, push it to origin, and then release the
|
|
4
|
-
# package publicly.
|
|
5
|
-
|
|
6
|
-
set -e
|
|
7
|
-
|
|
8
|
-
version="$(script/package | grep Version: | awk '{print $2}')"
|
|
9
|
-
[ -n "$version" ] || exit 1
|
|
10
|
-
|
|
11
|
-
git commit --allow-empty -a -m "Release $version"
|
|
12
|
-
git tag "v$version"
|
|
13
|
-
git push origin
|
|
14
|
-
git push origin "v$version"
|
|
15
|
-
git push legacy
|
|
16
|
-
git push legacy "v$version"
|
|
17
|
-
gem push pkg/*-${version}.gem
|