Repository navigation
Expand file tree
/
Copy pathRestClient.cpp
More file actions
141 lines (115 loc) · 4.63 KB
/
Copy pathRestClient.cpp
File metadata and controls
141 lines (115 loc) · 4.63 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright (c) 2024 Quantag IT Solutions GmbH
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "RestClient.h"
#include "Log.h"
#include "Utils.h"
#include <curl/curl.h>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
RestClient::RestClient() : url(PYTHON_EXECUTER_ENDPONT_URL) {
curl_global_init(CURL_GLOBAL_ALL);
headers = curl_slist_append(headers, "Content-Type: application/json");
}
RestClient::RestClient(const char* _url) : url(_url) {
curl_global_init(CURL_GLOBAL_ALL);
headers = curl_slist_append(headers, "Content-Type: application/json");
}
RestClient::~RestClient() {
curl_global_cleanup();
}
std::string RestClient::doPost(const std::string& jsonData, std::string* error) {
CURL* curl;
CURLcode res;
std::string response;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonData.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)jsonData.length());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
// Without limits a hanging executor blocks the debug launch forever
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, connectTimeoutSec);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeoutSec);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L); // timeouts in multi-threaded programs
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
LOGE("curl_easy_perform() failed: %s [%s]", curl_easy_strerror(res), url.c_str());
if (error) {
*error = std::string("Python executor ") + url + " did not respond: " + curl_easy_strerror(res);
if (res == CURLE_OPERATION_TIMEDOUT) {
*error += " (limit " + std::to_string(timeoutSec) + " s)";
}
}
response.clear();
}
curl_easy_cleanup(curl);
}
return response;
}
size_t RestClient::writeCallback(void* contents, size_t size, size_t nmemb, std::string* output) {
output->append((char*)contents, size * nmemb);
return size * nmemb;
}
ScriptExecResult RestClient::execCode(const std::string& code) {
LOGD("'%s' endpoint = [%s]", code.c_str(), url.c_str());
std::string encoded = Utils::encode64(code);
std::string req = "{\"src\":\"" + encoded + "\"}";
LOGD("req: '%s'", req.c_str());
std::string error;
std::string res = doPost(req, &error);
LOGD("response '%s'", res.c_str());
return parseExecResponse(res, error);
}
// Response of the Python executor: {"status": 0, "res": <base64>} or {"status": N, "err": ...}
ScriptExecResult RestClient::parseExecResponse(const std::string& res, const std::string& error) {
ScriptExecResult result;
result.status = 0;
if (!error.empty()) {
result.status = 2;
result.err = error;
LOGE("%s", error.c_str());
return result;
}
try {
json j = json::parse(res);
result.status = j["status"];
if (result.status != 0) {
result.err = j.value("err", std::string("Python executor returned status ") + std::to_string(result.status));
LOGE("Remote script execution failed [%s]", result.err.c_str());
}
else {
std::string encodedRes = j["res"];
result.res = Utils::decode64(encodedRes);
}
return result;
}
catch (std::exception& e) {
LOGE("Error during parsing JSON response: '%s'", res.c_str());
result.status = 3;
result.err = std::string("Invalid response from the Python executor ") + url + ": " + e.what();
return result;
}
}
ScriptExecResult RestClient::execCode(const std::string& code, const std::string& env) {
LOGI("'%s' endpoint = [%s] venv=[%s]", code.c_str(), url.c_str(), env.c_str());
std::string encoded = Utils::encode64(code);
json j;
j["src"] = encoded;
if (!env.empty()) {
j["env"] = env;
}
std::string req = j.dump();
LOGD("req: '%s'", req.c_str());
std::string error;
std::string res = doPost(req, &error);
LOGD("response '%s'", res.c_str());
return parseExecResponse(res, error);
}