comparison Framework/Toolbox/OrthancApiClient.h @ 300:b4abaeb783b1 am-callable-and-promise

messaging refactoring almost complete: works fine in native
author am@osimis.io
date Tue, 18 Sep 2018 15:23:21 +0200
parents 3897f9f28cfa
children 14ef1227120f
comparison
equal deleted inserted replaced
299:3897f9f28cfa 300:b4abaeb783b1
31 namespace OrthancStone 31 namespace OrthancStone
32 { 32 {
33 class OrthancApiClient: 33 class OrthancApiClient:
34 public IObservable 34 public IObservable
35 { 35 {
36 protected: 36 public:
37 class BaseRequest;
38 class GetJsonRequest;
39 37
40 struct InternalGetJsonResponseReadyMessage; 38 struct JsonResponseReadyMessage : public BaseMessage<MessageType_OrthancApi_GenericGetJson_Ready>
41 struct InternalGetJsonResponseErrorMessage; 39 {
40 Json::Value Response;
41 std::string Uri;
42 Orthanc::IDynamicObject* Payload;
42 43
43 public: 44 JsonResponseReadyMessage(const std::string& uri,
44 struct GetJsonResponseReadyMessage : public IMessage 45 const Json::Value& response,
45 { 46 Orthanc::IDynamicObject* payload = NULL)
46 Json::Value response_; 47 : BaseMessage(),
47 std::string uri_; 48 Response(response),
48 49 Uri(uri),
49 GetJsonResponseReadyMessage(MessageType messageType, 50 Payload(payload)
50 const std::string& uri,
51 const Json::Value& response)
52 : IMessage(messageType),
53 response_(response),
54 uri_(uri)
55 { 51 {
56 } 52 }
57 }; 53 };
58 54
59 struct NewGetJsonResponseReadyMessage : public BaseMessage<MessageType_OrthancApi_GenericGetJson_Ready> 55 struct HttpErrorMessage : public BaseMessage<MessageType_OrthancApi_GenericHttpError_Ready>
60 { 56 {
61 Json::Value response_; 57 std::string Uri;
62 std::string uri_; 58 Orthanc::IDynamicObject* Payload;
63 59
64 NewGetJsonResponseReadyMessage(const std::string& uri, 60 HttpErrorMessage(const std::string& uri,
65 const Json::Value& response) 61 Orthanc::IDynamicObject* payload = NULL)
66 : BaseMessage(), 62 : BaseMessage(),
67 response_(response), 63 Uri(uri),
68 uri_(uri) 64 Payload(payload)
69 { 65 {
70 } 66 }
71 }; 67 };
72 68
73 struct NewHttpErrorMessage : public BaseMessage<MessageType_OrthancApi_GenericHttpError_Ready> 69 struct BinaryResponseReadyMessage : public BaseMessage<MessageType_OrthancApi_GenericGetBinary_Ready>
74 { 70 {
75 std::string uri_; 71 const void* Answer;
72 size_t AnswerSize;
73 std::string Uri;
74 Orthanc::IDynamicObject* Payload;
76 75
77 NewHttpErrorMessage(const std::string& uri) 76 BinaryResponseReadyMessage(const std::string& uri,
77 const void* answer,
78 size_t answerSize,
79 Orthanc::IDynamicObject* payload = NULL)
78 : BaseMessage(), 80 : BaseMessage(),
79 uri_(uri) 81 Answer(answer),
82 AnswerSize(answerSize),
83 Uri(uri),
84 Payload(payload)
80 { 85 {
81 } 86 }
82 }; 87 };
88
83 89
84 90
85 public: 91 public:
86 92
87 enum Mode 93 enum Mode
89 Mode_GetJson 95 Mode_GetJson
90 }; 96 };
91 97
92 protected: 98 protected:
93 IWebService& orthanc_; 99 IWebService& orthanc_;
94 class WebCallback;
95 boost::shared_ptr<WebCallback> webCallback_; // This is a PImpl pattern
96 std::set<BaseRequest*> requestsInProgress_;
97
98 // int ScheduleGetJsonRequest(const std::string& uri);
99
100 void ReleaseRequest(BaseRequest* request);
101 100
102 public: 101 public:
103 OrthancApiClient(MessageBroker& broker, 102 OrthancApiClient(MessageBroker& broker,
104 IWebService& orthanc); 103 IWebService& orthanc);
105 virtual ~OrthancApiClient() {} 104 virtual ~OrthancApiClient() {}
106 105
107 // schedule a GET request expecting a JSON request. 106 // schedule a GET request expecting a JSON response.
108 // once the response is ready, it will emit a OrthancApiClient::GetJsonResponseReadyMessage message whose messageType is specified in the call 107 void GetJsonAsync(const std::string& uri,
109 void ScheduleGetJsonRequest(IObserver& responseObserver, const std::string& uri, MessageType messageToEmitWhenResponseReady); 108 MessageHandler<JsonResponseReadyMessage>* successCallback,
109 MessageHandler<HttpErrorMessage>* failureCallback = NULL,
110 Orthanc::IDynamicObject* payload = NULL);
110 111
111 void ScheduleGetStudyIds(IObserver& responseObserver) {ScheduleGetJsonRequest(responseObserver, "/studies", MessageType_OrthancApi_GetStudyIds_Ready);} 112 // schedule a GET request expecting a binary response.
112 void ScheduleGetStudy(IObserver& responseObserver, const std::string& studyId) {ScheduleGetJsonRequest(responseObserver, "/studies/" + studyId, MessageType_OrthancApi_GetStudy_Ready);} 113 void GetBinaryAsync(const std::string& uri,
113 void ScheduleGetSeries(IObserver& responseObserver, const std::string& seriesId) {ScheduleGetJsonRequest(responseObserver, "/series/" + seriesId, MessageType_OrthancApi_GetSeries_Ready);} 114 const std::string& contentType,
115 MessageHandler<BinaryResponseReadyMessage>* successCallback,
116 MessageHandler<HttpErrorMessage>* failureCallback = NULL,
117 Orthanc::IDynamicObject* payload = NULL)
118 {
119 IWebService::Headers headers;
120 headers["Accept"] = contentType;
121 GetBinaryAsync(uri, headers, successCallback, failureCallback, payload);
122 }
114 123
115 void GetJsonAsync(const std::string& uri, 124 // schedule a GET request expecting a binary response.
116 MessageHandler<NewGetJsonResponseReadyMessage>* successCallback, 125 void GetBinaryAsync(const std::string& uri,
117 MessageHandler<NewHttpErrorMessage>* failureCallback = NULL); 126 const IWebService::Headers& headers,
127 MessageHandler<BinaryResponseReadyMessage>* successCallback,
128 MessageHandler<HttpErrorMessage>* failureCallback = NULL,
129 Orthanc::IDynamicObject* payload = NULL);
118 130
131 // schedule a POST request expecting a JSON response.
132 void PostBinaryAsyncExpectJson(const std::string& uri,
133 const std::string& body,
134 MessageHandler<JsonResponseReadyMessage>* successCallback,
135 MessageHandler<HttpErrorMessage>* failureCallback = NULL,
136 Orthanc::IDynamicObject* payload = NULL);
119 137
120 138
121 }; 139 };
122 } 140 }