comparison Framework/Deprecated/Toolbox/OrthancApiClient.h @ 732:c35e98d22764

move Deprecated classes to a separate folder
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 21 May 2019 14:27:35 +0200
parents Framework/Toolbox/OrthancApiClient.h@4f2416d519b4
children 5df50e0f0390
comparison
equal deleted inserted replaced
729:529189f399ec 732:c35e98d22764
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #pragma once
23
24 #include <boost/shared_ptr.hpp>
25 #include <json/json.h>
26
27 #include "IWebService.h"
28 #include "../../Messages/IObservable.h"
29 #include "../../Messages/Promise.h"
30
31 namespace Deprecated
32 {
33 class OrthancApiClient :
34 public OrthancStone::IObservable,
35 public OrthancStone::IObserver
36 {
37 public:
38 class JsonResponseReadyMessage : public OrthancStone::IMessage
39 {
40 ORTHANC_STONE_MESSAGE(__FILE__, __LINE__);
41
42 private:
43 const std::string& uri_;
44 const Json::Value& json_;
45 const Orthanc::IDynamicObject* payload_;
46
47 public:
48 JsonResponseReadyMessage(const std::string& uri,
49 const Json::Value& json,
50 const Orthanc::IDynamicObject* payload) :
51 uri_(uri),
52 json_(json),
53 payload_(payload)
54 {
55 }
56
57 const std::string& GetUri() const
58 {
59 return uri_;
60 }
61
62 const Json::Value& GetJson() const
63 {
64 return json_;
65 }
66
67 bool HasPayload() const
68 {
69 return payload_ != NULL;
70 }
71
72 const Orthanc::IDynamicObject& GetPayload() const;
73 };
74
75
76 class BinaryResponseReadyMessage : public OrthancStone::IMessage
77 {
78 ORTHANC_STONE_MESSAGE(__FILE__, __LINE__);
79
80 private:
81 const std::string& uri_;
82 const void* answer_;
83 size_t answerSize_;
84 const Orthanc::IDynamicObject* payload_;
85
86 public:
87 BinaryResponseReadyMessage(const std::string& uri,
88 const void* answer,
89 size_t answerSize,
90 const Orthanc::IDynamicObject* payload) :
91 uri_(uri),
92 answer_(answer),
93 answerSize_(answerSize),
94 payload_(payload)
95 {
96 }
97
98 const std::string& GetUri() const
99 {
100 return uri_;
101 }
102
103 const void* GetAnswer() const
104 {
105 return answer_;
106 }
107
108 size_t GetAnswerSize() const
109 {
110 return answerSize_;
111 }
112
113 bool HasPayload() const
114 {
115 return payload_ != NULL;
116 }
117
118 const Orthanc::IDynamicObject& GetPayload() const;
119 };
120
121
122 class EmptyResponseReadyMessage : public OrthancStone::IMessage
123 {
124 ORTHANC_STONE_MESSAGE(__FILE__, __LINE__);
125
126 private:
127 const std::string& uri_;
128 const Orthanc::IDynamicObject* payload_;
129
130 public:
131 EmptyResponseReadyMessage(const std::string& uri,
132 const Orthanc::IDynamicObject* payload) :
133 uri_(uri),
134 payload_(payload)
135 {
136 }
137
138 const std::string& GetUri() const
139 {
140 return uri_;
141 }
142
143 bool HasPayload() const
144 {
145 return payload_ != NULL;
146 }
147
148 const Orthanc::IDynamicObject& GetPayload() const;
149 };
150
151
152
153 private:
154 class WebServicePayload;
155
156 protected:
157 IWebService& web_;
158 std::string baseUrl_;
159
160 public:
161 OrthancApiClient(OrthancStone::MessageBroker& broker,
162 IWebService& web,
163 const std::string& baseUrl);
164
165 virtual ~OrthancApiClient()
166 {
167 }
168
169 const std::string& GetBaseUrl() const {return baseUrl_;}
170
171 // schedule a GET request expecting a JSON response.
172 void GetJsonAsync(const std::string& uri,
173 OrthancStone::MessageHandler<JsonResponseReadyMessage>* successCallback,
174 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback = NULL,
175 Orthanc::IDynamicObject* payload = NULL /* takes ownership */);
176
177 // schedule a GET request expecting a binary response.
178 void GetBinaryAsync(const std::string& uri,
179 const std::string& contentType,
180 OrthancStone::MessageHandler<BinaryResponseReadyMessage>* successCallback,
181 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback = NULL,
182 Orthanc::IDynamicObject* payload = NULL /* takes ownership */);
183
184 // schedule a GET request expecting a binary response.
185 void GetBinaryAsync(const std::string& uri,
186 const IWebService::HttpHeaders& headers,
187 OrthancStone::MessageHandler<BinaryResponseReadyMessage>* successCallback,
188 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback = NULL,
189 Orthanc::IDynamicObject* payload = NULL /* takes ownership */);
190
191 // schedule a POST request expecting a JSON response.
192 void PostBinaryAsyncExpectJson(const std::string& uri,
193 const std::string& body,
194 OrthancStone::MessageHandler<JsonResponseReadyMessage>* successCallback,
195 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback = NULL,
196 Orthanc::IDynamicObject* payload = NULL /* takes ownership */);
197
198 // schedule a POST request expecting a JSON response.
199 void PostJsonAsyncExpectJson(const std::string& uri,
200 const Json::Value& data,
201 OrthancStone::MessageHandler<JsonResponseReadyMessage>* successCallback,
202 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback = NULL,
203 Orthanc::IDynamicObject* payload = NULL /* takes ownership */);
204
205 // schedule a POST request and don't mind the response.
206 void PostJsonAsync(const std::string& uri,
207 const Json::Value& data);
208
209 // schedule a POST request and don't expect any response.
210 void PostJsonAsync(const std::string& uri,
211 const Json::Value& data,
212 OrthancStone::MessageHandler<EmptyResponseReadyMessage>* successCallback,
213 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback = NULL,
214 Orthanc::IDynamicObject* payload = NULL /* takes ownership */);
215
216
217 // schedule a POST request and don't mind the response.
218 void PostBinaryAsync(const std::string& uri,
219 const std::string& body);
220
221 // schedule a POST request and don't expect any response.
222 void PostBinaryAsync(const std::string& uri,
223 const std::string& body,
224 OrthancStone::MessageHandler<EmptyResponseReadyMessage>* successCallback,
225 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback = NULL,
226 Orthanc::IDynamicObject* payload = NULL /* takes ownership */);
227
228 // schedule a DELETE request expecting an empty response.
229 void DeleteAsync(const std::string& uri,
230 OrthancStone::MessageHandler<EmptyResponseReadyMessage>* successCallback,
231 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback = NULL,
232 Orthanc::IDynamicObject* payload = NULL /* takes ownership */);
233
234 void NotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message);
235
236 void NotifyHttpError(const IWebService::HttpRequestErrorMessage& message);
237
238 private:
239 void HandleFromCache(const std::string& uri,
240 const IWebService::HttpHeaders& headers,
241 Orthanc::IDynamicObject* payload /* takes ownership */);
242 };
243 }