Class: ApiValve::Forwarder::Request

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(original_request, options = {}) ⇒ Request



23
24
25
26
# File 'lib/api_valve/forwarder/request.rb', line 23

def initialize(original_request, options = {})
  @original_request = original_request
  @options = options.with_indifferent_access
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/api_valve/forwarder/request.rb', line 10

def options
  @options
end

#original_requestObject (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

#bodyObject

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

#headersObject

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

#methodObject

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

#pathObject

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 = options['endpoint'] || ''
  if (override = override_path(options))
    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_paramsObject

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