class Idlc::AWSRestClient

def fetch(request)

def fetch(request)
  request = JSON.parse(request)
  endpoint = 'https://' + @backend_services[request['service']]['endpoint'] + '/' + @backend_services[request['service']]['stage']
  body = ''
  body = request['body'].to_json if request['body']
  retries = 0
  max_retries = 5
  sleep_time = 5
  exp_backoff = 2
  result = nil
  loop do
    begin
      resp = send_signed_request(
        request['method'],
        "#{endpoint.strip}#{request['path']}",
        body
      )
      result = JSON.parse(resp.body)
      message = "status: #{resp.code}, response: #{result}"
      raise message unless resp.code == '200'
      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
  # if request has 'outfile' param, write response body to file
  to_file(result, request['outfile']) if request['outfile']
  # return response body obj
  result
end