class Insights::API::Common::Request

Constants

FORWARDABLE_HEADER_KEYS
IDENTITY_KEY
OPTIONAL_AUTH_PATHS
PERSONA_KEY
REQUEST_ID_KEY

Attributes

headers[R]
original_url[R]

Public Class Methods

current() click to toggle source
# File lib/insights/api/common/request.rb, line 24
def self.current
  Thread.current[:current_request]
end
current!() click to toggle source
# File lib/insights/api/common/request.rb, line 36
def self.current!
  current || raise(RequestNotSet)
end
current=(request) click to toggle source
# File lib/insights/api/common/request.rb, line 40
def self.current=(request)
  Thread.current[:current_request] =
    case request
    when ActionDispatch::Request
      new(:headers => request.headers, :original_url => request.original_url)
    when Hash
      new(request)
    when Request, nil
      request
    else
      raise ArgumentError, 'Not an Insights::API::Common::Request or ActionDispatch::Request Class, Hash, or nil'
    end
end
current_forwardable() click to toggle source
# File lib/insights/api/common/request.rb, line 68
def self.current_forwardable
  current!.forwardable
end
current_request_id() click to toggle source
# File lib/insights/api/common/request.rb, line 28
def self.current_request_id
  Thread.current[:request_id]
end
current_request_id=(id) click to toggle source
# File lib/insights/api/common/request.rb, line 32
def self.current_request_id=(id)
  Thread.current[:request_id] = id
end
new(headers:, original_url:, **_kwargs) click to toggle source
# File lib/insights/api/common/request.rb, line 74
def initialize(headers:, original_url:, **_kwargs)
  headers = from_hash(headers) if headers.kind_of?(Hash)
  @headers, @original_url = headers, original_url
end
with_request(request) { |current| ... } click to toggle source
# File lib/insights/api/common/request.rb, line 54
def self.with_request(request)
  saved = current
  saved_request_id = current&.request_id
  self.current = request
  self.current_request_id = current&.request_id
  yield current
rescue => exception
  Rails.logger.error("#{exception.class.name}: #{exception.message}\n#{exception.backtrace.join("\n")}")
  raise
ensure
  self.current = saved
  self.current_request_id = saved_request_id
end

Public Instance Methods

auth_type() click to toggle source
# File lib/insights/api/common/request.rb, line 101
def auth_type
  identity.dig("identity", "auth_type")
end
entitlement() click to toggle source
# File lib/insights/api/common/request.rb, line 105
def entitlement
  @entitlement ||= Entitlement.new(identity)
end
forwardable() click to toggle source
# File lib/insights/api/common/request.rb, line 113
def forwardable
  FORWARDABLE_HEADER_KEYS.each_with_object({}) do |key, hash|
    hash[key] = headers[key] if headers.key?(key)
  end
end
identity() click to toggle source
# File lib/insights/api/common/request.rb, line 83
def identity
  @identity ||= JSON.parse(Base64.decode64(headers.fetch(IDENTITY_KEY)))
rescue KeyError
  raise IdentityError, "x-rh-identity not found"
end
optional_auth?() click to toggle source
# File lib/insights/api/common/request.rb, line 123
def optional_auth?
  uri_path = URI.parse(original_url).path
  OPTIONAL_AUTH_PATHS.any? { |optional_auth_path_regex| optional_auth_path_regex.match(uri_path) }
end
request_id() click to toggle source
# File lib/insights/api/common/request.rb, line 79
def request_id
  headers.fetch(REQUEST_ID_KEY, nil)
end
required_auth?() click to toggle source
# File lib/insights/api/common/request.rb, line 119
def required_auth?
  !optional_auth?
end
system() click to toggle source
# File lib/insights/api/common/request.rb, line 97
def system
  @system ||= System.new(identity) if identity.dig("identity", "system").present?
end
tenant() click to toggle source
# File lib/insights/api/common/request.rb, line 89
def tenant
  @tenant ||= Insights::API::Common::Tenant.new(identity).tenant
end
to_h() click to toggle source
# File lib/insights/api/common/request.rb, line 109
def to_h
  {:headers => forwardable, :original_url => original_url}
end
user() click to toggle source
# File lib/insights/api/common/request.rb, line 93
def user
  @user ||= User.new(identity)
end

Private Instance Methods

from_hash(hash) click to toggle source
# File lib/insights/api/common/request.rb, line 130
def from_hash(hash)
  ActionDispatch::Http::Headers.from_hash({}).tap do |headers|
    hash.each { |k, v| headers.add(k, v) }
  end
end