module NFAgent
  class Client
     SERVICE_HOST = "collector.service.netfox.com"

    def self.post(end_point, data_hash)
      proxy_class = Net::HTTP::Proxy(Config.proxy.host, Config.proxy.port, Config.proxy.user, Config.proxy.password)
      # TODO: Enable SSL
      proxy_class.start(SERVICE_HOST, 80) do |http|
        http.read_timeout = 240 # 4 minutes TODO: Make this a config option with 240 as default
        req = Net::HTTP::Post.new("/#{end_point}")
        req.set_form_data(data_hash.merge("key" => Config.client_key))
        ClientResponse.new do |resp|
          resp.response, resp.message = http.request(req)
        end
      end
    rescue Exception => e
      # Trap Exception class here to ensure we catch Timeout
      ClientResponse.new do |resp|
        resp.message = $!
      end
    end
  end
end