faraday 0.16.1 → 0.17.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +232 -0
- data/LICENSE.md +1 -1
- data/README.md +358 -18
- data/Rakefile +13 -0
- data/lib/faraday.rb +174 -93
- data/lib/faraday/adapter.rb +22 -36
- data/lib/faraday/adapter/em_http.rb +97 -140
- data/lib/faraday/adapter/em_http_ssl_patch.rb +17 -23
- data/lib/faraday/adapter/em_synchrony.rb +60 -104
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +15 -18
- data/lib/faraday/adapter/excon.rb +55 -100
- data/lib/faraday/adapter/httpclient.rb +39 -61
- data/lib/faraday/adapter/net_http.rb +51 -103
- data/lib/faraday/adapter/net_http_persistent.rb +28 -49
- data/lib/faraday/adapter/patron.rb +35 -54
- data/lib/faraday/adapter/rack.rb +12 -28
- data/lib/faraday/adapter/test.rb +53 -86
- data/lib/faraday/adapter/typhoeus.rb +1 -4
- data/lib/faraday/autoload.rb +36 -47
- data/lib/faraday/connection.rb +179 -321
- data/lib/faraday/deprecate.rb +109 -0
- data/lib/faraday/error.rb +72 -28
- data/lib/faraday/middleware.rb +28 -4
- data/lib/faraday/options.rb +183 -32
- data/lib/faraday/parameters.rb +197 -4
- data/lib/faraday/rack_builder.rb +55 -66
- data/lib/faraday/request.rb +36 -68
- data/lib/faraday/request/authorization.rb +30 -42
- data/lib/faraday/request/basic_authentication.rb +7 -14
- data/lib/faraday/request/instrumentation.rb +27 -45
- data/lib/faraday/request/multipart.rb +48 -79
- data/lib/faraday/request/retry.rb +171 -197
- data/lib/faraday/request/token_authentication.rb +10 -15
- data/lib/faraday/request/url_encoded.rb +23 -41
- data/lib/faraday/response.rb +13 -20
- data/lib/faraday/response/logger.rb +69 -22
- data/lib/faraday/response/raise_error.rb +18 -36
- data/lib/faraday/upload_io.rb +67 -0
- data/lib/faraday/utils.rb +245 -28
- data/spec/faraday/deprecate_spec.rb +147 -0
- data/spec/faraday/error_spec.rb +102 -0
- data/spec/faraday/response/raise_error_spec.rb +106 -0
- data/spec/spec_helper.rb +105 -0
- data/test/adapters/default_test.rb +14 -0
- data/test/adapters/em_http_test.rb +30 -0
- data/test/adapters/em_synchrony_test.rb +32 -0
- data/test/adapters/excon_test.rb +30 -0
- data/test/adapters/httpclient_test.rb +34 -0
- data/test/adapters/integration.rb +263 -0
- data/test/adapters/logger_test.rb +136 -0
- data/test/adapters/net_http_persistent_test.rb +114 -0
- data/test/adapters/net_http_test.rb +79 -0
- data/test/adapters/patron_test.rb +40 -0
- data/test/adapters/rack_test.rb +38 -0
- data/test/adapters/test_middleware_test.rb +157 -0
- data/test/adapters/typhoeus_test.rb +38 -0
- data/test/authentication_middleware_test.rb +65 -0
- data/test/composite_read_io_test.rb +109 -0
- data/test/connection_test.rb +738 -0
- data/test/env_test.rb +268 -0
- data/test/helper.rb +75 -0
- data/test/live_server.rb +67 -0
- data/test/middleware/instrumentation_test.rb +88 -0
- data/test/middleware/retry_test.rb +282 -0
- data/test/middleware_stack_test.rb +260 -0
- data/test/multibyte.txt +1 -0
- data/test/options_test.rb +333 -0
- data/test/parameters_test.rb +157 -0
- data/test/request_middleware_test.rb +126 -0
- data/test/response_middleware_test.rb +72 -0
- data/test/strawberry.rb +2 -0
- data/test/utils_test.rb +98 -0
- metadata +50 -26
- data/lib/faraday/adapter_registry.rb +0 -28
- data/lib/faraday/dependency_loader.rb +0 -37
- data/lib/faraday/deprecated_constant.rb +0 -53
- data/lib/faraday/encoders/flat_params_encoder.rb +0 -94
- data/lib/faraday/encoders/nested_params_encoder.rb +0 -171
- data/lib/faraday/file_part.rb +0 -128
- data/lib/faraday/logging/formatter.rb +0 -92
- data/lib/faraday/middleware_registry.rb +0 -129
- data/lib/faraday/options/connection_options.rb +0 -22
- data/lib/faraday/options/env.rb +0 -181
- data/lib/faraday/options/proxy_options.rb +0 -28
- data/lib/faraday/options/request_options.rb +0 -21
- data/lib/faraday/options/ssl_options.rb +0 -59
- data/lib/faraday/param_part.rb +0 -53
- data/lib/faraday/utils/headers.rb +0 -139
- data/lib/faraday/utils/params_hash.rb +0 -61
- data/spec/external_adapters/faraday_specs_setup.rb +0 -14
@@ -1,9 +1,6 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
module Faraday
|
4
2
|
class Adapter
|
5
|
-
#
|
6
|
-
# https://github.com/philsturgeon/typhoeus/blob/master/lib/typhoeus/adapters/faraday.rb
|
3
|
+
# This class is just a stub, the real adapter is in https://github.com/philsturgeon/typhoeus/blob/master/lib/typhoeus/adapters/faraday.rb
|
7
4
|
class Typhoeus < Faraday::Adapter
|
8
5
|
# Needs to define this method in order to support Typhoeus <= 1.3.0
|
9
6
|
def call; end
|
data/lib/faraday/autoload.rb
CHANGED
@@ -1,95 +1,84 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
module Faraday
|
4
|
-
# Adds the ability for other modules to manage autoloadable
|
2
|
+
# Internal: Adds the ability for other modules to manage autoloadable
|
5
3
|
# constants.
|
6
|
-
#
|
7
|
-
# @api private
|
8
4
|
module AutoloadHelper
|
9
|
-
# Registers the constants to be auto loaded.
|
5
|
+
# Internal: Registers the constants to be auto loaded.
|
10
6
|
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
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.
|
15
11
|
#
|
16
|
-
#
|
12
|
+
# Examples.
|
17
13
|
#
|
18
14
|
# Faraday.autoload_all 'faraday/foo',
|
19
|
-
# Bar
|
15
|
+
# :Bar => 'bar'
|
20
16
|
#
|
21
17
|
# # requires faraday/foo/bar to load Faraday::Bar.
|
22
18
|
# Faraday::Bar
|
23
19
|
#
|
24
|
-
#
|
20
|
+
#
|
21
|
+
# Returns nothing.
|
25
22
|
def autoload_all(prefix, options)
|
26
|
-
if prefix =~
|
23
|
+
if prefix =~ /^faraday(\/|$)/i
|
27
24
|
prefix = File.join(Faraday.root_path, prefix)
|
28
25
|
end
|
29
|
-
|
30
26
|
options.each do |const_name, path|
|
31
27
|
autoload const_name, File.join(prefix, path)
|
32
28
|
end
|
33
29
|
end
|
34
30
|
|
35
|
-
# Loads each autoloaded constant. If thread safety is a concern,
|
31
|
+
# Internal: Loads each autoloaded constant. If thread safety is a concern,
|
36
32
|
# wrap this in a Mutex.
|
37
33
|
#
|
38
|
-
#
|
34
|
+
# Returns nothing.
|
39
35
|
def load_autoloaded_constants
|
40
36
|
constants.each do |const|
|
41
37
|
const_get(const) if autoload?(const)
|
42
38
|
end
|
43
39
|
end
|
44
40
|
|
45
|
-
# Filters the module's contents with those that have been already
|
41
|
+
# Internal: Filters the module's contents with those that have been already
|
46
42
|
# autoloaded.
|
47
43
|
#
|
48
|
-
#
|
44
|
+
# Returns an Array of Class/Module objects.
|
49
45
|
def all_loaded_constants
|
50
|
-
constants
|
51
|
-
|
52
|
-
.select { |a| a.respond_to?(:loaded?) && a.loaded? }
|
46
|
+
constants.map { |c| const_get(c) }.
|
47
|
+
select { |a| a.respond_to?(:loaded?) && a.loaded? }
|
53
48
|
end
|
54
49
|
end
|
55
50
|
|
56
|
-
# Adapter is the base class for all Faraday adapters.
|
57
|
-
# @see lib/faraday/adapter.rb Original class location
|
58
51
|
class Adapter
|
59
52
|
extend AutoloadHelper
|
60
53
|
autoload_all 'faraday/adapter',
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
54
|
+
:NetHttp => 'net_http',
|
55
|
+
:NetHttpPersistent => 'net_http_persistent',
|
56
|
+
:EMSynchrony => 'em_synchrony',
|
57
|
+
:EMHttp => 'em_http',
|
58
|
+
:Typhoeus => 'typhoeus',
|
59
|
+
:Patron => 'patron',
|
60
|
+
:Excon => 'excon',
|
61
|
+
:Test => 'test',
|
62
|
+
:Rack => 'rack',
|
63
|
+
:HTTPClient => 'httpclient'
|
71
64
|
end
|
72
65
|
|
73
|
-
# Request represents a single HTTP request for a Faraday adapter to make.
|
74
|
-
# @see lib/faraday/request.rb Original class location
|
75
66
|
class Request
|
76
67
|
extend AutoloadHelper
|
77
68
|
autoload_all 'faraday/request',
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
69
|
+
:UrlEncoded => 'url_encoded',
|
70
|
+
:Multipart => 'multipart',
|
71
|
+
:Retry => 'retry',
|
72
|
+
:Authorization => 'authorization',
|
73
|
+
:BasicAuthentication => 'basic_authentication',
|
74
|
+
:TokenAuthentication => 'token_authentication',
|
75
|
+
:Instrumentation => 'instrumentation'
|
85
76
|
end
|
86
77
|
|
87
|
-
# Response represents the returned value of a sent Faraday request.
|
88
|
-
# @see lib/faraday/response.rb Original class location
|
89
78
|
class Response
|
90
79
|
extend AutoloadHelper
|
91
80
|
autoload_all 'faraday/response',
|
92
|
-
|
93
|
-
|
81
|
+
:RaiseError => 'raise_error',
|
82
|
+
:Logger => 'logger'
|
94
83
|
end
|
95
84
|
end
|
data/lib/faraday/connection.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
module Faraday
|
4
|
-
# Connection objects manage the default properties and the middleware
|
2
|
+
# Public: Connection objects manage the default properties and the middleware
|
5
3
|
# stack for fulfilling an HTTP request.
|
6
4
|
#
|
7
|
-
#
|
5
|
+
# Examples
|
8
6
|
#
|
9
7
|
# conn = Faraday::Connection.new 'http://sushi.com'
|
10
8
|
#
|
@@ -14,51 +12,51 @@ module Faraday
|
|
14
12
|
#
|
15
13
|
class Connection
|
16
14
|
# A Set of allowed HTTP verbs.
|
17
|
-
METHODS = Set.new
|
15
|
+
METHODS = Set.new [:get, :post, :put, :delete, :head, :patch, :options]
|
18
16
|
|
19
|
-
#
|
17
|
+
# Public: Returns a Hash of URI query unencoded key/value pairs.
|
20
18
|
attr_reader :params
|
21
19
|
|
22
|
-
#
|
20
|
+
# Public: Returns a Hash of unencoded HTTP header key/value pairs.
|
23
21
|
attr_reader :headers
|
24
22
|
|
25
|
-
#
|
26
|
-
#
|
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.
|
27
25
|
attr_reader :url_prefix
|
28
26
|
|
29
|
-
#
|
27
|
+
# Public: Returns the Faraday::Builder for this Connection.
|
30
28
|
attr_reader :builder
|
31
29
|
|
32
|
-
#
|
30
|
+
# Public: Returns a Hash of the request options.
|
31
|
+
attr_reader :options
|
32
|
+
|
33
|
+
# Public: Returns a Hash of the SSL options.
|
33
34
|
attr_reader :ssl
|
34
35
|
|
35
|
-
#
|
36
|
+
# Public: Returns the parallel manager for this Connection.
|
36
37
|
attr_reader :parallel_manager
|
37
38
|
|
38
|
-
# Sets the default parallel manager for this connection.
|
39
|
+
# Public: Sets the default parallel manager for this connection.
|
39
40
|
attr_writer :default_parallel_manager
|
40
41
|
|
41
|
-
#
|
42
|
-
attr_reader :proxy
|
42
|
+
# Public: Gets or Sets the Hash proxy options.
|
43
|
+
# attr_reader :proxy
|
43
44
|
|
44
|
-
# Initializes a new Faraday::Connection.
|
45
|
+
# Public: Initializes a new Faraday::Connection.
|
45
46
|
#
|
46
|
-
#
|
47
|
+
# url - URI or String base URL to use as a prefix for all
|
47
48
|
# requests (optional).
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
#
|
59
|
-
# @option options [String] :proxy[:user]
|
60
|
-
# @option options [String] :proxy[:password]
|
61
|
-
# @yield [self] after all setup has been done
|
49
|
+
# options - Hash or Faraday::ConnectionOptions.
|
50
|
+
# :url - URI or String base URL (default: "http:/").
|
51
|
+
# :params - Hash of URI query unencoded key/value pairs.
|
52
|
+
# :headers - Hash of unencoded HTTP header key/value pairs.
|
53
|
+
# :request - Hash of request options.
|
54
|
+
# :ssl - Hash of SSL options.
|
55
|
+
# :proxy - URI, String or Hash of HTTP proxy options
|
56
|
+
# (default: "http_proxy" environment variable).
|
57
|
+
# :uri - URI or String
|
58
|
+
# :user - String (optional)
|
59
|
+
# :password - String (optional)
|
62
60
|
def initialize(url = nil, options = nil)
|
63
61
|
options = ConnectionOptions.from(options)
|
64
62
|
|
@@ -76,7 +74,7 @@ module Faraday
|
|
76
74
|
|
77
75
|
@builder = options.builder || begin
|
78
76
|
# pass an empty block to Builder so it doesn't assume default middleware
|
79
|
-
options.new_builder(block_given? ?
|
77
|
+
options.new_builder(block_given? ? Proc.new { |b| } : nil)
|
80
78
|
end
|
81
79
|
|
82
80
|
self.url_prefix = url || 'http:/'
|
@@ -84,32 +82,21 @@ module Faraday
|
|
84
82
|
@params.update(options.params) if options.params
|
85
83
|
@headers.update(options.headers) if options.headers
|
86
84
|
|
87
|
-
|
85
|
+
@manual_proxy = !!options.proxy
|
86
|
+
@proxy = options.proxy ? ProxyOptions.from(options.proxy) : proxy_from_env(url)
|
87
|
+
@temp_proxy = @proxy
|
88
88
|
|
89
89
|
yield(self) if block_given?
|
90
90
|
|
91
91
|
@headers[:user_agent] ||= "Faraday v#{VERSION}"
|
92
92
|
end
|
93
93
|
|
94
|
-
|
95
|
-
@manual_proxy = !!options.proxy
|
96
|
-
@proxy =
|
97
|
-
if options.proxy
|
98
|
-
ProxyOptions.from(options.proxy)
|
99
|
-
else
|
100
|
-
proxy_from_env(url)
|
101
|
-
end
|
102
|
-
@temp_proxy = @proxy
|
103
|
-
end
|
104
|
-
|
105
|
-
# Sets the Hash of URI query unencoded key/value pairs.
|
106
|
-
# @param hash [Hash]
|
94
|
+
# Public: Sets the Hash of URI query unencoded key/value pairs.
|
107
95
|
def params=(hash)
|
108
96
|
@params.replace hash
|
109
97
|
end
|
110
98
|
|
111
|
-
# Sets the Hash of unencoded HTTP header key/value pairs.
|
112
|
-
# @param hash [Hash]
|
99
|
+
# Public: Sets the Hash of unencoded HTTP header key/value pairs.
|
113
100
|
def headers=(hash)
|
114
101
|
@headers.replace hash
|
115
102
|
end
|
@@ -118,171 +105,71 @@ module Faraday
|
|
118
105
|
|
119
106
|
def_delegators :builder, :build, :use, :request, :response, :adapter, :app
|
120
107
|
|
121
|
-
#
|
122
|
-
# Makes a GET HTTP request without a body.
|
123
|
-
# @!scope class
|
108
|
+
# Public: Makes an HTTP request without a body.
|
124
109
|
#
|
125
|
-
#
|
126
|
-
#
|
127
|
-
#
|
128
|
-
#
|
110
|
+
# url - The optional String base URL to use as a prefix for all
|
111
|
+
# requests. Can also be the options Hash.
|
112
|
+
# params - Hash of URI query unencoded key/value pairs.
|
113
|
+
# headers - Hash of unencoded HTTP header key/value pairs.
|
129
114
|
#
|
130
|
-
#
|
131
|
-
#
|
115
|
+
# Examples
|
116
|
+
#
|
117
|
+
# conn.get '/items', {:page => 1}, :accept => 'application/json'
|
118
|
+
# conn.head '/items/1'
|
132
119
|
#
|
133
120
|
# # ElasticSearch example sending a body with GET.
|
134
121
|
# conn.get '/twitter/tweet/_search' do |req|
|
135
122
|
# req.headers[:content_type] = 'application/json'
|
136
123
|
# req.params[:routing] = 'kimchy'
|
137
|
-
# req.body = JSON.generate(query
|
124
|
+
# req.body = JSON.generate(:query => {...})
|
138
125
|
# end
|
139
126
|
#
|
140
|
-
#
|
141
|
-
#
|
142
|
-
|
143
|
-
# @!method head(url = nil, params = nil, headers = nil)
|
144
|
-
# Makes a HEAD HTTP request without a body.
|
145
|
-
# @!scope class
|
146
|
-
#
|
147
|
-
# @param url [String] The optional String base URL to use as a prefix for
|
148
|
-
# all requests. Can also be the options Hash.
|
149
|
-
# @param params [Hash] Hash of URI query unencoded key/value pairs.
|
150
|
-
# @param headers [Hash] unencoded HTTP header key/value pairs.
|
151
|
-
#
|
152
|
-
# @example
|
153
|
-
# conn.head '/items/1'
|
154
|
-
#
|
155
|
-
# @yield [Faraday::Request] for further request customizations
|
156
|
-
# @return [Faraday::Response]
|
157
|
-
|
158
|
-
# @!method delete(url = nil, params = nil, headers = nil)
|
159
|
-
# Makes a DELETE HTTP request without a body.
|
160
|
-
# @!scope class
|
161
|
-
#
|
162
|
-
# @param url [String] The optional String base URL to use as a prefix for
|
163
|
-
# all requests. Can also be the options Hash.
|
164
|
-
# @param params [Hash] Hash of URI query unencoded key/value pairs.
|
165
|
-
# @param headers [Hash] unencoded HTTP header key/value pairs.
|
166
|
-
#
|
167
|
-
# @example
|
168
|
-
# conn.delete '/items/1'
|
127
|
+
# Yields a Faraday::Request for further request customizations.
|
128
|
+
# Returns a Faraday::Response.
|
169
129
|
#
|
170
|
-
#
|
171
|
-
# @return [Faraday::Response]
|
172
|
-
|
173
|
-
# @!method connect(url = nil, params = nil, headers = nil)
|
174
|
-
# Makes a CONNECT HTTP request without a body.
|
175
|
-
# @!scope class
|
130
|
+
# Signature
|
176
131
|
#
|
177
|
-
#
|
178
|
-
# all requests. Can also be the options Hash.
|
179
|
-
# @param params [Hash] Hash of URI query unencoded key/value pairs.
|
180
|
-
# @param headers [Hash] unencoded HTTP header key/value pairs.
|
132
|
+
# <verb>(url = nil, params = nil, headers = nil)
|
181
133
|
#
|
182
|
-
#
|
183
|
-
|
184
|
-
#
|
185
|
-
# @yield [Faraday::Request] for further request customizations
|
186
|
-
# @return [Faraday::Response]
|
187
|
-
|
188
|
-
# @!method trace(url = nil, params = nil, headers = nil)
|
189
|
-
# Makes a TRACE HTTP request without a body.
|
190
|
-
# @!scope class
|
191
|
-
#
|
192
|
-
# @param url [String] The optional String base URL to use as a prefix for
|
193
|
-
# all requests. Can also be the options Hash.
|
194
|
-
# @param params [Hash] Hash of URI query unencoded key/value pairs.
|
195
|
-
# @param headers [Hash] unencoded HTTP header key/value pairs.
|
196
|
-
#
|
197
|
-
# @example
|
198
|
-
# conn.connect '/items/1'
|
199
|
-
#
|
200
|
-
# @yield [Faraday::Request] for further request customizations
|
201
|
-
# @return [Faraday::Response]
|
202
|
-
|
203
|
-
# @!visibility private
|
204
|
-
METHODS_WITH_QUERY.each do |method|
|
134
|
+
# verb - An HTTP verb: get, head, or delete.
|
135
|
+
%w[get head delete].each do |method|
|
205
136
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
206
137
|
def #{method}(url = nil, params = nil, headers = nil)
|
207
|
-
run_request(:#{method}, url, nil, headers)
|
138
|
+
run_request(:#{method}, url, nil, headers) { |request|
|
208
139
|
request.params.update(params) if params
|
209
|
-
yield
|
210
|
-
|
140
|
+
yield(request) if block_given?
|
141
|
+
}
|
211
142
|
end
|
212
143
|
RUBY
|
213
144
|
end
|
214
145
|
|
215
|
-
#
|
216
|
-
# Returns current Connection options.
|
146
|
+
# Public: Makes an HTTP request with a body.
|
217
147
|
#
|
218
|
-
#
|
219
|
-
#
|
220
|
-
#
|
221
|
-
#
|
222
|
-
# @param headers [Hash] unencoded HTTP header key/value pairs.
|
148
|
+
# url - The optional String base URL to use as a prefix for all
|
149
|
+
# requests. Can also be the options Hash.
|
150
|
+
# body - The String body for the request.
|
151
|
+
# headers - Hash of unencoded HTTP header key/value pairs.
|
223
152
|
#
|
224
|
-
#
|
225
|
-
# conn.options '/items/1'
|
153
|
+
# Examples
|
226
154
|
#
|
227
|
-
#
|
228
|
-
# @return [Faraday::Response]
|
229
|
-
def options(*args)
|
230
|
-
return @options if args.size.zero?
|
231
|
-
|
232
|
-
url, params, headers = *args
|
233
|
-
run_request(:options, url, nil, headers) do |request|
|
234
|
-
request.params.update(params) if params
|
235
|
-
yield request if block_given?
|
236
|
-
end
|
237
|
-
end
|
238
|
-
|
239
|
-
# @!method post(url = nil, body = nil, headers = nil)
|
240
|
-
# Makes a POST HTTP request with a body.
|
241
|
-
# @!scope class
|
242
|
-
#
|
243
|
-
# @param url [String] The optional String base URL to use as a prefix for
|
244
|
-
# all requests. Can also be the options Hash.
|
245
|
-
# @param body [String] body for the request.
|
246
|
-
# @param headers [Hash] unencoded HTTP header key/value pairs.
|
247
|
-
#
|
248
|
-
# @example
|
249
|
-
# conn.post '/items', data, content_type: 'application/json'
|
155
|
+
# conn.post '/items', data, :content_type => 'application/json'
|
250
156
|
#
|
251
157
|
# # Simple ElasticSearch indexing sample.
|
252
158
|
# conn.post '/twitter/tweet' do |req|
|
253
159
|
# req.headers[:content_type] = 'application/json'
|
254
160
|
# req.params[:routing] = 'kimchy'
|
255
|
-
# req.body = JSON.generate(user
|
161
|
+
# req.body = JSON.generate(:user => 'kimchy', ...)
|
256
162
|
# end
|
257
163
|
#
|
258
|
-
#
|
259
|
-
#
|
260
|
-
|
261
|
-
# @!method put(url = nil, body = nil, headers = nil)
|
262
|
-
# Makes a PUT HTTP request with a body.
|
263
|
-
# @!scope class
|
164
|
+
# Yields a Faraday::Request for further request customizations.
|
165
|
+
# Returns a Faraday::Response.
|
264
166
|
#
|
265
|
-
#
|
266
|
-
# all requests. Can also be the options Hash.
|
267
|
-
# @param body [String] body for the request.
|
268
|
-
# @param headers [Hash] unencoded HTTP header key/value pairs.
|
167
|
+
# Signature
|
269
168
|
#
|
270
|
-
#
|
271
|
-
# # TODO: Make it a PUT example
|
272
|
-
# conn.post '/items', data, content_type: 'application/json'
|
169
|
+
# <verb>(url = nil, body = nil, headers = nil)
|
273
170
|
#
|
274
|
-
#
|
275
|
-
|
276
|
-
# req.headers[:content_type] = 'application/json'
|
277
|
-
# req.params[:routing] = 'kimchy'
|
278
|
-
# req.body = JSON.generate(user: 'kimchy', ...)
|
279
|
-
# end
|
280
|
-
#
|
281
|
-
# @yield [Faraday::Request] for further request customizations
|
282
|
-
# @return [Faraday::Response]
|
283
|
-
|
284
|
-
# @!visibility private
|
285
|
-
METHODS_WITH_BODY.each do |method|
|
171
|
+
# verb - An HTTP verb: post, put, or patch.
|
172
|
+
%w[post put patch].each do |method|
|
286
173
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
287
174
|
def #{method}(url = nil, body = nil, headers = nil, &block)
|
288
175
|
run_request(:#{method}, url, body, headers, &block)
|
@@ -290,110 +177,116 @@ module Faraday
|
|
290
177
|
RUBY
|
291
178
|
end
|
292
179
|
|
293
|
-
# Sets up the Authorization header with these credentials, encoded
|
180
|
+
# Public: Sets up the Authorization header with these credentials, encoded
|
294
181
|
# with base64.
|
295
182
|
#
|
296
|
-
#
|
297
|
-
#
|
183
|
+
# login - The authentication login.
|
184
|
+
# pass - The authentication password.
|
298
185
|
#
|
299
|
-
#
|
186
|
+
# Examples
|
300
187
|
#
|
301
188
|
# conn.basic_auth 'Aladdin', 'open sesame'
|
302
189
|
# conn.headers['Authorization']
|
303
190
|
# # => "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
|
304
191
|
#
|
305
|
-
#
|
192
|
+
# Returns nothing.
|
306
193
|
def basic_auth(login, pass)
|
307
194
|
set_authorization_header(:basic_auth, login, pass)
|
308
195
|
end
|
309
196
|
|
310
|
-
# Sets up the Authorization header with the given token.
|
197
|
+
# Public: Sets up the Authorization header with the given token.
|
311
198
|
#
|
312
|
-
#
|
313
|
-
#
|
199
|
+
# token - The String token.
|
200
|
+
# options - Optional Hash of extra token options.
|
314
201
|
#
|
315
|
-
#
|
202
|
+
# Examples
|
316
203
|
#
|
317
|
-
# conn.token_auth 'abcdef', foo
|
204
|
+
# conn.token_auth 'abcdef', :foo => 'bar'
|
318
205
|
# conn.headers['Authorization']
|
319
206
|
# # => "Token token=\"abcdef\",
|
320
207
|
# foo=\"bar\""
|
321
208
|
#
|
322
|
-
#
|
209
|
+
# Returns nothing.
|
323
210
|
def token_auth(token, options = nil)
|
324
211
|
set_authorization_header(:token_auth, token, options)
|
325
212
|
end
|
326
213
|
|
327
|
-
# Sets up a custom Authorization header.
|
214
|
+
# Public: Sets up a custom Authorization header.
|
328
215
|
#
|
329
|
-
#
|
330
|
-
#
|
331
|
-
# a Hash is encoded into comma
|
216
|
+
# type - The String authorization type.
|
217
|
+
# token - The String or Hash token. A String value is taken literally, and
|
218
|
+
# a Hash is encoded into comma separated key/value pairs.
|
332
219
|
#
|
333
|
-
#
|
220
|
+
# Examples
|
334
221
|
#
|
335
222
|
# conn.authorization :Bearer, 'mF_9.B5f-4.1JqM'
|
336
223
|
# conn.headers['Authorization']
|
337
224
|
# # => "Bearer mF_9.B5f-4.1JqM"
|
338
225
|
#
|
339
|
-
# conn.authorization :Token, token
|
226
|
+
# conn.authorization :Token, :token => 'abcdef', :foo => 'bar'
|
340
227
|
# conn.headers['Authorization']
|
341
228
|
# # => "Token token=\"abcdef\",
|
342
229
|
# foo=\"bar\""
|
343
230
|
#
|
344
|
-
#
|
231
|
+
# Returns nothing.
|
345
232
|
def authorization(type, token)
|
346
233
|
set_authorization_header(:authorization, type, token)
|
347
234
|
end
|
348
235
|
|
349
|
-
#
|
236
|
+
# Internal: Traverse the middleware stack in search of a
|
237
|
+
# parallel-capable adapter.
|
350
238
|
#
|
351
|
-
#
|
239
|
+
# Yields in case of not found.
|
352
240
|
#
|
353
|
-
#
|
354
|
-
# @api private
|
241
|
+
# Returns a parallel manager or nil if not found.
|
355
242
|
def default_parallel_manager
|
356
243
|
@default_parallel_manager ||= begin
|
357
|
-
|
244
|
+
handler = @builder.handlers.detect do |h|
|
245
|
+
h.klass.respond_to?(:supports_parallel?) and h.klass.supports_parallel?
|
246
|
+
end
|
358
247
|
|
359
|
-
if
|
360
|
-
|
248
|
+
if handler
|
249
|
+
handler.klass.setup_parallel_manager
|
361
250
|
elsif block_given?
|
362
251
|
yield
|
363
252
|
end
|
364
253
|
end
|
365
254
|
end
|
366
255
|
|
367
|
-
# Determine if this Faraday::Connection can make parallel requests.
|
256
|
+
# Public: Determine if this Faraday::Connection can make parallel requests.
|
368
257
|
#
|
369
|
-
#
|
258
|
+
# Returns true or false.
|
370
259
|
def in_parallel?
|
371
260
|
!!@parallel_manager
|
372
261
|
end
|
373
262
|
|
374
|
-
# Sets up the parallel manager to make a set of requests.
|
263
|
+
# Public: Sets up the parallel manager to make a set of requests.
|
375
264
|
#
|
376
|
-
#
|
377
|
-
# Adapter uses.
|
265
|
+
# manager - The parallel manager that this Connection's Adapter uses.
|
378
266
|
#
|
379
|
-
#
|
380
|
-
#
|
267
|
+
# Yields a block to execute multiple requests.
|
268
|
+
# Returns nothing.
|
381
269
|
def in_parallel(manager = nil)
|
382
|
-
@parallel_manager = manager || default_parallel_manager
|
383
|
-
warn
|
384
|
-
|
385
|
-
warn caller[2, 10].join("\n")
|
270
|
+
@parallel_manager = manager || default_parallel_manager {
|
271
|
+
warn "Warning: `in_parallel` called but no parallel-capable adapter on Faraday stack"
|
272
|
+
warn caller[2,10].join("\n")
|
386
273
|
nil
|
387
|
-
|
274
|
+
}
|
388
275
|
yield
|
389
|
-
@parallel_manager
|
276
|
+
@parallel_manager && @parallel_manager.run
|
390
277
|
ensure
|
391
278
|
@parallel_manager = nil
|
392
279
|
end
|
393
280
|
|
394
|
-
# Sets the Hash proxy options.
|
395
|
-
|
396
|
-
|
281
|
+
# Public: Gets or Sets the Hash proxy options.
|
282
|
+
def proxy(arg = nil)
|
283
|
+
return @proxy if arg.nil?
|
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
|
286
|
+
@proxy = ProxyOptions.from(arg)
|
287
|
+
end
|
288
|
+
|
289
|
+
# Public: Sets the Hash proxy options.
|
397
290
|
def proxy=(new_value)
|
398
291
|
@manual_proxy = true
|
399
292
|
@proxy = new_value ? ProxyOptions.from(new_value) : nil
|
@@ -402,14 +295,13 @@ module Faraday
|
|
402
295
|
def_delegators :url_prefix, :scheme, :scheme=, :host, :host=, :port, :port=
|
403
296
|
def_delegator :url_prefix, :path, :path_prefix
|
404
297
|
|
405
|
-
# Parses the
|
406
|
-
# components in this connection.
|
298
|
+
# Public: Parses the giving url with URI and stores the individual
|
299
|
+
# components in this connection. These components serve as defaults for
|
407
300
|
# requests made by this connection.
|
408
301
|
#
|
409
|
-
#
|
410
|
-
# @param encoder [Object]
|
302
|
+
# url - A String or URI.
|
411
303
|
#
|
412
|
-
#
|
304
|
+
# Examples
|
413
305
|
#
|
414
306
|
# conn = Faraday::Connection.new { ... }
|
415
307
|
# conn.url_prefix = "https://sushi.com/api"
|
@@ -417,6 +309,8 @@ module Faraday
|
|
417
309
|
# conn.path_prefix # => "/api"
|
418
310
|
#
|
419
311
|
# conn.get("nigiri?page=2") # accesses https://sushi.com/api/nigiri
|
312
|
+
#
|
313
|
+
# Returns the parsed URI from the given input..
|
420
314
|
def url_prefix=(url, encoder = nil)
|
421
315
|
uri = @url_prefix = Utils.URI(url)
|
422
316
|
self.path_prefix = uri.path
|
@@ -428,65 +322,54 @@ module Faraday
|
|
428
322
|
basic_auth user, password
|
429
323
|
uri.user = uri.password = nil
|
430
324
|
end
|
325
|
+
|
326
|
+
uri
|
431
327
|
end
|
432
328
|
|
433
|
-
# Sets the path prefix and ensures that it always has a leading
|
329
|
+
# Public: Sets the path prefix and ensures that it always has a leading
|
434
330
|
# slash.
|
435
331
|
#
|
436
|
-
#
|
332
|
+
# value - A String.
|
437
333
|
#
|
438
|
-
#
|
334
|
+
# Returns the new String path prefix.
|
439
335
|
def path_prefix=(value)
|
440
336
|
url_prefix.path = if value
|
441
|
-
|
442
|
-
|
443
|
-
|
337
|
+
value = '/' + value unless value[0,1] == '/'
|
338
|
+
value
|
339
|
+
end
|
444
340
|
end
|
445
341
|
|
446
|
-
# Takes a relative url for a request and combines it with the defaults
|
342
|
+
# Public: Takes a relative url for a request and combines it with the defaults
|
447
343
|
# set on the connection instance.
|
448
344
|
#
|
449
|
-
# @param url [String]
|
450
|
-
# @param extra_params [Hash]
|
451
|
-
#
|
452
|
-
# @example
|
453
345
|
# conn = Faraday::Connection.new { ... }
|
454
346
|
# conn.url_prefix = "https://sushi.com/api?token=abc"
|
455
347
|
# conn.scheme # => https
|
456
348
|
# conn.path_prefix # => "/api"
|
457
349
|
#
|
458
|
-
# conn.build_url("nigiri?page=2")
|
459
|
-
# # => https://sushi.com/api/nigiri?token=abc&page=2
|
460
|
-
#
|
461
|
-
# conn.build_url("nigiri", page: 2)
|
462
|
-
# # => https://sushi.com/api/nigiri?token=abc&page=2
|
350
|
+
# conn.build_url("nigiri?page=2") # => https://sushi.com/api/nigiri?token=abc&page=2
|
351
|
+
# conn.build_url("nigiri", :page => 2) # => https://sushi.com/api/nigiri?token=abc&page=2
|
463
352
|
#
|
464
353
|
def build_url(url = nil, extra_params = nil)
|
465
354
|
uri = build_exclusive_url(url)
|
466
355
|
|
467
356
|
query_values = params.dup.merge_query(uri.query, options.params_encoder)
|
468
|
-
query_values.update
|
469
|
-
uri.query =
|
470
|
-
if query_values.empty?
|
471
|
-
nil
|
472
|
-
else
|
473
|
-
query_values.to_query(options.params_encoder)
|
474
|
-
end
|
357
|
+
query_values.update extra_params if extra_params
|
358
|
+
uri.query = query_values.empty? ? nil : query_values.to_query(options.params_encoder)
|
475
359
|
|
476
360
|
uri
|
477
361
|
end
|
478
362
|
|
479
363
|
# Builds and runs the Faraday::Request.
|
480
364
|
#
|
481
|
-
#
|
482
|
-
#
|
483
|
-
#
|
484
|
-
#
|
485
|
-
# @param headers [Hash] unencoded HTTP header key/value pairs.
|
365
|
+
# method - The Symbol HTTP method.
|
366
|
+
# url - The String or URI to access.
|
367
|
+
# body - The request body that will eventually be converted to a string.
|
368
|
+
# headers - Hash of unencoded HTTP header key/value pairs.
|
486
369
|
#
|
487
|
-
#
|
370
|
+
# Returns a Faraday::Response.
|
488
371
|
def run_request(method, url, body, headers)
|
489
|
-
|
372
|
+
if !METHODS.include?(method)
|
490
373
|
raise ArgumentError, "unknown http method: #{method}"
|
491
374
|
end
|
492
375
|
|
@@ -494,7 +377,7 @@ module Faraday
|
|
494
377
|
@temp_proxy = proxy_for_request(url)
|
495
378
|
|
496
379
|
request = build_request(method) do |req|
|
497
|
-
req.options = req.options.merge(proxy
|
380
|
+
req.options = req.options.merge(:proxy => @temp_proxy)
|
498
381
|
req.url(url) if url
|
499
382
|
req.headers.update(headers) if headers
|
500
383
|
req.body = body if body
|
@@ -506,93 +389,73 @@ module Faraday
|
|
506
389
|
|
507
390
|
# Creates and configures the request object.
|
508
391
|
#
|
509
|
-
#
|
510
|
-
#
|
511
|
-
# @yield [Faraday::Request] if block given
|
512
|
-
# @return [Faraday::Request]
|
392
|
+
# Returns the new Request.
|
513
393
|
def build_request(method)
|
514
394
|
Request.create(method) do |req|
|
515
|
-
req.params = params.dup
|
516
|
-
req.headers = headers.dup
|
517
|
-
req.options = options
|
395
|
+
req.params = self.params.dup
|
396
|
+
req.headers = self.headers.dup
|
397
|
+
req.options = self.options
|
518
398
|
yield(req) if block_given?
|
519
399
|
end
|
520
400
|
end
|
521
401
|
|
522
|
-
# Build an absolute URL based on url_prefix.
|
402
|
+
# Internal: Build an absolute URL based on url_prefix.
|
523
403
|
#
|
524
|
-
#
|
525
|
-
#
|
526
|
-
# replace the query values
|
404
|
+
# url - A String or URI-like object
|
405
|
+
# params - A Faraday::Utils::ParamsHash to replace the query values
|
527
406
|
# of the resulting url (default: nil).
|
528
407
|
#
|
529
|
-
#
|
408
|
+
# Returns the resulting URI instance.
|
530
409
|
def build_exclusive_url(url = nil, params = nil, params_encoder = nil)
|
531
|
-
url = nil if url.respond_to?(:empty?)
|
410
|
+
url = nil if url.respond_to?(:empty?) and url.empty?
|
532
411
|
base = url_prefix
|
533
|
-
if url
|
412
|
+
if url and base.path and base.path !~ /\/$/
|
534
413
|
base = base.dup
|
535
|
-
base.path = base.path + '/'
|
414
|
+
base.path = base.path + '/' # ensure trailing slash
|
536
415
|
end
|
537
416
|
uri = url ? base + url : base
|
538
|
-
if params
|
539
|
-
|
540
|
-
end
|
541
|
-
# rubocop:disable Style/SafeNavigation
|
542
|
-
uri.query = nil if uri.query && uri.query.empty?
|
543
|
-
# rubocop:enable Style/SafeNavigation
|
417
|
+
uri.query = params.to_query(params_encoder || options.params_encoder) if params
|
418
|
+
uri.query = nil if uri.query and uri.query.empty?
|
544
419
|
uri
|
545
420
|
end
|
546
421
|
|
547
|
-
# Creates a duplicate of this Faraday::Connection.
|
422
|
+
# Internal: Creates a duplicate of this Faraday::Connection.
|
548
423
|
#
|
549
|
-
#
|
550
|
-
#
|
551
|
-
# @return [Faraday::Connection]
|
424
|
+
# Returns a Faraday::Connection.
|
552
425
|
def dup
|
553
426
|
self.class.new(build_exclusive_url,
|
554
|
-
headers
|
555
|
-
params
|
556
|
-
builder
|
557
|
-
ssl
|
558
|
-
request
|
427
|
+
:headers => headers.dup,
|
428
|
+
:params => params.dup,
|
429
|
+
:builder => builder.dup,
|
430
|
+
:ssl => ssl.dup,
|
431
|
+
:request => options.dup)
|
559
432
|
end
|
560
433
|
|
561
|
-
# Yields username and password extracted from a URI if they both exist.
|
562
|
-
#
|
563
|
-
# @param uri [URI]
|
564
|
-
# @yield [username, password] any username and password
|
565
|
-
# @yieldparam username [String] any username from URI
|
566
|
-
# @yieldparam password [String] any password from URI
|
567
|
-
# @return [void]
|
568
|
-
# @api private
|
434
|
+
# Internal: Yields username and password extracted from a URI if they both exist.
|
569
435
|
def with_uri_credentials(uri)
|
570
|
-
|
571
|
-
|
572
|
-
|
436
|
+
if uri.user and uri.password
|
437
|
+
yield(Utils.unescape(uri.user), Utils.unescape(uri.password))
|
438
|
+
end
|
573
439
|
end
|
574
440
|
|
575
441
|
def set_authorization_header(header_type, *args)
|
576
|
-
header = Faraday::Request
|
577
|
-
|
578
|
-
.header(*args)
|
579
|
-
|
442
|
+
header = Faraday::Request.lookup_middleware(header_type).
|
443
|
+
header(*args)
|
580
444
|
headers[Faraday::Request::Authorization::KEY] = header
|
581
445
|
end
|
582
446
|
|
583
447
|
def proxy_from_env(url)
|
584
448
|
return if Faraday.ignore_env_proxy
|
585
|
-
|
586
449
|
uri = nil
|
587
450
|
if URI.parse('').respond_to?(:find_proxy)
|
588
451
|
case url
|
589
452
|
when String
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
453
|
+
uri = Utils.URI(url)
|
454
|
+
uri = URI.parse("#{uri.scheme}://#{uri.hostname}").find_proxy
|
455
|
+
when URI
|
456
|
+
uri = url.find_proxy
|
457
|
+
when nil
|
458
|
+
uri = find_default_proxy
|
596
459
|
end
|
597
460
|
else
|
598
461
|
warn 'no_proxy is unsupported' if ENV['no_proxy'] || ENV['NO_PROXY']
|
@@ -603,24 +466,19 @@ module Faraday
|
|
603
466
|
|
604
467
|
def find_default_proxy
|
605
468
|
uri = ENV['http_proxy']
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
469
|
+
if uri && !uri.empty?
|
470
|
+
uri = 'http://' + uri if uri !~ /^http/i
|
471
|
+
uri
|
472
|
+
end
|
610
473
|
end
|
611
474
|
|
612
475
|
def proxy_for_request(url)
|
613
|
-
return proxy if @manual_proxy
|
614
|
-
|
476
|
+
return self.proxy if @manual_proxy
|
615
477
|
if url && Utils.URI(url).absolute?
|
616
478
|
proxy_from_env(url)
|
617
479
|
else
|
618
|
-
proxy
|
480
|
+
self.proxy
|
619
481
|
end
|
620
482
|
end
|
621
|
-
|
622
|
-
def support_parallel?(adapter)
|
623
|
-
adapter&.respond_to?(:supports_parallel?) && adapter&.supports_parallel?
|
624
|
-
end
|
625
483
|
end
|
626
484
|
end
|