#include "Request.h"

#include "httplib/include/httplib.h"

#include "Licensed.h"

Request::Request() noexcept {
}

Request::~Request() {
}

Request::Ptr Request::Create() {
	Ptr self(new Request());
	return self;
}

void Request::Check() {
	/*Licensed licensed;
	std::string key = licensed.GetKey();

	rapidjson::StringBuffer buffer;
	rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
	writer.StartObject();
	writer.Key("code");
	writer.String(key.c_str(), key.length());
	writer.EndObject();

	std::string body = buffer.GetString();
	Post("/token", body);*/
}

bool Request::Post(const std::string& url, const std::string& body, const std::string& contentType) {
	httplib::Headers headers;
	headers.emplace("Content-Type", contentType);
	headers.emplace("Content-Length", std::to_string(body.size()));
	//httplib::Client cli("http://127.0.0.1:7001");

	httplib::Client cli("http://weixin.api.myzxysw.com");
	auto res = cli.Post(url, headers, body, contentType);
	if (httplib::Error::Success != res.error()) {
		m_success = false;
		return false;
	}

	m_success = true;
	const httplib::Response& response = res.value();
	m_responseBody = response.body;
	return true;
}