faraday_middleware 0.13.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -21
- data/lib/faraday_middleware.rb +21 -17
- data/lib/faraday_middleware/backwards_compatibility.rb +3 -2
- data/lib/faraday_middleware/gzip.rb +21 -16
- data/lib/faraday_middleware/instrumentation.rb +8 -2
- data/lib/faraday_middleware/rack_compatible.rb +18 -11
- data/lib/faraday_middleware/redirect_limit_reached.rb +16 -0
- data/lib/faraday_middleware/request/encode_json.rb +12 -10
- data/lib/faraday_middleware/request/method_override.rb +7 -6
- data/lib/faraday_middleware/request/oauth.rb +13 -10
- data/lib/faraday_middleware/request/oauth2.rb +25 -22
- data/lib/faraday_middleware/response/caching.rb +30 -16
- data/lib/faraday_middleware/response/chunked.rb +10 -6
- data/lib/faraday_middleware/response/follow_redirects.rb +19 -25
- data/lib/faraday_middleware/response/mashify.rb +2 -0
- data/lib/faraday_middleware/response/parse_dates.rb +6 -3
- data/lib/faraday_middleware/response/parse_json.rb +8 -8
- data/lib/faraday_middleware/response/parse_marshal.rb +2 -0
- data/lib/faraday_middleware/response/parse_xml.rb +2 -0
- data/lib/faraday_middleware/response/parse_yaml.rb +9 -10
- data/lib/faraday_middleware/response/rashify.rb +2 -0
- data/lib/faraday_middleware/response_middleware.rb +25 -19
- data/lib/faraday_middleware/version.rb +4 -1
- metadata +7 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b061911e8925a657ce571d5d3a096e6a9b43e8abcca0bec121fc81edb3b65c7
|
4
|
+
data.tar.gz: 4722349158a61b9ee4789fc1046596ac80a44a7dd76eba178cb32330a5583a9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d331ca5a71fda7702da83967033355c6ad896ea6026af78e248bf21c027a7f4287ab117ae81cb9dad0c0dc2d50eaaba1ab1a02f6a54a15436970e9c4232306f
|
7
|
+
data.tar.gz: 49b8a45c1aa7e06764bc5433b83f469544c509c7c372bc986dadd497dd987054d5e02160d5019679b23acd068b32bd7c86c09498f2e29b9ccdca6376b316f634
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
Faraday Middleware
|
2
2
|
==================
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/faraday_middleware.svg)](https://rubygems.org/gems/faraday_middleware)
|
4
|
+
![GitHub Actions CI](https://github.com/lostisland/faraday_middleware/workflows/CI/badge.svg)
|
3
5
|
|
4
6
|
A collection of useful [Faraday][] middleware. [See the documentation][docs].
|
5
7
|
|
@@ -8,16 +10,20 @@ A collection of useful [Faraday][] middleware. [See the documentation][docs].
|
|
8
10
|
Dependencies
|
9
11
|
------------
|
10
12
|
|
13
|
+
Ruby >= 2.3.0
|
14
|
+
|
15
|
+
#### As of v0.16.0, `faraday` and `faraday_middleware` no longer officially support JRuby or Rubinius.
|
16
|
+
|
11
17
|
Some dependent libraries are needed only when using specific middleware:
|
12
18
|
|
13
19
|
| Middleware | Library | Notes |
|
14
20
|
| --------------------------- | -------------- | ----- |
|
15
|
-
| [FaradayMiddleware::Instrumentation](https://github.com/lostisland/faraday_middleware/blob/
|
16
|
-
| [FaradayMiddleware::OAuth](https://github.com/lostisland/faraday_middleware/blob/
|
17
|
-
| [FaradayMiddleware::ParseXml](https://github.com/lostisland/faraday_middleware/blob/
|
18
|
-
| [FaradayMiddleware::ParseYaml](https://github.com/lostisland/faraday_middleware/blob/
|
19
|
-
| [FaradayMiddleware::Mashify](https://github.com/lostisland/faraday_middleware/blob/
|
20
|
-
| [FaradayMiddleware::Rashify](https://github.com/lostisland/faraday_middleware/blob/
|
21
|
+
| [FaradayMiddleware::Instrumentation](https://github.com/lostisland/faraday_middleware/blob/main/lib/faraday_middleware/instrumentation.rb) | [`activesupport`](https://rubygems.org/gems/activesupport) | |
|
22
|
+
| [FaradayMiddleware::OAuth](https://github.com/lostisland/faraday_middleware/blob/main/lib/faraday_middleware/request/oauth.rb) | [`simple_oauth`](https://rubygems.org/gems/simple_oauth) | |
|
23
|
+
| [FaradayMiddleware::ParseXml](https://github.com/lostisland/faraday_middleware/blob/main/lib/faraday_middleware/response/parse_xml.rb) | [`multi_xml`](https://rubygems.org/gems/multi_xml) | |
|
24
|
+
| [FaradayMiddleware::ParseYaml](https://github.com/lostisland/faraday_middleware/blob/main/lib/faraday_middleware/response/parse_yaml.rb) | [`safe_yaml`](https://rubygems.org/gems/safe_yaml) | Not backwards compatible with versions of this middleware prior to `faraday_middleware` v0.12. See code comments for alternatives. |
|
25
|
+
| [FaradayMiddleware::Mashify](https://github.com/lostisland/faraday_middleware/blob/main/lib/faraday_middleware/response/mashify.rb) | [`hashie`](https://rubygems.org/gems/hashie) | |
|
26
|
+
| [FaradayMiddleware::Rashify](https://github.com/lostisland/faraday_middleware/blob/main/lib/faraday_middleware/response/rashify.rb) | [`rash_alt`](https://rubygems.org/gems/rash_alt) | Make sure to uninstall original `rash` gem to avoid conflict. |
|
21
27
|
|
22
28
|
Examples
|
23
29
|
--------
|
@@ -25,29 +31,16 @@ Examples
|
|
25
31
|
``` rb
|
26
32
|
require 'faraday_middleware'
|
27
33
|
|
28
|
-
## in Faraday 0.8 or above:
|
29
34
|
connection = Faraday.new 'http://example.com/api' do |conn|
|
30
35
|
conn.request :oauth2, 'TOKEN'
|
31
36
|
conn.request :json
|
32
37
|
|
33
|
-
conn.response :xml, :
|
34
|
-
conn.response :json, :
|
38
|
+
conn.response :xml, content_type: /\bxml$/
|
39
|
+
conn.response :json, content_type: /\bjson$/
|
35
40
|
|
36
41
|
conn.use :instrumentation
|
37
42
|
conn.adapter Faraday.default_adapter
|
38
43
|
end
|
39
|
-
|
40
|
-
## with Faraday 0.7:
|
41
|
-
connection = Faraday.new 'http://example.com/api' do |builder|
|
42
|
-
builder.use FaradayMiddleware::OAuth2, 'TOKEN'
|
43
|
-
builder.use FaradayMiddleware::EncodeJson
|
44
|
-
|
45
|
-
builder.use FaradayMiddleware::ParseXml, :content_type => /\bxml$/
|
46
|
-
builder.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
|
47
|
-
|
48
|
-
builder.use FaradayMiddleware::Instrumentation
|
49
|
-
builder.adapter Faraday.default_adapter
|
50
|
-
end
|
51
44
|
```
|
52
45
|
|
53
46
|
|
data/lib/faraday_middleware.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'faraday'
|
2
4
|
|
5
|
+
# Main FaradayMiddleware module.
|
3
6
|
module FaradayMiddleware
|
4
7
|
autoload :OAuth, 'faraday_middleware/request/oauth'
|
5
8
|
autoload :OAuth2, 'faraday_middleware/request/oauth2'
|
@@ -15,33 +18,34 @@ module FaradayMiddleware
|
|
15
18
|
autoload :Caching, 'faraday_middleware/response/caching'
|
16
19
|
autoload :Chunked, 'faraday_middleware/response/chunked'
|
17
20
|
autoload :RackCompatible, 'faraday_middleware/rack_compatible'
|
21
|
+
autoload :RedirectLimitReached, 'faraday_middleware/redirect_limit_reached'
|
18
22
|
autoload :FollowRedirects, 'faraday_middleware/response/follow_redirects'
|
19
23
|
autoload :Instrumentation, 'faraday_middleware/instrumentation'
|
20
24
|
autoload :Gzip, 'faraday_middleware/gzip'
|
21
25
|
|
22
26
|
if Faraday::Middleware.respond_to? :register_middleware
|
23
27
|
Faraday::Request.register_middleware \
|
24
|
-
:
|
25
|
-
:
|
26
|
-
:
|
27
|
-
:
|
28
|
+
oauth: -> { OAuth },
|
29
|
+
oauth2: -> { OAuth2 },
|
30
|
+
json: -> { EncodeJson },
|
31
|
+
method_override: -> { MethodOverride }
|
28
32
|
|
29
33
|
Faraday::Response.register_middleware \
|
30
|
-
:
|
31
|
-
:
|
32
|
-
:
|
33
|
-
:
|
34
|
-
:
|
35
|
-
:
|
36
|
-
:
|
37
|
-
:
|
38
|
-
:
|
39
|
-
:
|
40
|
-
:
|
34
|
+
mashify: -> { Mashify },
|
35
|
+
rashify: -> { Rashify },
|
36
|
+
json: -> { ParseJson },
|
37
|
+
json_fix: -> { ParseJson::MimeTypeFix },
|
38
|
+
xml: -> { ParseXml },
|
39
|
+
marshal: -> { ParseMarshal },
|
40
|
+
yaml: -> { ParseYaml },
|
41
|
+
dates: -> { ParseDates },
|
42
|
+
caching: -> { Caching },
|
43
|
+
follow_redirects: -> { FollowRedirects },
|
44
|
+
chunked: -> { Chunked }
|
41
45
|
|
42
46
|
Faraday::Middleware.register_middleware \
|
43
|
-
:
|
44
|
-
:
|
47
|
+
instrumentation: -> { Instrumentation },
|
48
|
+
gzip: -> { Gzip }
|
45
49
|
end
|
46
50
|
end
|
47
51
|
|
@@ -1,11 +1,13 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Faraday
|
4
|
+
# Autoload classes for Faraday::Request.
|
4
5
|
class Request
|
5
6
|
autoload :OAuth, 'faraday_middleware/request/oauth'
|
6
7
|
autoload :OAuth2, 'faraday_middleware/request/oauth2'
|
7
8
|
end
|
8
9
|
|
10
|
+
# Autoload classes for Faraday::Request.
|
9
11
|
class Response
|
10
12
|
autoload :Mashify, 'faraday_middleware/response/mashify'
|
11
13
|
autoload :Rashify, 'faraday_middleware/response/rashify'
|
@@ -15,4 +17,3 @@ module Faraday
|
|
15
17
|
autoload :ParseYaml, 'faraday_middleware/response/parse_yaml'
|
16
18
|
end
|
17
19
|
end
|
18
|
-
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'faraday'
|
2
4
|
|
3
5
|
module FaradayMiddleware
|
@@ -28,22 +30,25 @@ module FaradayMiddleware
|
|
28
30
|
encodings
|
29
31
|
end
|
30
32
|
|
31
|
-
ACCEPT_ENCODING = 'Accept-Encoding'
|
32
|
-
CONTENT_ENCODING = 'Content-Encoding'
|
33
|
-
CONTENT_LENGTH = 'Content-Length'
|
33
|
+
ACCEPT_ENCODING = 'Accept-Encoding'
|
34
|
+
CONTENT_ENCODING = 'Content-Encoding'
|
35
|
+
CONTENT_LENGTH = 'Content-Length'
|
34
36
|
SUPPORTED_ENCODINGS = supported_encodings.join(',').freeze
|
35
|
-
RUBY_ENCODING = '1.9'.respond_to?(:force_encoding)
|
36
37
|
|
37
38
|
def call(env)
|
38
39
|
env[:request_headers][ACCEPT_ENCODING] ||= SUPPORTED_ENCODINGS
|
39
40
|
@app.call(env).on_complete do |response_env|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
41
|
+
if response_env[:body].empty?
|
42
|
+
reset_body(response_env, &method(:raw_body))
|
43
|
+
else
|
44
|
+
case response_env[:response_headers][CONTENT_ENCODING]
|
45
|
+
when 'gzip'
|
46
|
+
reset_body(response_env, &method(:uncompress_gzip))
|
47
|
+
when 'deflate'
|
48
|
+
reset_body(response_env, &method(:inflate))
|
49
|
+
when 'br'
|
50
|
+
reset_body(response_env, &method(:brotli_inflate))
|
51
|
+
end
|
47
52
|
end
|
48
53
|
end
|
49
54
|
end
|
@@ -56,11 +61,7 @@ module FaradayMiddleware
|
|
56
61
|
|
57
62
|
def uncompress_gzip(body)
|
58
63
|
io = StringIO.new(body)
|
59
|
-
gzip_reader =
|
60
|
-
Zlib::GzipReader.new(io, :encoding => 'ASCII-8BIT')
|
61
|
-
else
|
62
|
-
Zlib::GzipReader.new(io)
|
63
|
-
end
|
64
|
+
gzip_reader = Zlib::GzipReader.new(io, encoding: 'ASCII-8BIT')
|
64
65
|
gzip_reader.read
|
65
66
|
end
|
66
67
|
|
@@ -81,5 +82,9 @@ module FaradayMiddleware
|
|
81
82
|
def brotli_inflate(body)
|
82
83
|
Brotli.inflate(body)
|
83
84
|
end
|
85
|
+
|
86
|
+
def raw_body(body)
|
87
|
+
body
|
88
|
+
end
|
84
89
|
end
|
85
90
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'faraday'
|
2
4
|
|
3
5
|
module FaradayMiddleware
|
@@ -7,11 +9,15 @@ module FaradayMiddleware
|
|
7
9
|
#
|
8
10
|
# Examples
|
9
11
|
#
|
10
|
-
# ActiveSupport::Notifications.
|
12
|
+
# ActiveSupport::Notifications.
|
13
|
+
# subscribe('request.faraday') do |name, starts, ends, _, env|
|
11
14
|
# url = env[:url]
|
12
15
|
# http_method = env[:method].to_s.upcase
|
13
16
|
# duration = ends - starts
|
14
|
-
# $stderr.puts '[%s] %s %s (%.3f s)' % [url.host,
|
17
|
+
# $stderr.puts '[%s] %s %s (%.3f s)' % [url.host,
|
18
|
+
# http_method,
|
19
|
+
# url.request_uri,
|
20
|
+
# duration]
|
15
21
|
# end
|
16
22
|
class Instrumentation < Faraday::Middleware
|
17
23
|
dependency 'active_support/notifications'
|
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'stringio'
|
2
4
|
|
3
5
|
module FaradayMiddleware
|
4
|
-
# Wraps a handler originally written for Rack
|
6
|
+
# Wraps a handler originally written for Rack for Faraday compatibility.
|
5
7
|
#
|
6
8
|
# Experimental. Only handles changes in request headers.
|
7
9
|
class RackCompatible
|
@@ -22,7 +24,7 @@ module FaradayMiddleware
|
|
22
24
|
finalize_response(env, rack_response)
|
23
25
|
end
|
24
26
|
|
25
|
-
|
27
|
+
NON_PREFIXED_HEADERS = %w[CONTENT_LENGTH CONTENT_TYPE].freeze
|
26
28
|
|
27
29
|
# faraday to rack-compatible
|
28
30
|
def prepare_env(faraday_env)
|
@@ -31,7 +33,11 @@ module FaradayMiddleware
|
|
31
33
|
url = faraday_env[:url]
|
32
34
|
env['rack.url_scheme'] = url.scheme
|
33
35
|
env['PATH_INFO'] = url.path
|
34
|
-
env['SERVER_PORT'] = url.respond_to?(:inferred_port)
|
36
|
+
env['SERVER_PORT'] = if url.respond_to?(:inferred_port)
|
37
|
+
url.inferred_port
|
38
|
+
else
|
39
|
+
url.port
|
40
|
+
end
|
35
41
|
env['QUERY_STRING'] = url.query
|
36
42
|
env['REQUEST_METHOD'] = faraday_env[:method].to_s.upcase
|
37
43
|
|
@@ -45,7 +51,7 @@ module FaradayMiddleware
|
|
45
51
|
rack_env = {}
|
46
52
|
env[:request_headers].each do |name, value|
|
47
53
|
name = name.upcase.tr('-', '_')
|
48
|
-
name = "HTTP_#{name}" unless
|
54
|
+
name = "HTTP_#{name}" unless NON_PREFIXED_HEADERS.include? name
|
49
55
|
rack_env[name] = value
|
50
56
|
end
|
51
57
|
rack_env
|
@@ -58,8 +64,9 @@ module FaradayMiddleware
|
|
58
64
|
headers.clear
|
59
65
|
|
60
66
|
rack_env.each do |name, value|
|
61
|
-
next unless String
|
62
|
-
|
67
|
+
next unless name.is_a?(String) && value.is_a?(String)
|
68
|
+
|
69
|
+
if NON_PREFIXED_HEADERS.include?(name) || name.start_with?('HTTP_')
|
63
70
|
name = name.sub(/^HTTP_/, '').downcase.tr('_', '-')
|
64
71
|
headers[name] = value
|
65
72
|
end
|
@@ -72,12 +79,12 @@ module FaradayMiddleware
|
|
72
79
|
|
73
80
|
def finalize_response(env, rack_response)
|
74
81
|
status, headers, body = rack_response
|
75
|
-
body = body.inject
|
76
|
-
headers = Faraday::Utils::Headers.new(headers) unless Faraday::Utils::Headers
|
82
|
+
body = body.inject { |str, part| str << part }
|
83
|
+
headers = Faraday::Utils::Headers.new(headers) unless headers.is_a?(Faraday::Utils::Headers)
|
77
84
|
|
78
|
-
env.update :
|
79
|
-
:
|
80
|
-
:
|
85
|
+
env.update status: status.to_i,
|
86
|
+
body: body,
|
87
|
+
response_headers: headers
|
81
88
|
|
82
89
|
env[:response] ||= Faraday::Response.new(env)
|
83
90
|
env[:response]
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
|
5
|
+
module FaradayMiddleware
|
6
|
+
# Exception thrown when the maximum amount of requests is
|
7
|
+
# exceeded.
|
8
|
+
class RedirectLimitReached < Faraday::ClientError
|
9
|
+
attr_reader :response
|
10
|
+
|
11
|
+
def initialize(response)
|
12
|
+
super "too many redirects; last one to: #{response['location']}"
|
13
|
+
@response = response
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'faraday'
|
2
4
|
|
3
5
|
module FaradayMiddleware
|
@@ -9,9 +11,9 @@ module FaradayMiddleware
|
|
9
11
|
#
|
10
12
|
# Doesn't try to encode bodies that already are in string form.
|
11
13
|
class EncodeJson < Faraday::Middleware
|
12
|
-
CONTENT_TYPE = 'Content-Type'
|
13
|
-
MIME_TYPE = 'application/json'
|
14
|
-
MIME_TYPE_REGEX =
|
14
|
+
CONTENT_TYPE = 'Content-Type'
|
15
|
+
MIME_TYPE = 'application/json'
|
16
|
+
MIME_TYPE_REGEX = %r{^application/(vnd\..+\+)?json$}.freeze
|
15
17
|
|
16
18
|
dependency do
|
17
19
|
require 'json' unless defined?(::JSON)
|
@@ -25,23 +27,23 @@ module FaradayMiddleware
|
|
25
27
|
end
|
26
28
|
|
27
29
|
def encode(data)
|
28
|
-
::JSON.
|
30
|
+
::JSON.generate data
|
29
31
|
end
|
30
32
|
|
31
33
|
def match_content_type(env)
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
34
|
+
return unless process_request?(env)
|
35
|
+
|
36
|
+
env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
|
37
|
+
yield env[:body] unless env[:body].respond_to?(:to_str)
|
36
38
|
end
|
37
39
|
|
38
40
|
def process_request?(env)
|
39
41
|
type = request_type(env)
|
40
|
-
has_body?(env)
|
42
|
+
has_body?(env) && (type.empty? || MIME_TYPE_REGEX =~ type)
|
41
43
|
end
|
42
44
|
|
43
45
|
def has_body?(env)
|
44
|
-
body = env[:body]
|
46
|
+
(body = env[:body]) && !(body.respond_to?(:to_str) && body.empty?)
|
45
47
|
end
|
46
48
|
|
47
49
|
def request_type(env)
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'faraday'
|
2
4
|
|
3
5
|
module FaradayMiddleware
|
@@ -11,8 +13,7 @@ module FaradayMiddleware
|
|
11
13
|
# Rack::MethodOverride module. See
|
12
14
|
# http://rack.rubyforge.org/doc/classes/Rack/MethodOverride.html
|
13
15
|
class MethodOverride < Faraday::Middleware
|
14
|
-
|
15
|
-
HEADER = "X-Http-Method-Override".freeze
|
16
|
+
HEADER = 'X-Http-Method-Override'
|
16
17
|
|
17
18
|
# Public: Initialize the middleware.
|
18
19
|
#
|
@@ -22,10 +23,10 @@ module FaradayMiddleware
|
|
22
23
|
# (default: all but GET and POST)
|
23
24
|
def initialize(app, options = nil)
|
24
25
|
super(app)
|
25
|
-
@methods = options
|
26
|
+
@methods = options&.fetch(:rewrite)&.map do |method|
|
26
27
|
method = method.downcase if method.respond_to? :downcase
|
27
28
|
method.to_sym
|
28
|
-
|
29
|
+
end
|
29
30
|
end
|
30
31
|
|
31
32
|
def call(env)
|
@@ -35,8 +36,8 @@ module FaradayMiddleware
|
|
35
36
|
end
|
36
37
|
|
37
38
|
def rewrite_request?(method)
|
38
|
-
if @methods.nil?
|
39
|
-
method != :get
|
39
|
+
if @methods.nil? || @methods.empty?
|
40
|
+
(method != :get) && (method != :post)
|
40
41
|
else
|
41
42
|
@methods.include? method
|
42
43
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'faraday'
|
2
4
|
require 'forwardable'
|
3
5
|
|
@@ -23,14 +25,12 @@ module FaradayMiddleware
|
|
23
25
|
class OAuth < Faraday::Middleware
|
24
26
|
dependency 'simple_oauth'
|
25
27
|
|
26
|
-
AUTH_HEADER = 'Authorization'
|
27
|
-
CONTENT_TYPE = 'Content-Type'
|
28
|
-
TYPE_URLENCODED = 'application/x-www-form-urlencoded'
|
28
|
+
AUTH_HEADER = 'Authorization'
|
29
|
+
CONTENT_TYPE = 'Content-Type'
|
30
|
+
TYPE_URLENCODED = 'application/x-www-form-urlencoded'
|
29
31
|
|
30
32
|
extend Forwardable
|
31
|
-
|
32
|
-
parser_module = ::Faraday::Utils.respond_to?(parser_method) ? 'Faraday::Utils' : 'Rack::Utils'
|
33
|
-
def_delegator parser_module, parser_method
|
33
|
+
def_delegator :'Faraday::Utils', :parse_nested_query
|
34
34
|
|
35
35
|
def initialize(app, options)
|
36
36
|
super(app)
|
@@ -54,7 +54,7 @@ module FaradayMiddleware
|
|
54
54
|
end
|
55
55
|
|
56
56
|
def oauth_options(env)
|
57
|
-
if extra = env[:request][:oauth]
|
57
|
+
if (extra = env[:request][:oauth]) && extra.is_a?(Hash) && !extra.empty?
|
58
58
|
@options.merge extra
|
59
59
|
else
|
60
60
|
@options
|
@@ -73,12 +73,15 @@ module FaradayMiddleware
|
|
73
73
|
|
74
74
|
def include_body_params?(env)
|
75
75
|
# see RFC 5849, section 3.4.1.3.1 for details
|
76
|
-
!(type = env[:request_headers][CONTENT_TYPE])
|
76
|
+
!(type = env[:request_headers][CONTENT_TYPE]) || (type == TYPE_URLENCODED)
|
77
77
|
end
|
78
78
|
|
79
79
|
def signature_params(params)
|
80
|
-
params.empty?
|
81
|
-
params
|
80
|
+
if params.empty?
|
81
|
+
params
|
82
|
+
else
|
83
|
+
params.reject { |_k, v| v.respond_to?(:content_type) }
|
84
|
+
end
|
82
85
|
end
|
83
86
|
end
|
84
87
|
end
|
@@ -1,14 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'faraday'
|
2
4
|
require 'forwardable'
|
3
5
|
|
4
6
|
module FaradayMiddleware
|
5
7
|
# Public: A simple middleware that adds an access token to each request.
|
6
8
|
#
|
7
|
-
# By default, the token is added as both "access_token" query parameter
|
8
|
-
# "Authorization" HTTP request header. It can alternatively be
|
9
|
-
# as a bearer token "Authorization" header by specifying
|
10
|
-
# of "bearer". However, an explicit "access_token"
|
11
|
-
# header for the current request are not
|
9
|
+
# By default, the token is added as both "access_token" query parameter
|
10
|
+
# and the "Authorization" HTTP request header. It can alternatively be
|
11
|
+
# added exclusively as a bearer token "Authorization" header by specifying
|
12
|
+
# a "token_type" option of "bearer". However, an explicit "access_token"
|
13
|
+
# parameter or "Authorization" header for the current request are not
|
14
|
+
# overriden.
|
12
15
|
#
|
13
16
|
# Examples
|
14
17
|
#
|
@@ -24,10 +27,9 @@ module FaradayMiddleware
|
|
24
27
|
# # default token value is optional:
|
25
28
|
# OAuth2.new(app, :param_name => 'my_oauth_token')
|
26
29
|
class OAuth2 < Faraday::Middleware
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
AUTH_HEADER = 'Authorization'.freeze
|
30
|
+
PARAM_NAME = 'access_token'
|
31
|
+
TOKEN_TYPE = 'param'
|
32
|
+
AUTH_HEADER = 'Authorization'
|
31
33
|
|
32
34
|
attr_reader :param_name, :token_type
|
33
35
|
|
@@ -53,26 +55,27 @@ module FaradayMiddleware
|
|
53
55
|
|
54
56
|
def initialize(app, token = nil, options = {})
|
55
57
|
super(app)
|
56
|
-
|
57
|
-
|
58
|
+
if token.is_a? Hash
|
59
|
+
options = token
|
60
|
+
token = nil
|
61
|
+
end
|
62
|
+
@token = token&.to_s
|
58
63
|
@param_name = options.fetch(:param_name, PARAM_NAME).to_s
|
59
64
|
@token_type = options.fetch(:token_type, TOKEN_TYPE).to_s
|
60
65
|
|
61
|
-
if @token_type == 'param' && @param_name.empty?
|
62
|
-
raise ArgumentError, ":param_name can't be blank"
|
63
|
-
end
|
66
|
+
raise ArgumentError, ":param_name can't be blank" if @token_type == 'param' && @param_name.empty?
|
64
67
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
68
|
+
return unless options[:token_type].nil?
|
69
|
+
|
70
|
+
warn "\nWarning: FaradayMiddleware::OAuth2 initialized with default "\
|
71
|
+
'token_type - token will be added as both a query string parameter '\
|
72
|
+
'and an Authorization header. In the next major release, tokens will '\
|
73
|
+
'be added exclusively as an Authorization header by default. Please '\
|
74
|
+
'see https://github.com/lostisland/faraday_middleware/wiki.'
|
72
75
|
end
|
73
76
|
|
74
77
|
def query_params(url)
|
75
|
-
if url.query.nil?
|
78
|
+
if url.query.nil? || url.query.empty?
|
76
79
|
{}
|
77
80
|
else
|
78
81
|
parse_query url.query
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'faraday'
|
2
4
|
require 'forwardable'
|
3
5
|
require 'digest/sha1'
|
@@ -6,6 +8,7 @@ module FaradayMiddleware
|
|
6
8
|
# Public: Caches GET responses and pulls subsequent ones from the cache.
|
7
9
|
class Caching < Faraday::Middleware
|
8
10
|
attr_reader :cache
|
11
|
+
|
9
12
|
# Internal: List of status codes that can be cached:
|
10
13
|
# * 200 - 'OK'
|
11
14
|
# * 203 - 'Non-Authoritative Information'
|
@@ -14,7 +17,7 @@ module FaradayMiddleware
|
|
14
17
|
# * 302 - 'Found'
|
15
18
|
# * 404 - 'Not Found'
|
16
19
|
# * 410 - 'Gone'
|
17
|
-
CACHEABLE_STATUS_CODES = [200, 203, 300, 301, 302, 404, 410]
|
20
|
+
CACHEABLE_STATUS_CODES = [200, 203, 300, 301, 302, 404, 410].freeze
|
18
21
|
|
19
22
|
extend Forwardable
|
20
23
|
def_delegators :'Faraday::Utils', :parse_query, :build_query
|
@@ -23,30 +26,36 @@ module FaradayMiddleware
|
|
23
26
|
#
|
24
27
|
# cache - An object that responds to read and write (default: nil).
|
25
28
|
# options - An options Hash (default: {}):
|
26
|
-
# :ignore_params - String name or Array names of query
|
27
|
-
# that should be ignored when forming
|
28
|
-
# key (default: []).
|
29
|
-
# :write_options - Hash of settings that should be passed as the
|
30
|
-
# options parameter to the cache's #write
|
31
|
-
# specified, no options parameter
|
29
|
+
# :ignore_params - String name or Array names of query
|
30
|
+
# params that should be ignored when forming
|
31
|
+
# the cache key (default: []).
|
32
|
+
# :write_options - Hash of settings that should be passed as the
|
33
|
+
# third options parameter to the cache's #write
|
34
|
+
# method. If not specified, no options parameter
|
35
|
+
# will be passed.
|
36
|
+
# :full_key - Boolean - use full URL as cache key:
|
37
|
+
# (url.host + url.request_uri)
|
32
38
|
#
|
33
39
|
# Yields if no cache is given. The block should return a cache object.
|
34
40
|
def initialize(app, cache = nil, options = {})
|
35
41
|
super(app)
|
36
|
-
|
42
|
+
if cache.is_a?(Hash) && block_given?
|
43
|
+
options = cache
|
44
|
+
cache = nil
|
45
|
+
end
|
37
46
|
@cache = cache || yield
|
38
47
|
@options = options
|
39
48
|
end
|
40
49
|
|
41
50
|
def call(env)
|
42
|
-
if :
|
51
|
+
if env[:method] == :get
|
43
52
|
if env[:parallel_manager]
|
44
53
|
# callback mode
|
45
54
|
cache_on_complete(env)
|
46
55
|
else
|
47
56
|
# synchronous mode
|
48
57
|
key = cache_key(env)
|
49
|
-
unless response = cache.read(key)
|
58
|
+
unless (response = cache.read(key)) && response
|
50
59
|
response = @app.call(env)
|
51
60
|
store_response_in_cache(key, response)
|
52
61
|
end
|
@@ -61,24 +70,29 @@ module FaradayMiddleware
|
|
61
70
|
url = env[:url].dup
|
62
71
|
if url.query && params_to_ignore.any?
|
63
72
|
params = parse_query url.query
|
64
|
-
params.reject! {|k,| params_to_ignore.include? k }
|
73
|
+
params.reject! { |k,| params_to_ignore.include? k }
|
65
74
|
url.query = params.any? ? build_query(params) : nil
|
66
75
|
end
|
67
76
|
url.normalize!
|
68
|
-
|
69
|
-
Digest::SHA1.hexdigest(
|
77
|
+
digest = full_key? ? url.host + url.request_uri : url.request_uri
|
78
|
+
Digest::SHA1.hexdigest(digest)
|
70
79
|
end
|
71
80
|
|
72
81
|
def params_to_ignore
|
73
|
-
@params_to_ignore ||= Array(@options[:ignore_params]).map
|
82
|
+
@params_to_ignore ||= Array(@options[:ignore_params]).map(&:to_s)
|
83
|
+
end
|
84
|
+
|
85
|
+
def full_key?
|
86
|
+
@full_key ||= @options[:full_key]
|
74
87
|
end
|
75
88
|
|
76
89
|
def cache_on_complete(env)
|
77
90
|
key = cache_key(env)
|
78
|
-
if cached_response = cache.read(key)
|
91
|
+
if (cached_response = cache.read(key))
|
79
92
|
finalize_response(cached_response, env)
|
80
93
|
else
|
81
|
-
# response.status is nil at this point
|
94
|
+
# response.status is nil at this point
|
95
|
+
# any checks need to be done inside on_complete block
|
82
96
|
@app.call(env).on_complete do |response_env|
|
83
97
|
store_response_in_cache(key, response_env.response)
|
84
98
|
response_env
|
@@ -1,16 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'faraday_middleware/response_middleware'
|
2
4
|
|
3
5
|
module FaradayMiddleware
|
4
|
-
# Public: Parse a Transfer-Encoding
|
6
|
+
# Public: Parse a Transfer-Encoding. Chunks response to just the original data
|
5
7
|
class Chunked < FaradayMiddleware::ResponseMiddleware
|
6
|
-
TRANSFER_ENCODING = 'transfer-encoding'
|
8
|
+
TRANSFER_ENCODING = 'transfer-encoding'
|
7
9
|
|
8
10
|
define_parser do |raw_body|
|
9
11
|
decoded_body = []
|
10
12
|
until raw_body.empty?
|
11
13
|
chunk_len, raw_body = raw_body.split("\r\n", 2)
|
12
|
-
chunk_len = chunk_len.split(';',2).first.hex
|
13
|
-
break if chunk_len
|
14
|
+
chunk_len = chunk_len.split(';', 2).first.hex
|
15
|
+
break if chunk_len.zero?
|
16
|
+
|
14
17
|
decoded_body << raw_body[0, chunk_len]
|
15
18
|
# The 2 is to strip the extra CRLF at the end of the chunk
|
16
19
|
raw_body = raw_body[chunk_len + 2, raw_body.length - chunk_len - 2]
|
@@ -19,11 +22,12 @@ module FaradayMiddleware
|
|
19
22
|
end
|
20
23
|
|
21
24
|
def parse_response?(env)
|
22
|
-
super
|
25
|
+
super && chunked_encoding?(env[:response_headers])
|
23
26
|
end
|
24
27
|
|
25
28
|
def chunked_encoding?(headers)
|
26
|
-
encoding = headers[TRANSFER_ENCODING]
|
29
|
+
(encoding = headers[TRANSFER_ENCODING]) &&
|
30
|
+
encoding.split(',').include?('chunked')
|
27
31
|
end
|
28
32
|
end
|
29
33
|
end
|
@@ -1,17 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'faraday'
|
2
4
|
require 'set'
|
3
5
|
|
4
6
|
module FaradayMiddleware
|
5
|
-
# Public: Exception thrown when the maximum amount of requests is exceeded.
|
6
|
-
class RedirectLimitReached < Faraday::Error::ClientError
|
7
|
-
attr_reader :response
|
8
|
-
|
9
|
-
def initialize(response)
|
10
|
-
super "too many redirects; last one to: #{response['location']}"
|
11
|
-
@response = response
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
7
|
# Public: Follow HTTP 301, 302, 303, 307, and 308 redirects.
|
16
8
|
#
|
17
9
|
# For HTTP 301, 302, and 303, the original GET, POST, PUT, DELETE, or PATCH
|
@@ -32,20 +24,20 @@ module FaradayMiddleware
|
|
32
24
|
# end
|
33
25
|
class FollowRedirects < Faraday::Middleware
|
34
26
|
# HTTP methods for which 30x redirects can be followed
|
35
|
-
ALLOWED_METHODS = Set.new [
|
27
|
+
ALLOWED_METHODS = Set.new %i[head options get post put patch delete]
|
36
28
|
# HTTP redirect status codes that this middleware implements
|
37
29
|
REDIRECT_CODES = Set.new [301, 302, 303, 307, 308]
|
38
30
|
# Keys in env hash which will get cleared between requests
|
39
|
-
ENV_TO_CLEAR = Set.new [
|
31
|
+
ENV_TO_CLEAR = Set.new %i[status response response_headers]
|
40
32
|
|
41
33
|
# Default value for max redirects followed
|
42
34
|
FOLLOW_LIMIT = 3
|
43
35
|
|
44
36
|
# Regex that matches characters that need to be escaped in URLs, sans
|
45
37
|
# the "%" character which we assume already represents an escaped sequence.
|
46
|
-
URI_UNSAFE =
|
38
|
+
URI_UNSAFE = %r{[^\-_.!~*'()a-zA-Z\d;/?:@&=+$,\[\]%]}.freeze
|
47
39
|
|
48
|
-
AUTH_HEADER = 'Authorization'
|
40
|
+
AUTH_HEADER = 'Authorization'
|
49
41
|
|
50
42
|
# Public: Initialize the middleware.
|
51
43
|
#
|
@@ -54,7 +46,7 @@ module FaradayMiddleware
|
|
54
46
|
# :standards_compliant - A Boolean indicating whether to respect
|
55
47
|
# the HTTP spec when following 301/302
|
56
48
|
# (default: false)
|
57
|
-
# :callback - A callable
|
49
|
+
# :callback - A callable used on redirects
|
58
50
|
# with the old and new envs
|
59
51
|
# :cookies - An Array of Strings (e.g.
|
60
52
|
# ['cookie1', 'cookie2']) to choose
|
@@ -78,7 +70,7 @@ module FaradayMiddleware
|
|
78
70
|
private
|
79
71
|
|
80
72
|
def convert_to_get?(response)
|
81
|
-
|
73
|
+
!%i[head options].include?(response.env[:method]) &&
|
82
74
|
@convert_to_get.include?(response.status)
|
83
75
|
end
|
84
76
|
|
@@ -89,8 +81,9 @@ module FaradayMiddleware
|
|
89
81
|
response.on_complete do |response_env|
|
90
82
|
if follow_redirect?(response_env, response)
|
91
83
|
raise RedirectLimitReached, response if follows.zero?
|
84
|
+
|
92
85
|
new_request_env = update_env(response_env.dup, request_body, response)
|
93
|
-
callback
|
86
|
+
callback&.call(response_env, new_request_env)
|
94
87
|
response = perform_with_redirection(new_request_env, follows - 1)
|
95
88
|
end
|
96
89
|
end
|
@@ -102,6 +95,8 @@ module FaradayMiddleware
|
|
102
95
|
redirect_to_url = safe_escape(response['location'] || '')
|
103
96
|
env[:url] += redirect_to_url
|
104
97
|
|
98
|
+
ENV_TO_CLEAR.each { |key| env.delete key }
|
99
|
+
|
105
100
|
if convert_to_get?(response)
|
106
101
|
env[:method] = :get
|
107
102
|
env[:body] = nil
|
@@ -111,14 +106,12 @@ module FaradayMiddleware
|
|
111
106
|
|
112
107
|
clear_authorization_header(env, redirect_from_url, redirect_to_url)
|
113
108
|
|
114
|
-
ENV_TO_CLEAR.each {|key| env.delete key }
|
115
|
-
|
116
109
|
env
|
117
110
|
end
|
118
111
|
|
119
112
|
def follow_redirect?(env, response)
|
120
|
-
ALLOWED_METHODS.include?
|
121
|
-
REDIRECT_CODES.include?
|
113
|
+
ALLOWED_METHODS.include?(env[:method]) &&
|
114
|
+
REDIRECT_CODES.include?(response.status)
|
122
115
|
end
|
123
116
|
|
124
117
|
def follow_limit
|
@@ -139,9 +132,9 @@ module FaradayMiddleware
|
|
139
132
|
# risk double-escaping.
|
140
133
|
def safe_escape(uri)
|
141
134
|
uri = uri.split('#')[0] # we want to remove the fragment if present
|
142
|
-
uri.to_s.gsub(URI_UNSAFE)
|
143
|
-
|
144
|
-
|
135
|
+
uri.to_s.gsub(URI_UNSAFE) do |match|
|
136
|
+
"%#{match.unpack('H2' * match.bytesize).join('%').upcase}"
|
137
|
+
end
|
145
138
|
end
|
146
139
|
|
147
140
|
def clear_authorization_header(env, from_url, to_url)
|
@@ -157,7 +150,8 @@ module FaradayMiddleware
|
|
157
150
|
from_uri = URI.parse(from_url)
|
158
151
|
to_uri = URI.parse(to_url)
|
159
152
|
|
160
|
-
[from_uri.scheme, from_uri.host, from_uri.port] ==
|
153
|
+
[from_uri.scheme, from_uri.host, from_uri.port] ==
|
154
|
+
[to_uri.scheme, to_uri.host, to_uri.port]
|
161
155
|
end
|
162
156
|
end
|
163
157
|
end
|
@@ -1,10 +1,13 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'time'
|
4
|
+
require 'faraday'
|
3
5
|
|
4
6
|
module FaradayMiddleware
|
5
7
|
# Parse dates from response body
|
6
8
|
class ParseDates < ::Faraday::Response::Middleware
|
7
|
-
ISO_DATE_FORMAT = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?
|
9
|
+
ISO_DATE_FORMAT = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?
|
10
|
+
(Z|((\+|-)\d{2}:?\d{2}))\Z/xm.freeze
|
8
11
|
|
9
12
|
def initialize(app, options = {})
|
10
13
|
@regexp = options[:match] || ISO_DATE_FORMAT
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'faraday_middleware/response_middleware'
|
2
4
|
|
3
5
|
module FaradayMiddleware
|
@@ -18,7 +20,7 @@ module FaradayMiddleware
|
|
18
20
|
# This is to fix responses from certain API providers that insist on serving
|
19
21
|
# JSON with wrong MIME-types such as "text/javascript".
|
20
22
|
class MimeTypeFix < ResponseMiddleware
|
21
|
-
MIME_TYPE = 'application/json'
|
23
|
+
MIME_TYPE = 'application/json'
|
22
24
|
|
23
25
|
def process_response(env)
|
24
26
|
old_type = env[:response_headers][CONTENT_TYPE].to_s
|
@@ -27,19 +29,17 @@ module FaradayMiddleware
|
|
27
29
|
env[:response_headers][CONTENT_TYPE] = new_type
|
28
30
|
end
|
29
31
|
|
30
|
-
BRACKETS = %w-
|
31
|
-
WHITESPACE = [
|
32
|
+
BRACKETS = %w-[ {-.freeze
|
33
|
+
WHITESPACE = [' ', "\n", "\r", "\t"].freeze
|
32
34
|
|
33
35
|
def parse_response?(env)
|
34
|
-
super
|
36
|
+
super && BRACKETS.include?(first_char(env[:body]))
|
35
37
|
end
|
36
38
|
|
37
39
|
def first_char(body)
|
38
40
|
idx = -1
|
39
|
-
|
40
|
-
|
41
|
-
char = char.chr if char
|
42
|
-
end while char and WHITESPACE.include? char
|
41
|
+
char = body[idx += 1]
|
42
|
+
char = body[idx += 1] while char && WHITESPACE.include?(char)
|
43
43
|
char
|
44
44
|
end
|
45
45
|
end
|
@@ -1,13 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'faraday_middleware/response_middleware'
|
2
4
|
|
3
5
|
module FaradayMiddleware
|
4
6
|
# Public: Parse response bodies as YAML.
|
5
7
|
#
|
6
|
-
# Warning: This is not backwards compatible with versions of this middleware
|
7
|
-
# faraday_middleware v0.12 - prior to this version, we used
|
8
|
-
# YAMl.safe_load, which exposes serious remote code
|
9
|
-
# https://github.com/ruby/psych/issues/119 for details.
|
10
|
-
# YAML you're passing, you can set up an unsafe
|
8
|
+
# Warning: This is not backwards compatible with versions of this middleware
|
9
|
+
# prior to faraday_middleware v0.12 - prior to this version, we used
|
10
|
+
# YAML.load rather than YAMl.safe_load, which exposes serious remote code
|
11
|
+
# execution risks - see https://github.com/ruby/psych/issues/119 for details.
|
12
|
+
# If you're sure you can trust YAML you're passing, you can set up an unsafe
|
13
|
+
# version of this middleware like this:
|
11
14
|
#
|
12
15
|
# class UnsafelyParseYaml < FaradayMiddleware::ResponseMiddleware
|
13
16
|
# dependency do
|
@@ -27,11 +30,7 @@ module FaradayMiddleware
|
|
27
30
|
dependency 'safe_yaml/load'
|
28
31
|
|
29
32
|
define_parser do |body, parser_options|
|
30
|
-
|
31
|
-
SafeYAML.load(body, nil, parser_options || {})
|
32
|
-
else
|
33
|
-
SafeYAML.load(body, parser_options || {})
|
34
|
-
end
|
33
|
+
SafeYAML.load(body, nil, parser_options || {})
|
35
34
|
end
|
36
35
|
end
|
37
36
|
end
|
@@ -1,23 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'faraday'
|
2
4
|
|
5
|
+
# Main FaradayMiddleware module.
|
3
6
|
module FaradayMiddleware
|
4
7
|
# Internal: The base class for middleware that parses responses.
|
5
8
|
class ResponseMiddleware < Faraday::Middleware
|
6
|
-
CONTENT_TYPE = 'Content-Type'
|
9
|
+
CONTENT_TYPE = 'Content-Type'
|
7
10
|
|
8
11
|
class << self
|
9
12
|
attr_accessor :parser
|
10
13
|
end
|
11
14
|
|
12
15
|
# Store a Proc that receives the body and returns the parsed result.
|
13
|
-
def self.define_parser(parser = nil)
|
14
|
-
@parser = parser ||
|
16
|
+
def self.define_parser(parser = nil, &block)
|
17
|
+
@parser = parser ||
|
18
|
+
block ||
|
19
|
+
raise(ArgumentError, 'Define parser with a block')
|
15
20
|
end
|
16
21
|
|
17
22
|
def self.inherited(subclass)
|
18
23
|
super
|
19
|
-
subclass.load_error =
|
20
|
-
subclass.parser =
|
24
|
+
subclass.load_error = load_error if subclass.respond_to? :load_error=
|
25
|
+
subclass.parser = parser
|
21
26
|
end
|
22
27
|
|
23
28
|
def initialize(app = nil, options = {})
|
@@ -29,17 +34,15 @@ module FaradayMiddleware
|
|
29
34
|
|
30
35
|
def call(environment)
|
31
36
|
@app.call(environment).on_complete do |env|
|
32
|
-
if process_response_type?(response_type(env))
|
33
|
-
process_response(env)
|
34
|
-
end
|
37
|
+
process_response(env) if process_response_type?(response_type(env)) && parse_response?(env)
|
35
38
|
end
|
36
39
|
end
|
37
40
|
|
38
41
|
def process_response(env)
|
39
42
|
env[:raw_body] = env[:body] if preserve_raw?(env)
|
40
43
|
env[:body] = parse(env[:body])
|
41
|
-
rescue Faraday::
|
42
|
-
raise Faraday::
|
44
|
+
rescue Faraday::ParsingError => e
|
45
|
+
raise Faraday::ParsingError.new(e.wrapped_exception, env[:response])
|
43
46
|
end
|
44
47
|
|
45
48
|
# Parse the response body.
|
@@ -49,9 +52,11 @@ module FaradayMiddleware
|
|
49
52
|
if self.class.parser
|
50
53
|
begin
|
51
54
|
self.class.parser.call(body, @parser_options)
|
52
|
-
rescue StandardError, SyntaxError =>
|
53
|
-
raise
|
54
|
-
|
55
|
+
rescue StandardError, SyntaxError => e
|
56
|
+
raise e if e.is_a?(SyntaxError) &&
|
57
|
+
e.class.name != 'Psych::SyntaxError'
|
58
|
+
|
59
|
+
raise Faraday::ParsingError, e
|
55
60
|
end
|
56
61
|
else
|
57
62
|
body
|
@@ -65,9 +70,9 @@ module FaradayMiddleware
|
|
65
70
|
end
|
66
71
|
|
67
72
|
def process_response_type?(type)
|
68
|
-
@content_types.empty?
|
73
|
+
@content_types.empty? || @content_types.any? do |pattern|
|
69
74
|
pattern.is_a?(Regexp) ? type =~ pattern : type == pattern
|
70
|
-
|
75
|
+
end
|
71
76
|
end
|
72
77
|
|
73
78
|
def parse_response?(env)
|
@@ -84,17 +89,18 @@ module FaradayMiddleware
|
|
84
89
|
attr_accessor :preserve_raw
|
85
90
|
|
86
91
|
def to_hash
|
87
|
-
super.update(:
|
92
|
+
super.update(preserve_raw: preserve_raw)
|
88
93
|
end
|
89
94
|
|
90
95
|
def each
|
91
96
|
return to_enum(:each) unless block_given?
|
97
|
+
|
92
98
|
super
|
93
99
|
yield :preserve_raw, preserve_raw
|
94
100
|
end
|
95
101
|
|
96
102
|
def fetch(key, *args)
|
97
|
-
if
|
103
|
+
if key == :preserve_raw
|
98
104
|
value = __send__(key)
|
99
105
|
value.nil? ? args.fetch(0) : value
|
100
106
|
else
|
@@ -105,9 +111,9 @@ module FaradayMiddleware
|
|
105
111
|
|
106
112
|
if defined?(Faraday::RequestOptions)
|
107
113
|
begin
|
108
|
-
Faraday::RequestOptions.from(:
|
114
|
+
Faraday::RequestOptions.from(preserve_raw: true)
|
109
115
|
rescue NoMethodError
|
110
|
-
Faraday::RequestOptions.
|
116
|
+
Faraday::RequestOptions.include OptionsExtension
|
111
117
|
end
|
112
118
|
end
|
113
119
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday_middleware
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Erik Michaels-Ober
|
@@ -9,26 +9,20 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2021-07-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- - "
|
19
|
-
- !ruby/object:Gem::Version
|
20
|
-
version: 0.7.4
|
21
|
-
- - "<"
|
18
|
+
- - "~>"
|
22
19
|
- !ruby/object:Gem::Version
|
23
20
|
version: '1.0'
|
24
21
|
type: :runtime
|
25
22
|
prerelease: false
|
26
23
|
version_requirements: !ruby/object:Gem::Requirement
|
27
24
|
requirements:
|
28
|
-
- - "
|
29
|
-
- !ruby/object:Gem::Version
|
30
|
-
version: 0.7.4
|
31
|
-
- - "<"
|
25
|
+
- - "~>"
|
32
26
|
- !ruby/object:Gem::Version
|
33
27
|
version: '1.0'
|
34
28
|
description:
|
@@ -46,6 +40,7 @@ files:
|
|
46
40
|
- lib/faraday_middleware/gzip.rb
|
47
41
|
- lib/faraday_middleware/instrumentation.rb
|
48
42
|
- lib/faraday_middleware/rack_compatible.rb
|
43
|
+
- lib/faraday_middleware/redirect_limit_reached.rb
|
49
44
|
- lib/faraday_middleware/request/encode_json.rb
|
50
45
|
- lib/faraday_middleware/request/method_override.rb
|
51
46
|
- lib/faraday_middleware/request/oauth.rb
|
@@ -74,14 +69,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
74
69
|
requirements:
|
75
70
|
- - ">="
|
76
71
|
- !ruby/object:Gem::Version
|
77
|
-
version: '
|
72
|
+
version: '2.3'
|
78
73
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
74
|
requirements:
|
80
75
|
- - ">="
|
81
76
|
- !ruby/object:Gem::Version
|
82
77
|
version: '0'
|
83
78
|
requirements: []
|
84
|
-
rubygems_version: 3.0.
|
79
|
+
rubygems_version: 3.0.3.1
|
85
80
|
signing_key:
|
86
81
|
specification_version: 4
|
87
82
|
summary: Various middleware for Faraday
|