comparison Framework/Toolbox/IWebService.h @ 301:547e1cf7aa7b am-callable-and-promise

cleanup
author am@osimis.io
date Tue, 18 Sep 2018 15:34:28 +0200
parents b4abaeb783b1
children 14ef1227120f
comparison
equal deleted inserted replaced
300:b4abaeb783b1 301:547e1cf7aa7b
40 protected: 40 protected:
41 MessageBroker& broker_; 41 MessageBroker& broker_;
42 public: 42 public:
43 typedef std::map<std::string, std::string> Headers; 43 typedef std::map<std::string, std::string> Headers;
44 44
45 struct NewHttpRequestSuccessMessage: public BaseMessage<MessageType_HttpRequestSuccess> 45 struct HttpRequestSuccessMessage: public BaseMessage<MessageType_HttpRequestSuccess>
46 { 46 {
47 const std::string& Uri; 47 const std::string& uri_;
48 const void* Answer; 48 const void* answer_;
49 size_t AnswerSize; 49 size_t answerSize_;
50 Orthanc::IDynamicObject* Payload; 50 Orthanc::IDynamicObject* payload_;
51 NewHttpRequestSuccessMessage(const std::string& uri, 51 HttpRequestSuccessMessage(const std::string& uri,
52 const void* answer, 52 const void* answer,
53 size_t answerSize, 53 size_t answerSize,
54 Orthanc::IDynamicObject* payload) 54 Orthanc::IDynamicObject* payload)
55 : BaseMessage(), 55 : BaseMessage(),
56 Uri(uri), 56 uri_(uri),
57 Answer(answer), 57 answer_(answer),
58 AnswerSize(answerSize), 58 answerSize_(answerSize),
59 Payload(payload) 59 payload_(payload)
60 {} 60 {}
61 }; 61 };
62 62
63 struct NewHttpRequestErrorMessage: public BaseMessage<MessageType_HttpRequestError> 63 struct HttpRequestErrorMessage: public BaseMessage<MessageType_HttpRequestError>
64 { 64 {
65 const std::string& Uri; 65 const std::string& uri_;
66 Orthanc::IDynamicObject* Payload; 66 Orthanc::IDynamicObject* payload_;
67 NewHttpRequestErrorMessage(const std::string& uri, 67 HttpRequestErrorMessage(const std::string& uri,
68 Orthanc::IDynamicObject* payload) 68 Orthanc::IDynamicObject* payload)
69 : BaseMessage(), 69 : BaseMessage(),
70 Uri(uri), 70 uri_(uri),
71 Payload(payload) 71 payload_(payload)
72 {} 72 {}
73 }; 73 };
74 74
75 75
76 class ICallback : public IObserver
77 {
78 public:
79 struct HttpRequestSuccessMessage: public IMessage
80 {
81 const std::string& Uri;
82 const void* Answer;
83 size_t AnswerSize;
84 Orthanc::IDynamicObject* Payload;
85 HttpRequestSuccessMessage(const std::string& uri,
86 const void* answer,
87 size_t answerSize,
88 Orthanc::IDynamicObject* payload)
89 : IMessage(MessageType_HttpRequestSuccess),
90 Uri(uri),
91 Answer(answer),
92 AnswerSize(answerSize),
93 Payload(payload)
94 {}
95 };
96
97 struct HttpRequestErrorMessage: public IMessage
98 {
99 const std::string& Uri;
100 Orthanc::IDynamicObject* Payload;
101 HttpRequestErrorMessage(const std::string& uri,
102 Orthanc::IDynamicObject* payload)
103 : IMessage(MessageType_HttpRequestError),
104 Uri(uri),
105 Payload(payload)
106 {}
107 };
108
109 ICallback(MessageBroker& broker)
110 : IObserver(broker)
111 {
112 }
113 virtual ~ICallback()
114 {
115 }
116
117 virtual void HandleMessage(IObservable& from, const IMessage& message)
118 {
119 switch(message.GetType())
120 {
121 case MessageType_HttpRequestError:
122 {
123 const HttpRequestErrorMessage& msg = dynamic_cast<const HttpRequestErrorMessage&>(message);
124 OnHttpRequestError(msg.Uri,
125 msg.Payload);
126 }; break;
127
128 case MessageType_HttpRequestSuccess:
129 {
130 const HttpRequestSuccessMessage& msg = dynamic_cast<const HttpRequestSuccessMessage&>(message);
131 OnHttpRequestSuccess(msg.Uri,
132 msg.Answer,
133 msg.AnswerSize,
134 msg.Payload);
135 }; break;
136 default:
137 VLOG("unhandled message type" << message.GetType());
138 }
139 }
140
141 virtual void OnHttpRequestError(const std::string& uri,
142 Orthanc::IDynamicObject* payload) = 0;
143
144 virtual void OnHttpRequestSuccess(const std::string& uri,
145 const void* answer,
146 size_t answerSize,
147 Orthanc::IDynamicObject* payload) = 0;
148 };
149 76
150 IWebService(MessageBroker& broker) 77 IWebService(MessageBroker& broker)
151 : broker_(broker) 78 : broker_(broker)
152 {} 79 {}
153 80
156 } 83 }
157 84
158 virtual void GetAsync(const std::string& uri, 85 virtual void GetAsync(const std::string& uri,
159 const Headers& headers, 86 const Headers& headers,
160 Orthanc::IDynamicObject* payload, 87 Orthanc::IDynamicObject* payload,
161 MessageHandler<IWebService::NewHttpRequestSuccessMessage>* successCallback, 88 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback,
162 MessageHandler<IWebService::NewHttpRequestErrorMessage>* failureCallback) = 0; 89 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback) = 0;
163 90
164 virtual void PostAsync(const std::string& uri, 91 virtual void PostAsync(const std::string& uri,
165 const Headers& headers, 92 const Headers& headers,
166 const std::string& body, 93 const std::string& body,
167 Orthanc::IDynamicObject* payload, 94 Orthanc::IDynamicObject* payload,
168 MessageHandler<IWebService::NewHttpRequestSuccessMessage>* successCallback, 95 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback,
169 MessageHandler<IWebService::NewHttpRequestErrorMessage>* failureCallback) = 0; 96 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback) = 0;
170 97
171 }; 98 };
172 } 99 }