class WCC::Data::FaradayClientAppTokenAuth

Attributes

cache[R]
token_factory[R]

Public Class Methods

new(app, cache: default_cache, token_factory: method(:default_token_factory)) click to toggle source
Calls superclass method
# File lib/wcc/data/faraday_client_app_token_auth.rb, line 37
def initialize(app, cache: default_cache, token_factory: method(:default_token_factory))
  super(app)
  @cache = cache
  @token_factory = token_factory
end

Public Instance Methods

call(env, retry_on_forbidden: true) click to toggle source
# File lib/wcc/data/faraday_client_app_token_auth.rb, line 47
def call(env, retry_on_forbidden: true)
  request_body = env[:body]
  host = env[:url].host
  env[:request_headers]["Authorization"] = "Bearer #{token_for(host)}"
  @app.call(env).on_complete do |response_env|
    response = Faraday::Response.new(response_env)
    cache[host] = nil unless response.success?
    if response.status == 403 && retry_on_forbidden
      env[:body] = request_body
      call(env, retry_on_forbidden: false)
    end
  end
end
token_for(host) click to toggle source
# File lib/wcc/data/faraday_client_app_token_auth.rb, line 43
def token_for(host)
  @cache[host] ||= token_factory.(host)
end

Private Instance Methods

default_cache() click to toggle source
# File lib/wcc/data/faraday_client_app_token_auth.rb, line 67
def default_cache
  RedisCache.new(Sidekiq.method(:redis))
end
default_token_factory(host) click to toggle source
# File lib/wcc/data/faraday_client_app_token_auth.rb, line 63
def default_token_factory(host)
  WCC::Data::Nucleus::ClientAppToken.create(hostname: host).token
end