/* Copyright 2020, Michele Santullo
 * This file is part of orotool.
 *
 * Orotool is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Orotool is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Orotool.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "quick_rest.hpp"
#include "private/page_fetch.hpp"
#include <cassert>

namespace nap {

QuickRest::QuickRest (std::size_t body_padding) :
	m_body_padding(body_padding)
{
}

void QuickRest::add_headers (std::initializer_list<HeaderPairView> headers) {
	m_header_lines.reserve(m_header_lines.size() + headers.size());

	for (const auto& entry : headers) {
		std::string line;
		line.reserve(entry.first.size() + 2 + entry.second.size());
		line.append(entry.first);
		line.append(": ");
		line.append(entry.second);
		m_header_lines.push_back(std::move(line));
	}
}

void QuickRest::set_user_agent (std::string&& name) {
	m_user_agent = std::move(name);
}

HttpResponse QuickRest::fetch (std::string_view url) {
	return page_fetch(std::string(url), m_user_agent, m_header_lines, m_body_padding);
}
} //namespace nap
