class EjabberdApiClient
  def initialize
    @base_url = Setting.ejabberd_api_url
  end

  def post(endpoint, payload)
    res = Faraday.post("#{@base_url}/#{endpoint}", payload.to_json,
                       "Content-Type" => "application/json")

    if res.status != 200
      Rails.logger.error "[ejabberd] API request failed:"
      Rails.logger.error res.body
      #TODO Send custom event to Sentry
    end
  end

  def add_rosteritem(payload)
    post "add_rosteritem", payload
  end

  def send_message(payload)
    post "send_message", payload
  end

  def private_set(user, content)
    payload = { user: user.cn, host: user.ou, element: content }
    post "private_set", payload
  end
end
