class FDK::Call

Call represents a call to the target function or lambda

Constants

FILTER_HEADERS

Attributes

error[RW]
request[R]
response[R]

Public Class Methods

new(request:, response:) click to toggle source
# File lib/fdk/call.rb, line 27
def initialize(request:, response:)
  @request = request
  @response = response
end

Public Instance Methods

context() click to toggle source
# File lib/fdk/call.rb, line 32
def context
  @context ||= FDK::Context.new(headers_in, headers_out)
end
error_response(error:) click to toggle source
# File lib/fdk/call.rb, line 72
def error_response(error:)
  response["content-type"] = "application/json"
  response.status = 502
  response.body = { message: "An error occurred in the function",
                    detail: error.to_s }.to_json
end
filtered_request_header() click to toggle source
# File lib/fdk/call.rb, line 48
def filtered_request_header
  request.header.reject { |k| FILTER_HEADERS.include? k }
end
format_response_body(fn_return:) click to toggle source
# File lib/fdk/call.rb, line 60
def format_response_body(fn_return:)
  return response.body = fn_return.to_s unless fn_return.respond_to?(:to_json)

  response.body = fn_return.to_json
  response["content-type"] = "application/json" unless response["content-type"]
end
good_response() click to toggle source
# File lib/fdk/call.rb, line 67
def good_response
  response.status = 200
  headers_out.each { |k, v| response[k] = v.join(",") }
end
headers_in() click to toggle source
# File lib/fdk/call.rb, line 44
def headers_in
  @headers_in ||= FDK::InHeaders.new(filtered_request_header, nil)
end
headers_out() click to toggle source
# File lib/fdk/call.rb, line 40
def headers_out
  @headers_out ||= FDK::OutHeaders.new({}, nil)
end
input() click to toggle source
# File lib/fdk/call.rb, line 36
def input
  @input ||= ParsedInput.new(raw_input: request.body.to_s)
end
process() { |context: context, input: parsed| ... } click to toggle source
# File lib/fdk/call.rb, line 52
def process
  format_response_body(fn_return: yield(context: context, input: input.parsed))
  good_response
rescue StandardError => e
  FDK.log_error(error: e)
  error_response(error: e)
end