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.



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

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.



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

def headers
  @headers
end

#original_urlObject (readonly)

Returns the value of attribute original_url.



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

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



65
66
67
# File 'lib/insights/api/common/request.rb', line 65

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
# 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
ensure
  self.current = saved
  self.current_request_id = saved_request_id
end

Instance Method Details

#auth_typeObject



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

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

#entitlementObject



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

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

#forwardableObject



110
111
112
113
114
# File 'lib/insights/api/common/request.rb', line 110

def forwardable
  FORWARDABLE_HEADER_KEYS.each_with_object({}) do |key, hash|
    hash[key] = headers[key] if headers.key?(key)
  end
end

#identityObject



80
81
82
83
84
# File 'lib/insights/api/common/request.rb', line 80

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)


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

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



76
77
78
# File 'lib/insights/api/common/request.rb', line 76

def request_id
  headers.fetch(REQUEST_ID_KEY, nil)
end

#required_auth?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/insights/api/common/request.rb', line 116

def required_auth?
  !optional_auth?
end

#systemObject



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

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

#tenantObject



86
87
88
# File 'lib/insights/api/common/request.rb', line 86

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

#to_hObject



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

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

#userObject



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

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