annotate Platforms/Wasm/WasmWebService.cpp @ 309:14ef1227120f am-callable-and-promise

web services: better handling of failures
author am@osimis.io
date Fri, 28 Sep 2018 15:02:43 +0200
parents ed1a4302154f
children aad37d0b6407
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
1 #include "WasmWebService.h"
257
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
2 #include "json/value.h"
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
3 #include "json/writer.h"
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
4 #include <emscripten/emscripten.h>
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
5
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
6 #ifdef __cplusplus
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
7 extern "C" {
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
8 #endif
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
9
303
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
10 extern void WasmWebService_GetAsync(void* callableSuccess,
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
11 void* callableFailure,
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
12 const char* uri,
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
13 const char* headersInJsonString,
309
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
14 void* payload,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
15 unsigned int timeoutInSeconds);
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
16
303
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
17 extern void WasmWebService_PostAsync(void* callableSuccess,
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
18 void* callableFailure,
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
19 const char* uri,
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
20 const char* headersInJsonString,
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
21 const void* body,
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
22 size_t bodySize,
309
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
23 void* payload,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
24 unsigned int timeoutInSeconds);
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
25
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
26
303
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
27 void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifyError(void* failureCallable,
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
28 const char* uri,
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
29 void* payload)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
30 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
31 if (failureCallable == NULL)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
32 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
33 throw;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
34 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
35 else
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
36 {
303
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
37 reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestErrorMessage>*>(failureCallable)->
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
38 Apply(OrthancStone::IWebService::HttpRequestErrorMessage(uri, reinterpret_cast<Orthanc::IDynamicObject*>(payload)));
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
39 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
40 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
41
303
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
42 void EMSCRIPTEN_KEEPALIVE WasmWebService_NotifySuccess(void* successCallable,
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
43 const char* uri,
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
44 const void* body,
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
45 size_t bodySize,
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
46 void* payload)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
47 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
48 if (successCallable == NULL)
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
49 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
50 throw;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
51 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
52 else
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
53 {
303
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
54 reinterpret_cast<OrthancStone::MessageHandler<OrthancStone::IWebService::HttpRequestSuccessMessage>*>(successCallable)->
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
55 Apply(OrthancStone::IWebService::HttpRequestSuccessMessage(uri, body, bodySize, reinterpret_cast<Orthanc::IDynamicObject*>(payload)));
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
56 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
57 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
58
253
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
59 void EMSCRIPTEN_KEEPALIVE WasmWebService_SetBaseUri(const char* baseUri)
240
ddbb339ed4cf orthancBaseUrl is now configurable
am@osimis.io
parents: 236
diff changeset
60 {
253
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
61 OrthancStone::WasmWebService::GetInstance().SetBaseUri(baseUri);
240
ddbb339ed4cf orthancBaseUrl is now configurable
am@osimis.io
parents: 236
diff changeset
62 }
ddbb339ed4cf orthancBaseUrl is now configurable
am@osimis.io
parents: 236
diff changeset
63
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
64 #ifdef __cplusplus
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
65 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
66 #endif
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
67
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
68
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
69
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
70 namespace OrthancStone
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
71 {
253
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
72 MessageBroker* WasmWebService::broker_ = NULL;
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
73
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
74 void WasmWebService::SetBaseUri(const std::string baseUri)
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
75 {
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
76 // Make sure the base url ends with "/"
253
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
77 if (baseUri.empty() ||
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
78 baseUri[baseUri.size() - 1] != '/')
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
79 {
253
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
80 baseUri_ = baseUri + "/";
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
81 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
82 else
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
83 {
253
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
84 baseUri_ = baseUri;
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
85 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
86 }
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
87
257
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
88 void ToJsonString(std::string& output, const IWebService::Headers& headers)
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
89 {
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
90 Json::Value jsonHeaders;
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
91 for (IWebService::Headers::const_iterator it = headers.begin(); it != headers.end(); it++ )
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
92 {
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
93 jsonHeaders[it->first] = it->second;
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
94 }
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
95
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
96 Json::StreamWriterBuilder builder;
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
97 std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
98 std::ostringstream outputStr;
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
99
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
100 writer->write(jsonHeaders, &outputStr);
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
101 output = outputStr.str();
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
102 }
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
103
303
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
104 void WasmWebService::PostAsync(const std::string& relativeUri,
309
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
105 const Headers& headers,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
106 const std::string& body,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
107 Orthanc::IDynamicObject* payload,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
108 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
109 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
110 unsigned int timeoutInSeconds)
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
111 {
253
8ff70c04c6df IObservable/IObserver now working in WASM too
am@osimis.io
parents: 240
diff changeset
112 std::string uri = baseUri_ + relativeUri;
257
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
113 std::string headersInJsonString;
9afafb192180 using PAM
am@osimis.io
parents: 253
diff changeset
114 ToJsonString(headersInJsonString, headers);
303
ed1a4302154f new messages in wasm too
am@osimis.io
parents: 299
diff changeset
115 WasmWebService_PostAsync(successCallable, failureCallable, uri.c_str(), headersInJsonString.c_str(),
309
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
116 body.c_str(), body.size(), payload, timeoutInSeconds);
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
117 }
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
118
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
119 void WasmWebService::GetAsync(const std::string& relativeUri,
309
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
120 const Headers& headers,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
121 Orthanc::IDynamicObject* payload,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
122 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallable,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
123 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallable,
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
124 unsigned int timeoutInSeconds)
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
125 {
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
126 std::string uri = baseUri_ + relativeUri;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
127 std::string headersInJsonString;
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
128 ToJsonString(headersInJsonString, headers);
309
14ef1227120f web services: better handling of failures
am@osimis.io
parents: 303
diff changeset
129 WasmWebService_GetAsync(successCallable, failureCallable, uri.c_str(), headersInJsonString.c_str(), payload, timeoutInSeconds);
299
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
130 }
3897f9f28cfa backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents: 257
diff changeset
131
221
d7b2590744f8 wip: building applications reusable in SDL and WASM
am@osimis.io
parents:
diff changeset
132 }