Mercurial > hg > orthanc-stone
comparison RenderingPlugin/Resources/Orthanc/Plugins/OrthancPluginCppWrapper.cpp @ 1955:c0aa4b03f219
sync
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 27 Oct 2022 10:50:48 +0200 |
parents | 224d7763eede |
children | 07964689cb0b |
comparison
equal
deleted
inserted
replaced
1954:b33a104821a7 | 1955:c0aa4b03f219 |
---|---|
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) |
1455 } | 1516 } |
1456 return true; | 1517 return true; |
1457 } | 1518 } |
1458 } | 1519 } |
1459 | 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 | |
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, |
1464 bool applyPlugins) | 1549 bool applyPlugins) |
1798 } | 1883 } |
1799 | 1884 |
1800 | 1885 |
1801 bool OrthancPeers::DoGet(MemoryBuffer& target, | 1886 bool OrthancPeers::DoGet(MemoryBuffer& target, |
1802 size_t index, | 1887 size_t index, |
1803 const std::string& uri) const | 1888 const std::string& uri, |
1889 const std::map<std::string, std::string>& headers) const | |
1804 { | 1890 { |
1805 if (index >= index_.size()) | 1891 if (index >= index_.size()) |
1806 { | 1892 { |
1807 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | 1893 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); |
1808 } | 1894 } |
1809 | 1895 |
1810 OrthancPlugins::MemoryBuffer answer; | 1896 OrthancPlugins::MemoryBuffer answer; |
1811 uint16_t status; | 1897 uint16_t status; |
1898 PluginHttpHeaders pluginHeaders(headers); | |
1899 | |
1812 OrthancPluginErrorCode code = OrthancPluginCallPeerApi | 1900 OrthancPluginErrorCode code = OrthancPluginCallPeerApi |
1813 (GetGlobalContext(), *answer, NULL, &status, peers_, | 1901 (GetGlobalContext(), *answer, NULL, &status, peers_, |
1814 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Get, uri.c_str(), | 1902 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Get, uri.c_str(), |
1815 0, NULL, NULL, NULL, 0, timeout_); | 1903 pluginHeaders.GetSize(), pluginHeaders.GetKeys(), pluginHeaders.GetValues(), NULL, 0, timeout_); |
1816 | 1904 |
1817 if (code == OrthancPluginErrorCode_Success) | 1905 if (code == OrthancPluginErrorCode_Success) |
1818 { | 1906 { |
1819 target.Swap(answer); | 1907 target.Swap(answer); |
1820 return (status == 200); | 1908 return (status == 200); |
1826 } | 1914 } |
1827 | 1915 |
1828 | 1916 |
1829 bool OrthancPeers::DoGet(MemoryBuffer& target, | 1917 bool OrthancPeers::DoGet(MemoryBuffer& target, |
1830 const std::string& name, | 1918 const std::string& name, |
1831 const std::string& uri) const | 1919 const std::string& uri, |
1920 const std::map<std::string, std::string>& headers) const | |
1832 { | 1921 { |
1833 size_t index; | 1922 size_t index; |
1834 return (LookupName(index, name) && | 1923 return (LookupName(index, name) && |
1835 DoGet(target, index, uri)); | 1924 DoGet(target, index, uri, headers)); |
1836 } | 1925 } |
1837 | 1926 |
1838 | 1927 |
1839 bool OrthancPeers::DoGet(Json::Value& target, | 1928 bool OrthancPeers::DoGet(Json::Value& target, |
1840 size_t index, | 1929 size_t index, |
1841 const std::string& uri) const | 1930 const std::string& uri, |
1931 const std::map<std::string, std::string>& headers) const | |
1842 { | 1932 { |
1843 MemoryBuffer buffer; | 1933 MemoryBuffer buffer; |
1844 | 1934 |
1845 if (DoGet(buffer, index, uri)) | 1935 if (DoGet(buffer, index, uri, headers)) |
1846 { | 1936 { |
1847 buffer.ToJson(target); | 1937 buffer.ToJson(target); |
1848 return true; | 1938 return true; |
1849 } | 1939 } |
1850 else | 1940 else |
1854 } | 1944 } |
1855 | 1945 |
1856 | 1946 |
1857 bool OrthancPeers::DoGet(Json::Value& target, | 1947 bool OrthancPeers::DoGet(Json::Value& target, |
1858 const std::string& name, | 1948 const std::string& name, |
1859 const std::string& uri) const | 1949 const std::string& uri, |
1950 const std::map<std::string, std::string>& headers) const | |
1860 { | 1951 { |
1861 MemoryBuffer buffer; | 1952 MemoryBuffer buffer; |
1862 | 1953 |
1863 if (DoGet(buffer, name, uri)) | 1954 if (DoGet(buffer, name, uri, headers)) |
1864 { | 1955 { |
1865 buffer.ToJson(target); | 1956 buffer.ToJson(target); |
1866 return true; | 1957 return true; |
1867 } | 1958 } |
1868 else | 1959 else |
1873 | 1964 |
1874 | 1965 |
1875 bool OrthancPeers::DoPost(MemoryBuffer& target, | 1966 bool OrthancPeers::DoPost(MemoryBuffer& target, |
1876 const std::string& name, | 1967 const std::string& name, |
1877 const std::string& uri, | 1968 const std::string& uri, |
1878 const std::string& body) const | 1969 const std::string& body, |
1970 const std::map<std::string, std::string>& headers) const | |
1879 { | 1971 { |
1880 size_t index; | 1972 size_t index; |
1881 return (LookupName(index, name) && | 1973 return (LookupName(index, name) && |
1882 DoPost(target, index, uri, body)); | 1974 DoPost(target, index, uri, body, headers)); |
1883 } | 1975 } |
1884 | 1976 |
1885 | 1977 |
1886 bool OrthancPeers::DoPost(Json::Value& target, | 1978 bool OrthancPeers::DoPost(Json::Value& target, |
1887 size_t index, | 1979 size_t index, |
1888 const std::string& uri, | 1980 const std::string& uri, |
1889 const std::string& body) const | 1981 const std::string& body, |
1982 const std::map<std::string, std::string>& headers) const | |
1890 { | 1983 { |
1891 MemoryBuffer buffer; | 1984 MemoryBuffer buffer; |
1892 | 1985 |
1893 if (DoPost(buffer, index, uri, body)) | 1986 if (DoPost(buffer, index, uri, body, headers)) |
1894 { | 1987 { |
1895 buffer.ToJson(target); | 1988 buffer.ToJson(target); |
1896 return true; | 1989 return true; |
1897 } | 1990 } |
1898 else | 1991 else |
1903 | 1996 |
1904 | 1997 |
1905 bool OrthancPeers::DoPost(Json::Value& target, | 1998 bool OrthancPeers::DoPost(Json::Value& target, |
1906 const std::string& name, | 1999 const std::string& name, |
1907 const std::string& uri, | 2000 const std::string& uri, |
1908 const std::string& body) const | 2001 const std::string& body, |
2002 const std::map<std::string, std::string>& headers) const | |
1909 { | 2003 { |
1910 MemoryBuffer buffer; | 2004 MemoryBuffer buffer; |
1911 | 2005 |
1912 if (DoPost(buffer, name, uri, body)) | 2006 if (DoPost(buffer, name, uri, body, headers)) |
1913 { | 2007 { |
1914 buffer.ToJson(target); | 2008 buffer.ToJson(target); |
1915 return true; | 2009 return true; |
1916 } | 2010 } |
1917 else | 2011 else |
1922 | 2016 |
1923 | 2017 |
1924 bool OrthancPeers::DoPost(MemoryBuffer& target, | 2018 bool OrthancPeers::DoPost(MemoryBuffer& target, |
1925 size_t index, | 2019 size_t index, |
1926 const std::string& uri, | 2020 const std::string& uri, |
1927 const std::string& body) const | 2021 const std::string& body, |
2022 const std::map<std::string, std::string>& headers) const | |
1928 { | 2023 { |
1929 if (index >= index_.size()) | 2024 if (index >= index_.size()) |
1930 { | 2025 { |
1931 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | 2026 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); |
1932 } | 2027 } |
1937 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | 2032 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); |
1938 } | 2033 } |
1939 | 2034 |
1940 OrthancPlugins::MemoryBuffer answer; | 2035 OrthancPlugins::MemoryBuffer answer; |
1941 uint16_t status; | 2036 uint16_t status; |
2037 PluginHttpHeaders pluginHeaders(headers); | |
2038 | |
1942 OrthancPluginErrorCode code = OrthancPluginCallPeerApi | 2039 OrthancPluginErrorCode code = OrthancPluginCallPeerApi |
1943 (GetGlobalContext(), *answer, NULL, &status, peers_, | 2040 (GetGlobalContext(), *answer, NULL, &status, peers_, |
1944 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Post, uri.c_str(), | 2041 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Post, uri.c_str(), |
1945 0, NULL, NULL, body.empty() ? NULL : body.c_str(), body.size(), timeout_); | 2042 pluginHeaders.GetSize(), pluginHeaders.GetKeys(), pluginHeaders.GetValues(), body.empty() ? NULL : body.c_str(), body.size(), timeout_); |
1946 | 2043 |
1947 if (code == OrthancPluginErrorCode_Success) | 2044 if (code == OrthancPluginErrorCode_Success) |
1948 { | 2045 { |
1949 target.Swap(answer); | 2046 target.Swap(answer); |
1950 return (status == 200); | 2047 return (status == 200); |
1956 } | 2053 } |
1957 | 2054 |
1958 | 2055 |
1959 bool OrthancPeers::DoPut(size_t index, | 2056 bool OrthancPeers::DoPut(size_t index, |
1960 const std::string& uri, | 2057 const std::string& uri, |
1961 const std::string& body) const | 2058 const std::string& body, |
2059 const std::map<std::string, std::string>& headers) const | |
1962 { | 2060 { |
1963 if (index >= index_.size()) | 2061 if (index >= index_.size()) |
1964 { | 2062 { |
1965 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | 2063 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); |
1966 } | 2064 } |
1971 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | 2069 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); |
1972 } | 2070 } |
1973 | 2071 |
1974 OrthancPlugins::MemoryBuffer answer; | 2072 OrthancPlugins::MemoryBuffer answer; |
1975 uint16_t status; | 2073 uint16_t status; |
2074 PluginHttpHeaders pluginHeaders(headers); | |
2075 | |
1976 OrthancPluginErrorCode code = OrthancPluginCallPeerApi | 2076 OrthancPluginErrorCode code = OrthancPluginCallPeerApi |
1977 (GetGlobalContext(), *answer, NULL, &status, peers_, | 2077 (GetGlobalContext(), *answer, NULL, &status, peers_, |
1978 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Put, uri.c_str(), | 2078 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Put, uri.c_str(), |
1979 0, NULL, NULL, body.empty() ? NULL : body.c_str(), body.size(), timeout_); | 2079 pluginHeaders.GetSize(), pluginHeaders.GetKeys(), pluginHeaders.GetValues(), body.empty() ? NULL : body.c_str(), body.size(), timeout_); |
1980 | 2080 |
1981 if (code == OrthancPluginErrorCode_Success) | 2081 if (code == OrthancPluginErrorCode_Success) |
1982 { | 2082 { |
1983 return (status == 200); | 2083 return (status == 200); |
1984 } | 2084 } |
1989 } | 2089 } |
1990 | 2090 |
1991 | 2091 |
1992 bool OrthancPeers::DoPut(const std::string& name, | 2092 bool OrthancPeers::DoPut(const std::string& name, |
1993 const std::string& uri, | 2093 const std::string& uri, |
1994 const std::string& body) const | 2094 const std::string& body, |
2095 const std::map<std::string, std::string>& headers) const | |
1995 { | 2096 { |
1996 size_t index; | 2097 size_t index; |
1997 return (LookupName(index, name) && | 2098 return (LookupName(index, name) && |
1998 DoPut(index, uri, body)); | 2099 DoPut(index, uri, body, headers)); |
1999 } | 2100 } |
2000 | 2101 |
2001 | 2102 |
2002 bool OrthancPeers::DoDelete(size_t index, | 2103 bool OrthancPeers::DoDelete(size_t index, |
2003 const std::string& uri) const | 2104 const std::string& uri, |
2105 const std::map<std::string, std::string>& headers) const | |
2004 { | 2106 { |
2005 if (index >= index_.size()) | 2107 if (index >= index_.size()) |
2006 { | 2108 { |
2007 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); | 2109 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_ParameterOutOfRange); |
2008 } | 2110 } |
2009 | 2111 |
2010 OrthancPlugins::MemoryBuffer answer; | 2112 OrthancPlugins::MemoryBuffer answer; |
2011 uint16_t status; | 2113 uint16_t status; |
2114 PluginHttpHeaders pluginHeaders(headers); | |
2115 | |
2012 OrthancPluginErrorCode code = OrthancPluginCallPeerApi | 2116 OrthancPluginErrorCode code = OrthancPluginCallPeerApi |
2013 (GetGlobalContext(), *answer, NULL, &status, peers_, | 2117 (GetGlobalContext(), *answer, NULL, &status, peers_, |
2014 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Delete, uri.c_str(), | 2118 static_cast<uint32_t>(index), OrthancPluginHttpMethod_Delete, uri.c_str(), |
2015 0, NULL, NULL, NULL, 0, timeout_); | 2119 pluginHeaders.GetSize(), pluginHeaders.GetKeys(), pluginHeaders.GetValues(), NULL, 0, timeout_); |
2016 | 2120 |
2017 if (code == OrthancPluginErrorCode_Success) | 2121 if (code == OrthancPluginErrorCode_Success) |
2018 { | 2122 { |
2019 return (status == 200); | 2123 return (status == 200); |
2020 } | 2124 } |
2024 } | 2128 } |
2025 } | 2129 } |
2026 | 2130 |
2027 | 2131 |
2028 bool OrthancPeers::DoDelete(const std::string& name, | 2132 bool OrthancPeers::DoDelete(const std::string& name, |
2029 const std::string& uri) const | 2133 const std::string& uri, |
2134 const std::map<std::string, std::string>& headers) const | |
2030 { | 2135 { |
2031 size_t index; | 2136 size_t index; |
2032 return (LookupName(index, name) && | 2137 return (LookupName(index, name) && |
2033 DoDelete(index, uri)); | 2138 DoDelete(index, uri, headers)); |
2034 } | 2139 } |
2035 #endif | 2140 #endif |
2036 | 2141 |
2037 | 2142 |
2038 | 2143 |
3794 { | 3899 { |
3795 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code); | 3900 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code); |
3796 } | 3901 } |
3797 } | 3902 } |
3798 #endif | 3903 #endif |
3904 | |
3905 void GetHttpHeaders(std::map<std::string, std::string>& result, const OrthancPluginHttpRequest* request) | |
3906 { | |
3907 result.clear(); | |
3908 | |
3909 for (uint32_t i = 0; i < request->headersCount; ++i) | |
3910 { | |
3911 result[request->headersKeys[i]] = request->headersValues[i]; | |
3912 } | |
3913 } | |
3799 } | 3914 } |