faraday_middleware 0.10.1 → 0.11.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 19207ab27ddadf4621b943f32e3ee0a54ebbe0f4
4
- data.tar.gz: 17df1b80453f02f7218052cdac77f477efe3f99d
3
+ metadata.gz: 5d8c7e371a69bb82054c6d04c347395f191bc994
4
+ data.tar.gz: 2a06c40d062986727564b0ef21c284a163d1ef48
5
5
  SHA512:
6
- metadata.gz: e4b8edf32f496c1c52b139390c1fe0ae9908891e2dda234958a5cb40efed32a75bebebacc275f44ba075f8d78ae342950272006c1ddf16db0bb8ea78bef04398
7
- data.tar.gz: f024bdcfb98632eaed13da4dc6206ba4389e31c5d569b3ec67f7f10b51729b7a7a69504ca0be4ab38b6d551344810d18ccf29c9b2df5c24420f9d00e3c05bdbf
6
+ metadata.gz: 7e7427a5ff50a3a5231d578d072787dc9f1391b7dd89a4bd57cda4675c30bc888c3a9a996f6133666ba1858b0103bca7f7d3c011ca3a5982287d71be07bbc71f
7
+ data.tar.gz: e807d97ea850b2f27d1e413d19607349672d5f386353a88d9e86d6e8c524713b1bc0dff9ad29337e5926122aad20e09c820586ad575c90ea7fe2c33c556e9f9b
@@ -48,7 +48,17 @@ module FaradayMiddleware
48
48
  end
49
49
 
50
50
  def inflate(body)
51
+ # Inflate as a DEFLATE (RFC 1950+RFC 1951) stream
51
52
  Zlib::Inflate.inflate(body)
53
+ rescue Zlib::DataError
54
+ # Fall back to inflating as a "raw" deflate stream which
55
+ # Microsoft servers return
56
+ inflate = Zlib::Inflate.new(-Zlib::MAX_WBITS)
57
+ begin
58
+ inflate.inflate(body)
59
+ ensure
60
+ inflate.close
61
+ end
52
62
  end
53
63
  end
54
64
  end
@@ -9,8 +9,9 @@ module FaradayMiddleware
9
9
  #
10
10
  # Doesn't try to encode bodies that already are in string form.
11
11
  class EncodeJson < Faraday::Middleware
12
- CONTENT_TYPE = 'Content-Type'.freeze
13
- MIME_TYPE = 'application/json'.freeze
12
+ CONTENT_TYPE = 'Content-Type'.freeze
13
+ MIME_TYPE = 'application/json'.freeze
14
+ MIME_TYPE_REGEX = /^application\/(vnd\..+\+)?json$/
14
15
 
15
16
  dependency do
16
17
  require 'json' unless defined?(::JSON)
@@ -36,7 +37,7 @@ module FaradayMiddleware
36
37
 
37
38
  def process_request?(env)
38
39
  type = request_type(env)
39
- has_body?(env) and (type.empty? or type == MIME_TYPE)
40
+ has_body?(env) and (type.empty? or MIME_TYPE_REGEX =~ type)
40
41
  end
41
42
 
42
43
  def has_body?(env)
@@ -4,10 +4,11 @@ require 'forwardable'
4
4
  module FaradayMiddleware
5
5
  # Public: A simple middleware that adds an access token to each request.
6
6
  #
7
- # The token is added as both "access_token" query parameter and the
8
- # "Authorization" HTTP request header. However, an explicit "access_token"
9
- # parameter or "Authorization" header for the current request are not
10
- # overriden.
7
+ # By default, the token is added as both "access_token" query parameter and the
8
+ # "Authorization" HTTP request header. It can alternatively be added exclusively
9
+ # as a bearer token "Authorization" header by specifying a "token_type" option
10
+ # of "bearer". However, an explicit "access_token" parameter or "Authorization"
11
+ # header for the current request are not overriden.
11
12
  #
12
13
  # Examples
13
14
  #
@@ -17,14 +18,18 @@ module FaradayMiddleware
17
18
  # # configure query parameter name:
18
19
  # OAuth2.new(app, 'abc123', :param_name => 'my_oauth_token')
19
20
  #
21
+ # # use bearer token authorization header only
22
+ # OAuth2.new(app, 'abc123', :token_type => 'bearer')
23
+ #
20
24
  # # default token value is optional:
21
25
  # OAuth2.new(app, :param_name => 'my_oauth_token')
22
26
  class OAuth2 < Faraday::Middleware
23
27
 
24
28
  PARAM_NAME = 'access_token'.freeze
29
+ TOKEN_TYPE = 'param'.freeze
25
30
  AUTH_HEADER = 'Authorization'.freeze
26
31
 
27
- attr_reader :param_name
32
+ attr_reader :param_name, :token_type
28
33
 
29
34
  extend Forwardable
30
35
  def_delegators :'Faraday::Utils', :parse_query, :build_query
@@ -34,8 +39,13 @@ module FaradayMiddleware
34
39
  token = params[param_name]
35
40
 
36
41
  if token.respond_to?(:empty?) && !token.empty?
