11 #include <boost/uuid/uuid.hpp>
12 #include <boost/uuid/uuid_generators.hpp>
13 #include <boost/uuid/uuid_io.hpp>
14 #include <boost/asio.hpp>
29 m_root_folder(root_folder), m_isRewrite(rewriteIfExists) {
39 CrestClient::CrestClient(
const std::string& _host,
const std::string& _port,
bool check_version) : m_host(_host), m_port(_port), m_mode(
41 if (check_version ==
true) {
48 size_t found =
url.find_first_of(
':');
49 size_t n =
url.size();
51 std::string_view url_new =
url.substr(
found + 3);
52 size_t found1 = url_new.find_first_of(
':');
53 size_t found2 = url_new.find_first_of(
'/');
55 std::string_view host;
56 std::string_view port;
57 std::string_view resources;
58 if (found1 != std::string::npos && found2 != std::string::npos) {
59 host = url_new.substr(0, found1);
60 port = url_new.substr(found1 + 1, found2 - found1 - 1);
61 }
else if (found1 != std::string::npos) {
62 host = url_new.substr(0, found1);
63 port = url_new.substr(found1 + 1);
64 }
else if (found2 != std::string::npos) {
66 host = url_new.substr(0, found2);
73 resources = url_new.substr(found2,
n - 1);
77 m_port = std::string(port);
78 m_host = std::string(host);
80 if (check_version ==
true) {
91 std::string
str(
"http://");
104 const char* method_name =
"CrestClient::listTags";
108 std::string current_path =
m_PATH;
122 const char* method_name =
"CrestClient::listTags";
126 int size_default = 1000;
127 int page_default = 0;
128 std::string size_param =
"";
129 std::string page_param =
"";
132 if (
size == size_default &&
page == page_default) {
137 if (
size != size_default) {
138 size_param =
"size=";
142 if (
page != page_default) {
143 page_param =
"page=";
145 if (page_param.empty()) {
153 std::string current_path =
m_PATH;
168 const char* method_name =
"CrestClient::listTags";
174 if (
tagname.find(
"%25") == std::string::npos) {
179 std::string current_path =
m_PATH;
183 std::string nameString =
"?name=";
185 current_path += nameString;
186 current_path +=
"&size=";
188 current_path +=
"&page=";
190 current_path +=
"&sort=";
191 current_path += sort;
193 current_path +=
"?size=";
195 current_path +=
"&page=";
197 current_path +=
"&sort=";
198 current_path += sort;
210 const char* method_name =
"removeTag";
214 std::string current_path =
m_PATH;
218 current_path += tagName;
227 const char* method_name =
"findTag";
248 {
"lastValidatedTime", 0}, {
"synchronization",
"none"}, {
"payloadSpec",
"json"},
249 {
"name",
name}, {
"timeType", timeType}};
254 const std::string& payloadSpec) {
256 {
"lastValidatedTime", 0}, {
"synchronization",
"none"}, {
"payloadSpec", payloadSpec},
257 {
"name",
name}, {
"timeType", timeType}};
263 const char* method_name =
"CrestClient::createTag";
270 std::string current_path =
m_PATH;
279 const char* method_name =
"CrestClient::updateTag";
283 std::string current_path =
m_PATH;
294 const char* method_name =
"CrestClient::updateTagSpecification";
298 std::string current_path =
m_PATH;
312 std::string
name = js[
"name"];
341 std::ofstream myfile;
342 myfile.open(tagFile.c_str());
358 std::ofstream myfile;
360 myfile.open(tagFile.c_str());
371 const char* method_name =
"CrestClient::createIov";
382 std::string
message =
"ERROR in ";
385 throw std::runtime_error(
message);
391 const char* method_name =
"CrestClient::findAllIovs";
397 std::string current_path =
m_PATH;
399 current_path +=
"?method=";
401 current_path +=
"&tagname=";
416 const std::string& dateformat) {
417 const char* method_name =
"CrestClient::findAllIovs";
427 std::string current_path =
m_PATH;
429 current_path +=
"?method=";
431 current_path +=
"&tagname=";
433 current_path +=
"&size=";
435 current_path +=
"&page=";
437 current_path +=
"&sort=";
438 current_path += sort;
439 current_path +=
"&dateformat=";
440 current_path += dateformat;
455 const char* method_name =
"CrestClient::getSize";
461 std::string current_path =
m_PATH;
464 current_path +=
"?tagname=";
479 if (respond.find(
"size") != respond.end()) {
480 int sizeR = respond[
"size"];
481 if (sizeR == 0)
return result;
484 if (
res.find(
"niovs") !=
res.end()) {
487 throw std::runtime_error(
"ERROR in CrestClient::getSize CREST Server JSON response has no \"size\" key.");
494 const char* method_name =
"CrestClient::selectIovs";
498 std::string current_path =
m_PATH;
500 current_path +=
"?method=";
502 current_path +=
"&tagname=";
518 const char* method_name =
"CrestClient::selectIovs";
522 std::string current_path =
m_PATH;
525 current_path +=
"?method=";
527 current_path +=
"&tagname=";
530 current_path +=
"&snapshot=";
545 const char* method_name =
"CrestClient::selectIovs";
549 std::string current_path =
m_PATH;
552 current_path +=
"?method=";
554 current_path +=
"&tagname=";
557 current_path +=
"&since=";
559 current_path +=
"&until=";
561 current_path +=
"INF";
565 current_path +=
"&snapshot=";
580 const char* method_name =
"CrestClient::selectGroups";
584 std::string current_path =
m_PATH;
587 current_path +=
"?method=";
589 current_path +=
"&tagname=";
605 const char* method_name =
"CrestClient::selectGroups";
609 std::string current_path =
m_PATH;
612 current_path +=
"?method=";
614 current_path +=
"&tagname=";
617 current_path +=
"&snapshot=";
631 const char* method_name =
"CrestClient::storeBatchIOVs";
656 {
"datatype",
"iovs"},
657 {
"format",
"IovSetDto"},
662 std::string current_path =
m_PATH;
673 const char* method_name =
"CrestClient::updateGlobalTag";
677 std::string current_path =
m_PATH;
681 current_path +=
name;
689 const char* method_name =
"CrestClient::createGlobalTag";
696 std::string current_path =
m_PATH;
703 const char* method_name =
"CrestClient::createGlobalTag";
716 {
"scenario",
"undefined"},
717 {
"workflow",
"undefined"},
725 const char* method_name =
"CrestClient::findGlobalTagAsString";
729 std::string
s = j.dump();
733 std::string current_path =
m_PATH;
736 current_path +=
name;
746 const char* method_name =
"CrestClient::findGlobalTag";
759 const char* method_name =
"CrestClient::removeGlobalTag";
763 std::string current_path =
m_PATH;
767 current_path +=
name;
774 const char* method_name =
"CrestClient::listGlobalTags";
778 std::string current_path =
m_PATH;
789 const char* method_name =
"CrestClient::listGlobalTagsAsString";
793 std::string current_path =
m_PATH;
806 const char* method_name =
"CrestClient::listGlobalTags";
812 if (
tagname.find(
"%25") == std::string::npos) {
817 std::string current_path =
m_PATH;
820 std::string nameString =
"?name=";
822 current_path += nameString;
823 current_path +=
"&size=";
825 current_path +=
"&page=";
827 current_path +=
"&sort=";
828 current_path += sort;
830 current_path +=
"?size=";
832 current_path +=
"&page=";
834 current_path +=
"&sort=";
835 current_path += sort;
850 const char* method_name =
"CrestClient::findGlobalTagMap";
856 std::string current_path =
m_PATH;
859 current_path +=
name;
873 const char* method_name =
"CrestClient::createGlobalTagMap";
880 std::string current_path =
m_PATH;
889 const std::string& record,
const std::string&
label) {
890 const char* method_name =
"CrestClient::createGlobalTagMap";
916 const char* method_name =
"CrestClient::getBlob";
922 std::string current_path =
m_PATH;
925 current_path +=
hash;
941 std::string current_path =
m_PATH;
944 current_path +=
hash;
957 const char* method_name =
"CrestClient::getPayloadMetaInfo";
963 std::string current_path =
m_PATH;
966 current_path +=
hash;
967 current_path +=
"?format=META";
981 const char* method_name =
"CrestClient::getPayloadMetaInfoAsString";
987 std::string current_path =
m_PATH;
990 current_path +=
hash;
991 current_path +=
"?format=META";
1003 const char* method_name =
"CrestClient::getPayloadTagInfo";
1007 std::string current_path =
m_PATH;
1009 current_path +=
"?tagname=";
1024 const char* method_name =
"CrestClient::getPayloadTagInfo";
1028 std::string current_path =
m_PATH;
1043 const char* method_name =
"CrestClient::getPayloadAsJson";
1049 std::string current_path =
m_PATH;
1051 current_path +=
'/';
1052 current_path +=
hash;
1053 current_path +=
"?format=DTO";
1067 const char* method_name =
"CrestClient::getPayloadAsString";
1073 std::string current_path =
m_PATH;
1075 current_path +=
'/';
1076 current_path +=
hash;
1077 current_path +=
"?format=BLOB";
1089 const char* method_name =
"CrestClient::createPayload";
1103 const char* method_name =
"CrestClient::storeBatchPayloads";
1110 const char* method_name =
"CrestClient::storeBatchPayloads";
1116 if (!js.is_array()) {
1117 throw std::runtime_error(
"ERROR in " + std::string(method_name) +
" JSON has wrong type (must be array)");
1121 jsObj[
"datatype"] =
"iovs";
1122 jsObj[
"format"] =
"StoreSetDto";
1123 jsObj[
"size"] = js.size();
1124 jsObj[
"resources"] = js;
1125 std::string
str = jsObj.dump();
1132 const char* method_name =
"CrestClient::storeBatchPayloads";
1139 const char* method_name =
"CrestClient::storeBatchPayloads";
1145 if (!js.is_array()) {
1146 throw std::runtime_error(
"ERROR in " + std::string(method_name) +
" JSON has wrong type (must be array)");
1150 jsObj[
"datatype"] =
"iovs";
1151 jsObj[
"format"] =
"StoreSetDto";
1152 jsObj[
"size"] = js.size();
1153 jsObj[
"resources"] = js;
1154 std::string
str = jsObj.dump();
1164 const char* method_name =
"CrestClient::listFolders";
1182 const char* method_name =
"CrestClient::createFolder";
1199 const char* method_name =
"CrestClient::listRunLumiInfo";
1217 const char* method_name =
"CrestClient::createRunLumiInfo";
1229 const char* method_name =
"CrestClient::findRunLumiInfo";
1233 std::string current_path =
m_PATH;
1236 current_path +=
'?';
1237 current_path +=
params.getParams();
1265 data.push_back(itemD);
1274 size_t newLength =
size * nmemb;
1280 catch (std::bad_alloc&
e)
1290 const char* method_name) {
1297 std::string sanitisedPath(current_path);
1298 auto caretPosition = sanitisedPath.find(
'^');
1299 if (caretPosition != std::string::npos) sanitisedPath.replace(caretPosition, 1,
"%5E");
1301 curl = curl_easy_init();
1303 struct curl_slist*
headers = NULL;
1307 std::cout <<
"cURL request to " <<
url << std::endl;
1313 curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
1319 curl_easy_setopt(curl, CURLOPT_URL,
url.c_str());
1321 if (
action ==
DELETE) curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST,
"DELETE");
1325 if (
action ==
PUT) curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST,
"PUT");
1327 headers = curl_slist_append(
headers,
"Content-Type: application/json");
1330 curl_easy_setopt(curl, CURLOPT_HTTPHEADER,
headers);
1331 curl_easy_setopt(curl, CURLOPT_POST, 1L);
1334 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, stt.c_str());
1338 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &
s);
1342 res = curl_easy_perform(curl);
1347 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
1350 curl_easy_cleanup(curl);
1353 curl_global_cleanup();
1361 throw std::runtime_error(std::string(
"ERROR in ") + std::string(method_name) +
" | CURL not init");
1367 const char*
method =
"Unknown";
1379 std::ofstream&
out) {
1381 boost::asio::io_service io_service;
1390 boost::asio::ip::tcp::socket socket(io_service);
1393 std::string retv =
"Invalid response\n";
1395 boost::asio::streambuf request;
1396 std::ostream request_stream(&request);
1398 std::string cmd_line =
"";
1400 else if (
action ==
PUT) cmd_line =
"PUT";
1404 request_stream << cmd_line <<
" " << current_path <<
" HTTP/1.0\r\n";
1405 request_stream <<
"Host: " <<
m_host <<
':' <<
m_port <<
" \r\n";
1406 request_stream <<
"Accept: */*\r\n";
1407 request_stream <<
"Connection: close\r\n\r\n";
1409 std::string
s = js.dump();
1410 request_stream <<
action <<
" " << current_path <<
" HTTP/1.0\r\n";
1411 request_stream <<
"Host: " <<
m_host <<
':' <<
m_port <<
" \r\n";
1412 request_stream <<
"Content-Type: application/json; \r\n";
1413 request_stream <<
"Accept: */*\r\n";
1414 request_stream <<
"Content-Length: " <<
s.length() <<
"\r\n";
1415 request_stream <<
"Connection: close\r\n\r\n";
1416 request_stream <<
s;
1429 boost::asio::read_until(socket,
response,
"\r\n");
1433 boost::asio::read_until(socket,
response,
"\r\n");
1436 std::istream response_stream(&
response);
1437 std::string http_version;
1438 response_stream >> http_version;
1439 unsigned int status_code;
1440 response_stream >> status_code;
1442 std::string status_message;
1443 std::getline(response_stream, status_message);
1446 if (!response_stream || http_version.substr(0, 5) !=
"HTTP/") {
1447 std::cout <<
"Invalid response" << std::endl;
1448 std::cout <<
"response_stream : " << status_code << std::endl;
1451 if (status_code != 200) {
1452 std::cout <<
"Response returned with status code " << status_code << std::endl;
1458 boost::asio::read_until(socket,
response,
"\r\n\r\n");
1462 while (std::getline(response_stream,
header) &&
header !=
"\r") {
1463 std::cout <<
header << std::endl;
1465 std::cout << std::endl;
1467 std::ostringstream
txt;
1476 boost::asio::transfer_at_least(1),
error))
1479 if (
error != boost::asio::error::eof) {
1480 std::cerr <<
"connection error" << std::endl;
1481 std::string st0 =
"";
1490 catch (boost::system::system_error&
e) {
1491 std::cerr <<
e.what() <<
" | " <<
e.code() << std::endl;
1492 std::string st0 =
"";
1496 std::cerr <<
"connection error" << std::endl;
1497 std::string st0 =
"";
1506 std::string current_path =
m_PATH;
1514 curl_global_init(CURL_GLOBAL_DEFAULT);
1515 curl = curl_easy_init();
1517 struct curl_slist*
headers = NULL;
1526 curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
1536 curl_easy_setopt(curl, CURLOPT_URL,
url.c_str());
1537 headers = curl_slist_append(
headers,
"X-Crest-PayloadFormat: JSON");
1539 headers = curl_slist_append(
headers,
"Content-Type: multipart/form-data");
1541 curl_easy_setopt(curl, CURLOPT_HTTPHEADER,
headers);
1544 #if LIBCURL_VERSION_MAJOR < 8
1545 struct curl_httppost* formpost = NULL;
1546 struct curl_httppost* lastptr = NULL;
1547 curl_formadd(&formpost,
1549 CURLFORM_COPYNAME,
"tag",
1550 CURLFORM_COPYCONTENTS,
tag.c_str(),
1553 curl_mime* mime = curl_mime_init(curl);
1555 curl_mimepart*
part = curl_mime_addpart(mime);
1556 curl_mime_name(
part,
"tag");
1557 curl_mime_data(
part,
tag.c_str(),
tag.size());
1561 #if LIBCURL_VERSION_MAJOR < 8
1562 curl_formadd(&formpost,
1564 CURLFORM_COPYNAME,
"endtime",
1569 curl_mimepart*
part = curl_mime_addpart(mime);
1570 curl_mime_name(
part,
"endtime");
1571 curl_mime_data(
part, endtime_s.c_str(), endtime_s.size());
1574 #if LIBCURL_VERSION_MAJOR < 8
1575 curl_formadd(&formpost,
1577 CURLFORM_COPYNAME,
"storeset",
1578 CURLFORM_COPYCONTENTS, js.c_str(),
1579 CURLFORM_CONTENTTYPE,
"application/json",
1581 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
1584 curl_mimepart*
part = curl_mime_addpart(mime);
1585 curl_mime_name(
part,
"storeset");
1586 curl_mime_type(
part,
"application/json");
1587 curl_mime_data(
part, js.c_str(), js.size());
1589 curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
1593 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &
s);
1596 res = curl_easy_perform(curl);
1600 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
1601 const char* method_name =
"CrestClient::storeBatchPayloads";
1604 curl_easy_cleanup(curl);
1605 #if LIBCURL_VERSION_MAJOR < 8
1606 curl_formfree(formpost);
1610 curl_mime_free(mime);
1613 curl_global_cleanup();
1620 std::string mes =
"ERROR in CrestClient::storeBatchPayloads";
1621 throw std::runtime_error(mes +
" | CURL not init");
1636 catch (nlohmann::json::parse_error&
e) {
1640 std::string
wh =
e.what();
1641 throw std::runtime_error(
"ERROR in JSON conversion: " +
wh +
" | In string: " +
str);
1645 throw std::runtime_error(
"ERROR in " + std::string(
method) +
" | CREST Server response : " + str3);
1659 std::string plainString =
"";
1662 for (
long unsigned int i = 0;
i < xmlBuffer.length();
i++) {
1663 char convertc = xmlBuffer[
i];
1665 if (convertc ==
'<')
copy =
false;
1666 else if (convertc ==
'>') {
1671 if (
copy) plainString += convertc;
1678 std::string str2 =
str;
1682 while ((
pos = str2.find(needle)) != str2.npos) {
1693 catch (nlohmann::json::parse_error&
e) {
1699 const char* method_name) {
1702 if (
res != CURLE_OK) {
1703 std::string mes =
"ERROR in ";
1718 if (response_code >= 400 || response_code == 303) {
1721 std::string mes =
"ERROR in ";
1724 throw std::runtime_error(mes +
"CREST Server response : " +
s);
1734 std::ifstream ifs(
path);
1735 return std::string((std::istreambuf_iterator<char>(ifs)),
1736 (std::istreambuf_iterator<char>()));
1741 for (
auto i = std::filesystem::directory_iterator(
p);
i != std::filesystem::directory_iterator();
i++) {
1742 std::string
file =
i->path().filename().string();
1743 if (
file !=
"data") {
1744 std::cout <<
file << std::endl;
1752 for (
auto i = std::filesystem::directory_iterator(
p);
i != std::filesystem::directory_iterator();
i++) {
1753 std::string
file =
i->path().filename().string();
1755 if (
file !=
"data") {
1775 throw std::runtime_error(
"ERROR in CrestClient::findTagFs cannot get the tag information form file storage.");
1795 throw std::runtime_error(
"ERROR in CrestClient::findAllIovsFs : cannot get the iov information form file storage");
1813 std::ifstream in(
"./test-03.txt");
1814 std::ofstream
fout(
"./copy.txt");
1815 if (in.is_open() &&
fout.is_open()) {
1816 while (getline(in,
line)) {
1827 const char* method_name =
"CrestClient::storeBatchPayloadsFs";
1830 if (!js.is_array()) {
1831 throw std::runtime_error(
"ERROR in CrestClient::storeBatchPayloadsFs: JSON has wrong type (must be array");
1838 const char* method_name =
"CrestClient::storeBatchPayloadsFs";
1841 for (
auto& kvp : js) {
1842 std::string
payload = kvp.value(
"data",
"");
1843 int since = kvp.value(
"since", 0);
1848 throw std::runtime_error(
"ERROR in " + std::string(method_name) +
" cannot store the data in a file");
1857 std::string respond =
"";
1862 if (pPath != NULL) respond = std::string(pPath);
1867 const char*
varName =
"DAT_PATH";
1870 respond +=
"/data/crestapi";
1877 auto res = js.find(
"type");
1881 if (
res != js.end()) {
1882 std::string
type = js.value(
"type",
" unknown type ");
1883 if (
type ==
"error" ||
type ==
"info") {
1884 std::string
message = js.value(
"message",
" unknown message ");
1885 throw std::runtime_error(
"ERROR in " + std::string(
method) +
" | CREST response: " +
message);
1891 auto r1 = js.find(
"error");
1892 auto r2 = js.find(
"message");
1894 if (r1 != js.end()) {
1895 std::string
error = js.value(
"error",
" unknown error ");
1896 if (r2 != js.end()) {
1897 std::string
message = js.value(
"message",
"");
1899 throw std::runtime_error(
"ERROR in " + std::string(
method) +
" | CREST response: " +
error);
1901 throw std::runtime_error(
"ERROR in " + std::string(
method) +
" | CREST response: " +
message);
1910 throw std::runtime_error(
"ERROR in " + std::string(
1911 method_name) +
" This methods is unsupported for FILESYSTEM mode");
1938 int array_length = js.size();
1939 for (
int i = 0;
i < array_length;
i++) {
1941 if (
auto itr = elem.find(
name); itr != elem.end()) {
1949 std::string jsName =
"tagInfo";
1950 const char* method_name =
"CrestClient::convertTagMetaInfo2IOVDbSvc";
1958 std::string tagInfo = js.dump();
1961 result[
"tagInfo"] = std::move(tagInfo);
1962 result[
"description"] =
"none";
1964 std::string node_desc = js[
"node_description"];
1965 std::string payload_spec = js[
"payload_spec"];
1967 int chan_size = chan_list.size();
1968 result[
"chansize"] = chan_size;
1971 colsize =
split(payload_spec,
',').size();
1977 result[
"colsize"] = colsize;
1983 std::vector<std::string>
tokens;
1984 std::size_t prev = 0,
pos = 0;
1986 pos =
str.find(delim, prev);
1987 if (
pos == std::string::npos)
pos =
str.length();
1988 std::string_view token =
str.substr(prev,
pos - prev);
1989 if (!token.empty())
tokens.emplace_back(token);
1991 }
while (
pos <
str.length() && prev <
str.length());
1999 const char* method_name =
"CrestClient::createTagMetaInfo";
2008 tagname =
static_cast<std::string
> (js[
"tagName"]);
2011 throw std::runtime_error(
2012 "ERROR in CrestClient::createTagMetaInfo cannot get the tag name from tag meta info JSON.");
2015 std::string current_path =
m_PATH;
2017 current_path +=
'/';
2029 std::string
name = js[
"tagName"];
2031 throw std::runtime_error(
"ERROR in CrestClient::createTagMetaInfo tagname in method and in JSON are different.");
2038 const char* method_name =
"CrestClient::getTagMetaInfo";
2044 std::string current_path =
m_PATH;
2046 current_path +=
'/';
2061 const char* method_name =
"CrestClient::updateTagMetaInfo";
2065 tagname =
static_cast<std::string
> (js[
"tagName"]);
2068 throw std::runtime_error(
2069 "ERROR in CrestClient::updateTagMetaInfo cannot get the tag name from tag meta info JSON.");
2086 const char* method_name =
"CrestClient::updateTagMetaInfo";
2089 std::string
name =
"";
2091 name =
static_cast<std::string
> (js[
"tagName"]);
2094 throw std::runtime_error(
2095 "ERROR in CrestClient::updateTagMetaInfo cannot get the tag name from tag meta info JSON.");
2099 throw std::runtime_error(
"ERROR in CrestClient::updateTagMetaInfo tagname in method and in JSON are different.");
2106 std::string current_path =
m_PATH;
2108 current_path +=
'/';
2121 const char* method_name =
"CrestClient::updateTagMetaInfo";
2135 const char* method_name =
"CrestClient::createTagMetaInfoIOVDbSvc";
2149 const char* method_name =
"CrestClient::createTagMetaInfoIOVDbSvc";
2166 std::string
name =
"";
2168 name =
static_cast<std::string
> (js[
"tagName"]);
2171 throw std::runtime_error(
2172 "ERROR in CrestClient::createTagMetaInfoFs cannot get the tag name from tag meta info JSON.");
2183 std::string tagMetaFile = workDir;
2195 std::ofstream myfile;
2197 myfile.open(tagMetaFile.c_str());
2217 throw std::runtime_error(
2218 "ERROR in CrestClient::getTagMetaInfoFs cannot get the tag meta information form file storage.");
2226 auto it = js.find(
"tagName");
2228 if (
it != js.end()) {
2229 if (js[
"tagName"] !=
tagname) {
2230 throw std::runtime_error(
"ERROR in CrestClient::updateTagMetaInfoFs: JSON contains wrong tag name.");
2239 throw std::runtime_error(
2240 "ERROR in CrestClient::updateTagMetaInfoFs: the file storage does not contain the tag meta info for the tag " +
tagname +
2245 for (
auto it = js.begin();
it != js.end();
it++) {
2246 jstmp[
it.key()] =
it.value();
2253 throw std::runtime_error(
2254 "ERROR in CrestClient::updateTagMetaInfoFs: cannot write the tag meta info for the tag " +
tagname +
2255 " on file storage.");
2267 std::string
name =
"";
2269 name =
static_cast<std::string
>(js[
"name"]);
2272 throw std::runtime_error(
"ERROR in CrestClient::createGlobalTagFs: cannot get the global tag name from JSON.");
2277 std::string workDir = gTagDir;
2280 std::string globalTagFile = workDir;
2297 std::ofstream myfile;
2299 myfile.open(globalTagFile.c_str());
2322 throw std::runtime_error(
2323 "ERROR in CrestClient::getGlobalTagFs: cannot get the global tag " +
name +
" form the file storage.");
2331 std::string
name =
"";
2333 name =
static_cast<std::string
>(js[
"globalTagName"]);
2336 throw std::runtime_error(
"ERROR in CrestClient::createGlobalTagMapFs: cannot get the global tag name from JSON.");
2342 tagname =
static_cast<std::string
>(js[
"tagName"]);
2345 throw std::runtime_error(
"ERROR in CrestClient::createGlobalTagMapFs: cannot get the tag name from JSON.");
2348 std::string
fname =
"/";
2354 std::string workDir = gTagDir;
2378 int m = cathalogue.size();
2379 for (
int i = 0;
i <
m;
i++) {
2380 const std::string& tn = cathalogue[
i][
"tagName"];
2382 cathalogue.erase(
i);
2389 cathalogue.push_back(js);
2390 std::ofstream myfile;
2393 myfile << cathalogue;
2399 cathalogue.push_back(js);
2401 std::ofstream myfile;
2404 myfile << cathalogue;
2426 throw std::runtime_error(
2427 "ERROR in CrestClient::getGlobalTagMapFs: cannot get the global tag map " +
name +
2428 " form the file storage.");
2443 return hash_hex_str;
2447 std::ofstream myfile;
2450 std::string hashCode =
getHash(js);
2458 workDir += hashCode;
2463 std::string tagFile = workDir +
"/payload.json";
2465 myfile.open(tagFile);
2485 {
"objectType",
"JSON"},
2486 {
"version",
"default"},
2487 {
"size", js.size()},
2488 {
"streamerInfo",
"none"},
2494 std::string metaFile = workDir +
"/meta.json";
2496 myfile.open(metaFile);
2497 myfile << jsn.dump();
2505 m_data.insert(std::pair<std::string, nlohmann::json>(
tag, jsi));
2511 m_data.insert(std::pair<std::string, nlohmann::json>(
tag, jsFree));
2514 throw std::runtime_error(
2515 "ERROR in CrestClient::storePayloadDump cannot get data for tag \"" +
tag +
"\" from file storage.");
2522 std::string link = hashCode;
2527 obj[
"insertionTime"] = 0;
2528 obj[
"payloadHash"] = link;
2539 localtime_r(&
now, &tstruct);
2540 strftime(buf,
sizeof(buf),
"%Y-%m-%d %X", &tstruct);
2553 std::string
filePath = workDir +
"/payload.json";
2554 std::string
res =
"";
2560 throw std::runtime_error(
"ERROR in CrestClient::getPayloadAsStringFS cannot get the payload form file storage.");
2582 std::string
res =
"";
2588 throw std::runtime_error(
"ERROR in CrestClient::getPayloadAsStringFS cannot get the payload form file storage.");
2606 throw std::runtime_error(
"ERROR in CrestClient::getBlobFs : cannot get the blob information form file storage");
2612 std::string
str =
"";
2625 if (in.is_open() &&
out.is_open()) {
2627 while (getline(in,
line)) {
2631 throw std::runtime_error(
2632 "ERROR in CrestClient::getBlobInStreamFs : cannot get the blob information form file storage");
2675 if (kmin >
dataSize - 1)
return js;
2683 for (
int i = kmin;
i < kmax;
i++) {
2684 js.push_back(
data[
i]);
2695 for (
int i = 0;
i < niovs;
i++) {
2698 long currentS =
iovList[
i][
"since"];
2699 if (currentS >=
since && currentS <=
until) {
2706 throw std::runtime_error(
"ERROR in CrestClient::selectIovsFS : cannot get the iov list form file storage");
2713 std::cout << std::endl <<
"test: getTagDataInfo" << std::endl;
2717 std::cout << std::endl <<
"IOV Number (" <<
tagname <<
") = "
2718 <<
info << std::endl;
2721 std::cout << std::endl <<
"test: getTagDataInfo (failed)" << std::endl;
2722 std::cout <<
e.what() << std::endl;
2728 std::list <std::string> hashList;
2730 for (
int i = 0;
i < niovs;
i++) {
2734 pos =
find(hashList.begin(), hashList.end(),
hash);
2735 if (
pos != hashList.end()) {
2737 hashList.push_back(
hash);
2742 int hashNumber = hashList.size();
2743 std::cout <<
"Hash Number (" <<
tagname <<
") = "
2744 << hashNumber << std::endl;
2747 std::cout << std::endl <<
"ERROR getTagDataInfo: cannot get IOV List" << std::endl;
2748 std::cout <<
e.what() << std::endl;
2754 for (
auto it : std::filesystem::directory_iterator(
path)) {
2755 dirNumber = dirNumber + 1;
2757 std::cout <<
"Directory Number (" <<
tagname <<
") = "
2758 << dirNumber << std::endl;
2768 const char* method_name =
"getMgmtInfo";
2784 const char* method_name =
"getCrestVersion";
2790 auto subjectIdIter1 =
info.find(
"build");
2791 if (subjectIdIter1 !=
info.end()){
2793 auto subjectIdIter2 =
build.find(
"version");
2794 if (subjectIdIter2 !=
build.end()){
2798 throw std::runtime_error(
"ERROR in CrestClient::" + (std::string) method_name +
": CREST Server response has no \"version\" key.");
2803 throw std::runtime_error(
"ERROR in CrestClient::" + (std::string) method_name +
": CREST Server response has no \"build\" key.");
2815 int n =
str.find(
'.');
2818 throw std::runtime_error(
"ERROR in CrestClient::getMajorVersion: string \"" +
str +
"\" does not contain major version.");
2821 std::string vers =
str.substr(0,
n);
2824 v = std::stoi(
str );
2827 throw std::runtime_error(
"ERROR in CrestClient::getMajorVersion: string \"" +
str +
"\" does not contain major version.");
2840 if (clientVersion != serverVersion) {
2841 throw std::runtime_error(
"ERROR in CrestClient::checkCrestVersion: CrestApi version \"" +
client +
"\" does not correspond to the server version \"" +
server +
"\".");
2848 const char* method_name =
"getMgmtInfo2";
2864 const char* method_name =
"getCrestVersion";
2870 auto subjectIdIter1 =
info.find(
"build");
2871 if (subjectIdIter1 !=
info.end()){
2873 auto subjectIdIter2 =
build.find(
"version");
2874 if (subjectIdIter2 !=
build.end()){
2878 throw std::runtime_error(
"ERROR in CrestClient::" + (std::string) method_name +
": CREST Server response has no \"version\" key.");
2883 throw std::runtime_error(
"ERROR in CrestClient::" + (std::string) method_name +
": CREST Server response has no \"build\" key.");
2896 if (clientVersion != serverVersion) {
2897 throw std::runtime_error(
"ERROR in CrestClient::checkCrestVersion: CrestApi version \"" +
client +
"\" does not correspond to the server version \"" +
server +
"\".");
2904 const char* method_name =
"CrestClient::removeTagFromGlobalTagMap";
2908 std::string current_path =
m_PATH;
2910 current_path +=
'/';
2911 current_path += global_tag;
2912 current_path +=
'?';
2913 current_path +=
"tagname=";
2914 current_path +=
tag;
2915 current_path +=
"&label=";
2916 current_path +=
label;
2926 const char* method_name =
"CrestClient::storeBatchPayloads";
2932 if (!js.is_array()) {
2933 throw std::runtime_error(
"ERROR in " + std::string(method_name) +
" JSON has wrong type (must be array)");
2937 jsObj[
"datatype"] =
"iovs";
2938 jsObj[
"format"] =
"StoreSetDto";
2939 jsObj[
"size"] = js.size();
2940 jsObj[
"resources"] = js;
2941 std::string
str = jsObj.dump();
2949 std::string current_path =
m_PATH;
2955 auto subjectIdIter1 = json0.find(
"resources");
2956 if (subjectIdIter1 != json0.end()){
2957 resources = json0[
"resources"];
2960 int partN = resources.size();
2967 curl_global_init(CURL_GLOBAL_DEFAULT);
2968 curl = curl_easy_init();
2970 struct curl_slist*
headers = NULL;
2979 curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
2989 curl_easy_setopt(curl, CURLOPT_URL,
url.c_str());
2991 headers = curl_slist_append(
headers,
"X-Crest-PayloadFormat: FILE");
2993 headers = curl_slist_append(
headers,
"Content-Type: multipart/form-data");
2995 curl_easy_setopt(curl, CURLOPT_HTTPHEADER,
headers);
2998 #if LIBCURL_VERSION_MAJOR < 8
2999 struct curl_httppost* formpost = NULL;
3000 struct curl_httppost* lastptr = NULL;
3001 curl_formadd(&formpost,
3003 CURLFORM_COPYNAME,
"tag",
3004 CURLFORM_COPYCONTENTS,
tag.c_str(),
3007 curl_mime* mime = curl_mime_init(curl);
3009 curl_mimepart*
part = curl_mime_addpart(mime);
3010 curl_mime_name(
part,
"tag");
3011 curl_mime_data(
part,
tag.c_str(),
tag.size());
3015 #if LIBCURL_VERSION_MAJOR < 8
3016 curl_formadd(&formpost,
3018 CURLFORM_COPYNAME,
"endtime",
3023 curl_mimepart*
part = curl_mime_addpart(mime);
3024 curl_mime_name(
part,
"endtime");
3025 curl_mime_data(
part, endtime_s.c_str(), endtime_s.size());
3028 #if LIBCURL_VERSION_MAJOR < 8
3029 curl_formadd(&formpost,
3031 CURLFORM_COPYNAME,
"storeset",
3032 CURLFORM_COPYCONTENTS, js.c_str(),
3035 for (
int i = 0;
i < partN;
i++){
3037 std::string file_param;
3039 auto subjectIdIter1 = element.find(
"data");
3040 if (subjectIdIter1 != element.end()){
3041 file_param = element[
"data"];
3044 int found_dots = file_param.find_first_of(
':');
3045 int word_size = file_param.size();
3046 std::string data_file = file_param.substr(found_dots + 3, word_size);
3048 curl_formadd(&formpost, &lastptr,
3049 CURLFORM_COPYNAME,
"files",
3050 CURLFORM_FILE, data_file.c_str(),
3055 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
3058 curl_mimepart*
part = curl_mime_addpart(mime);
3059 curl_mime_name(
part,
"storeset");
3060 curl_mime_data(
part, js.c_str(), js.size());
3063 for (
int i = 0;
i < partN;
i++){
3065 std::string file_param;
3067 auto subjectIdIter1 = element.find(
"data");
3068 if (subjectIdIter1 != element.end()){
3069 file_param = element[
"data"];
3072 int found_dots = file_param.find_first_of(
':');
3073 int word_size = file_param.size();
3074 std::string data_file = file_param.substr(found_dots + 3, word_size);
3077 curl_mimepart*
part = curl_mime_addpart(mime);
3078 curl_mime_name(
part,
"files");
3079 curl_mime_filedata(
part, data_file.c_str());
3084 curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
3088 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &
s);
3091 res = curl_easy_perform(curl);
3095 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
3096 const char* method_name =
"CrestClient::storeBatchPayloads";
3099 curl_easy_cleanup(curl);
3100 #if LIBCURL_VERSION_MAJOR < 8
3101 curl_formfree(formpost);
3105 curl_mime_free(mime);
3108 curl_global_cleanup();
3115 std::string mes =
"ERROR in CrestClient::storeBatchPayloads";
3116 throw std::runtime_error(mes +
" | CURL not init");
3120 const char* method_name =
"CrestClient::storePayloadAsFile";
3122 std::string
d =
"file://" +
file;
3134 for(
it=
m.begin();
it !=
m.end();++
it){
3139 {
"since",
it->first},
3143 payload_data.push_back(js_element);
3159 while((
pos = subject.find(
search,
pos)) != std::string::npos) {