changeset 215:56aef13ad10c

fix forward to core api
author Alain Mazy <am@orthanc.team>
date Wed, 26 Feb 2025 10:28:05 +0100 (5 months ago)
parents a45ba8f12791
children 993653b3f265 380ac7bda84e
files Plugin/Plugin.cpp Resources/Orthanc/Plugins/OrthancPluginCppWrapper.cpp Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h
diffstat 3 files changed, 57 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/Plugin/Plugin.cpp	Tue Feb 25 19:19:11 2025 +0100
+++ b/Plugin/Plugin.cpp	Wed Feb 26 10:28:05 2025 +0100
@@ -940,7 +940,8 @@
 
   if (request->method != OrthancPluginHttpMethod_Get)
   {
-    OrthancPluginSendMethodNotAllowed(context, output, "GET");
+    OrthancPlugins::RestApiClient coreApi(url, request);
+    coreApi.Forward(context, output);
   }
   else
   {
@@ -952,18 +953,9 @@
 
     Json::Value response;
 
-    OrthancPlugins::HttpHeaders headers;
-    OrthancPlugins::GetHttpHeaders(headers, request);
-    std::string getArguments;
-    OrthancPlugins::SerializeGetArguments(getArguments, request);
+    OrthancPlugins::RestApiClient coreApi(url, request);
 
-    std::string coreUrl = url;
-    if (!getArguments.empty())
-    {
-      coreUrl += "?" + getArguments;
-    }
-
-    if (OrthancPlugins::RestApiGet(response, coreUrl, headers, false))
+    if (coreApi.Execute() && coreApi.GetAnswerJson(response))
     {
       jsonLabelsFilter(response, profile);
       OrthancPlugins::AnswerJson(response, output);
--- a/Resources/Orthanc/Plugins/OrthancPluginCppWrapper.cpp	Tue Feb 25 19:19:11 2025 +0100
+++ b/Resources/Orthanc/Plugins/OrthancPluginCppWrapper.cpp	Wed Feb 26 10:28:05 2025 +0100
@@ -4151,6 +4151,24 @@
     httpStatus_(0)
   {
   }
+
+  RestApiClient::RestApiClient(const char* url,
+                               const OrthancPluginHttpRequest* request) :
+    method_(request->method),
+    path_(url),
+    afterPlugins_(false),
+    httpStatus_(0)
+  {
+    OrthancPlugins::GetHttpHeaders(requestHeaders_, request);
+
+    std::string getArguments;
+    OrthancPlugins::SerializeGetArguments(getArguments, request);
+
+    if (!getArguments.empty())
+    {
+      path_ += "?" + getArguments;
+    }
+  }
 #endif
 
 
@@ -4216,6 +4234,32 @@
       }
     }
   }
+
+  void RestApiClient::Forward(OrthancPluginContext* context, OrthancPluginRestOutput* output)
+  {
+    if (Execute() && httpStatus_ == 200)
+    {
+      const char* mimeType = NULL;
+      for (HttpHeaders::const_iterator h = answerHeaders_.begin(); h != answerHeaders_.end(); ++h)
+      {
+        if (h->first == "content-type")
+        {
+          mimeType = h->second.c_str();
+        }
+      }
+      
+      AnswerString(answerBody_, mimeType, output);
+    }
+    else
+    {
+      AnswerHttpError(httpStatus_, output);
+    }
+  }
+
+  bool RestApiClient::GetAnswerJson(Json::Value& output) const
+  {
+    return ReadJson(output, answerBody_);
+  }
 #endif
 
 
--- a/Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h	Tue Feb 25 19:19:11 2025 +0100
+++ b/Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h	Wed Feb 26 10:28:05 2025 +0100
@@ -1531,6 +1531,10 @@
 
   public:
     RestApiClient();
+    
+    // used to forward a call from the plugin to the core
+    RestApiClient(const char* url,
+                  const OrthancPluginHttpRequest* request);
 
     void SetMethod(OrthancPluginHttpMethod method)
     {
@@ -1587,12 +1591,17 @@
 
     bool Execute();
 
+    // Execute and forward the response as is
+    void Forward(OrthancPluginContext* context, OrthancPluginRestOutput* output);
+
     uint16_t GetHttpStatus() const;
 
     bool LookupAnswerHeader(std::string& value,
                             const std::string& key) const;
 
     const std::string& GetAnswerBody() const;
+
+    bool GetAnswerJson(Json::Value& output) const;
   };
 #endif
 }