Class: Insights::API::Common::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/insights/api/common/request.rb

Constant Summary collapse

REQUEST_ID_KEY =
"x-rh-insights-request-id".freeze
IDENTITY_KEY =
'x-rh-identity'.freeze
PERSONA_KEY =
'x-rh-persona'.freeze
FORWARDABLE_HEADER_KEYS =
[REQUEST_ID_KEY, IDENTITY_KEY, PERSONA_KEY].freeze
OPTIONAL_AUTH_PATHS =
[
  %r{\A/api/v[0-9]+(\.[0-9]+)?/openapi.json\z},
  %r{\A/api/[^/]+/v[0-9]+(\.[0-9]+)?/openapi.json\z},
  %r{\A/health\z}
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers:, original_url:, **_kwargs) ⇒ Request

Returns a new instance of Request.



74
75
76
77
# 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

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



72
73
74
# File 'lib/insights/api/common/request.rb', line 72

def headers
  @headers
end

#original_urlObject (readonly)

Returns the value of attribute original_url.



72
73
74
# File 'lib/insights/api/common/request.rb', line 72

def original_url
  @original_url
end

Class Method Details

.currentObject



24
25
26
# File 'lib/insights/api/common/request.rb', line 24

def self.current
  Thread.current[:current_request]
end

.current!Object



36
37
38
# File 'lib/insights/api/common/request.rb', line 36

def self.current!
  current || raise(RequestNotSet)
end

.current=(request) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# 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_forwardableObject



68
69
70
# File 'lib/insights/api/common/request.rb', line 68

def self.current_forwardable
  current!.forwardable
end

.current_request_idObject



28
29
30
# File 'lib/insights/api/common/request.rb', line 28

def self.current_request_id
  Thread.current[:request_id]
end

.current_request_id=(id) ⇒ Object



32
33
34
# File 'lib/insights/api/common/request.rb', line 32

def self.current_request_id=(id)
  Thread.current[:request_id] = id
end

.with_request(request) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# 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

Instance Method Details

#auth_typeObject



101
102
103
# File 'lib/insights/api/common/request.rb', line 101

def auth_type
  identity.dig("identity", "auth_type")
end

#entitlementObject



105
106
107
# File 'lib/insights/api/common/request.rb', line 105

def entitlement
  @entitlement ||= Entitlement.new(identity)
end

#forwardableObject



113
114
115
116
117
# 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

#identityObject



83
84
85
86
87
# 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?Boolean

Returns:

  • (Boolean)


123
124
125
126
# 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_idObject



79
80
81
# File 'lib/insights/api/common/request.rb', line 79

def request_id
  headers.fetch(REQUEST_ID_KEY, nil)
end

#required_auth?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/insights/api/common/request.rb', line 119

def required_auth?
  !optional_auth?
end

#systemObject



97
98
99
# File 'lib/insights/api/common/request.rb', line 97

def system
  @system ||= System.new(identity) if identity.dig("identity", "system").present?
end

#tenantObject



89
90
91
# File 'lib/insights/api/common/request.rb', line 89

def tenant
  @tenant ||= Insights::API::Common::Tenant.new(identity).tenant
end

#to_hObject



109
110
111
# File 'lib/insights/api/common/request.rb', line 109

def to_h
  {:headers => forwardable, :original_url => original_url}
end

#userObject



93
94
95
# File 'lib/insights/api/common/request.rb', line 93

def user
  @user ||= User.new(identity)
end