78 lines
2.0 KiB
C++
78 lines
2.0 KiB
C++
#include <pistache/client.h>
|
|
#include <rapidjson/document.h>
|
|
|
|
#include <argparse.hpp>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
|
|
namespace rj = rapidjson;
|
|
|
|
Pistache::Http::Response REQUEST(std::string url, std::string payload = "")
|
|
{
|
|
Pistache::Http::Experimental::Client client;
|
|
client.init();
|
|
Pistache::Http::Response response;
|
|
auto reqSpec = (payload.empty() ? client.get(url) : client.post(url));
|
|
reqSpec.timeout(std::chrono::seconds(2));
|
|
if (!payload.empty()) {
|
|
reqSpec.body(payload);
|
|
}
|
|
auto request = reqSpec.send();
|
|
bool ok = false, error = false;
|
|
std::string msg;
|
|
request.then(
|
|
[&](Pistache::Http::Response rsp) {
|
|
ok = true;
|
|
response = rsp;
|
|
},
|
|
[&](std::exception_ptr ptr) {
|
|
error = true;
|
|
try {
|
|
std::rethrow_exception(ptr);
|
|
}
|
|
catch (std::exception &e) {
|
|
msg = e.what();
|
|
}
|
|
});
|
|
|
|
Pistache::Async::Barrier<Pistache::Http::Response> barrier(request);
|
|
barrier.wait_for(std::chrono::seconds(2));
|
|
client.shutdown();
|
|
if (error) {
|
|
throw std::runtime_error(msg);
|
|
}
|
|
return response;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
argparse::ArgumentParser args("Daggy Client");
|
|
|
|
args.add_argument("-v", "--verbose")
|
|
.default_value(false)
|
|
.implicit_value(true);
|
|
args.add_argument("--url")
|
|
.help("base URL of server")
|
|
.default_value("http://localhost:2503");
|
|
args.add_argument("--sync").default_value(false).implicit_value(true).help(
|
|
"Poll for job to complete");
|
|
args.add_argument("--action")
|
|
.help("Number of tasks to run concurrently")
|
|
.default_value(30)
|
|
.action([](const std::string &value) { return std::stoull(value); });
|
|
|
|
try {
|
|
args.parse_args(argc, argv);
|
|
}
|
|
catch (std::exception &e) {
|
|
std::cout << "Error: " << e.what() << std::endl;
|
|
std::cout << args;
|
|
exit(1);
|
|
}
|
|
|
|
std::string baseURL = args.get<std::string>("--url");
|
|
|
|
auto response = REQUEST(baseURL + "/ready");
|
|
}
|