class Idlc::AWSLambdaProxy

def fetch(request)

def fetch(request)
  client = Aws::Lambda::Client.new()
  region = ENV['AWS_REGION']
  backend_services = Idlc::SERVICES[region]
  request[:function] = "#{request[:service]}-" + backend_services[request[:service]]['stage'] + "-#{request[:lambda]}"
  request[:httpMethod] = request[:method]
  retries = 0
  max_retries = 5
  sleep_time = 5
  exp_backoff = 2
  result = nil
  loop do
    begin
      resp = client.invoke({
        function_name: "service-lambda-proxy",
        invocation_type: "RequestResponse",
        log_type: "None",
        payload: request.to_json,
      })
      result = JSON.parse(JSON.parse(JSON.parse(resp.payload.string)['Payload'])['body'])
      break
    rescue Exception => e
      break if retries >= max_retries
      retries += 1
      msg("RequestFailed: #{e} - Waiting #{sleep_time}s then retrying... (#{retries} of #{max_retries})")
      sleep sleep_time
      sleep_time *= exp_backoff # use an exponential backoff when retrying requests
    end
  end
  result
end