diff Plugin/AuthorizationWebService.cpp @ 70:786b202ef24e

use more recent httpclient
author Alain Mazy <am@osimis.io>
date Tue, 21 Feb 2023 09:23:47 +0100
parents af44dce56328
children 30fb3ce960d9
line wrap: on
line diff
--- a/Plugin/AuthorizationWebService.cpp	Mon Feb 20 11:56:14 2023 +0100
+++ b/Plugin/AuthorizationWebService.cpp	Tue Feb 21 09:23:47 2023 +0100
@@ -83,14 +83,24 @@
       body["identifier"] = Json::nullValue;
     }
 
-    MemoryBuffer answerBody;
-    MemoryBuffer answerHeaders;
-    uint16_t httpStatus = 0;
+    Orthanc::WebServiceParameters authWebservice;
+    authWebservice.SetUrl(url_);
+
+    if (!username_.empty())
+    {
+      authWebservice.SetCredentials(username_, password_);
+    }
 
-    uint32_t headersCount = 0;
-    const char* headersKeys[2];
-    const char* headersValues[2];
-      
+    std::string bodyAsString;
+    Orthanc::Toolbox::WriteFastJson(bodyAsString, body);
+
+    Orthanc::HttpClient authClient(authWebservice, "");
+    authClient.AssignBody(bodyAsString);
+    authClient.SetMethod(Orthanc::HttpMethod_Post);
+    authClient.AddHeader("Content-Type", "application/json");
+    authClient.AddHeader("Expect", "");
+    authClient.SetTimeout(10);
+
     if (token != NULL &&
         token->GetType() == TokenType_HttpHeader)
     {
@@ -101,38 +111,12 @@
       
       if (!(lowerTokenKey == "authorization" && !username_.empty()))
       {
-        headersKeys[headersCount] = token->GetKey().c_str();
-        headersValues[headersCount] = tokenValue.c_str();
-        headersCount++;
+        authClient.AddHeader(token->GetKey(), tokenValue);
       }
     }
-
-    // set the correct content type for the outgoing
-    headersKeys[headersCount] = "Content-Type";
-    headersValues[headersCount] = "application/json";
-    headersCount++;
-
-    // set the correct content type for the outgoing
-    headersKeys[headersCount] = "Expect";
-    headersValues[headersCount] = "";
-    headersCount++;
-
-    std::string flatBody = body.toStyledString();
       
-    if (OrthancPluginHttpClient(GetGlobalContext(), *answerBody, *answerHeaders,
-                                &httpStatus, OrthancPluginHttpMethod_Post,
-                                url_.c_str(), headersCount, headersKeys, headersValues,
-                                flatBody.c_str(), flatBody.size(),
-                                username_.empty() ? NULL : username_.c_str(),
-                                password_.empty() ? NULL : password_.c_str(),
-                                10 /* timeout */, NULL, NULL, NULL, 0)
-        != OrthancPluginErrorCode_Success)
-    {
-      throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol);        
-    }
-
     Json::Value answer;
-    answerBody.ToJson(answer);
+    authClient.ApplyAndThrowException(answer);
 
     static const char* GRANTED = "granted";
     static const char* VALIDITY = "validity";
@@ -222,6 +206,7 @@
       authClient.SetMethod(Orthanc::HttpMethod_Post);
       authClient.AddHeader("Content-Type", "application/json");
       authClient.AddHeader("Expect", "");
+      authClient.SetTimeout(10);
 
       authClient.ApplyAndThrowException(profile);
       return true;