diff Platforms/Wasm/WasmWebService.cpp @ 257:9afafb192180 am-2

using PAM
author am@osimis.io
date Tue, 10 Jul 2018 12:39:01 +0200
parents 8ff70c04c6df
children 3897f9f28cfa
line wrap: on
line diff
--- a/Platforms/Wasm/WasmWebService.cpp	Tue Jul 03 13:19:56 2018 +0200
+++ b/Platforms/Wasm/WasmWebService.cpp	Tue Jul 10 12:39:01 2018 +0200
@@ -1,5 +1,6 @@
 #include "WasmWebService.h"
-
+#include "json/value.h"
+#include "json/writer.h"
 #include <emscripten/emscripten.h>
 
 #ifdef __cplusplus
@@ -8,10 +9,12 @@
 
   extern void WasmWebService_ScheduleGetRequest(void* callback,
                                                 const char* uri,
+                                                const char* headersInJsonString,
                                                 void* payload);
   
   extern void WasmWebService_SchedulePostRequest(void* callback,
                                                  const char* uri,
+                                                 const char* headersInJsonString,
                                                  const void* body,
                                                  size_t bodySize,
                                                  void* payload);
@@ -77,21 +80,43 @@
     }
   }
 
+  void ToJsonString(std::string& output, const IWebService::Headers& headers)
+  {
+    Json::Value jsonHeaders;
+    for (IWebService::Headers::const_iterator it = headers.begin(); it != headers.end(); it++ )
+    {
+      jsonHeaders[it->first] = it->second;
+    }
+
+    Json::StreamWriterBuilder builder;
+    std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
+    std::ostringstream outputStr;
+
+    writer->write(jsonHeaders, &outputStr);
+    output = outputStr.str();
+  }
+
   void WasmWebService::ScheduleGetRequest(ICallback& callback,
                                           const std::string& relativeUri,
+                                          const Headers& headers,
                                           Orthanc::IDynamicObject* payload)
   {
     std::string uri = baseUri_ + relativeUri;
-    WasmWebService_ScheduleGetRequest(&callback, uri.c_str(), payload);
+    std::string headersInJsonString;
+    ToJsonString(headersInJsonString, headers);
+    WasmWebService_ScheduleGetRequest(&callback, uri.c_str(), headersInJsonString.c_str(), payload);
   }
 
   void WasmWebService::SchedulePostRequest(ICallback& callback,
                                            const std::string& relativeUri,
+                                           const Headers& headers,
                                            const std::string& body,
                                            Orthanc::IDynamicObject* payload)
   {
     std::string uri = baseUri_ + relativeUri;
-    WasmWebService_SchedulePostRequest(&callback, uri.c_str(),
+    std::string headersInJsonString;
+    ToJsonString(headersInJsonString, headers);
+    WasmWebService_SchedulePostRequest(&callback, uri.c_str(), headersInJsonString.c_str(),
                                        body.c_str(), body.size(), payload);
   }
 }