Newer
Older
PrismSoftware / ECTrainer2 / REST_Handler.cpp
#include "REST_Handler.h"

http_response REST_Handler::POST_Request(const TCHAR* baseURL,
	json::value data) {

	http_client httpClient(baseURL);
	http_response httpResponse = httpClient.request(methods::POST, L"",
		data == json::value::Null ? _T("") : data.serialize(), L"application/json").get();
	return httpResponse;
}

std::wstring REST_Handler::WaitForStatus(
	const TCHAR* baseURL, 
	std::wstring key, 
	std::vector<std::wstring> values) {
	int count = 0;
	bool running = true;
	int maxTries = 20;
	json::value json;
	std::wcout << baseURL << std::endl;
	while (running) {
		auto response = GET_Request(baseURL);
		json = response.extract_json().get();
		std::wcout << _T("Status: ") << json[key].as_string() << std::endl;
		if (count >= maxTries) running = false;
		else {
			for (int i = 0; i < values.size(); i++) {
				if (json[key].as_string().compare(values[i]) == 0) {
					running = false;
				}
			}
		}
		if (!running) break;
		Sleep(1000);
		count++;
	}
	return json[key].as_string();
}

http_response REST_Handler::GET_Request(const TCHAR* baseURL) {
	http_client httpClient(baseURL);
	http_response httpResponse = httpClient.request(methods::GET, L"", L"", L"application/json").get();
	return httpResponse;
}