6 #include <SimoxUtility/json.h>
32 RESTStorageMixin(
const std::string& exportName,
const armem::MemoryID&
id, std::string host =
"localhost",
int port = 8080) : exportName_(exportName), id_(id), host_(host), port_(port) {
33 client_ = std::make_shared<httplib::Client>(host, port);
42 std::string path =
"/ltm";
44 auto res = client_->Get(path);
48 ARMARX_INFO <<
"MemoryX-REST server is running at host=" << host_ <<
", port=" << port_;
54 ARMARX_INFO <<
"MemoryX-REST server is not running at host=" << host_ <<
", port=" << port_;
66 client_ = std::make_shared<httplib::Client>(host_, port_);
71 std::string path = buildPath();
72 std::string query =
"key=" + key;
73 std::string url = path +
"?" + query;
75 ARMARX_DEBUG <<
"GET request: Read data from MemoryX-REST server: key=" << key <<
", url=" << url;
77 auto res = client_->Get(url);
81 if (res && res->status == 200)
83 ARMARX_DEBUG <<
"GET request successful: REST-LTM contain item with key=" << key;
88 else if (res && res->status == 404)
90 ARMARX_DEBUG <<
"GET request successful: REST-LTM does not contain item with key=" << key;
97 ARMARX_ERROR <<
"GET request failed. Status: " << res->status;
102 ARMARX_DEBUG <<
"GET request failed. No connection could be established to the MemoryX-REST server at host=" << host_ <<
", port=" << port_;
111 std::string path = buildPath();
112 std::string query =
"key=" + key;
113 std::string url = path +
"?" + query;
115 ARMARX_DEBUG <<
"PUT request: Write data to MemoryX-REST server: key=" << key <<
", url=" << url;
117 std::string body(
data.begin(),
data.end());
119 auto res = client_->Put(url, body,
"application/json");
121 if (res && res->status == 200)
127 ARMARX_ERROR <<
"PUT request failed. Status: " << res->status;
131 ARMARX_DEBUG <<
"PUT request failed. No connection could be established to the MemoryX-REST server at host=" << host_ <<
", port=" << port_;
137 std::string path = buildPath();
138 std::string query =
"key=" + key;
139 std::string url = path +
"?" + query;
141 ARMARX_DEBUG <<
"GET request: Read data from MemoryX-REST server: key=" << key <<
", url=" << url;
143 auto res = client_->Get(url);
145 std::vector<unsigned char>
data;
147 if (res && res->status == 200)
151 data = std::vector<unsigned char>(res->body.begin(), res->body.end());
153 else if (res && res->status == 404)
155 ARMARX_ERROR <<
"GET request failed: REST-LTM does not contain any item with key=" << key;
159 ARMARX_ERROR <<
"GET request failed. Status: " << res->status;
163 ARMARX_DEBUG <<
"GET request failed. No connection could be established to the MemoryX-REST server at host=" << host_ <<
", port=" << port_;
171 std::string path = buildPath();
172 std::string url = path;
174 ARMARX_DEBUG <<
"GET request: Get items from MemoryX-REST server: url=" << url;
176 auto res = client_->Get(url);
178 std::vector<std::string> item_keys;
180 if (res && res->status == 200)
186 nlohmann::json json_response = nlohmann::json::parse(res->body);
188 if (json_response.contains(
"keys") && json_response[
"keys"].is_array())
190 item_keys = json_response[
"keys"].get<std::vector<std::string>>();
193 catch (
const std::exception& e)
195 ARMARX_ERROR <<
"GET request: Error parsing JSON: " << e.what();
200 ARMARX_ERROR <<
"GET request failed. Status: " << res->status;
204 ARMARX_DEBUG <<
"GET request failed. No connection could be established to the MemoryX-REST server at host=" << host_ <<
", port=" << port_;
213 std::string str_data = jsonData.dump();
214 std::vector<unsigned char>
data(str_data.begin(), str_data.end());
221 std::string path = buildPath();
222 std::string mime_type = getMimeType(type);
223 std::string query =
"?item=" + key;
225 std::string body(
data.begin(),
data.end());
227 auto res = client_->Put(path + query, body, mime_type);
236 ARMARX_ERROR <<
"Request failed. Error: " << res.error();
244 std::string path = buildPath();
245 std::string query =
"key=" + key;
246 std::string url = path +
"?" + query;
248 ARMARX_DEBUG <<
"DELETE request: Remove item from MemoryX-REST server: key=" << key <<
", url=" << url;
251 auto res = client_->Delete(url);
253 if (res && res->status == 200)
255 ARMARX_DEBUG <<
"DELETE request successlful: " << res->body;
257 else if (res && res->status == 404)
259 ARMARX_ERROR <<
"DELETE request failed. REST-LTM does not contain any item with key=" << key;
263 ARMARX_ERROR <<
"DELETE request failed. Status: " << res->status;
267 ARMARX_DEBUG <<
"DELETE request failed. No connection could be established to the MemoryX-REST server at host=" << host_ <<
", port=" << port_;
278 exportName_ = exportName;
283 defs->optional(host_, prefix +
"host");
284 defs->optional(port_, prefix +
"port");
285 defs->optional(exportName_, prefix +
"exportName");
312 std::string exportName_;
313 std::shared_ptr<httplib::Client> client_;
316 std::string host_ =
"localhost";
319 std::string buildPath() {
320 std::string url =
"/ltm/" + exportName_;
350 std::string getMimeType(
DataType type) {
354 return "application/json";