comparison Framework/Toolbox/OrthancApiClient.h @ 377:8eb4fe74000f

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 05 Nov 2018 16:18:27 +0100
parents c2e040ea8fbe
children dd4c7e82b4be
comparison
equal deleted inserted replaced
376:70256a53ff21 377:8eb4fe74000f
28 #include "../Messages/IObservable.h" 28 #include "../Messages/IObservable.h"
29 #include "../Messages/Promise.h" 29 #include "../Messages/Promise.h"
30 30
31 namespace OrthancStone 31 namespace OrthancStone
32 { 32 {
33 class OrthancApiClient: 33 class OrthancApiClient : public IObservable
34 public IObservable
35 { 34 {
36 public: 35 public:
37 36 class JsonResponseReadyMessage : public BaseMessage<MessageType_OrthancApi_GenericGetJson_Ready>
38 struct JsonResponseReadyMessage : public BaseMessage<MessageType_OrthancApi_GenericGetJson_Ready> 37 {
39 { 38 private:
40 Json::Value Response; 39 const std::string& uri_;
41 std::string Uri; 40 const Json::Value& json_;
42 std::auto_ptr<Orthanc::IDynamicObject> Payload; 41 const Orthanc::IDynamicObject* payload_;
43 42
43 public:
44 JsonResponseReadyMessage(const std::string& uri, 44 JsonResponseReadyMessage(const std::string& uri,
45 const Json::Value& response, 45 const Json::Value& json,
46 Orthanc::IDynamicObject* payload = NULL) 46 const Orthanc::IDynamicObject* payload) :
47 : BaseMessage(), 47 uri_(uri),
48 Response(response), 48 json_(json),
49 Uri(uri), 49 payload_(payload)
50 Payload(payload) 50 {
51 { 51 }
52 } 52
53 }; 53 const std::string& GetUri() const
54 54 {
55 struct EmptyResponseReadyMessage : public BaseMessage<MessageType_OrthancApi_GenericEmptyResponse_Ready> 55 return uri_;
56 { 56 }
57 std::string Uri; 57
58 std::auto_ptr<Orthanc::IDynamicObject> Payload; 58 const Json::Value& GetJson() const
59 59 {
60 EmptyResponseReadyMessage(const std::string& uri, 60 return json_;
61 Orthanc::IDynamicObject* payload = NULL) 61 }
62 : BaseMessage(), 62
63 Uri(uri), 63 bool HasPayload() const
64 Payload(payload) 64 {
65 { 65 return payload_ != NULL;
66 } 66 }
67 }; 67
68 68 const Orthanc::IDynamicObject& GetPayload() const;
69 struct HttpErrorMessage : public BaseMessage<MessageType_OrthancApi_GenericHttpError_Ready> 69 };
70 { 70
71 std::string Uri; 71
72 std::auto_ptr<Orthanc::IDynamicObject> Payload; 72 class BinaryResponseReadyMessage : public BaseMessage<MessageType_OrthancApi_GenericGetBinary_Ready>
73 73 {
74 HttpErrorMessage(const std::string& uri, 74 private:
75 Orthanc::IDynamicObject* payload = NULL) 75 const std::string& uri_;
76 : BaseMessage(), 76 const void* answer_;
77 Uri(uri), 77 size_t answerSize_;
78 Payload(payload) 78 const Orthanc::IDynamicObject* payload_;
79 { 79
80 } 80 public:
81 };
82
83 struct BinaryResponseReadyMessage : public BaseMessage<MessageType_OrthancApi_GenericGetBinary_Ready>
84 {
85 const void* Answer;
86 size_t AnswerSize;
87 std::string Uri;
88 std::auto_ptr<Orthanc::IDynamicObject> Payload;
89
90 BinaryResponseReadyMessage(const std::string& uri, 81 BinaryResponseReadyMessage(const std::string& uri,
91 const void* answer, 82 const void* answer,
92 size_t answerSize, 83 size_t answerSize,
93 Orthanc::IDynamicObject* payload = NULL) 84 const Orthanc::IDynamicObject* payload) :
94 : BaseMessage(), 85 uri_(uri),
95 Answer(answer), 86 answer_(answer),
96 AnswerSize(answerSize), 87 answerSize_(answerSize),
97 Uri(uri), 88 payload_(payload)
98 Payload(payload) 89 {
99 { 90 }
100 } 91
101 }; 92 const std::string& GetUri() const
102 93 {
103 94 return uri_;
104 95 }
105 public: 96
106 97 const void* GetAnswer() const
107 enum Mode 98 {
108 { 99 return answer_;
109 Mode_GetJson 100 }
110 }; 101
102 size_t GetAnswerSize() const
103 {
104 return answerSize_;
105 }
106
107 bool HasPayload() const
108 {
109 return payload_ != NULL;
110 }
111
112 const Orthanc::IDynamicObject& GetPayload() const;
113 };
114
115
116 class EmptyResponseReadyMessage : public BaseMessage<MessageType_OrthancApi_GenericEmptyResponse_Ready>
117 {
118 private:
119 const std::string& uri_;
120 const Orthanc::IDynamicObject* payload_;
121
122 public:
123 EmptyResponseReadyMessage(const std::string& uri,
124 const Orthanc::IDynamicObject* payload) :
125 uri_(uri),
126 payload_(payload)
127 {
128 }
129
130 const std::string& GetUri() const
131 {
132 return uri_;
133 }
134
135 bool HasPayload() const
136 {
137 return payload_ != NULL;
138 }
139
140 const Orthanc::IDynamicObject& GetPayload() const;
141 };
142
143
144 class HttpErrorMessage : public BaseMessage<MessageType_OrthancApi_GenericHttpError_Ready>
145 {
146 private:
147 const std::string& uri_;
148 const Orthanc::IDynamicObject* payload_;
149
150 public:
151 HttpErrorMessage(const std::string& uri,
152 const Orthanc::IDynamicObject* payload) :
153 uri_(uri),
154 payload_(payload)
155 {
156 }
157
158 const std::string& GetUri() const
159 {
160 return uri_;
161 }
162
163 bool HasPayload() const
164 {
165 return payload_ != NULL;
166 }
167
168 const Orthanc::IDynamicObject& GetPayload() const;
169 };
170
111 171
112 protected: 172 protected:
113 IWebService& orthanc_; 173 IWebService& orthanc_;
114 174
115 public: 175 public:
116 OrthancApiClient(MessageBroker& broker, 176 OrthancApiClient(MessageBroker& broker,
117 IWebService& orthanc); 177 IWebService& orthanc);
118 virtual ~OrthancApiClient() {} 178
179 virtual ~OrthancApiClient()
180 {
181 }
119 182
120 // schedule a GET request expecting a JSON response. 183 // schedule a GET request expecting a JSON response.
121 void GetJsonAsync(const std::string& uri, 184 void GetJsonAsync(const std::string& uri,
122 MessageHandler<JsonResponseReadyMessage>* successCallback, 185 MessageHandler<JsonResponseReadyMessage>* successCallback,
123 MessageHandler<HttpErrorMessage>* failureCallback = NULL, 186 MessageHandler<HttpErrorMessage>* failureCallback = NULL,
124 Orthanc::IDynamicObject* payload = NULL); 187 Orthanc::IDynamicObject* payload = NULL /* takes ownership */);
125 188
126 // schedule a GET request expecting a binary response. 189 // schedule a GET request expecting a binary response.
127 void GetBinaryAsync(const std::string& uri, 190 void GetBinaryAsync(const std::string& uri,
128 const std::string& contentType, 191 const std::string& contentType,
129 MessageHandler<BinaryResponseReadyMessage>* successCallback, 192 MessageHandler<BinaryResponseReadyMessage>* successCallback,
130 MessageHandler<HttpErrorMessage>* failureCallback = NULL, 193 MessageHandler<HttpErrorMessage>* failureCallback = NULL,
131 Orthanc::IDynamicObject* payload = NULL) 194 Orthanc::IDynamicObject* payload = NULL /* takes ownership */)
132 { 195 {
133 IWebService::Headers headers; 196 IWebService::Headers headers;
134 headers["Accept"] = contentType; 197 headers["Accept"] = contentType;
135 GetBinaryAsync(uri, headers, successCallback, failureCallback, payload); 198 GetBinaryAsync(uri, headers, successCallback, failureCallback, payload);
136 } 199 }
138 // schedule a GET request expecting a binary response. 201 // schedule a GET request expecting a binary response.
139 void GetBinaryAsync(const std::string& uri, 202 void GetBinaryAsync(const std::string& uri,
140 const IWebService::Headers& headers, 203 const IWebService::Headers& headers,
141 MessageHandler<BinaryResponseReadyMessage>* successCallback, 204 MessageHandler<BinaryResponseReadyMessage>* successCallback,
142 MessageHandler<HttpErrorMessage>* failureCallback = NULL, 205 MessageHandler<HttpErrorMessage>* failureCallback = NULL,
143 Orthanc::IDynamicObject* payload = NULL); 206 Orthanc::IDynamicObject* payload = NULL /* takes ownership */);
144 207
145 // schedule a POST request expecting a JSON response. 208 // schedule a POST request expecting a JSON response.
146 void PostBinaryAsyncExpectJson(const std::string& uri, 209 void PostBinaryAsyncExpectJson(const std::string& uri,
147 const std::string& body, 210 const std::string& body,
148 MessageHandler<JsonResponseReadyMessage>* successCallback, 211 MessageHandler<JsonResponseReadyMessage>* successCallback,
149 MessageHandler<HttpErrorMessage>* failureCallback = NULL, 212 MessageHandler<HttpErrorMessage>* failureCallback = NULL,
150 Orthanc::IDynamicObject* payload = NULL); 213 Orthanc::IDynamicObject* payload = NULL /* takes ownership */);
151 214
152 // schedule a POST request expecting a JSON response. 215 // schedule a POST request expecting a JSON response.
153 void PostJsonAsyncExpectJson(const std::string& uri, 216 void PostJsonAsyncExpectJson(const std::string& uri,
154 const Json::Value& data, 217 const Json::Value& data,
155 MessageHandler<JsonResponseReadyMessage>* successCallback, 218 MessageHandler<JsonResponseReadyMessage>* successCallback,
156 MessageHandler<HttpErrorMessage>* failureCallback = NULL, 219 MessageHandler<HttpErrorMessage>* failureCallback = NULL,
157 Orthanc::IDynamicObject* payload = NULL); 220 Orthanc::IDynamicObject* payload = NULL /* takes ownership */);
158 221
159 // schedule a DELETE request expecting an empty response. 222 // schedule a DELETE request expecting an empty response.
160 void DeleteAsync(const std::string& uri, 223 void DeleteAsync(const std::string& uri,
161 MessageHandler<EmptyResponseReadyMessage>* successCallback, 224 MessageHandler<EmptyResponseReadyMessage>* successCallback,
162 MessageHandler<HttpErrorMessage>* failureCallback = NULL, 225 MessageHandler<HttpErrorMessage>* failureCallback = NULL,
163 Orthanc::IDynamicObject* payload = NULL); 226 Orthanc::IDynamicObject* payload = NULL /* takes ownership */);
164 227
165 228
166 }; 229 };
167 } 230 }