Class: Insights::API::Common::Request
- Inherits:
-
Object
- Object
- Insights::API::Common::Request
- 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
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#original_url ⇒ Object
readonly
Returns the value of attribute original_url.
Class Method Summary collapse
- .current ⇒ Object
- .current! ⇒ Object
- .current=(request) ⇒ Object
- .current_forwardable ⇒ Object
- .with_request(request) ⇒ Object
Instance Method Summary collapse
- #auth_type ⇒ Object
- #entitlement ⇒ Object
- #forwardable ⇒ Object
- #identity ⇒ Object
-
#initialize(headers:, original_url:, **_kwargs) ⇒ Request
constructor
A new instance of Request.
- #optional_auth? ⇒ Boolean
- #request_id ⇒ Object
- #required_auth? ⇒ Boolean
- #system ⇒ Object
- #tenant ⇒ Object
- #to_h ⇒ Object
- #user ⇒ Object
Constructor Details
#initialize(headers:, original_url:, **_kwargs) ⇒ Request
Returns a new instance of Request.
60 61 62 63 |
# File 'lib/insights/api/common/request.rb', line 60 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
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
58 59 60 |
# File 'lib/insights/api/common/request.rb', line 58 def headers @headers end |
#original_url ⇒ Object (readonly)
Returns the value of attribute original_url.
58 59 60 |
# File 'lib/insights/api/common/request.rb', line 58 def original_url @original_url end |
Class Method Details
.current ⇒ Object
24 25 26 |
# File 'lib/insights/api/common/request.rb', line 24 def self.current Thread.current[:current_request] end |
.current! ⇒ Object
28 29 30 |
# File 'lib/insights/api/common/request.rb', line 28 def self.current! current || raise(RequestNotSet) end |
.current=(request) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/insights/api/common/request.rb', line 32 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 ⇒ Object
54 55 56 |
# File 'lib/insights/api/common/request.rb', line 54 def self.current_forwardable current!.forwardable end |
.with_request(request) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/insights/api/common/request.rb', line 46 def self.with_request(request) saved = current self.current = request yield current ensure self.current = saved end |
Instance Method Details
#auth_type ⇒ Object
87 88 89 |
# File 'lib/insights/api/common/request.rb', line 87 def auth_type identity.dig("identity", "auth_type") end |
#entitlement ⇒ Object
91 92 93 |
# File 'lib/insights/api/common/request.rb', line 91 def entitlement @entitlement ||= Entitlement.new(identity) end |
#forwardable ⇒ Object
99 100 101 102 103 |
# File 'lib/insights/api/common/request.rb', line 99 def forwardable FORWARDABLE_HEADER_KEYS.each_with_object({}) do |key, hash| hash[key] = headers[key] if headers.key?(key) end end |
#identity ⇒ Object
69 70 71 72 73 |
# File 'lib/insights/api/common/request.rb', line 69 def identity @identity ||= JSON.parse(Base64.decode64(headers.fetch(IDENTITY_KEY))) rescue KeyError raise IdentityError, "x-rh-identity not found" end |
#optional_auth? ⇒ Boolean
109 110 111 112 |
# File 'lib/insights/api/common/request.rb', line 109 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 ⇒ Object
65 66 67 |
# File 'lib/insights/api/common/request.rb', line 65 def request_id headers.fetch(REQUEST_ID_KEY, nil) end |
#required_auth? ⇒ Boolean
105 106 107 |
# File 'lib/insights/api/common/request.rb', line 105 def required_auth? !optional_auth? end |
#system ⇒ Object
83 84 85 |
# File 'lib/insights/api/common/request.rb', line 83 def system @system ||= System.new(identity) if identity.dig("identity", "system").present? end |
#tenant ⇒ Object
75 76 77 |
# File 'lib/insights/api/common/request.rb', line 75 def tenant @tenant ||= Insights::API::Common::Tenant.new(identity).tenant end |
#to_h ⇒ Object
95 96 97 |
# File 'lib/insights/api/common/request.rb', line 95 def to_h {:headers => forwardable, :original_url => original_url} end |