diff Core/HttpClient.cpp @ 1606:31f4adefb88f

issuing HTTP requests from the plugin SDK
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 01 Sep 2015 17:37:26 +0200
parents ba0226474e22
children b1291df2f780
line wrap: on
line diff
--- a/Core/HttpClient.cpp	Tue Sep 01 16:41:16 2015 +0200
+++ b/Core/HttpClient.cpp	Tue Sep 01 17:37:26 2015 +0200
@@ -84,6 +84,26 @@
   };
 
 
+  static void ThrowException(HttpStatus status)
+  {
+    switch (status)
+    {
+      case HttpStatus_400_BadRequest:
+        throw OrthancException(ErrorCode_BadRequest);
+
+      case HttpStatus_401_Unauthorized:
+        throw OrthancException(ErrorCode_Unauthorized);
+
+      case HttpStatus_404_NotFound:
+        throw OrthancException(ErrorCode_InexistentItem);
+
+      default:
+        throw OrthancException(ErrorCode_NetworkProtocol);
+    }
+  }
+
+
+
   static CURLcode CheckCode(CURLcode code)
   {
     if (code != CURLE_OK)
@@ -275,10 +295,10 @@
     if (method_ == HttpMethod_Post ||
         method_ == HttpMethod_Put)
     {
-      if (postData_.size() > 0)
+      if (body_.size() > 0)
       {
-        CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, postData_.c_str()));
-        CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, postData_.size()));
+        CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, body_.c_str()));
+        CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, body_.size()));
       }
       else
       {
@@ -392,4 +412,21 @@
     LOG(INFO) << "Setting the default timeout for HTTP client connections: " << timeout << " seconds";
     globalTimeout_ = timeout;
   }
+
+
+  void HttpClient::ApplyAndThrowException(std::string& answer)
+  {
+    if (!Apply(answer))
+    {
+      ThrowException(GetLastStatus());
+    }
+  }
+  
+  void HttpClient::ApplyAndThrowException(Json::Value& answer)
+  {
+    if (!Apply(answer))
+    {
+      ThrowException(GetLastStatus());
+    }
+  }
 }