Class: ApiValve::Forwarder::Request
- Inherits:
-
Object
- Object
- ApiValve::Forwarder::Request
- Includes:
- PermissionHandler::RequestIntegration
- Defined in:
- lib/api_valve/forwarder/request.rb
Overview
This class is wraps the original request. It’s methods are called by the Forwarder to make the actual request in the target endpoint. So by changing the public methods in this call, we can control how the request is forwarded
Constant Summary collapse
- WHITELISTED_HEADERS =
%w( Accept Content-Type User-Agent X-Real-IP ).freeze
- NOT_PREFIXED_HEADERS =
%w( Content-Length Content-Type ).freeze
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#original_request ⇒ Object
readonly
Returns the value of attribute original_request.
Instance Method Summary collapse
-
#body ⇒ Object
Returns body to forward to the target endpoint Override to control the payload that is passed through.
-
#headers ⇒ Object
Returns a hash of headers to forward to the target endpoint Override to control the HTTP headers that will be passed through.
-
#initialize(original_request, options = {}) ⇒ Request
constructor
A new instance of Request.
-
#method ⇒ Object
HTTP method to use when forwarding.
-
#path ⇒ Object
URL path to use when forwarding.
-
#url_params ⇒ Object
Returns query params to forward to the target endpoint Override to control the query parameters that can be passed through.
Constructor Details
#initialize(original_request, options = {}) ⇒ Request
23 24 25 26 |
# File 'lib/api_valve/forwarder/request.rb', line 23 def initialize(original_request, = {}) @original_request = original_request = .with_indifferent_access end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/api_valve/forwarder/request.rb', line 10 def end |
#original_request ⇒ Object (readonly)
Returns the value of attribute original_request.
10 11 12 |
# File 'lib/api_valve/forwarder/request.rb', line 10 def original_request @original_request end |
Instance Method Details
#body ⇒ Object
Returns body to forward to the target endpoint Override to control the payload that is passed through
61 62 63 64 65 |
# File 'lib/api_valve/forwarder/request.rb', line 61 def body return unless i(put post patch).include? method original_request.body.read end |
#headers ⇒ Object
Returns a hash of headers to forward to the target endpoint Override to control the HTTP headers that will be passed through
51 52 53 54 55 56 57 |
# File 'lib/api_valve/forwarder/request.rb', line 51 def headers whitelisted_headers.each_with_object({}) do |key, h| h[key] = header(key) end.merge(forwarded_headers).merge( 'X-Request-Id' => Thread.current[:request_id] ).compact end |
#method ⇒ Object
HTTP method to use when forwarding. Must return sym. Returns original request method
33 34 35 |
# File 'lib/api_valve/forwarder/request.rb', line 33 def method @method ||= original_request.request_method.downcase.to_sym end |
#path ⇒ Object
URL path to use when forwarding
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/api_valve/forwarder/request.rb', line 38 def path path = ['endpoint'] || '' if (override = override_path()) path += override else path += original_request.path_info end # we remove leading slash so we can use endpoints with deeper folder levels path.gsub(%r{^/}, '') end |
#url_params ⇒ Object
Returns query params to forward to the target endpoint Override to control the query parameters that can be passed through
69 70 71 72 73 |
# File 'lib/api_valve/forwarder/request.rb', line 69 def url_params return unless original_request.query_string.present? @url_params ||= Rack::Utils.parse_nested_query(original_request.query_string) end |