faraday 2.7.4 → 2.13.1
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/CHANGELOG.md +1 -1
- data/README.md +23 -11
- data/Rakefile +6 -1
- data/lib/faraday/adapter/test.rb +3 -3
- data/lib/faraday/adapter.rb +1 -1
- data/lib/faraday/connection.rb +27 -23
- data/lib/faraday/encoders/nested_params_encoder.rb +1 -1
- data/lib/faraday/error.rb +21 -3
- data/lib/faraday/logging/formatter.rb +13 -17
- data/lib/faraday/middleware.rb +40 -1
- data/lib/faraday/options/connection_options.rb +7 -6
- data/lib/faraday/options/env.rb +68 -69
- data/lib/faraday/options/proxy_options.rb +11 -5
- data/lib/faraday/options/request_options.rb +7 -6
- data/lib/faraday/options/ssl_options.rb +57 -50
- data/lib/faraday/options.rb +4 -3
- data/lib/faraday/rack_builder.rb +20 -20
- data/lib/faraday/request/instrumentation.rb +3 -1
- data/lib/faraday/request/json.rb +18 -3
- data/lib/faraday/request.rb +10 -7
- data/lib/faraday/response/json.rb +21 -1
- data/lib/faraday/response/logger.rb +5 -3
- data/lib/faraday/response/raise_error.rb +36 -17
- data/lib/faraday/utils/headers.rb +8 -2
- data/lib/faraday/utils.rb +3 -4
- data/lib/faraday/version.rb +1 -1
- data/spec/faraday/connection_spec.rb +2 -2
- data/spec/faraday/error_spec.rb +39 -6
- data/spec/faraday/middleware_spec.rb +143 -0
- data/spec/faraday/options/options_spec.rb +1 -1
- data/spec/faraday/options/proxy_options_spec.rb +35 -0
- data/spec/faraday/params_encoders/nested_spec.rb +2 -1
- data/spec/faraday/request/json_spec.rb +88 -0
- data/spec/faraday/response/json_spec.rb +89 -0
- data/spec/faraday/response/logger_spec.rb +49 -4
- data/spec/faraday/response/raise_error_spec.rb +104 -1
- data/spec/faraday/utils/headers_spec.rb +9 -0
- data/spec/faraday/utils_spec.rb +3 -1
- data/spec/faraday_spec.rb +10 -4
- data/spec/spec_helper.rb +6 -5
- data/spec/support/faraday_middleware_subclasses.rb +18 -0
- metadata +26 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ab8d55c7b360596e25721ecc32dcf99c65cea19d04598bb468fc2a422e26146
|
4
|
+
data.tar.gz: 624ddc291aec0a438fded658897b2127d62b8fe10e00590e3ad0a609307711d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2394c6ccb0b3e12d16e2dcd739d8ecf053199b40ce5d8d4a1902cc141c12746190e21b928e4979405ffcb2660db80a4777d46ee839df10f75cb66b34039285e
|
7
|
+
data.tar.gz: d91f5258bb49c40ff720aaba295ab4c86ea975cb7e0d4ad96647921e34924013501c1b4dddcf1c5497488fda0480ef39296f76b16188201f0162da004a490ba5
|
data/CHANGELOG.md
CHANGED
@@ -517,7 +517,7 @@ Breaking changes:
|
|
517
517
|
- Drop support for Ruby 1.8
|
518
518
|
|
519
519
|
Features:
|
520
|
-
- Include wrapped exception/
|
520
|
+
- Include wrapped exception/response in ClientErrors
|
521
521
|
- Add `response.reason_phrase`
|
522
522
|
- Provide option to selectively skip logging request/response headers
|
523
523
|
- Add regex support for pattern matching in `test` adapter
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# [][website]
|
2
2
|
|
3
3
|
[](https://rubygems.org/gems/faraday)
|
4
4
|
[](https://github.com/lostisland/faraday/actions?query=workflow%3ACI)
|
@@ -6,20 +6,36 @@
|
|
6
6
|
|
7
7
|
Faraday is an HTTP client library abstraction layer that provides a common interface over many
|
8
8
|
adapters (such as Net::HTTP) and embraces the concept of Rack middleware when processing the request/response cycle.
|
9
|
-
|
10
|
-
|
9
|
+
Take a look at [Awesome Faraday][awesome] for a list of available adapters and middleware.
|
10
|
+
|
11
|
+
## Why use Faraday?
|
12
|
+
|
13
|
+
Faraday gives you the power of Rack middleware for manipulating HTTP requests and responses,
|
14
|
+
making it easier to build sophisticated API clients or web service libraries that abstract away
|
15
|
+
the details of how HTTP requests are made.
|
16
|
+
|
17
|
+
Faraday comes with a lot of features out of the box, such as:
|
18
|
+
* Support for multiple adapters (Net::HTTP, Typhoeus, Patron, Excon, HTTPClient, and more)
|
19
|
+
* Persistent connections (keep-alive)
|
20
|
+
* Parallel requests
|
21
|
+
* Automatic response parsing (JSON, XML, YAML)
|
22
|
+
* Customization of the request/response cycle with middleware
|
23
|
+
* Support for streaming responses
|
24
|
+
* Support for uploading files
|
25
|
+
* And much more!
|
11
26
|
|
12
27
|
## Getting Started
|
13
28
|
|
14
29
|
The best starting point is the [Faraday Website][website], with its introduction and explanation.
|
15
|
-
|
30
|
+
|
31
|
+
Need more details? See the [Faraday API Documentation][apidoc] to see how it works internally, or take a look at [Advanced techniques for calling HTTP APIs in Ruby](https://mattbrictson.com/blog/advanced-http-techniques-in-ruby) blog post from [@mattbrictson](https://github.com/mattbrictson) 🚀
|
16
32
|
|
17
33
|
## Supported Ruby versions
|
18
34
|
|
19
35
|
This library aims to support and is [tested against][actions] the currently officially supported Ruby
|
20
36
|
implementations. This means that, even without a major release, we could add or drop support for Ruby versions,
|
21
37
|
following their [EOL](https://endoflife.date/ruby).
|
22
|
-
Currently that means we support Ruby
|
38
|
+
Currently that means we support Ruby 3.0+
|
23
39
|
|
24
40
|
If something doesn't work on one of these Ruby versions, it's a bug.
|
25
41
|
|
@@ -42,14 +58,10 @@ But before you start coding, please read our [Contributing Guide][contributing]
|
|
42
58
|
|
43
59
|
## Copyright
|
44
60
|
|
45
|
-
© 2009 - 2023, the
|
61
|
+
© 2009 - 2023, the Faraday Team. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
|
46
62
|
|
47
63
|
[awesome]: https://github.com/lostisland/awesome-faraday/#adapters
|
48
64
|
[website]: https://lostisland.github.io/faraday
|
49
|
-
[
|
50
|
-
[contributing]: https://github.com/lostisland/faraday/blob/master/.github/CONTRIBUTING.md
|
65
|
+
[contributing]: https://github.com/lostisland/faraday/blob/main/.github/CONTRIBUTING.md
|
51
66
|
[apidoc]: https://www.rubydoc.info/github/lostisland/faraday
|
52
67
|
[actions]: https://github.com/lostisland/faraday/actions
|
53
|
-
[jruby]: http://jruby.org/
|
54
|
-
[rubinius]: http://rubini.us/
|
55
|
-
[license]: LICENSE.md
|
data/Rakefile
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'rspec/core/rake_task'
|
4
|
+
require 'bundler'
|
4
5
|
|
5
|
-
|
6
|
+
Bundler::GemHelper.install_tasks
|
7
|
+
|
8
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
9
|
+
task.ruby_opts = %w[-W]
|
10
|
+
end
|
6
11
|
|
7
12
|
task default: :spec
|
data/lib/faraday/adapter/test.rb
CHANGED
@@ -146,7 +146,7 @@ module Faraday
|
|
146
146
|
# which means that all of a path, parameters, and headers must be the same as an actual request.
|
147
147
|
def strict_mode=(value)
|
148
148
|
@strict_mode = value
|
149
|
-
@stack.
|
149
|
+
@stack.each_value do |stubs|
|
150
150
|
stubs.each do |stub|
|
151
151
|
stub.strict_mode = value
|
152
152
|
end
|
@@ -184,7 +184,7 @@ module Faraday
|
|
184
184
|
end
|
185
185
|
|
186
186
|
# Stub request
|
187
|
-
|
187
|
+
Stub = Struct.new(:host, :path, :query, :headers, :body, :strict_mode, :block) do
|
188
188
|
# @param env [Faraday::Env]
|
189
189
|
def matches?(env)
|
190
190
|
request_host = env[:url].host
|
@@ -275,7 +275,7 @@ module Faraday
|
|
275
275
|
|
276
276
|
unless stub
|
277
277
|
raise Stubs::NotFound, "no stubbed request for #{env[:method]} " \
|
278
|
-
"#{env[:url]} #{env[:body]}"
|
278
|
+
"#{env[:url]} #{env[:body]} #{env[:headers]}"
|
279
279
|
end
|
280
280
|
|
281
281
|
block_arity = stub.block.arity
|
data/lib/faraday/adapter.rb
CHANGED
data/lib/faraday/connection.rb
CHANGED
@@ -15,7 +15,7 @@ module Faraday
|
|
15
15
|
class Connection
|
16
16
|
# A Set of allowed HTTP verbs.
|
17
17
|
METHODS = Set.new %i[get post put delete head patch options trace]
|
18
|
-
USER_AGENT = "Faraday v#{VERSION}"
|
18
|
+
USER_AGENT = "Faraday v#{VERSION}".freeze
|
19
19
|
|
20
20
|
# @return [Hash] URI query unencoded key/value pairs.
|
21
21
|
attr_reader :params
|
@@ -314,15 +314,23 @@ module Faraday
|
|
314
314
|
#
|
315
315
|
# @yield a block to execute multiple requests.
|
316
316
|
# @return [void]
|
317
|
-
def in_parallel(manager = nil)
|
317
|
+
def in_parallel(manager = nil, &block)
|
318
318
|
@parallel_manager = manager || default_parallel_manager do
|
319
319
|
warn 'Warning: `in_parallel` called but no parallel-capable adapter ' \
|
320
320
|
'on Faraday stack'
|
321
321
|
warn caller[2, 10].join("\n")
|
322
322
|
nil
|
323
323
|
end
|
324
|
-
yield
|
325
|
-
|
324
|
+
return yield unless @parallel_manager
|
325
|
+
|
326
|
+
if @parallel_manager.respond_to?(:execute)
|
327
|
+
# Execute is the new method that is responsible for executing the block.
|
328
|
+
@parallel_manager.execute(&block)
|
329
|
+
else
|
330
|
+
# TODO: Old behaviour, deprecate and remove in 3.0
|
331
|
+
yield
|
332
|
+
@parallel_manager.run
|
333
|
+
end
|
326
334
|
ensure
|
327
335
|
@parallel_manager = nil
|
328
336
|
end
|
@@ -423,8 +431,8 @@ module Faraday
|
|
423
431
|
#
|
424
432
|
# @param method [Symbol] HTTP method.
|
425
433
|
# @param url [String, URI, nil] String or URI to access.
|
426
|
-
# @param body [String, nil] The request body that will eventually be converted to
|
427
|
-
# a string.
|
434
|
+
# @param body [String, Hash, Array, nil] The request body that will eventually be converted to
|
435
|
+
# a string; middlewares can be used to support more complex types.
|
428
436
|
# @param headers [Hash, nil] unencoded HTTP header key/value pairs.
|
429
437
|
#
|
430
438
|
# @return [Faraday::Response]
|
@@ -473,7 +481,8 @@ module Faraday
|
|
473
481
|
if url && !base.path.end_with?('/')
|
474
482
|
base.path = "#{base.path}/" # ensure trailing slash
|
475
483
|
end
|
476
|
-
url
|
484
|
+
# Ensure relative url will be parsed correctly (such as `service:search` )
|
485
|
+
url = "./#{url}" if url.respond_to?(:start_with?) && !url.start_with?('http://', 'https://', '/', './', '../')
|
477
486
|
uri = url ? base + url : base
|
478
487
|
if params
|
479
488
|
uri.query = params.to_query(params_encoder || options.params_encoder)
|
@@ -514,22 +523,17 @@ module Faraday
|
|
514
523
|
return if Faraday.ignore_env_proxy
|
515
524
|
|
516
525
|
uri = nil
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
when nil
|
529
|
-
uri = find_default_proxy
|
530
|
-
end
|
531
|
-
else
|
532
|
-
warn 'no_proxy is unsupported' if ENV['no_proxy'] || ENV['NO_PROXY']
|
526
|
+
case url
|
527
|
+
when String
|
528
|
+
uri = Utils.URI(url)
|
529
|
+
uri = if uri.host.nil?
|
530
|
+
find_default_proxy
|
531
|
+
else
|
532
|
+
URI.parse("#{uri.scheme}://#{uri.host}").find_proxy
|
533
|
+
end
|
534
|
+
when URI
|
535
|
+
uri = url.find_proxy
|
536
|
+
when nil
|
533
537
|
uri = find_default_proxy
|
534
538
|
end
|
535
539
|
ProxyOptions.from(uri) if uri
|
data/lib/faraday/error.rb
CHANGED
@@ -29,15 +29,21 @@ module Faraday
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def response_status
|
32
|
-
|
32
|
+
return unless @response
|
33
|
+
|
34
|
+
@response.is_a?(Faraday::Response) ? @response.status : @response[:status]
|
33
35
|
end
|
34
36
|
|
35
37
|
def response_headers
|
36
|
-
|
38
|
+
return unless @response
|
39
|
+
|
40
|
+
@response.is_a?(Faraday::Response) ? @response.headers : @response[:headers]
|
37
41
|
end
|
38
42
|
|
39
43
|
def response_body
|
40
|
-
|
44
|
+
return unless @response
|
45
|
+
|
46
|
+
@response.is_a?(Faraday::Response) ? @response.body : @response[:body]
|
41
47
|
end
|
42
48
|
|
43
49
|
protected
|
@@ -106,6 +112,10 @@ module Faraday
|
|
106
112
|
class ProxyAuthError < ClientError
|
107
113
|
end
|
108
114
|
|
115
|
+
# Raised by Faraday::Response::RaiseError in case of a 408 response.
|
116
|
+
class RequestTimeoutError < ClientError
|
117
|
+
end
|
118
|
+
|
109
119
|
# Raised by Faraday::Response::RaiseError in case of a 409 response.
|
110
120
|
class ConflictError < ClientError
|
111
121
|
end
|
@@ -114,6 +124,10 @@ module Faraday
|
|
114
124
|
class UnprocessableEntityError < ClientError
|
115
125
|
end
|
116
126
|
|
127
|
+
# Raised by Faraday::Response::RaiseError in case of a 429 response.
|
128
|
+
class TooManyRequestsError < ClientError
|
129
|
+
end
|
130
|
+
|
117
131
|
# Faraday server error class. Represents 5xx status responses.
|
118
132
|
class ServerError < Error
|
119
133
|
end
|
@@ -144,4 +158,8 @@ module Faraday
|
|
144
158
|
# Raised by middlewares that parse the response, like the JSON response middleware.
|
145
159
|
class ParsingError < Error
|
146
160
|
end
|
161
|
+
|
162
|
+
# Raised by Faraday::Middleware and subclasses when invalid default_options are used
|
163
|
+
class InitializationError < Error
|
164
|
+
end
|
147
165
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'pp' #
|
3
|
+
require 'pp' # This require is necessary for Hash#pretty_inspect to work, do not remove it, people rely on it.
|
4
4
|
|
5
5
|
module Faraday
|
6
6
|
module Logging
|
@@ -13,25 +13,26 @@ module Faraday
|
|
13
13
|
|
14
14
|
def initialize(logger:, options:)
|
15
15
|
@logger = logger
|
16
|
-
@filter = []
|
17
16
|
@options = DEFAULT_OPTIONS.merge(options)
|
17
|
+
unless %i[debug info warn error fatal].include?(@options[:log_level])
|
18
|
+
@options[:log_level] = :info
|
19
|
+
end
|
20
|
+
@filter = []
|
18
21
|
end
|
19
22
|
|
20
23
|
def_delegators :@logger, :debug, :info, :warn, :error, :fatal
|
21
24
|
|
22
25
|
def request(env)
|
23
|
-
|
24
|
-
"#{env.method.upcase} #{apply_filters(env.url.to_s)}"
|
26
|
+
public_send(log_level) do
|
27
|
+
"request: #{env.method.upcase} #{apply_filters(env.url.to_s)}"
|
25
28
|
end
|
26
|
-
public_send(log_level, 'request', &request_log)
|
27
29
|
|
28
30
|
log_headers('request', env.request_headers) if log_headers?(:request)
|
29
31
|
log_body('request', env[:body]) if env[:body] && log_body?(:request)
|
30
32
|
end
|
31
33
|
|
32
34
|
def response(env)
|
33
|
-
|
34
|
-
public_send(log_level, 'response', &status)
|
35
|
+
public_send(log_level) { "response: Status #{env.status}" }
|
35
36
|
|
36
37
|
log_headers('response', env.response_headers) if log_headers?(:response)
|
37
38
|
log_body('response', env[:body]) if env[:body] && log_body?(:response)
|
@@ -40,8 +41,7 @@ module Faraday
|
|
40
41
|
def exception(exc)
|
41
42
|
return unless log_errors?
|
42
43
|
|
43
|
-
|
44
|
-
public_send(log_level, 'error', &error_log)
|
44
|
+
public_send(log_level) { "error: #{exc.full_message}" }
|
45
45
|
|
46
46
|
log_headers('error', exc.response_headers) if exc.respond_to?(:response_headers) && log_headers?(:error)
|
47
47
|
return unless exc.respond_to?(:response_body) && exc.response_body && log_body?(:error)
|
@@ -56,6 +56,8 @@ module Faraday
|
|
56
56
|
private
|
57
57
|
|
58
58
|
def dump_headers(headers)
|
59
|
+
return if headers.nil?
|
60
|
+
|
59
61
|
headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n")
|
60
62
|
end
|
61
63
|
|
@@ -101,21 +103,15 @@ module Faraday
|
|
101
103
|
end
|
102
104
|
|
103
105
|
def log_level
|
104
|
-
unless %i[debug info warn error fatal].include?(@options[:log_level])
|
105
|
-
return :info
|
106
|
-
end
|
107
|
-
|
108
106
|
@options[:log_level]
|
109
107
|
end
|
110
108
|
|
111
109
|
def log_headers(type, headers)
|
112
|
-
|
113
|
-
public_send(log_level, type, &headers_log)
|
110
|
+
public_send(log_level) { "#{type}: #{apply_filters(dump_headers(headers))}" }
|
114
111
|
end
|
115
112
|
|
116
113
|
def log_body(type, body)
|
117
|
-
|
118
|
-
public_send(log_level, type, &body_log)
|
114
|
+
public_send(log_level) { "#{type}: #{apply_filters(dump_body(body))}" }
|
119
115
|
end
|
120
116
|
end
|
121
117
|
end
|
data/lib/faraday/middleware.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'monitor'
|
4
|
+
|
3
5
|
module Faraday
|
4
6
|
# Middleware is the basic base class of any Faraday middleware.
|
5
7
|
class Middleware
|
@@ -7,9 +9,46 @@ module Faraday
|
|
7
9
|
|
8
10
|
attr_reader :app, :options
|
9
11
|
|
12
|
+
DEFAULT_OPTIONS = {}.freeze
|
13
|
+
LOCK = Mutex.new
|
14
|
+
|
10
15
|
def initialize(app = nil, options = {})
|
11
16
|
@app = app
|
12
|
-
@options = options
|
17
|
+
@options = self.class.default_options.merge(options)
|
18
|
+
end
|
19
|
+
|
20
|
+
class << self
|
21
|
+
# Faraday::Middleware::default_options= allows user to set default options at the Faraday::Middleware
|
22
|
+
# class level.
|
23
|
+
#
|
24
|
+
# @example Set the Faraday::Response::RaiseError option, `include_request` to `false`
|
25
|
+
# my_app/config/initializers/my_faraday_middleware.rb
|
26
|
+
#
|
27
|
+
# Faraday::Response::RaiseError.default_options = { include_request: false }
|
28
|
+
#
|
29
|
+
def default_options=(options = {})
|
30
|
+
validate_default_options(options)
|
31
|
+
LOCK.synchronize do
|
32
|
+
@default_options = default_options.merge(options)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# default_options attr_reader that initializes class instance variable
|
37
|
+
# with the values of any Faraday::Middleware defaults, and merges with
|
38
|
+
# subclass defaults
|
39
|
+
def default_options
|
40
|
+
@default_options ||= DEFAULT_OPTIONS.merge(self::DEFAULT_OPTIONS)
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def validate_default_options(options)
|
46
|
+
invalid_keys = options.keys.reject { |opt| self::DEFAULT_OPTIONS.key?(opt) }
|
47
|
+
return unless invalid_keys.any?
|
48
|
+
|
49
|
+
raise(Faraday::InitializationError,
|
50
|
+
"Invalid options provided. Keys not found in #{self}::DEFAULT_OPTIONS: #{invalid_keys.join(', ')}")
|
51
|
+
end
|
13
52
|
end
|
14
53
|
|
15
54
|
def call(env)
|
@@ -1,12 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Faraday
|
4
|
-
#
|
5
|
-
#
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
# @!parse
|
5
|
+
# # ConnectionOptions contains the configurable properties for a Faraday
|
6
|
+
# # connection object.
|
7
|
+
# class ConnectionOptions < Options; end
|
8
|
+
ConnectionOptions = Options.new(:request, :proxy, :ssl, :builder, :url,
|
9
|
+
:parallel_manager, :params, :headers,
|
10
|
+
:builder_class) do
|
10
11
|
options request: RequestOptions, ssl: SSLOptions
|
11
12
|
|
12
13
|
memoized(:request) { self.class.options_for(:request).new }
|
data/lib/faraday/options/env.rb
CHANGED
@@ -1,71 +1,70 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Faraday
|
4
|
-
# @!
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
# - `:
|
28
|
-
#
|
29
|
-
# - `:
|
30
|
-
# - `:
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
#
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
ContentLength
|
62
|
-
StatusesWithoutBody
|
63
|
-
SuccessfulStatuses
|
64
|
-
# rubocop:enable Naming/ConstantName
|
4
|
+
# @!parse
|
5
|
+
# # @!attribute method
|
6
|
+
# # @return [Symbol] HTTP method (`:get`, `:post`)
|
7
|
+
# #
|
8
|
+
# # @!attribute body
|
9
|
+
# # @return [String] The request body that will eventually be converted to a
|
10
|
+
# # string.
|
11
|
+
# #
|
12
|
+
# # @!attribute url
|
13
|
+
# # @return [URI] URI instance for the current request.
|
14
|
+
# #
|
15
|
+
# # @!attribute request
|
16
|
+
# # @return [Hash] options for configuring the request.
|
17
|
+
# # Options for configuring the request.
|
18
|
+
# #
|
19
|
+
# # - `:timeout` - time limit for the entire request (Integer in
|
20
|
+
# # seconds)
|
21
|
+
# # - `:open_timeout` - time limit for just the connection phase (e.g.
|
22
|
+
# # handshake) (Integer in seconds)
|
23
|
+
# # - `:read_timeout` - time limit for the first response byte received from
|
24
|
+
# # the server (Integer in seconds)
|
25
|
+
# # - `:write_timeout` - time limit for the client to send the request to the
|
26
|
+
# # server (Integer in seconds)
|
27
|
+
# # - `:on_data` - Proc for streaming
|
28
|
+
# # - `:proxy` - Hash of proxy options
|
29
|
+
# # - `:uri` - Proxy server URI
|
30
|
+
# # - `:user` - Proxy server username
|
31
|
+
# # - `:password` - Proxy server password
|
32
|
+
# #
|
33
|
+
# # @!attribute request_headers
|
34
|
+
# # @return [Hash] HTTP Headers to be sent to the server.
|
35
|
+
# #
|
36
|
+
# # @!attribute ssl
|
37
|
+
# # @return [Hash] options for configuring SSL requests
|
38
|
+
# #
|
39
|
+
# # @!attribute parallel_manager
|
40
|
+
# # @return [Object] sent if the connection is in parallel mode
|
41
|
+
# #
|
42
|
+
# # @!attribute params
|
43
|
+
# # @return [Hash]
|
44
|
+
# #
|
45
|
+
# # @!attribute response
|
46
|
+
# # @return [Response]
|
47
|
+
# #
|
48
|
+
# # @!attribute response_headers
|
49
|
+
# # @return [Hash] HTTP headers from the server
|
50
|
+
# #
|
51
|
+
# # @!attribute status
|
52
|
+
# # @return [Integer] HTTP response status code
|
53
|
+
# #
|
54
|
+
# # @!attribute reason_phrase
|
55
|
+
# # @return [String]
|
56
|
+
# class Env < Options; end
|
57
|
+
Env = Options.new(:method, :request_body, :url, :request,
|
58
|
+
:request_headers, :ssl, :parallel_manager, :params,
|
59
|
+
:response, :response_headers, :status,
|
60
|
+
:reason_phrase, :response_body) do
|
61
|
+
const_set(:ContentLength, 'Content-Length')
|
62
|
+
const_set(:StatusesWithoutBody, Set.new([204, 304]))
|
63
|
+
const_set(:SuccessfulStatuses, 200..299)
|
65
64
|
|
66
65
|
# A Set of HTTP verbs that typically send a body. If no body is set for
|
67
66
|
# these requests, the Content-Length header is set to 0.
|
68
|
-
MethodsWithBodies
|
67
|
+
const_set(:MethodsWithBodies, Set.new(Faraday::METHODS_WITH_BODY.map(&:to_sym)))
|
69
68
|
|
70
69
|
options request: RequestOptions,
|
71
70
|
request_headers: Utils::Headers, response_headers: Utils::Headers
|
@@ -126,25 +125,25 @@ module Faraday
|
|
126
125
|
|
127
126
|
# @return [Boolean] true if status is in the set of {SuccessfulStatuses}.
|
128
127
|
def success?
|
129
|
-
SuccessfulStatuses.include?(status)
|
128
|
+
Env::SuccessfulStatuses.include?(status)
|
130
129
|
end
|
131
130
|
|
132
131
|
# @return [Boolean] true if there's no body yet, and the method is in the
|
133
|
-
# set of {MethodsWithBodies}.
|
132
|
+
# set of {Env::MethodsWithBodies}.
|
134
133
|
def needs_body?
|
135
|
-
!body && MethodsWithBodies.include?(method)
|
134
|
+
!body && Env::MethodsWithBodies.include?(method)
|
136
135
|
end
|
137
136
|
|
138
137
|
# Sets content length to zero and the body to the empty string.
|
139
138
|
def clear_body
|
140
|
-
request_headers[ContentLength] = '0'
|
139
|
+
request_headers[Env::ContentLength] = '0'
|
141
140
|
self.body = +''
|
142
141
|
end
|
143
142
|
|
144
143
|
# @return [Boolean] true if the status isn't in the set of
|
145
|
-
# {StatusesWithoutBody}.
|
144
|
+
# {Env::StatusesWithoutBody}.
|
146
145
|
def parse_body?
|
147
|
-
!StatusesWithoutBody.include?(status)
|
146
|
+
!Env::StatusesWithoutBody.include?(status)
|
148
147
|
end
|
149
148
|
|
150
149
|
# @return [Boolean] true if there is a parallel_manager
|
@@ -170,7 +169,7 @@ module Faraday
|
|
170
169
|
def stream_response(&block)
|
171
170
|
size = 0
|
172
171
|
yielded = false
|
173
|
-
block_result = block.call do |chunk|
|
172
|
+
block_result = block.call do |chunk|
|
174
173
|
if chunk.bytesize.positive? || size.positive?
|
175
174
|
yielded = true
|
176
175
|
size += chunk.bytesize
|