-
Notifications
You must be signed in to change notification settings - Fork 0
/
edsmapiv1.cpp
77 lines (67 loc) · 2.1 KB
/
edsmapiv1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "edsmapiv1.h"
#include "utils/exec_exit.h"
#include "utils/strutils.h"
#include "point.h"
#include <iostream>
#include <thread>
#define ERROR(MSG) throw std::runtime_error(MSG)
EdsmApiV1::~EdsmApiV1()
{
threads.stop(true);
}
void EdsmApiV1::executeRequest(const std::string &api, const RestClient::parameters ¶ms, bool is_get, EdsmApiV1::callback_t callback, int timeout_seconds)
{
using namespace nlohmann;
const auto url = (utility::strcontains(api, "https://")) ? api : stringfmt("https://www.edsm.net/api-v1/%s", api);
//TODO: uncoment to print GET link
//std::cout << "API: " << url << std::endl;
const auto eparams{RestClient::encodePOSTParameters(params)};
if (!is_get)
{
callback("POST not implemented yet", {});
return;
}
++working;
const auto executor = [url, eparams, callback, is_get, this, timeout_seconds](auto id)
{
(void)id;
// std::cout << "Running task on thread " << id << "Tasks in q: " << tasksCount() << std::endl;
exec_onexit ensure([this]()
{
--this->working;
});
(void)ensure;
try
{
if (is_get)
{
const auto res_url{stringfmt("%s?%s", url, eparams)};
for (int i = 0; i < 50; ++i)
{
const auto resp2{RestClient::get(res_url, timeout_seconds)};
if (resp2.body.empty())
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
else
{
const auto j2{json::parse(resp2.body)};
callback("", j2.at(0)); //for some reason json parser adds extra array
return;
}
}
ERROR("Result didn't come in time.");
}
}
catch (const std::exception& e)
{
callback(e.what(), {});
}
};
threads.push(executor, []()
{
return true;
});
}
void EdsmApiV1::clearAllPendings()
{
threads.clear_queue();
}