Class: ActiveRecordApi::Request::FaradayAuthTokenRetry
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- ActiveRecordApi::Request::FaradayAuthTokenRetry
- Defined in:
- lib/active_record_api/request/faraday_auth_token_retry.rb
Constant Summary collapse
- AUTH_KEY =
'Authorization'.freeze
Instance Attribute Summary collapse
-
#credentials ⇒ Object
readonly
Returns the value of attribute credentials.
-
#debug ⇒ Object
readonly
Returns the value of attribute debug.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#token_path ⇒ Object
readonly
Returns the value of attribute token_path.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, credentials: nil, host: nil, token_path: nil, debug: false) ⇒ FaradayAuthTokenRetry
constructor
A new instance of FaradayAuthTokenRetry.
- #retrieve_token ⇒ Object
- #token_cache ⇒ Object
Constructor Details
#initialize(app, credentials: nil, host: nil, token_path: nil, debug: false) ⇒ FaradayAuthTokenRetry
Returns a new instance of FaradayAuthTokenRetry.
11 12 13 14 15 16 17 |
# File 'lib/active_record_api/request/faraday_auth_token_retry.rb', line 11 def initialize(app, credentials: nil, host: nil, token_path: nil, debug: false) super(app) @credentials = credentials @host = host @token_path = token_path @debug = debug end |
Instance Attribute Details
#credentials ⇒ Object (readonly)
Returns the value of attribute credentials.
9 10 11 |
# File 'lib/active_record_api/request/faraday_auth_token_retry.rb', line 9 def credentials @credentials end |
#debug ⇒ Object (readonly)
Returns the value of attribute debug.
9 10 11 |
# File 'lib/active_record_api/request/faraday_auth_token_retry.rb', line 9 def debug @debug end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
9 10 11 |
# File 'lib/active_record_api/request/faraday_auth_token_retry.rb', line 9 def host @host end |
#token_path ⇒ Object (readonly)
Returns the value of attribute token_path.
9 10 11 |
# File 'lib/active_record_api/request/faraday_auth_token_retry.rb', line 9 def token_path @token_path end |
Instance Method Details
#call(env) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/active_record_api/request/faraday_auth_token_retry.rb', line 19 def call(env) if env.request_headers['Retries'].present? env.request_headers['Retries'] = (env.request_headers['Retries'].to_i + 1).to_s else env.request_headers['Retries'] = '0' end if env.request_headers['InjectToken'].present? && env.request_headers['InjectToken'] == 'true' env.request_headers[AUTH_KEY] = "Bearer #{retrieve_token}" end @app.call(env) rescue Faraday::ClientError => error if error.respond_to?(:response) && [401, 403].include?(error.response.try(:[], :status)) && env.request_headers['Retries'] != '5' if @debug Honeybadger.notify(error, {context: {message: 'faraday error, flushing'}}) end token_cache.flush call(env) else if @debug Honeybadger.notify(error, {context: {message: 'faraday error, not flushing'}}) end raise error end end |
#retrieve_token ⇒ Object
44 45 46 |
# File 'lib/active_record_api/request/faraday_auth_token_retry.rb', line 44 def retrieve_token token_cache.fetch_token Proc.new { TokenRetriever.new(credentials: credentials, host: host, token_path: token_path, debug: debug).fetch_token } end |
#token_cache ⇒ Object
48 49 50 |
# File 'lib/active_record_api/request/faraday_auth_token_retry.rb', line 48 def token_cache @token_cache ||= TokenCache.new(debug: debug) end |