comparison OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp @ 5049:8c9a1cce076e

CPP Wrapper: new helpers
author Alain Mazy <am@osimis.io>
date Wed, 29 Jun 2022 15:32:44 +0200
parents 03632ed1eb67
children fcb2ed6c88ff
comparison
equal deleted inserted replaced
5048:22966345eaba 5049:8c9a1cce076e
248 { 248 {
249 return CheckHttp(OrthancPluginRestApiGet(GetGlobalContext(), &buffer_, uri.c_str())); 249 return CheckHttp(OrthancPluginRestApiGet(GetGlobalContext(), &buffer_, uri.c_str()));
250 } 250 }
251 } 251 }
252 252
253 // helper class to convert std::map of headers to the plugin SDK C structure
254 class PluginHttpHeaders
255 {
256 std::vector<const char*> headersKeys_;
257 std::vector<const char*> headersValues_;
258 public:
259
260 PluginHttpHeaders(const std::map<std::string, std::string>& httpHeaders)
261 {
262 for (std::map<std::string, std::string>::const_iterator
263 it = httpHeaders.begin(); it != httpHeaders.end(); it++)
264 {
265 headersKeys_.push_back(it->first.c_str());
266 headersValues_.push_back(it->second.c_str());
267 }
268 }
269
270 const char* const* GetKeys()
271 {
272 return (headersKeys_.empty() ? NULL : &headersKeys_[0]);
273 }
274
275 const char* const* GetValues()
276 {
277 return (headersValues_.empty() ? NULL : &headersValues_[0]);
278 }
279
280 uint32_t GetSize()
281 {
282 return static_cast<uint32_t>(headersKeys_.size());
283 }
284 };
285
253 bool MemoryBuffer::RestApiGet(const std::string& uri, 286 bool MemoryBuffer::RestApiGet(const std::string& uri,
254 const std::map<std::string, std::string>& httpHeaders, 287 const std::map<std::string, std::string>& httpHeaders,
255 bool applyPlugins) 288 bool applyPlugins)
256 { 289 {
257 Clear(); 290 Clear();
258 291
259 std::vector<const char*> headersKeys; 292 PluginHttpHeaders headers(httpHeaders);
260 std::vector<const char*> headersValues;
261
262 for (std::map<std::string, std::string>::const_iterator
263 it = httpHeaders.begin(); it != httpHeaders.end(); it++)
264 {
265 headersKeys.push_back(it->first.c_str());
266 headersValues.push_back(it->second.c_str());
267 }
268 293
269 return CheckHttp(OrthancPluginRestApiGet2( 294 return CheckHttp(OrthancPluginRestApiGet2(
270 GetGlobalContext(), &buffer_, uri.c_str(), httpHeaders.size(), 295 GetGlobalContext(), &buffer_, uri.c_str(),
271 (headersKeys.empty() ? NULL : &headersKeys[0]), 296 headers.GetSize(),
272 (headersValues.empty() ? NULL : &headersValues[0]), applyPlugins)); 297 headers.GetKeys(),
298 headers.GetValues(), applyPlugins));
273 } 299 }
274 300
275 bool MemoryBuffer::RestApiPost(const std::string& uri, 301 bool MemoryBuffer::RestApiPost(const std::string& uri,
276 const void* body, 302 const void* body,
277 size_t bodySize, 303 size_t bodySize,
290 { 316 {
291 return CheckHttp(OrthancPluginRestApiPost(GetGlobalContext(), &buffer_, uri.c_str(), b, bodySize)); 317 return CheckHttp(OrthancPluginRestApiPost(GetGlobalContext(), &buffer_, uri.c_str(), b, bodySize));
292 } 318 }
293 } 319 }
294 320
321 #if HAS_ORTHANC_PLUGIN_GENERIC_CALL_REST_API == 1
322
323 bool MemoryBuffer::RestApiPost(const std::string& uri,
324 const void* body,
325 size_t bodySize,
326 const std::map<std::string, std::string>& httpHeaders,
327 bool applyPlugins)
328 {
329 MemoryBuffer answerHeaders;
330 uint16_t httpStatus;
331
332 PluginHttpHeaders headers(httpHeaders);
333
334 return CheckHttp(OrthancPluginCallRestApi(GetGlobalContext(),
335 &buffer_,
336 *answerHeaders,
337 &httpStatus,
338 OrthancPluginHttpMethod_Post,
339 uri.c_str(),
340 headers.GetSize(), headers.GetKeys(), headers.GetValues(),
341 body, bodySize,
342 applyPlugins));
343 }
344
345
346 bool MemoryBuffer::RestApiPost(const std::string& uri,
347 const Json::Value& body,
348 const std::map<std::string, std::string>& httpHeaders,
349 bool applyPlugins)
350 {
351 std::string s;
352 WriteFastJson(s, body);
353 return RestApiPost(uri, s.c_str(), s.size(), httpHeaders, applyPlugins);
354 }
355 #endif
295 356
296 bool MemoryBuffer::RestApiPut(const std::string& uri, 357 bool MemoryBuffer::RestApiPut(const std::string& uri,
297 const void* body, 358 const void* body,
298 size_t bodySize, 359 size_t bodySize,
299 bool applyPlugins) 360 bool applyPlugins)
1454 answer.ToJson(result); 1515 answer.ToJson(result);
1455 } 1516 }
1456 return true; 1517 return true;
1457 } 1518 }
1458 } 1519 }
1520
1521 #if HAS_ORTHANC_PLUGIN_GENERIC_CALL_REST_API == 1
1522 bool RestApiPost(Json::Value& result,
1523 const std::string& uri,
1524 const Json::Value& body,
1525 const std::map<std::string, std::string>& httpHeaders,
1526 bool applyPlugins)
1527 {
1528 MemoryBuffer answer;
1529
1530 if (!answer.RestApiPost(uri, body, httpHeaders, applyPlugins))
1531 {
1532 return false;
1533 }
1534 else
1535 {
1536 if (!answer.IsEmpty())
1537 {
1538 answer.ToJson(result);
1539 }
1540 return true;
1541 }
1542 }
1543 #endif
1459 1544
1460 1545
1461 bool RestApiPost(Json::Value& result, 1546 bool RestApiPost(Json::Value& result,
1462 const std::string& uri, 1547 const std::string& uri,
1463 const Json::Value& body, 1548 const Json::Value& body,
3794 { 3879 {
3795 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code); 3880 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code);
3796 } 3881 }
3797 } 3882 }
3798 #endif 3883 #endif
3884
3885 void GetHttpHeaders(std::map<std::string, std::string>& result, const OrthancPluginHttpRequest* request)
3886 {
3887 result.clear();
3888
3889 for (uint32_t i = 0; i < request->headersCount; ++i)
3890 {
3891 result[request->headersKeys[i]] = request->headersValues[i];
3892 }
3893 }
3799 } 3894 }