15 return std::vector<std::string>();
18 return getRestContainers(
id);
27 return std::vector<std::string>();
30 return getRestItems(
id);
42 return restContainsContainer(
id, key);
54 return restContainsItem(
id, key);
66 writeItemToRest(
id, key,
data);
75 return std::vector<unsigned char>();
80 return readItemFromRest(
id, key);
83 return std::vector<unsigned char>();
88 defs->optional(host_, prefix +
"host");
89 defs->optional(port_, prefix +
"port");
94 std::string path =
"/ltm";
96 auto res = client_->Get(path);
100 ARMARX_IMPORTANT <<
"MemoryX-REST server is running at host=" << host_ <<
", port=" << port_;
105 ARMARX_IMPORTANT <<
"MemoryX-REST server is not running at host=" << host_ <<
", port=" << port_;
110 void RestPersistence::resetClient()
117 client_ = std::make_shared<httplib::Client>(host_, port_);
122 std::string path = buildPath(
id);
123 std::string query =
"item=" + key;
124 std::string url = path +
"?" + query;
126 ARMARX_DEBUG <<
"GET request: Read data from MemoryX-REST server: item=" << key <<
", url=" << url;
128 auto res = client_->Get(url);
134 ARMARX_DEBUG <<
"GET request successful: REST-LTM contain item with key=" << key;
141 ARMARX_DEBUG <<
"GET request successful: REST-LTM does not contain item with key=" << key;
148 ARMARX_DEBUG <<
"GET request failed. Status: " << res->status;
153 ARMARX_DEBUG <<
"GET request failed. No connection could be established to the MemoryX-REST server at host=" << host_ <<
", port=" << port_;
161 std::string path = buildPath(
id);
162 std::string query =
"container=" + key;
163 std::string url = path +
"?" + query;
165 ARMARX_DEBUG <<
"GET request: Read data from MemoryX-REST server: container=" << key <<
", url=" << url;
167 auto res = client_->Get(url);
173 ARMARX_DEBUG <<
"GET request successful: REST-LTM contain container with key=" << key;
180 ARMARX_DEBUG <<
"GET request successful: REST-LTM does not contain container with key=" << key;
187 ARMARX_DEBUG <<
"GET request failed. Status: " << res->status;
192 ARMARX_DEBUG <<
"GET request failed. No connection could be established to the MemoryX-REST server at host=" << host_ <<
", port=" << port_;
200 std::string path = buildPath(
id);
201 std::string query =
"items";
202 std::string url = path +
"?" + query;
204 ARMARX_DEBUG <<
"GET request: Get items from MemoryX-REST server: url=" << url;
206 auto res = client_->Get(url);
208 std::vector<std::string> item_keys;
216 nlohmann::json json_response = nlohmann::json::parse(res->body);
218 if (json_response.contains(
"keys") && json_response[
"keys"].is_array())
220 item_keys = json_response[
"keys"].get<std::vector<std::string>>();
223 catch (
const std::exception& e)
225 ARMARX_DEBUG <<
"GET request: Error parsing JSON: " << e.what();
230 ARMARX_DEBUG <<
"GET request failed. status=" << res->status <<
", url=" << url;
234 ARMARX_DEBUG <<
"GET request failed. No connection could be established to the MemoryX-REST server at host=" << host_ <<
", port=" << port_;
242 std::string path = buildPath(
id);
243 std::string query =
"containers";
244 std::string url = path +
"?" + query;
246 ARMARX_DEBUG <<
"GET request: Get containers from MemoryX-REST server: url=" << url;
248 auto res = client_->Get(url);
250 std::vector<std::string> item_keys;
258 nlohmann::json json_response = nlohmann::json::parse(res->body);
260 if (json_response.contains(
"keys") && json_response[
"keys"].is_array())
262 item_keys = json_response[
"keys"].get<std::vector<std::string>>();
265 catch (
const std::exception& e)
267 ARMARX_ERROR <<
"GET request: Error parsing JSON: " << e.what();
272 ARMARX_DEBUG <<
"GET request failed. status=" << res->status <<
", url=" << url;
276 ARMARX_DEBUG <<
"GET request failed. No connection could be established to the MemoryX-REST server at host=" << host_ <<
", port=" << port_;
282 std::vector<unsigned char> RestPersistence::readItemFromRest(
const armarx::armem::MemoryID&
id, std::string& key)
284 std::string path = buildPath(
id);
285 std::string query =
"item=" + key;
286 std::string url = path +
"?" + query;
288 ARMARX_DEBUG <<
"GET request: Read data from MemoryX-REST server: key=" << key <<
", url=" << url;
290 auto res = client_->Get(url);
292 std::vector<unsigned char>
data;
298 data = std::vector<unsigned char>(res->body.begin(), res->body.end());
302 ARMARX_DEBUG <<
"GET request failed: REST-LTM does not contain any item with key=" << key;
306 ARMARX_DEBUG <<
"GET request failed. status=" << res->status <<
", key=" << key <<
", url=" << url;
310 ARMARX_DEBUG <<
"GET request failed. No connection could be established to the MemoryX-REST server at host=" << host_ <<
", port=" << port_;
318 std::string path = buildPath(
id);
319 std::string query =
"item=" + key;
320 std::string url = path +
"?" + query;
322 ARMARX_DEBUG <<
"PUT request: Write data to MemoryX-REST server: key=" << key <<
", url=" << url;
324 std::string body(
data.begin(),
data.end());
326 auto res = client_->Put(url, body,
"application/json");
334 ARMARX_DEBUG <<
"PUT request failed. status=" << res->status <<
", key=" << key <<
", url=" << url;
338 ARMARX_DEBUG <<
"PUT request failed. No connection could be established to the MemoryX-REST server at host=" << host_ <<
", port=" << port_;
342 void RestPersistence::writeItemToRest(
const armarx::armem::MemoryID&
id, std::string& key, nlohmann::json& jsonData)
344 std::string str_data = jsonData.dump();
345 std::vector<unsigned char>
data(str_data.begin(), str_data.end());
352 std::string path = buildPath(
id);
353 std::string mime_type = getMimeType(type);
354 std::string query =
"?item=" + key;
356 std::string body(
data.begin(),
data.end());
358 auto res = client_->Put(path + query, body, mime_type);
367 ARMARX_DEBUG <<
"Request failed. Error: " << res.error();
373 std::string path = buildPath(
id);
374 std::string query =
"item=" + key;
375 std::string url = path +
"?" + query;
377 ARMARX_DEBUG <<
"DELETE request: Remove item from MemoryX-REST server: key=" << key <<
", url=" << url;
380 auto res = client_->Delete(url);
384 ARMARX_DEBUG <<
"DELETE request successful: " << res->body;
388 ARMARX_DEBUG <<
"DELETE request failed. REST-LTM does not contain any item with key=" << key;
392 ARMARX_DEBUG <<
"DELETE request failed. Status: " << res->status;
396 ARMARX_DEBUG <<
"DELETE request failed. No connection could be established to the MemoryX-REST server at host=" << host_ <<
", port=" << port_;
400 void RestPersistence::setPort(
int port)
406 int RestPersistence::getPort()
const
411 void RestPersistence::setHost(std::string& host)
417 std::string RestPersistence::getHost()
425 if (
id.hasMemoryName())
429 if (
id.hasCoreSegmentName())
433 if (
id.hasProviderSegmentName())
437 if (
id.hasEntityName())
441 if (
id.hasTimestamp())
443 url +=
"/" +
id.timestampStr();
445 if (
id.hasInstanceIndex())
447 url +=
"/" +
id.instanceIndexStr();
453 std::string RestPersistence::getMimeType(
DataType type) {
455 std::string mime_type;
459 mime_type =
"application/json";
463 mime_type =
"image/png";