faraday 0.15.4 → 0.16.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 +18 -344
- data/lib/faraday.rb +93 -175
- data/lib/faraday/adapter.rb +36 -22
- data/lib/faraday/adapter/em_http.rb +142 -99
- data/lib/faraday/adapter/em_http_ssl_patch.rb +23 -17
- data/lib/faraday/adapter/em_synchrony.rb +104 -60
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
- data/lib/faraday/adapter/excon.rb +100 -55
- data/lib/faraday/adapter/httpclient.rb +61 -39
- data/lib/faraday/adapter/net_http.rb +104 -51
- data/lib/faraday/adapter/net_http_persistent.rb +48 -27
- data/lib/faraday/adapter/patron.rb +54 -35
- data/lib/faraday/adapter/rack.rb +28 -12
- data/lib/faraday/adapter/test.rb +86 -53
- data/lib/faraday/adapter/typhoeus.rb +4 -1
- data/lib/faraday/adapter_registry.rb +28 -0
- data/lib/faraday/autoload.rb +47 -36
- data/lib/faraday/connection.rb +321 -179
- data/lib/faraday/dependency_loader.rb +37 -0
- data/lib/faraday/encoders/flat_params_encoder.rb +94 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +171 -0
- data/lib/faraday/error.rb +67 -33
- data/lib/faraday/file_part.rb +128 -0
- data/lib/faraday/logging/formatter.rb +92 -0
- data/lib/faraday/middleware.rb +4 -28
- data/lib/faraday/middleware_registry.rb +129 -0
- data/lib/faraday/options.rb +35 -186
- data/lib/faraday/options/connection_options.rb +22 -0
- data/lib/faraday/options/env.rb +181 -0
- data/lib/faraday/options/proxy_options.rb +28 -0
- data/lib/faraday/options/request_options.rb +21 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/param_part.rb +53 -0
- data/lib/faraday/parameters.rb +4 -197
- data/lib/faraday/rack_builder.rb +67 -56
- data/lib/faraday/request.rb +68 -36
- data/lib/faraday/request/authorization.rb +42 -30
- data/lib/faraday/request/basic_authentication.rb +14 -7
- data/lib/faraday/request/instrumentation.rb +45 -27
- data/lib/faraday/request/multipart.rb +79 -48
- data/lib/faraday/request/retry.rb +198 -169
- data/lib/faraday/request/token_authentication.rb +15 -10
- data/lib/faraday/request/url_encoded.rb +41 -23
- data/lib/faraday/response.rb +23 -16
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +36 -14
- data/lib/faraday/utils.rb +28 -245
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- metadata +21 -5
- data/lib/faraday/upload_io.rb +0 -67
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 010e4dd27187eb834e8a0c185cab419a1b69c6e3f8a5728f6681109b7848091d
|
4
|
+
data.tar.gz: ccd678f8acf84929c15a19b7a6ca1b22b2d634eabfae21c84c4a0f0ba97fb8b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 150271b18c60bf5b55cc7e74f39fb01d6c83eecc39119d425fe8d399829ba728183900a5a948dbb874f432fc95996b88c367e87b5274b45addbd15d6175d7bc0
|
7
|
+
data.tar.gz: 3f4ef0b5d787aeffef29d79af04297a62a6b1ae77f034858b7d68ac4bdcfc00734c976cb8dff8b76b0af007590c6f4a5376797c14d3798cc13193aed96689d0c
|
data/LICENSE.md
CHANGED
data/README.md
CHANGED
@@ -1,353 +1,32 @@
|
|
1
|
-
# Faraday
|
1
|
+
# 
|
2
2
|
|
3
3
|
[](https://rubygems.org/gems/faraday)
|
4
|
-
[](https://circleci.com/gh/lostisland/faraday/tree/master)
|
5
|
+
[](https://codeclimate.com/github/lostisland/faraday/test_coverage)
|
6
|
+
[](https://codeclimate.com/github/lostisland/faraday/maintainability)
|
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 library 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
|
+
## Getting Started
|
15
15
|
|
16
|
-
|
17
|
-
|
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
|
-
# Last middleware must be the adapter:
|
194
|
-
conn.adapter :net_http
|
195
|
-
end
|
196
|
-
```
|
197
|
-
|
198
|
-
This request middleware setup affects POST/PUT requests in the following way:
|
199
|
-
|
200
|
-
1. `Request::Multipart` checks for files in the payload, otherwise leaves
|
201
|
-
everything untouched;
|
202
|
-
2. `Request::UrlEncoded` encodes as "application/x-www-form-urlencoded" if not
|
203
|
-
already encoded or of another type
|
204
|
-
|
205
|
-
Swapping middleware means giving the other priority. Specifying the
|
206
|
-
"Content-Type" for the request is explicitly stating which middleware should
|
207
|
-
process it.
|
208
|
-
|
209
|
-
Examples:
|
210
|
-
|
211
|
-
```ruby
|
212
|
-
# uploading a file:
|
213
|
-
payload[:profile_pic] = Faraday::UploadIO.new('/path/to/avatar.jpg', 'image/jpeg')
|
214
|
-
|
215
|
-
# "Multipart" middleware detects files and encodes with "multipart/form-data":
|
216
|
-
conn.put '/profile', payload
|
217
|
-
```
|
218
|
-
|
219
|
-
## Writing middleware
|
220
|
-
|
221
|
-
Middleware are classes that implement a `call` instance method. They hook into
|
222
|
-
the request/response cycle.
|
223
|
-
|
224
|
-
```ruby
|
225
|
-
def call(request_env)
|
226
|
-
# do something with the request
|
227
|
-
# request_env[:request_headers].merge!(...)
|
228
|
-
|
229
|
-
@app.call(request_env).on_complete do |response_env|
|
230
|
-
# do something with the response
|
231
|
-
# response_env[:response_headers].merge!(...)
|
232
|
-
end
|
233
|
-
end
|
234
|
-
```
|
235
|
-
|
236
|
-
It's important to do all processing of the response only in the `on_complete`
|
237
|
-
block. This enables middleware to work in parallel mode where requests are
|
238
|
-
asynchronous.
|
239
|
-
|
240
|
-
The `env` is a hash with symbol keys that contains info about the request and,
|
241
|
-
later, response. Some keys are:
|
242
|
-
|
243
|
-
```
|
244
|
-
# request phase
|
245
|
-
:method - :get, :post, ...
|
246
|
-
:url - URI for the current request; also contains GET parameters
|
247
|
-
:body - POST parameters for :post/:put requests
|
248
|
-
:request_headers
|
249
|
-
|
250
|
-
# response phase
|
251
|
-
:status - HTTP response status code, such as 200
|
252
|
-
:body - the response body
|
253
|
-
:response_headers
|
254
|
-
```
|
255
|
-
|
256
|
-
## Ad-hoc adapters customization
|
257
|
-
|
258
|
-
Faraday is intended to be a generic interface between your code and the adapter. However, sometimes you need to access a feature specific to one of the adapters that is not covered in Faraday's interface.
|
259
|
-
|
260
|
-
When that happens, you can pass a block when specifying the adapter to customize it. The block parameter will change based on the adapter you're using. See below for some examples.
|
261
|
-
|
262
|
-
### NetHttp
|
263
|
-
```ruby
|
264
|
-
conn = Faraday.new(...) do |f|
|
265
|
-
f.adapter :net_http do |http| # yields Net::HTTP
|
266
|
-
http.idle_timeout = 100
|
267
|
-
http.verify_callback = lambda do | preverify_ok, cert_store |
|
268
|
-
# do something here...
|
269
|
-
end
|
270
|
-
end
|
271
|
-
end
|
272
|
-
```
|
273
|
-
|
274
|
-
### NetHttpPersistent
|
275
|
-
```ruby
|
276
|
-
conn = Faraday.new(...) do |f|
|
277
|
-
f.adapter :net_http_persistent, pool_size: 5 do |http| # yields Net::HTTP::Persistent
|
278
|
-
http.idle_timeout = 100
|
279
|
-
http.retry_change_requests = true
|
280
|
-
end
|
281
|
-
end
|
282
|
-
```
|
283
|
-
|
284
|
-
### Patron
|
285
|
-
```ruby
|
286
|
-
conn = Faraday.new(...) do |f|
|
287
|
-
f.adapter :patron do |session| # yields Patron::Session
|
288
|
-
session.max_redirects = 10
|
289
|
-
end
|
290
|
-
end
|
291
|
-
```
|
292
|
-
|
293
|
-
### HTTPClient
|
294
|
-
```ruby
|
295
|
-
conn = Faraday.new(...) do |f|
|
296
|
-
f.adapter :httpclient do |client| # yields HTTPClient
|
297
|
-
client.keep_alive_timeout = 20
|
298
|
-
client.ssl_config.timeout = 25
|
299
|
-
end
|
300
|
-
end
|
301
|
-
```
|
302
|
-
|
303
|
-
## Using Faraday for testing
|
304
|
-
|
305
|
-
```ruby
|
306
|
-
# It's possible to define stubbed request outside a test adapter block.
|
307
|
-
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
|
308
|
-
stub.get('/tamago') { |env| [200, {}, 'egg'] }
|
309
|
-
end
|
310
|
-
|
311
|
-
# You can pass stubbed request to the test adapter or define them in a block
|
312
|
-
# or a combination of the two.
|
313
|
-
test = Faraday.new do |builder|
|
314
|
-
builder.adapter :test, stubs do |stub|
|
315
|
-
stub.get('/ebi') { |env| [ 200, {}, 'shrimp' ]}
|
316
|
-
end
|
317
|
-
end
|
318
|
-
|
319
|
-
# It's also possible to stub additional requests after the connection has
|
320
|
-
# been initialized. This is useful for testing.
|
321
|
-
stubs.get('/uni') { |env| [ 200, {}, 'urchin' ]}
|
322
|
-
|
323
|
-
resp = test.get '/tamago'
|
324
|
-
resp.body # => 'egg'
|
325
|
-
resp = test.get '/ebi'
|
326
|
-
resp.body # => 'shrimp'
|
327
|
-
resp = test.get '/uni'
|
328
|
-
resp.body # => 'urchin'
|
329
|
-
resp = test.get '/else' #=> raises "no such stub" error
|
330
|
-
|
331
|
-
# If you like, you can treat your stubs as mocks by verifying that all of
|
332
|
-
# the stubbed calls were made. NOTE that this feature is still fairly
|
333
|
-
# experimental: It will not verify the order or count of any stub, only that
|
334
|
-
# it was called once during the course of the test.
|
335
|
-
stubs.verify_stubbed_calls
|
336
|
-
```
|
16
|
+
The best starting point is the [Faraday Website][website], with its introduction and explanation.
|
17
|
+
Need more details? See the [Faraday API Documentation][apidoc] to see how it works internally.
|
337
18
|
|
338
19
|
## Supported Ruby versions
|
339
20
|
|
340
|
-
This library aims to support and is [tested against][
|
21
|
+
This library aims to support and is [tested against][circle_ci] the following Ruby
|
341
22
|
implementations:
|
342
23
|
|
343
|
-
* Ruby
|
344
|
-
* [JRuby][] 1.7+
|
345
|
-
* [Rubinius][] 2+
|
24
|
+
* Ruby 2.3+
|
346
25
|
|
347
26
|
If something doesn't work on one of these Ruby versions, it's a bug.
|
348
27
|
|
349
28
|
This library may inadvertently work (or seem to work) on other Ruby
|
350
|
-
implementations, however support will only be provided for the versions listed
|
29
|
+
implementations and versions, however support will only be provided for the versions listed
|
351
30
|
above.
|
352
31
|
|
353
32
|
If you would like this library to support another Ruby version, you may
|
@@ -361,21 +40,16 @@ of a major release, support for that Ruby version may be dropped.
|
|
361
40
|
|
362
41
|
Do you want to contribute to Faraday?
|
363
42
|
Open the issues page and check for the `help wanted` label!
|
364
|
-
But before you start coding, please read our [Contributing Guide]
|
43
|
+
But before you start coding, please read our [Contributing Guide][contributing]
|
365
44
|
|
366
45
|
## Copyright
|
46
|
+
© 2009 - 2019, the [Faraday Team][faraday_team]. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
|
367
47
|
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
[
|
372
|
-
[
|
373
|
-
[travis]: https://travis-ci.org/lostisland/faraday
|
374
|
-
[excon]: https://github.com/excon/excon#readme
|
375
|
-
[patron]: http://toland.github.io/patron/
|
376
|
-
[eventmachine]: https://github.com/igrigorik/em-http-request#readme
|
377
|
-
[httpclient]: https://github.com/nahi/httpclient
|
378
|
-
[typhoeus]: https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/adapters/faraday.rb
|
48
|
+
[website]: https://lostisland.github.io/faraday
|
49
|
+
[faraday_team]: https://lostisland.github.io/faraday/team
|
50
|
+
[contributing]: https://github.com/lostisland/faraday/blob/master/.github/CONTRIBUTING.md
|
51
|
+
[apidoc]: http://www.rubydoc.info/gems/faraday
|
52
|
+
[circle_ci]: https://circleci.com/gh/lostisland/faraday
|
379
53
|
[jruby]: http://jruby.org/
|
380
54
|
[rubinius]: http://rubini.us/
|
381
55
|
[license]: LICENSE.md
|
data/lib/faraday.rb
CHANGED
@@ -1,129 +1,153 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require 'cgi'
|
3
4
|
require 'set'
|
4
5
|
require 'forwardable'
|
6
|
+
require 'faraday/middleware_registry'
|
7
|
+
require 'faraday/dependency_loader'
|
5
8
|
|
6
|
-
#
|
7
|
-
# create Faraday::Connection objects, or access it directly.
|
9
|
+
# This is the main namespace for Faraday.
|
8
10
|
#
|
9
|
-
#
|
11
|
+
# It provides methods to create {Connection} objects, and HTTP-related
|
12
|
+
# methods to use directly.
|
10
13
|
#
|
14
|
+
# @example Helpful class methods for easy usage
|
11
15
|
# Faraday.get "http://faraday.com"
|
12
16
|
#
|
17
|
+
# @example Helpful class method `.new` to create {Connection} objects.
|
13
18
|
# conn = Faraday.new "http://faraday.com"
|
14
19
|
# conn.get '/'
|
15
20
|
#
|
16
21
|
module Faraday
|
17
|
-
VERSION =
|
22
|
+
VERSION = '0.16.0'
|
23
|
+
METHODS_WITH_QUERY = %w[get head delete connect trace].freeze
|
24
|
+
METHODS_WITH_BODY = %w[post put patch].freeze
|
18
25
|
|
19
26
|
class << self
|
20
|
-
#
|
21
|
-
#
|
27
|
+
# The root path that Faraday is being loaded from.
|
28
|
+
#
|
29
|
+
# This is the root from where the libraries are auto-loaded.
|
30
|
+
#
|
31
|
+
# @return [String]
|
22
32
|
attr_accessor :root_path
|
23
33
|
|
24
|
-
#
|
34
|
+
# Gets or sets the path that the Faraday libs are loaded from.
|
35
|
+
# @return [String]
|
25
36
|
attr_accessor :lib_path
|
26
37
|
|
27
|
-
#
|
28
|
-
#
|
38
|
+
# @overload default_adapter
|
39
|
+
# Gets the Symbol key identifying a default Adapter to use
|
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.
|
29
45
|
attr_reader :default_adapter
|
30
46
|
|
31
|
-
#
|
32
|
-
# access the Faraday constant directly.
|
33
|
-
#
|
34
|
-
# Faraday.get "https://faraday.com"
|
47
|
+
# Documented below, see default_connection
|
35
48
|
attr_writer :default_connection
|
36
49
|
|
37
|
-
#
|
50
|
+
# Tells Faraday to ignore the environment proxy (http_proxy).
|
51
|
+
# Defaults to `false`.
|
52
|
+
# @return [Boolean]
|
38
53
|
attr_accessor :ignore_env_proxy
|
39
54
|
|
40
|
-
#
|
41
|
-
#
|
42
|
-
# url
|
43
|
-
# requests. Can also be the options Hash.
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
55
|
+
# Initializes a new {Connection}.
|
56
|
+
#
|
57
|
+
# @param url [String,Hash] The optional String base URL to use as a prefix
|
58
|
+
# for all requests. Can also be the options Hash. Any of these
|
59
|
+
# values will be set on every request made, unless overridden
|
60
|
+
# for a specific request.
|
61
|
+
# @param options [Hash]
|
62
|
+
# @option options [String] :url Base URL
|
63
|
+
# @option options [Hash] :params Hash of unencoded URI query params.
|
64
|
+
# @option options [Hash] :headers Hash of unencoded HTTP headers.
|
65
|
+
# @option options [Hash] :request Hash of request options.
|
66
|
+
# @option options [Hash] :ssl Hash of SSL options.
|
67
|
+
# @option options [Hash] :proxy Hash of Proxy options.
|
68
|
+
# @return [Faraday::Connection]
|
69
|
+
#
|
70
|
+
# @example With an URL argument
|
56
71
|
# Faraday.new 'http://faraday.com'
|
57
|
-
#
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
# #
|
62
|
-
#
|
63
|
-
#
|
64
|
-
#
|
65
|
-
#
|
66
|
-
#
|
67
|
-
def new(url = nil, options =
|
68
|
-
|
69
|
-
options = options ? default_connection_options.merge(options) : default_connection_options
|
72
|
+
# # => Faraday::Connection to http://faraday.com
|
73
|
+
#
|
74
|
+
# @example With an URL argument and an options hash
|
75
|
+
# Faraday.new 'http://faraday.com', params: { page: 1 }
|
76
|
+
# # => Faraday::Connection to http://faraday.com?page=1
|
77
|
+
#
|
78
|
+
# @example With everything in an options hash
|
79
|
+
# Faraday.new url: 'http://faraday.com',
|
80
|
+
# params: { page: 1 }
|
81
|
+
# # => Faraday::Connection to http://faraday.com?page=1
|
82
|
+
def new(url = nil, options = {}, &block)
|
83
|
+
options = default_connection_options.merge(options)
|
70
84
|
Faraday::Connection.new(url, options, &block)
|
71
85
|
end
|
72
86
|
|
87
|
+
# @private
|
73
88
|
# Internal: Requires internal Faraday libraries.
|
74
89
|
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
# Returns nothing.
|
90
|
+
# @param libs [Array] one or more relative String names to Faraday classes.
|
91
|
+
# @return [void]
|
78
92
|
def require_libs(*libs)
|
79
93
|
libs.each do |lib|
|
80
94
|
require "#{lib_path}/#{lib}"
|
81
95
|
end
|
82
96
|
end
|
83
97
|
|
84
|
-
|
85
|
-
|
86
|
-
#
|
87
|
-
# Returns the new default_adapter.
|
98
|
+
alias require_lib require_libs
|
99
|
+
|
100
|
+
# Documented elsewhere, see default_adapter reader
|
88
101
|
def default_adapter=(adapter)
|
89
102
|
@default_connection = nil
|
90
103
|
@default_adapter = adapter
|
91
104
|
end
|
92
105
|
|
93
|
-
|
94
|
-
|
95
|
-
def respond_to?(symbol, include_private = false)
|
106
|
+
def respond_to_missing?(symbol, include_private = false)
|
96
107
|
default_connection.respond_to?(symbol, include_private) || super
|
97
108
|
end
|
98
109
|
|
99
|
-
|
110
|
+
private
|
111
|
+
|
100
112
|
# Internal: Proxies method calls on the Faraday constant to
|
101
|
-
#
|
113
|
+
# .default_connection.
|
102
114
|
def method_missing(name, *args, &block)
|
103
|
-
default_connection.
|
115
|
+
if default_connection.respond_to?(name)
|
116
|
+
default_connection.send(name, *args, &block)
|
117
|
+
else
|
118
|
+
super
|
119
|
+
end
|
104
120
|
end
|
105
121
|
end
|
106
122
|
|
107
123
|
self.ignore_env_proxy = false
|
108
|
-
self.root_path = File.expand_path
|
109
|
-
self.lib_path = File.expand_path
|
124
|
+
self.root_path = File.expand_path __dir__
|
125
|
+
self.lib_path = File.expand_path 'faraday', __dir__
|
110
126
|
self.default_adapter = :net_http
|
111
127
|
|
112
|
-
#
|
113
|
-
#
|
114
|
-
#
|
128
|
+
# @overload default_connection
|
129
|
+
# Gets the default connection used for simple scripts.
|
130
|
+
# @return [Faraday::Connection] a connection configured with
|
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>.
|
115
137
|
def self.default_connection
|
116
138
|
@default_connection ||= Connection.new(default_connection_options)
|
117
139
|
end
|
118
140
|
|
119
|
-
# Gets the default connection options used when calling Faraday#new.
|
141
|
+
# Gets the default connection options used when calling {Faraday#new}.
|
120
142
|
#
|
121
|
-
#
|
143
|
+
# @return [Faraday::ConnectionOptions]
|
122
144
|
def self.default_connection_options
|
123
145
|
@default_connection_options ||= ConnectionOptions.new
|
124
146
|
end
|
125
147
|
|
126
|
-
#
|
148
|
+
# Sets the default options used when calling {Faraday#new}.
|
149
|
+
#
|
150
|
+
# @param options [Hash, Faraday::ConnectionOptions]
|
127
151
|
def self.default_connection_options=(options)
|
128
152
|
@default_connection = nil
|
129
153
|
@default_connection_options = ConnectionOptions.from(options)
|
@@ -134,115 +158,9 @@ module Faraday
|
|
134
158
|
Timer = Timeout
|
135
159
|
end
|
136
160
|
|
137
|
-
|
138
|
-
|
139
|
-
|
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
|
161
|
+
require_libs 'utils', 'options', 'connection', 'rack_builder', 'parameters',
|
162
|
+
'middleware', 'adapter', 'request', 'response', 'error',
|
163
|
+
'file_part', 'param_part'
|
172
164
|
|
173
|
-
|
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
|
165
|
+
require_lib 'autoload' unless ENV['FARADAY_NO_AUTOLOAD']
|
248
166
|
end
|