comparison Framework/Toolbox/OrthancApiClient.h @ 422:4fe42ae3653a am-vsol-upgrade

introduced a basic cache at OrthancApiClient level so it can be used with the RadiographyScene
author am@osimis.io
date Tue, 20 Nov 2018 21:44:48 +0100
parents aee3d7941c9b
children bb573a52fc63
comparison
equal deleted inserted replaced
420:8bf717c4e497 422:4fe42ae3653a
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 :
34 public IObservable, 34 public IObservable,
35 public IObserver 35 public IObserver
36 { 36 {
37 public: 37 public:
38 class JsonResponseReadyMessage : 38 class JsonResponseReadyMessage :
39 public BaseMessage<MessageType_OrthancApi_GenericGetJson_Ready> 39 public BaseMessage<MessageType_OrthancApi_GenericGetJson_Ready>
40 { 40 {
41 private: 41 private:
42 const std::string& uri_; 42 const std::string& uri_;
43 const Json::Value& json_; 43 const Json::Value& json_;
44 const Orthanc::IDynamicObject* payload_; 44 const Orthanc::IDynamicObject* payload_;
71 const Orthanc::IDynamicObject& GetPayload() const; 71 const Orthanc::IDynamicObject& GetPayload() const;
72 }; 72 };
73 73
74 74
75 class BinaryResponseReadyMessage : 75 class BinaryResponseReadyMessage :
76 public BaseMessage<MessageType_OrthancApi_GenericGetBinary_Ready> 76 public BaseMessage<MessageType_OrthancApi_GenericGetBinary_Ready>
77 { 77 {
78 private: 78 private:
79 const std::string& uri_; 79 const std::string& uri_;
80 const void* answer_; 80 const void* answer_;
81 size_t answerSize_; 81 size_t answerSize_;
116 const Orthanc::IDynamicObject& GetPayload() const; 116 const Orthanc::IDynamicObject& GetPayload() const;
117 }; 117 };
118 118
119 119
120 class EmptyResponseReadyMessage : 120 class EmptyResponseReadyMessage :
121 public BaseMessage<MessageType_OrthancApi_GenericEmptyResponse_Ready> 121 public BaseMessage<MessageType_OrthancApi_GenericEmptyResponse_Ready>
122 { 122 {
123 private: 123 private:
124 const std::string& uri_; 124 const std::string& uri_;
125 const Orthanc::IDynamicObject* payload_; 125 const Orthanc::IDynamicObject* payload_;
126 126
147 147
148 148
149 149
150 private: 150 private:
151 class WebServicePayload; 151 class WebServicePayload;
152 152 class CachedHttpRequestSuccessMessage;
153
153 protected: 154 protected:
154 IWebService& web_; 155 IWebService& web_;
155 std::string baseUrl_; 156 std::string baseUrl_;
156 157
158 std::map<std::string, boost::shared_ptr<CachedHttpRequestSuccessMessage>> cache_; // TODO: this is currently an infinite cache !
159 bool cacheEnabled_;
157 public: 160 public:
158 OrthancApiClient(MessageBroker& broker, 161 OrthancApiClient(MessageBroker& broker,
159 IWebService& web, 162 IWebService& web,
160 const std::string& baseUrl); 163 const std::string& baseUrl);
161 164
162 virtual ~OrthancApiClient() 165 virtual ~OrthancApiClient()
163 { 166 {
164 } 167 }
165 168
169 void EnableCache(bool enable)
170 {
171 cacheEnabled_ = enable;
172 }
173
166 // schedule a GET request expecting a JSON response. 174 // schedule a GET request expecting a JSON response.
167 void GetJsonAsync(const std::string& uri, 175 void GetJsonAsync(const std::string& uri,
168 MessageHandler<JsonResponseReadyMessage>* successCallback, 176 MessageHandler<JsonResponseReadyMessage>* successCallback,
169 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback = NULL, 177 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback = NULL,
170 Orthanc::IDynamicObject* payload = NULL /* takes ownership */); 178 Orthanc::IDynamicObject* payload = NULL /* takes ownership */);
204 Orthanc::IDynamicObject* payload = NULL /* takes ownership */); 212 Orthanc::IDynamicObject* payload = NULL /* takes ownership */);
205 213
206 void NotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message); 214 void NotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message);
207 215
208 void NotifyHttpError(const IWebService::HttpRequestErrorMessage& message); 216 void NotifyHttpError(const IWebService::HttpRequestErrorMessage& message);
217
218 void CacheAndNotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message);
219
220 private:
221 void HandleFromCache(const std::string& uri,
222 const IWebService::HttpHeaders& headers,
223 Orthanc::IDynamicObject* payload /* takes ownership */);
209 }; 224 };
210 } 225 }