4#include "nlohmann/json.hpp"
13map<int, string>
const Errors{{401,
"Unauthorized Access (check user login and password)"},
14 {403,
"Token invalid or expired"},
15 {404,
"Requested Data not found"},
16 {409,
"Incorrect Database Request (e.g. violating constraints)"},
17 {422,
"Incorrect Input Request"},
18 {500,
"Internal Server Error"},
19 {503,
"Service Unavaialble"},
20 {504,
"Server Timeout"}};
25 return it !=
Errors.end() ? it->second :
"";
29 : EventAttribute(attribute)
30 , EventMatchCriteria(condition)
35 : EventAttribute(attribute)
36 , EventMatchCriteria(condition)
42 : EventAttribute(attribute)
43 , EventMatchCriteria(condition)
49 : EventAttribute(attribute)
50 , EventMatchCriteria(condition)
56 : EventAttribute(attribute)
57 , EventMatchCriteria(condition)
63EventCondition::EventCondition()
64 : EventAttribute(kError)
65 , EventMatchCriteria(kEqual)
72 curl_global_init(CURL_GLOBAL_DEFAULT);
73 curlInstance = curl_easy_init();
76 throw runtime_error(
"CURL initialisation failed");
84 cout <<
"DEBUG: ServiceUrl = " << ServiceUrl <<
", ServiceAccount = " << ServiceAccount << endl;
90 curl_easy_cleanup(curlInstance);
91 curl_global_cleanup();
102 vector<unique_ptr<EventInfo>> event_array;
103 cout <<
"ERROR: EventCatalogue::GetEventList is not implemented yet" << endl;
117 unique_ptr<EventInfo> event_info;
118 cout <<
"ERROR: EventCatalogue::GetEventInfo is not implemented yet" << endl;
134 cout <<
"ERROR: EventCatalogue::AddEventInfo is not implemented yet" << endl;
141 size_t realsize = size * nmemb;
143 data->append((
char*)contents, realsize);
144 }
catch (std::bad_alloc& e) {
152static size_t header_callback(
char* buffer,
size_t size,
size_t nitems,
void* userdata)
154 string* headers = (
string*)userdata;
156 headers->append(buffer, nitems * size);
157 }
catch (std::bad_alloc& e) {
162 return nitems * size;
172 ifstream json_file(json_file_path);
174 sstr << json_file.rdbuf();
175 string json_string = sstr.str();
176 if (VerboseLevel > 2)
177 cout <<
"DEBUG: json_string = " << json_string << endl;
179 string success_message =
180 "Event metadata have been written from JSON file '" + json_file_path +
"' to the Event Catalogue";
181 int return_code = PostRequest(ServiceUrl +
"event", json_string, success_message);
197 cout <<
"ERROR: EventCatalogue::UpdateEventInfo is not implemented yet" << endl;
212 cout <<
"ERROR: EventCatalogue::DeleteEventInfo is not implemented yet" << endl;
227 cout <<
"ERROR: EventCatalogue::CheckEventInfo is not implemented yet" << endl;
242 cout <<
"ERROR: EventCatalogue::PrintEventInfo is not implemented yet" << endl;
258 string json_string =
"{\"storage_id\": -1, \"storage_name\": \"" + storage_name +
"\"}";
259 if (VerboseLevel > 2)
260 cout <<
"DEBUG: json_string = " << json_string << endl;
262 string success_message =
"Storage information has been added for '" + storage_name +
"' to the Event Catalogue";
263 int return_code = PostRequest(ServiceUrl +
"storage", json_string, success_message);
279 string json_string =
"{\"software_id\": -1, \"software_version\": \"" + software_version +
"\"}";
280 if (VerboseLevel > 2)
281 cout <<
"ERROR: json_string = " << json_string << endl;
283 string success_message =
284 "Software version has been added for '" + software_version +
"' version to the Event Catalogue";
285 int return_code = PostRequest(ServiceUrl +
"software", json_string, success_message);
298 vector<unique_ptr<EventInfo>> event_array;
299 cout <<
"ERROR: EventCatalogue::EventCatalogueRequest is not implemented yet" << endl;
312 cout <<
"ERROR: EventCatalogue::EventCatalogueChecks is not implemented yet" << endl;
324 cout <<
"ERROR: EventCatalogue::EventCatalogueStats is not implemented yet" << endl;
329int EventCatalogue::PostRequest(
string RequestUrl,
string json_string,
string success_message)
331 struct curl_slist* http_header =
nullptr;
332 http_header = curl_slist_append(http_header,
"Accept: */*");
333 http_header = curl_slist_append(http_header,
"Content-Type: application/json");
334 http_header = curl_slist_append(http_header,
"charset: utf-8");
335 curl_easy_setopt(curlInstance, CURLOPT_HTTPHEADER, http_header);
336 curl_easy_setopt(curlInstance, CURLOPT_URL, RequestUrl.c_str());
337 curl_easy_setopt(curlInstance, CURLOPT_USERPWD, ServiceAccount.c_str());
344 curl_easy_setopt(curlInstance, CURLOPT_POST, 1);
345 curl_easy_setopt(curlInstance, CURLOPT_POSTFIELDS, json_string.c_str());
347 string response_string, header_string;
349 curl_easy_setopt(curlInstance, CURLOPT_WRITEDATA, &response_string);
350 curl_easy_setopt(curlInstance, CURLOPT_HEADERFUNCTION, header_callback);
351 curl_easy_setopt(curlInstance, CURLOPT_HEADERDATA, &header_string);
354 int res = curl_easy_perform(curlInstance);
356 cout <<
"ERROR: sending command to '" << RequestUrl <<
"' failed with code = " << res << endl;
357 curl_slist_free_all(http_header);
360 curl_slist_free_all(http_header);
363 curl_easy_getinfo(curlInstance, CURLINFO_RESPONSE_CODE, &response_code);
364 if (VerboseLevel > 2) {
366 curl_easy_getinfo(curlInstance, CURLINFO_TOTAL_TIME, &elapsed);
368 curl_easy_getinfo(curlInstance, CURLINFO_EFFECTIVE_URL, &url);
369 cout <<
"DEBUG: CURLINFO_TOTAL_TIME = " << elapsed <<
", CURLINFO_EFFECTIVE_URL = " <<
string(url) << endl;
372 if ((response_code >= 200) && (response_code < 205)) {
373 if (VerboseLevel > 0)
374 cout << success_message << endl;
375 if (VerboseLevel > 1)
376 cout <<
"Request has been completed with the following response: " << response_string << endl;
379 cout <<
"ERROR: Sending request to '" << RequestUrl <<
"' has been failed";
380 if (error_message !=
"")
381 cout <<
": " << error_message << endl;
383 cout <<
" with code = " << response_code << endl;
384 if (VerboseLevel > 1)
385 cout <<
"Error Message:" << endl
386 << response_string << endl
387 <<
"Error Output Header:" << endl
388 << header_string << endl;
389 return -1 * response_code;
map< int, string > const Errors
size_t response_callback(void *contents, size_t size, size_t nmemb, string *data)
string const GetErrorString(int error_id)
virtual ~EventCatalogue()
int DeleteEventInfo(string storage_name, string file_path, int event_number)
int AddSoftwareInfo(string software_version)
int UpdateEventInfo(string storage_name, string file_path, int event_number, EventInfo event_metadata)
vector< unique_ptr< EventInfo > > EventCatalogueRequest(string rest_api_request)
int AddEventInfo(string storage_name, string file_path, int event_number, EventInfo event_metadata)
int CheckEventInfo(string storage_name, string file_path, int event_number)
int AddStorageInfo(string storage_name)
int EventCatalogueStats(int &record_count)
int EventCatalogueChecks(int &file_not_found, vector< unique_ptr< EventInfo > > &event_list)
vector< unique_ptr< EventInfo > > GetEventList(vector< unique_ptr< EventCondition > > event_conditions)
int PrintEventInfo(string storage_name, string file_path, int event_number)
unique_ptr< EventInfo > GetEventInfo(string storage_name, string file_path, int event_number)
searchAttributes
enumeration for attributes, which can be used in searching event metadata
matchCriteria
enumeration for conditions, which can be used for matching with a specified value
EventCondition(searchAttributes attribute, matchCriteria condition)
Class presenting Event Info (Metadata) Structure.
static const int Version
structure (API) version
a class to store JSON values
iterator find(KeyT &&key)
find an element in a JSON object
const char *const EVENT_API_HOST
const char *const EVENT_API_NAME
const char *const EVENT_API_PASSWORD
const char *const EVENT_API_USERNAME
basic_json< nlohmann::ordered_map > ordered_json
specialization that maintains the insertion order of object keys