37
- env[:url].query = build_query params
38
- env[:request_headers][AUTH_HEADER] ||= %(Token token="#{token}")
42
+ case @token_type.downcase
43
+ when 'param'
44
+ env[:url].query = build_query params
45
+ env[:request_headers][AUTH_HEADER] ||= %(Token token="#{token}")
46
+ when 'bearer'
47
+ env[:request_headers][AUTH_HEADER] ||= %(Bearer #{token})
48
+ end
39
49
  end
40
50
 
41
51
  @app.call env
@@ -46,7 +56,19 @@ module FaradayMiddleware
46
56
  options, token = token, nil if token.is_a? Hash
47
57
  @token = token && token.to_s
48
58
  @param_name = options.fetch(:param_name, PARAM_NAME).to_s
49
- raise ArgumentError, ":param_name can't be blank" if @param_name.empty?
59
+ @token_type = options.fetch(:token_type, TOKEN_TYPE).to_s
60
+
61
+ if @token_type == 'param' && @param_name.empty?
62
+ raise ArgumentError, ":param_name can't be blank"
63
+ end
64
+
65
+ if options[:token_type].nil?
66
+ warn "\nWarning: FaradayMiddleware::OAuth2 initialized with default "\
67
+ "token_type - token will be added as both a query string parameter "\
68
+ "and an Authorization header. In the next major release, tokens will "\
69
+ "be added exclusively as an Authorization header by default. Please "\
70
+ "visit https://github.com/lostisland/faraday_middleware/wiki for more information."
71
+ end
50
72
  end
51
73
 
52
74
  def query_params(url)
@@ -79,11 +79,11 @@ module FaradayMiddleware
79
79
  finalize_response(cached_response, env)
80
80
  else
81
81
  # response.status is nil at this point, any checks need to be done inside on_complete block
82
- @app.call(env).on_complete do |response|
83
- if CACHEABLE_STATUS_CODES.include?(response.status)
84
- cache.write(key, response)
82
+ @app.call(env).on_complete do |response_env|
83
+ if CACHEABLE_STATUS_CODES.include?(response_env.status)
84
+ cache.write(key, response_env.response)
85
85
  end
86
- response
86
+ response_env
87
87
  end
88
88
  end
89
89
  end
@@ -12,7 +12,7 @@ module FaradayMiddleware
12
12
  end
13
13
  end
14
14
 
15
- # Public: Follow HTTP 301, 302, 303, and 307 redirects.
15
+ # Public: Follow HTTP 301, 302, 303, 307, and 308 redirects.
16
16
  #
17
17
  # For HTTP 301, 302, and 303, the original GET, POST, PUT, DELETE, or PATCH
18
18
  # request gets converted into a GET. With `:standards_compliant => true`,
@@ -34,7 +34,7 @@ module FaradayMiddleware
34
34
  # HTTP methods for which 30x redirects can be followed
35
35
  ALLOWED_METHODS = Set.new [:head, :options, :get, :post, :put, :patch, :delete]
36
36
  # HTTP redirect status codes that this middleware implements
37
- REDIRECT_CODES = Set.new [301, 302, 303, 307]
37
+ REDIRECT_CODES = Set.new [301, 302, 303, 307, 308]
38
38
  # Keys in env hash which will get cleared between requests
39
39
  ENV_TO_CLEAR = Set.new [:status, :response, :response_headers]
40
40
 
@@ -52,6 +52,8 @@ module FaradayMiddleware
52
52
  # :standards_compliant - A Boolean indicating whether to respect
53
53
  # the HTTP spec when following 301/302
54
54
  # (default: false)
55
+ # :callback - A callable that will be called on redirects
56
+ # with the old and new envs
55
57
  def initialize(app, options = {})
56
58
  super(app)
57
59
  @options = options
@@ -78,7 +80,8 @@ module FaradayMiddleware
78
80
  response.on_complete do |response_env|
79
81
  if follow_redirect?(response_env, response)
80
82
  raise RedirectLimitReached, response if follows.zero?
81
- new_request_env = update_env(response_env, request_body, response)
83
+ new_request_env = update_env(response_env.dup, request_body, response)
84
+ callback.call(response_env, new_request_env) if callback
82
85
  response = perform_with_redirection(new_request_env, follows - 1)
83
86
  end
84
87
  end
@@ -113,11 +116,16 @@ module FaradayMiddleware
113
116
  @options.fetch(:standards_compliant, false)
114
117
  end
115
118
 
119
+ def callback
120
+ @options[:callback]
121
+ end
122
+
116
123
  # Internal: escapes unsafe characters from an URL which might be a path
117
124
  # component only or a fully qualified URI so that it can be joined onto an
118
125
  # URI:HTTP using the `+` operator. Doesn't escape "%" characters so to not
119
126
  # risk double-escaping.
120
127
  def safe_escape(uri)
128
+ uri = uri.split('#')[0] # we want to remove the fragment if present
121
129
  uri.to_s.gsub(URI_UNSAFE) { |match|
122
130
  '%' + match.unpack('H2' * match.bytesize).join('%').upcase
123
131
  }
@@ -37,6 +37,8 @@ module FaradayMiddleware
37
37
  def process_response(env)
38
38
  env[:raw_body] = env[:body] if preserve_raw?(env)
39
39
  env[:body] = parse(env[:body])
40
+ rescue Faraday::Error::ParsingError => err
41
+ raise Faraday::Error::ParsingError.new(err, env[:response])
40
42
  end
41
43
 
42
44
  # Parse the response body.
@@ -1,3 +1,3 @@
1
1
  module FaradayMiddleware
2
- VERSION = "0.10.1" unless defined?(FaradayMiddleware::VERSION)
2
+ VERSION = "0.11.0" unless defined?(FaradayMiddleware::VERSION)
3
3
  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: 0.10.1
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Michaels-Ober
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-11-11 00:00:00.000000000 Z
12
+ date: 2017-01-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday