comparison 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
comparison
equal deleted inserted replaced
255:65562a28fe05 257:9afafb192180
1 #include "WasmWebService.h" 1 #include "WasmWebService.h"
2 2 #include "json/value.h"
3 #include "json/writer.h"
3 #include <emscripten/emscripten.h> 4 #include <emscripten/emscripten.h>
4 5
5 #ifdef __cplusplus 6 #ifdef __cplusplus
6 extern "C" { 7 extern "C" {
7 #endif 8 #endif
8 9
9 extern void WasmWebService_ScheduleGetRequest(void* callback, 10 extern void WasmWebService_ScheduleGetRequest(void* callback,
10 const char* uri, 11 const char* uri,
12 const char* headersInJsonString,
11 void* payload); 13 void* payload);
12 14
13 extern void WasmWebService_SchedulePostRequest(void* callback, 15 extern void WasmWebService_SchedulePostRequest(void* callback,
14 const char* uri, 16 const char* uri,
17 const char* headersInJsonString,
15 const void* body, 18 const void* body,
16 size_t bodySize, 19 size_t bodySize,
17 void* payload); 20 void* payload);
18 21
19 void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifyError(void* callback, 22 void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifyError(void* callback,
75 { 78 {
76 baseUri_ = baseUri; 79 baseUri_ = baseUri;
77 } 80 }
78 } 81 }
79 82
83 void ToJsonString(std::string& output, const IWebService::Headers& headers)
84 {
85 Json::Value jsonHeaders;
86 for (IWebService::Headers::const_iterator it = headers.begin(); it != headers.end(); it++ )
87 {
88 jsonHeaders[it->first] = it->second;
89 }
90
91 Json::StreamWriterBuilder builder;
92 std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
93 std::ostringstream outputStr;
94
95 writer->write(jsonHeaders, &outputStr);
96 output = outputStr.str();
97 }
98
80 void WasmWebService::ScheduleGetRequest(ICallback& callback, 99 void WasmWebService::ScheduleGetRequest(ICallback& callback,
81 const std::string& relativeUri, 100 const std::string& relativeUri,
101 const Headers& headers,
82 Orthanc::IDynamicObject* payload) 102 Orthanc::IDynamicObject* payload)
83 { 103 {
84 std::string uri = baseUri_ + relativeUri; 104 std::string uri = baseUri_ + relativeUri;
85 WasmWebService_ScheduleGetRequest(&callback, uri.c_str(), payload); 105 std::string headersInJsonString;
106 ToJsonString(headersInJsonString, headers);
107 WasmWebService_ScheduleGetRequest(&callback, uri.c_str(), headersInJsonString.c_str(), payload);
86 } 108 }
87 109
88 void WasmWebService::SchedulePostRequest(ICallback& callback, 110 void WasmWebService::SchedulePostRequest(ICallback& callback,
89 const std::string& relativeUri, 111 const std::string& relativeUri,
112 const Headers& headers,
90 const std::string& body, 113 const std::string& body,
91 Orthanc::IDynamicObject* payload) 114 Orthanc::IDynamicObject* payload)
92 { 115 {
93 std::string uri = baseUri_ + relativeUri; 116 std::string uri = baseUri_ + relativeUri;
94 WasmWebService_SchedulePostRequest(&callback, uri.c_str(), 117 std::string headersInJsonString;
118 ToJsonString(headersInJsonString, headers);
119 WasmWebService_SchedulePostRequest(&callback, uri.c_str(), headersInJsonString.c_str(),
95 body.c_str(), body.size(), payload); 120 body.c_str(), body.size(), payload);
96 } 121 }
97 } 122 }