faraday 0.16.2 → 0.17.0
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 +4 -4
- data/LICENSE.md +1 -1
- data/README.md +347 -18
- data/lib/faraday.rb +175 -93
- data/lib/faraday/adapter.rb +22 -36
- data/lib/faraday/adapter/em_http.rb +99 -142
- 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 -104
- data/lib/faraday/adapter/net_http_persistent.rb +27 -48
- 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/error.rb +32 -80
- data/lib/faraday/middleware.rb +28 -4
- data/lib/faraday/options.rb +186 -35
- data/lib/faraday/parameters.rb +197 -4
- data/lib/faraday/rack_builder.rb +56 -67
- 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 +170 -197
- data/lib/faraday/request/token_authentication.rb +10 -15
- data/lib/faraday/request/url_encoded.rb +23 -41
- data/lib/faraday/response.rb +16 -23
- data/lib/faraday/response/logger.rb +69 -22
- data/lib/faraday/response/raise_error.rb +14 -36
- data/lib/faraday/upload_io.rb +67 -0
- data/lib/faraday/utils.rb +245 -28
- metadata +5 -22
- data/lib/faraday/adapter_registry.rb +0 -28
- data/lib/faraday/dependency_loader.rb +0 -37
- data/lib/faraday/deprecated_class.rb +0 -28
- 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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5fec2e4b9c0c8796f1b0dcb24807f456b121f13ae7f8de0f791d59e424ecb547
|
4
|
+
data.tar.gz: f2006014355f091959bda79594951e6fc7304e721433d203ee96ebe4c0058d63
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53b8e3270ae2cb39b9608ca8fd860379210c87e1c3d9674db5b23b7fa123c44fe2a24e9c70618396525552875ac35f5372be0abd0aeffed7977151573e3cb833
|
7
|
+
data.tar.gz: 1de826ef4f4c6d448b64b808a5e93e9e0e92cd6f1799a14d9cdba0ef77c16114632fefd2c4bc80dcfb6015495610165035685f56454b9d4d3acb5fcd9b3f0d79
|
data/LICENSE.md
CHANGED
data/README.md
CHANGED
@@ -1,32 +1,356 @@
|
|
1
|
-
#
|
1
|
+
# Faraday
|
2
2
|
|
3
3
|
[](https://rubygems.org/gems/faraday)
|
4
|
-
[](https://travis-ci.org/lostisland/faraday)
|
5
|
+
[](https://coveralls.io/github/lostisland/faraday?branch=master)
|
6
|
+
[](https://codeclimate.com/github/lostisland/faraday)
|
7
7
|
[](https://gitter.im/lostisland/faraday?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
8
8
|
|
9
9
|
|
10
|
-
Faraday is an HTTP client
|
10
|
+
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
|
-
|
14
|
+
Faraday supports these adapters out of the box:
|
15
15
|
|
16
|
-
|
17
|
-
|
16
|
+
* [Net::HTTP][net_http] _(default)_
|
17
|
+
* [Net::HTTP::Persistent][persistent]
|
18
|
+
* [Excon][]
|
19
|
+
* [Patron][]
|
20
|
+
* [EventMachine][]
|
21
|
+
* [HTTPClient][]
|
22
|
+
|
23
|
+
Adapters are slowly being moved into their own gems, or bundled with HTTP clients:
|
24
|
+
|
25
|
+
* [Typhoeus][]
|
26
|
+
|
27
|
+
It also includes a Rack adapter for hitting loaded Rack applications through
|
28
|
+
Rack::Test, and a Test adapter for stubbing requests by hand.
|
29
|
+
|
30
|
+
## API documentation
|
31
|
+
|
32
|
+
Available at [rubydoc.info](http://www.rubydoc.info/gems/faraday).
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
### Basic Use
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
response = Faraday.get 'http://sushi.com/nigiri/sake.json'
|
40
|
+
```
|
41
|
+
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
|
42
|
+
stack and default adapter (see [Faraday::RackBuilder#initialize](https://github.com/lostisland/faraday/blob/master/lib/faraday/rack_builder.rb)).
|
43
|
+
|
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:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
conn = Faraday.new(:url => 'http://www.example.com')
|
48
|
+
response = conn.get '/users' # GET http://www.example.com/users'
|
49
|
+
```
|
50
|
+
|
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.
|
52
|
+
Since the default middleware stack uses url\_encoded middleware and default adapter, use them on building your own middleware stack.
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
conn = Faraday.new(:url => 'http://sushi.com') do |faraday|
|
56
|
+
faraday.request :url_encoded # form-encode POST params
|
57
|
+
faraday.response :logger # log requests to $stdout
|
58
|
+
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
|
59
|
+
end
|
60
|
+
|
61
|
+
# Filter sensitive information from logs with a regex matcher
|
62
|
+
|
63
|
+
conn = Faraday.new(:url => 'http://sushi.com/api_key=s3cr3t') do |faraday|
|
64
|
+
faraday.request :url_encoded # form-encode POST params
|
65
|
+
faraday.response :logger do | logger |
|
66
|
+
logger.filter(/(api_key=)(\w+)/,'\1[REMOVED]')
|
67
|
+
end
|
68
|
+
faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
Once you have the connection object, use it to make HTTP requests. You can pass parameters to it in a few different ways:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
## GET ##
|
76
|
+
|
77
|
+
response = conn.get '/nigiri/sake.json' # GET http://sushi.com/nigiri/sake.json
|
78
|
+
response.body
|
79
|
+
|
80
|
+
conn.get '/nigiri', { :name => 'Maguro' } # GET http://sushi.com/nigiri?name=Maguro
|
81
|
+
|
82
|
+
conn.get do |req| # GET http://sushi.com/search?page=2&limit=100
|
83
|
+
req.url '/search', :page => 2
|
84
|
+
req.params['limit'] = 100
|
85
|
+
end
|
86
|
+
|
87
|
+
## POST ##
|
88
|
+
|
89
|
+
conn.post '/nigiri', { :name => 'Maguro' } # POST "name=maguro" to http://sushi.com/nigiri
|
90
|
+
```
|
91
|
+
|
92
|
+
Some configuration options can be adjusted per request:
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
# post payload as JSON instead of "www-form-urlencoded" encoding:
|
96
|
+
conn.post do |req|
|
97
|
+
req.url '/nigiri'
|
98
|
+
req.headers['Content-Type'] = 'application/json'
|
99
|
+
req.body = '{ "name": "Unagi" }'
|
100
|
+
end
|
101
|
+
|
102
|
+
## Per-request options ##
|
103
|
+
|
104
|
+
conn.get do |req|
|
105
|
+
req.url '/search'
|
106
|
+
req.options.timeout = 5 # open/read timeout in seconds
|
107
|
+
req.options.open_timeout = 2 # connection open timeout in seconds
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
111
|
+
And you can inject arbitrary data into the request using the `context` option:
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
# Anything you inject using context option will be available in the env on all middlewares
|
115
|
+
|
116
|
+
conn.get do |req|
|
117
|
+
req.url '/search'
|
118
|
+
req.options.context = {
|
119
|
+
foo: 'foo',
|
120
|
+
bar: 'bar'
|
121
|
+
}
|
122
|
+
end
|
123
|
+
```
|
124
|
+
|
125
|
+
### Changing how parameters are serialized
|
126
|
+
|
127
|
+
Sometimes you need to send the same URL parameter multiple times with different
|
128
|
+
values. This requires manually setting the parameter encoder and can be done on
|
129
|
+
either per-connection or per-request basis.
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
# per-connection setting
|
133
|
+
conn = Faraday.new :request => { :params_encoder => Faraday::FlatParamsEncoder }
|
134
|
+
|
135
|
+
conn.get do |req|
|
136
|
+
# per-request setting:
|
137
|
+
# req.options.params_encoder = my_encoder
|
138
|
+
req.params['roll'] = ['california', 'philadelphia']
|
139
|
+
end
|
140
|
+
# GET 'http://sushi.com?roll=california&roll=philadelphia'
|
141
|
+
```
|
142
|
+
|
143
|
+
The value of Faraday `params_encoder` can be any object that responds to:
|
144
|
+
|
145
|
+
* `encode(hash) #=> String`
|
146
|
+
* `decode(string) #=> Hash`
|
147
|
+
|
148
|
+
The encoder will affect both how query strings are processed and how POST bodies
|
149
|
+
get serialized. The default encoder is Faraday::NestedParamsEncoder.
|
150
|
+
|
151
|
+
## Authentication
|
152
|
+
|
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.
|
154
|
+
|
155
|
+
```ruby
|
156
|
+
Faraday.new(...) do |conn|
|
157
|
+
conn.basic_auth('username', 'password')
|
158
|
+
end
|
159
|
+
|
160
|
+
Faraday.new(...) do |conn|
|
161
|
+
conn.token_auth('authentication-token')
|
162
|
+
end
|
163
|
+
```
|
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
|
+
|
181
|
+
## Advanced middleware usage
|
182
|
+
|
183
|
+
The order in which middleware is stacked is important. Like with Rack, the
|
184
|
+
first middleware on the list wraps all others, while the last middleware is the
|
185
|
+
innermost one, so that must be the adapter.
|
186
|
+
|
187
|
+
```ruby
|
188
|
+
Faraday.new(...) do |conn|
|
189
|
+
# POST/PUT params encoders:
|
190
|
+
conn.request :multipart
|
191
|
+
conn.request :url_encoded
|
192
|
+
|
193
|
+
# add custom middleware
|
194
|
+
conn.use MyMiddleware
|
195
|
+
|
196
|
+
# Last middleware must be the adapter:
|
197
|
+
conn.adapter :net_http
|
198
|
+
end
|
199
|
+
```
|
200
|
+
|
201
|
+
This request middleware setup affects POST/PUT requests in the following way:
|
202
|
+
|
203
|
+
1. `Request::Multipart` checks for files in the payload, otherwise leaves
|
204
|
+
everything untouched;
|
205
|
+
2. `Request::UrlEncoded` encodes as "application/x-www-form-urlencoded" if not
|
206
|
+
already encoded or of another type
|
207
|
+
|
208
|
+
Swapping middleware means giving the other priority. Specifying the
|
209
|
+
"Content-Type" for the request is explicitly stating which middleware should
|
210
|
+
process it.
|
211
|
+
|
212
|
+
Examples:
|
213
|
+
|
214
|
+
```ruby
|
215
|
+
# uploading a file:
|
216
|
+
payload[:profile_pic] = Faraday::UploadIO.new('/path/to/avatar.jpg', 'image/jpeg')
|
217
|
+
|
218
|
+
# "Multipart" middleware detects files and encodes with "multipart/form-data":
|
219
|
+
conn.put '/profile', payload
|
220
|
+
```
|
221
|
+
|
222
|
+
## Writing middleware
|
223
|
+
|
224
|
+
Middleware are classes that implement a `call` instance method. They hook into
|
225
|
+
the request/response cycle.
|
226
|
+
|
227
|
+
```ruby
|
228
|
+
def call(request_env)
|
229
|
+
# do something with the request
|
230
|
+
# request_env[:request_headers].merge!(...)
|
231
|
+
|
232
|
+
@app.call(request_env).on_complete do |response_env|
|
233
|
+
# do something with the response
|
234
|
+
# response_env[:response_headers].merge!(...)
|
235
|
+
end
|
236
|
+
end
|
237
|
+
```
|
238
|
+
|
239
|
+
It's important to do all processing of the response only in the `on_complete`
|
240
|
+
block. This enables middleware to work in parallel mode where requests are
|
241
|
+
asynchronous.
|
242
|
+
|
243
|
+
The `env` is a hash with symbol keys that contains info about the request and,
|
244
|
+
later, response. Some keys are:
|
245
|
+
|
246
|
+
```
|
247
|
+
# request phase
|
248
|
+
:method - :get, :post, ...
|
249
|
+
:url - URI for the current request; also contains GET parameters
|
250
|
+
:body - POST parameters for :post/:put requests
|
251
|
+
:request_headers
|
252
|
+
|
253
|
+
# response phase
|
254
|
+
:status - HTTP response status code, such as 200
|
255
|
+
:body - the response body
|
256
|
+
:response_headers
|
257
|
+
```
|
258
|
+
|
259
|
+
## Ad-hoc adapters customization
|
260
|
+
|
261
|
+
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.
|
262
|
+
|
263
|
+
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.
|
264
|
+
|
265
|
+
### NetHttp
|
266
|
+
```ruby
|
267
|
+
conn = Faraday.new(...) do |f|
|
268
|
+
f.adapter :net_http do |http| # yields Net::HTTP
|
269
|
+
http.idle_timeout = 100
|
270
|
+
http.verify_callback = lambda do | preverify_ok, cert_store |
|
271
|
+
# do something here...
|
272
|
+
end
|
273
|
+
end
|
274
|
+
end
|
275
|
+
```
|
276
|
+
|
277
|
+
### NetHttpPersistent
|
278
|
+
```ruby
|
279
|
+
conn = Faraday.new(...) do |f|
|
280
|
+
f.adapter :net_http_persistent, pool_size: 5 do |http| # yields Net::HTTP::Persistent
|
281
|
+
http.idle_timeout = 100
|
282
|
+
http.retry_change_requests = true
|
283
|
+
end
|
284
|
+
end
|
285
|
+
```
|
286
|
+
|
287
|
+
### Patron
|
288
|
+
```ruby
|
289
|
+
conn = Faraday.new(...) do |f|
|
290
|
+
f.adapter :patron do |session| # yields Patron::Session
|
291
|
+
session.max_redirects = 10
|
292
|
+
end
|
293
|
+
end
|
294
|
+
```
|
295
|
+
|
296
|
+
### HTTPClient
|
297
|
+
```ruby
|
298
|
+
conn = Faraday.new(...) do |f|
|
299
|
+
f.adapter :httpclient do |client| # yields HTTPClient
|
300
|
+
client.keep_alive_timeout = 20
|
301
|
+
client.ssl_config.timeout = 25
|
302
|
+
end
|
303
|
+
end
|
304
|
+
```
|
305
|
+
|
306
|
+
## Using Faraday for testing
|
307
|
+
|
308
|
+
```ruby
|
309
|
+
# It's possible to define stubbed request outside a test adapter block.
|
310
|
+
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
|
311
|
+
stub.get('/tamago') { |env| [200, {}, 'egg'] }
|
312
|
+
end
|
313
|
+
|
314
|
+
# You can pass stubbed request to the test adapter or define them in a block
|
315
|
+
# or a combination of the two.
|
316
|
+
test = Faraday.new do |builder|
|
317
|
+
builder.adapter :test, stubs do |stub|
|
318
|
+
stub.get('/ebi') { |env| [ 200, {}, 'shrimp' ]}
|
319
|
+
end
|
320
|
+
end
|
321
|
+
|
322
|
+
# It's also possible to stub additional requests after the connection has
|
323
|
+
# been initialized. This is useful for testing.
|
324
|
+
stubs.get('/uni') { |env| [ 200, {}, 'urchin' ]}
|
325
|
+
|
326
|
+
resp = test.get '/tamago'
|
327
|
+
resp.body # => 'egg'
|
328
|
+
resp = test.get '/ebi'
|
329
|
+
resp.body # => 'shrimp'
|
330
|
+
resp = test.get '/uni'
|
331
|
+
resp.body # => 'urchin'
|
332
|
+
resp = test.get '/else' #=> raises "no such stub" error
|
333
|
+
|
334
|
+
# If you like, you can treat your stubs as mocks by verifying that all of
|
335
|
+
# the stubbed calls were made. NOTE that this feature is still fairly
|
336
|
+
# experimental: It will not verify the order or count of any stub, only that
|
337
|
+
# it was called once during the course of the test.
|
338
|
+
stubs.verify_stubbed_calls
|
339
|
+
```
|
18
340
|
|
19
341
|
## Supported Ruby versions
|
20
342
|
|
21
|
-
This library aims to support and is [tested against][
|
343
|
+
This library aims to support and is [tested against][travis] the following Ruby
|
22
344
|
implementations:
|
23
345
|
|
24
|
-
* Ruby
|
346
|
+
* Ruby 1.9.3+
|
347
|
+
* [JRuby][] 1.7+
|
348
|
+
* [Rubinius][] 2+
|
25
349
|
|
26
350
|
If something doesn't work on one of these Ruby versions, it's a bug.
|
27
351
|
|
28
352
|
This library may inadvertently work (or seem to work) on other Ruby
|
29
|
-
implementations
|
353
|
+
implementations, however support will only be provided for the versions listed
|
30
354
|
above.
|
31
355
|
|
32
356
|
If you would like this library to support another Ruby version, you may
|
@@ -40,16 +364,21 @@ of a major release, support for that Ruby version may be dropped.
|
|
40
364
|
|
41
365
|
Do you want to contribute to Faraday?
|
42
366
|
Open the issues page and check for the `help wanted` label!
|
43
|
-
But before you start coding, please read our [Contributing Guide]
|
367
|
+
But before you start coding, please read our [Contributing Guide](https://github.com/lostisland/faraday/blob/master/.github/CONTRIBUTING.md)
|
44
368
|
|
45
369
|
## Copyright
|
46
|
-
© 2009 - 2019, the [Faraday Team][faraday_team]. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
|
47
370
|
|
48
|
-
[
|
49
|
-
[
|
50
|
-
|
51
|
-
[
|
52
|
-
[
|
371
|
+
Copyright (c) 2009-2017 [Rick Olson](mailto:technoweenie@gmail.com), Zack Hobson.
|
372
|
+
See [LICENSE][] for details.
|
373
|
+
|
374
|
+
[net_http]: http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/Net/HTTP.html
|
375
|
+
[persistent]: https://github.com/drbrain/net-http-persistent
|
376
|
+
[travis]: https://travis-ci.org/lostisland/faraday
|
377
|
+
[excon]: https://github.com/excon/excon#readme
|
378
|
+
[patron]: http://toland.github.io/patron/
|
379
|
+
[eventmachine]: https://github.com/igrigorik/em-http-request#readme
|
380
|
+
[httpclient]: https://github.com/nahi/httpclient
|
381
|
+
[typhoeus]: https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/adapters/faraday.rb
|
53
382
|
[jruby]: http://jruby.org/
|
54
383
|
[rubinius]: http://rubini.us/
|
55
384
|
[license]: LICENSE.md
|
data/lib/faraday.rb
CHANGED
@@ -1,153 +1,129 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
require 'thread'
|
3
2
|
require 'cgi'
|
4
3
|
require 'set'
|
5
4
|
require 'forwardable'
|
6
|
-
require 'faraday/middleware_registry'
|
7
|
-
require 'faraday/dependency_loader'
|
8
5
|
|
9
|
-
# This is the main namespace for Faraday.
|
6
|
+
# Public: This is the main namespace for Faraday. You can either use it to
|
7
|
+
# create Faraday::Connection objects, or access it directly.
|
10
8
|
#
|
11
|
-
#
|
12
|
-
# methods to use directly.
|
9
|
+
# Examples
|
13
10
|
#
|
14
|
-
# @example Helpful class methods for easy usage
|
15
11
|
# Faraday.get "http://faraday.com"
|
16
12
|
#
|
17
|
-
# @example Helpful class method `.new` to create {Connection} objects.
|
18
13
|
# conn = Faraday.new "http://faraday.com"
|
19
14
|
# conn.get '/'
|
20
15
|
#
|
21
16
|
module Faraday
|
22
|
-
VERSION =
|
23
|
-
METHODS_WITH_QUERY = %w[get head delete connect trace].freeze
|
24
|
-
METHODS_WITH_BODY = %w[post put patch].freeze
|
17
|
+
VERSION = "0.17.0"
|
25
18
|
|
26
19
|
class << self
|
27
|
-
#
|
28
|
-
#
|
29
|
-
# This is the root from where the libraries are auto-loaded.
|
30
|
-
#
|
31
|
-
# @return [String]
|
20
|
+
# Public: Gets or sets the root path that Faraday is being loaded from.
|
21
|
+
# This is the root from where the libraries are auto-loaded from.
|
32
22
|
attr_accessor :root_path
|
33
23
|
|
34
|
-
# Gets or sets the path that the Faraday libs are loaded from.
|
35
|
-
# @return [String]
|
24
|
+
# Public: Gets or sets the path that the Faraday libs are loaded from.
|
36
25
|
attr_accessor :lib_path
|
37
26
|
|
38
|
-
#
|
39
|
-
#
|
40
|
-
# for the default {Faraday::Connection}. Defaults to `:net_http`.
|
41
|
-
# @return [Symbol] the default adapter
|
42
|
-
# @overload default_adapter=(adapter)
|
43
|
-
# Updates default adapter while resetting {.default_connection}.
|
44
|
-
# @return [Symbol] the new default_adapter.
|
27
|
+
# Public: Gets or sets the Symbol key identifying a default Adapter to use
|
28
|
+
# for the default Faraday::Connection.
|
45
29
|
attr_reader :default_adapter
|
46
30
|
|
47
|
-
#
|
31
|
+
# Public: Sets the default Faraday::Connection for simple scripts that
|
32
|
+
# access the Faraday constant directly.
|
33
|
+
#
|
34
|
+
# Faraday.get "https://faraday.com"
|
48
35
|
attr_writer :default_connection
|
49
36
|
|
50
|
-
# Tells
|
51
|
-
# Defaults to `false`.
|
52
|
-
# @return [Boolean]
|
37
|
+
# Public: Tells faraday to ignore the environment proxy (http_proxy).
|
53
38
|
attr_accessor :ignore_env_proxy
|
54
39
|
|
55
|
-
# Initializes a new
|
56
|
-
#
|
57
|
-
#
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
63
|
-
#
|
64
|
-
#
|
65
|
-
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
#
|
69
|
-
#
|
70
|
-
#
|
40
|
+
# Public: Initializes a new Faraday::Connection.
|
41
|
+
#
|
42
|
+
# url - The optional String base URL to use as a prefix for all
|
43
|
+
# requests. Can also be the options Hash.
|
44
|
+
# options - The optional Hash used to configure this Faraday::Connection.
|
45
|
+
# Any of these values will be set on every request made, unless
|
46
|
+
# overridden for a specific request.
|
47
|
+
# :url - String base URL.
|
48
|
+
# :params - Hash of URI query unencoded key/value pairs.
|
49
|
+
# :headers - Hash of unencoded HTTP header key/value pairs.
|
50
|
+
# :request - Hash of request options.
|
51
|
+
# :ssl - Hash of SSL options.
|
52
|
+
# :proxy - Hash of Proxy options.
|
53
|
+
#
|
54
|
+
# Examples
|
55
|
+
#
|
71
56
|
# Faraday.new 'http://faraday.com'
|
72
|
-
#
|
73
|
-
#
|
74
|
-
#
|
75
|
-
#
|
76
|
-
# #
|
77
|
-
#
|
78
|
-
#
|
79
|
-
#
|
80
|
-
#
|
81
|
-
#
|
82
|
-
def new(url = nil, options =
|
83
|
-
|
57
|
+
#
|
58
|
+
# # http://faraday.com?page=1
|
59
|
+
# Faraday.new 'http://faraday.com', :params => {:page => 1}
|
60
|
+
#
|
61
|
+
# # same
|
62
|
+
#
|
63
|
+
# Faraday.new :url => 'http://faraday.com',
|
64
|
+
# :params => {:page => 1}
|
65
|
+
#
|
66
|
+
# Returns a Faraday::Connection.
|
67
|
+
def new(url = nil, options = nil)
|
68
|
+
block = block_given? ? Proc.new : nil
|
69
|
+
options = options ? default_connection_options.merge(options) : default_connection_options
|
84
70
|
Faraday::Connection.new(url, options, &block)
|
85
71
|
end
|
86
72
|
|
87
|
-
# @private
|
88
73
|
# Internal: Requires internal Faraday libraries.
|
89
74
|
#
|
90
|
-
#
|
91
|
-
#
|
75
|
+
# *libs - One or more relative String names to Faraday classes.
|
76
|
+
#
|
77
|
+
# Returns nothing.
|
92
78
|
def require_libs(*libs)
|
93
79
|
libs.each do |lib|
|
94
80
|
require "#{lib_path}/#{lib}"
|
95
81
|
end
|
96
82
|
end
|
97
83
|
|
98
|
-
|
99
|
-
|
100
|
-
#
|
84
|
+
# Public: Updates default adapter while resetting
|
85
|
+
# #default_connection.
|
86
|
+
#
|
87
|
+
# Returns the new default_adapter.
|
101
88
|
def default_adapter=(adapter)
|
102
89
|
@default_connection = nil
|
103
90
|
@default_adapter = adapter
|
104
91
|
end
|
105
92
|
|
106
|
-
|
93
|
+
alias require_lib require_libs
|
94
|
+
|
95
|
+
def respond_to?(symbol, include_private = false)
|
107
96
|
default_connection.respond_to?(symbol, include_private) || super
|
108
97
|
end
|
109
98
|
|
110
|
-
|
111
|
-
|
99
|
+
private
|
112
100
|
# Internal: Proxies method calls on the Faraday constant to
|
113
|
-
#
|
101
|
+
# #default_connection.
|
114
102
|
def method_missing(name, *args, &block)
|
115
|
-
|
116
|
-
default_connection.send(name, *args, &block)
|
117
|
-
else
|
118
|
-
super
|
119
|
-
end
|
103
|
+
default_connection.send(name, *args, &block)
|
120
104
|
end
|
121
105
|
end
|
122
106
|
|
123
107
|
self.ignore_env_proxy = false
|
124
|
-
self.root_path = File.expand_path
|
125
|
-
self.lib_path = File.expand_path
|
108
|
+
self.root_path = File.expand_path "..", __FILE__
|
109
|
+
self.lib_path = File.expand_path "../faraday", __FILE__
|
126
110
|
self.default_adapter = :net_http
|
127
111
|
|
128
|
-
#
|
129
|
-
#
|
130
|
-
#
|
131
|
-
# the default_adapter.
|
132
|
-
# @overload default_connection=(connection)
|
133
|
-
# @param connection [Faraday::Connection]
|
134
|
-
# Sets the default {Faraday::Connection} for simple scripts that
|
135
|
-
# access the Faraday constant directly, such as
|
136
|
-
# <code>Faraday.get "https://faraday.com"</code>.
|
112
|
+
# Gets the default connection used for simple scripts.
|
113
|
+
#
|
114
|
+
# Returns a Faraday::Connection, configured with the #default_adapter.
|
137
115
|
def self.default_connection
|
138
116
|
@default_connection ||= Connection.new(default_connection_options)
|
139
117
|
end
|
140
118
|
|
141
|
-
# Gets the default connection options used when calling
|
119
|
+
# Gets the default connection options used when calling Faraday#new.
|
142
120
|
#
|
143
|
-
#
|
121
|
+
# Returns a Faraday::ConnectionOptions.
|
144
122
|
def self.default_connection_options
|
145
123
|
@default_connection_options ||= ConnectionOptions.new
|
146
124
|
end
|
147
125
|
|
148
|
-
# Sets the default options used when calling
|
149
|
-
#
|
150
|
-
# @param options [Hash, Faraday::ConnectionOptions]
|
126
|
+
# Public: Sets the default options used when calling Faraday#new.
|
151
127
|
def self.default_connection_options=(options)
|
152
128
|
@default_connection = nil
|
153
129
|
@default_connection_options = ConnectionOptions.from(options)
|
@@ -158,9 +134,115 @@ module Faraday
|
|
158
134
|
Timer = Timeout
|
159
135
|
end
|
160
136
|
|
161
|
-
|
162
|
-
|
163
|
-
|
137
|
+
# Public: Adds the ability for other modules to register and lookup
|
138
|
+
# middleware classes.
|
139
|
+
module MiddlewareRegistry
|
140
|
+
# Public: Register middleware class(es) on the current module.
|
141
|
+
#
|
142
|
+
# mapping - A Hash mapping Symbol keys to classes. Classes can be expressed
|
143
|
+
# as fully qualified constant, or a Proc that will be lazily
|
144
|
+
# called to return the former.
|
145
|
+
#
|
146
|
+
# Examples
|
147
|
+
#
|
148
|
+
# module Faraday
|
149
|
+
# class Whatever
|
150
|
+
# # Middleware looked up by :foo returns Faraday::Whatever::Foo.
|
151
|
+
# register_middleware :foo => Foo
|
152
|
+
#
|
153
|
+
# # Middleware looked up by :bar returns Faraday::Whatever.const_get(:Bar)
|
154
|
+
# register_middleware :bar => :Bar
|
155
|
+
#
|
156
|
+
# # Middleware looked up by :baz requires 'baz' and returns Faraday::Whatever.const_get(:Baz)
|
157
|
+
# register_middleware :baz => [:Baz, 'baz']
|
158
|
+
# end
|
159
|
+
# end
|
160
|
+
#
|
161
|
+
# Returns nothing.
|
162
|
+
def register_middleware(autoload_path = nil, mapping = nil)
|
163
|
+
if mapping.nil?
|
164
|
+
mapping = autoload_path
|
165
|
+
autoload_path = nil
|
166
|
+
end
|
167
|
+
middleware_mutex do
|
168
|
+
@middleware_autoload_path = autoload_path if autoload_path
|
169
|
+
(@registered_middleware ||= {}).update(mapping)
|
170
|
+
end
|
171
|
+
end
|
164
172
|
|
165
|
-
|
173
|
+
# Public: Lookup middleware class with a registered Symbol shortcut.
|
174
|
+
#
|
175
|
+
# key - The Symbol key for the registered middleware.
|
176
|
+
#
|
177
|
+
# Examples
|
178
|
+
#
|
179
|
+
# module Faraday
|
180
|
+
# class Whatever
|
181
|
+
# register_middleware :foo => Foo
|
182
|
+
# end
|
183
|
+
# end
|
184
|
+
#
|
185
|
+
# Faraday::Whatever.lookup_middleware(:foo)
|
186
|
+
# # => Faraday::Whatever::Foo
|
187
|
+
#
|
188
|
+
# Returns a middleware Class.
|
189
|
+
def lookup_middleware(key)
|
190
|
+
load_middleware(key) ||
|
191
|
+
raise(Faraday::Error.new("#{key.inspect} is not registered on #{self}"))
|
192
|
+
end
|
193
|
+
|
194
|
+
def middleware_mutex(&block)
|
195
|
+
@middleware_mutex ||= begin
|
196
|
+
require 'monitor'
|
197
|
+
Monitor.new
|
198
|
+
end
|
199
|
+
@middleware_mutex.synchronize(&block)
|
200
|
+
end
|
201
|
+
|
202
|
+
def fetch_middleware(key)
|
203
|
+
defined?(@registered_middleware) && @registered_middleware[key]
|
204
|
+
end
|
205
|
+
|
206
|
+
def load_middleware(key)
|
207
|
+
value = fetch_middleware(key)
|
208
|
+
case value
|
209
|
+
when Module
|
210
|
+
value
|
211
|
+
when Symbol, String
|
212
|
+
middleware_mutex do
|
213
|
+
@registered_middleware[key] = const_get(value)
|
214
|
+
end
|
215
|
+
when Proc
|
216
|
+
middleware_mutex do
|
217
|
+
@registered_middleware[key] = value.call
|
218
|
+
end
|
219
|
+
when Array
|
220
|
+
middleware_mutex do
|
221
|
+
const, path = value
|
222
|
+
if root = @middleware_autoload_path
|
223
|
+
path = "#{root}/#{path}"
|
224
|
+
end
|
225
|
+
require(path)
|
226
|
+
@registered_middleware[key] = const
|
227
|
+
end
|
228
|
+
load_middleware(key)
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
def self.const_missing(name)
|
234
|
+
if name.to_sym == :Builder
|
235
|
+
warn "Faraday::Builder is now Faraday::RackBuilder."
|
236
|
+
const_set name, RackBuilder
|
237
|
+
else
|
238
|
+
super
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
require_libs "utils", "options", "connection", "rack_builder", "parameters",
|
243
|
+
"middleware", "adapter", "request", "response", "upload_io", "error"
|
244
|
+
|
245
|
+
if !ENV["FARADAY_NO_AUTOLOAD"]
|
246
|
+
require_lib 'autoload'
|
247
|
+
end
|
166
248
|
end
|