changeset 5099:edefb278cb77

cleanup
author Alain Mazy <am@osimis.io>
date Wed, 19 Oct 2022 08:57:34 +0200
parents d842e4446e63
children 9d51c000e91a
files OrthancServer/Plugins/Engine/OrthancPlugins.cpp
diffstat 1 files changed, 33 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Plugins/Engine/OrthancPlugins.cpp	Thu Oct 13 17:11:43 2022 +0200
+++ b/OrthancServer/Plugins/Engine/OrthancPlugins.cpp	Wed Oct 19 08:57:34 2022 +0200
@@ -3113,6 +3113,27 @@
     CopyToMemoryBuffer(*p.target, dicom);
   }
 
+  static void ThrowOnHttpError(HttpStatus httpStatus)
+  {
+    int intHttpStatus = static_cast<int>(httpStatus);
+    if (intHttpStatus >= 200 && intHttpStatus <= 300)
+    {
+      return; // not an error
+    }
+    else if (intHttpStatus == 401 || intHttpStatus == 403)
+    {
+      throw OrthancException(ErrorCode_Unauthorized);
+    }
+    else if (intHttpStatus == 404)
+    {
+      throw OrthancException(ErrorCode_UnknownResource);
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_BadRequest);
+    }
+  }
+
 
   void OrthancPlugins::RestApiGet(const void* parameters,
                                   bool afterPlugins)
@@ -3133,14 +3154,9 @@
     std::map<std::string, std::string> httpHeaders;
 
     std::string result;
-    if (IHttpHandler::SimpleGet(result, NULL, *handler, RequestOrigin_Plugins, p.uri, httpHeaders) == HttpStatus_200_Ok)
-    {
-      CopyToMemoryBuffer(*p.target, result);
-    }
-    else
-    {
-      throw OrthancException(ErrorCode_UnknownResource);
-    }
+
+    ThrowOnHttpError(IHttpHandler::SimpleGet(result, NULL, *handler, RequestOrigin_Plugins, p.uri, httpHeaders));
+    CopyToMemoryBuffer(*p.target, result);
   }
 
 
@@ -3169,14 +3185,9 @@
     }
       
     std::string result;
-    if (IHttpHandler::SimpleGet(result, NULL, *handler, RequestOrigin_Plugins, p.uri, headers) == HttpStatus_200_Ok)
-    {
-      CopyToMemoryBuffer(*p.target, result);
-    }
-    else
-    {
-      throw OrthancException(ErrorCode_UnknownResource);
-    }
+
+    ThrowOnHttpError(IHttpHandler::SimpleGet(result, NULL, *handler, RequestOrigin_Plugins, p.uri, headers));
+    CopyToMemoryBuffer(*p.target, result);
   }
 
 
@@ -3200,18 +3211,13 @@
     std::map<std::string, std::string> httpHeaders;
 
     std::string result;
-    if (isPost ? 
+    
+    ThrowOnHttpError((isPost ? 
         IHttpHandler::SimplePost(result, NULL, *handler, RequestOrigin_Plugins, p.uri,
-                                 p.body, p.bodySize, httpHeaders) == HttpStatus_200_Ok :
+                                 p.body, p.bodySize, httpHeaders) :
         IHttpHandler::SimplePut(result, NULL, *handler, RequestOrigin_Plugins, p.uri,
-                                p.body, p.bodySize, httpHeaders) == HttpStatus_200_Ok)
-    {
-      CopyToMemoryBuffer(*p.target, result);
-    }
-    else
-    {
-      throw OrthancException(ErrorCode_UnknownResource);
-    }
+                                p.body, p.bodySize, httpHeaders)));
+    CopyToMemoryBuffer(*p.target, result);
   }
 
 
@@ -3231,10 +3237,7 @@
       
     std::map<std::string, std::string> httpHeaders;
 
-    if (IHttpHandler::SimpleDelete(NULL, *handler, RequestOrigin_Plugins, uri, httpHeaders) != HttpStatus_200_Ok)
-    {
-      throw OrthancException(ErrorCode_UnknownResource);
-    }
+    ThrowOnHttpError(IHttpHandler::SimpleDelete(NULL, *handler, RequestOrigin_Plugins, uri, httpHeaders));
   }