diff 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
line wrap: on
line diff
--- a/Framework/Toolbox/OrthancApiClient.h	Fri Sep 14 16:44:01 2018 +0200
+++ b/Framework/Toolbox/OrthancApiClient.h	Tue Sep 18 15:23:21 2018 +0200
@@ -33,55 +33,61 @@
   class OrthancApiClient:
       public IObservable
   {
-  protected:
-    class BaseRequest;
-    class GetJsonRequest;
+  public:
 
-    struct InternalGetJsonResponseReadyMessage;
-    struct InternalGetJsonResponseErrorMessage;
-
-  public:
-    struct GetJsonResponseReadyMessage : public IMessage
+    struct JsonResponseReadyMessage : public BaseMessage<MessageType_OrthancApi_GenericGetJson_Ready>
     {
-      Json::Value   response_;
-      std::string   uri_;
+      Json::Value   Response;
+      std::string   Uri;
+      Orthanc::IDynamicObject*  Payload;
 
-      GetJsonResponseReadyMessage(MessageType messageType,
-                                  const std::string& uri,
-                                  const Json::Value& response)
-        : IMessage(messageType),
-          response_(response),
-          uri_(uri)
+      JsonResponseReadyMessage(const std::string& uri,
+                               const Json::Value& response,
+                               Orthanc::IDynamicObject*  payload = NULL)
+        : BaseMessage(),
+          Response(response),
+          Uri(uri),
+          Payload(payload)
       {
       }
     };
 
-    struct NewGetJsonResponseReadyMessage : public BaseMessage<MessageType_OrthancApi_GenericGetJson_Ready>
+    struct HttpErrorMessage : public BaseMessage<MessageType_OrthancApi_GenericHttpError_Ready>
     {
-      Json::Value   response_;
-      std::string   uri_;
+      std::string   Uri;
+      Orthanc::IDynamicObject*  Payload;
 
-      NewGetJsonResponseReadyMessage(const std::string& uri,
-                                     const Json::Value& response)
+      HttpErrorMessage(const std::string& uri,
+                       Orthanc::IDynamicObject*  payload = NULL)
         : BaseMessage(),
-          response_(response),
-          uri_(uri)
+          Uri(uri),
+          Payload(payload)
       {
       }
     };
 
-    struct NewHttpErrorMessage : public BaseMessage<MessageType_OrthancApi_GenericHttpError_Ready>
+    struct BinaryResponseReadyMessage : public BaseMessage<MessageType_OrthancApi_GenericGetBinary_Ready>
     {
-      std::string   uri_;
+      const void* Answer;
+      size_t AnswerSize;
+      std::string   Uri;
+      Orthanc::IDynamicObject*  Payload;
 
-      NewHttpErrorMessage(const std::string& uri)
+      BinaryResponseReadyMessage(const std::string& uri,
+                                 const void* answer,
+                                 size_t answerSize,
+                                 Orthanc::IDynamicObject*  payload = NULL)
         : BaseMessage(),
-          uri_(uri)
+          Answer(answer),
+          AnswerSize(answerSize),
+          Uri(uri),
+          Payload(payload)
       {
       }
     };
 
 
+
   public:
 
     enum Mode
@@ -91,31 +97,43 @@
 
   protected:
     IWebService&                      orthanc_;
-    class WebCallback;
-    boost::shared_ptr<WebCallback>    webCallback_;  // This is a PImpl pattern
-    std::set<BaseRequest*>            requestsInProgress_;
-
-    //    int ScheduleGetJsonRequest(const std::string& uri);
-
-    void ReleaseRequest(BaseRequest* request);
 
   public:
     OrthancApiClient(MessageBroker& broker,
                      IWebService& orthanc);
     virtual ~OrthancApiClient() {}
 
-    // schedule a GET request expecting a JSON request.
-    // once the response is ready, it will emit a OrthancApiClient::GetJsonResponseReadyMessage message whose messageType is specified in the call
-    void ScheduleGetJsonRequest(IObserver& responseObserver, const std::string& uri, MessageType messageToEmitWhenResponseReady);
+    // schedule a GET request expecting a JSON response.
+    void GetJsonAsync(const std::string& uri,
+                      MessageHandler<JsonResponseReadyMessage>* successCallback,
+                      MessageHandler<HttpErrorMessage>* failureCallback = NULL,
+                      Orthanc::IDynamicObject* payload = NULL);
 
-    void ScheduleGetStudyIds(IObserver& responseObserver) {ScheduleGetJsonRequest(responseObserver, "/studies", MessageType_OrthancApi_GetStudyIds_Ready);}
-    void ScheduleGetStudy(IObserver& responseObserver, const std::string& studyId) {ScheduleGetJsonRequest(responseObserver, "/studies/" + studyId, MessageType_OrthancApi_GetStudy_Ready);}
-    void ScheduleGetSeries(IObserver& responseObserver, const std::string& seriesId) {ScheduleGetJsonRequest(responseObserver, "/series/" + seriesId, MessageType_OrthancApi_GetSeries_Ready);}
+    // schedule a GET request expecting a binary response.
+    void GetBinaryAsync(const std::string& uri,
+                        const std::string& contentType,
+                        MessageHandler<BinaryResponseReadyMessage>* successCallback,
+                        MessageHandler<HttpErrorMessage>* failureCallback = NULL,
+                        Orthanc::IDynamicObject* payload = NULL)
+    {
+      IWebService::Headers headers;
+      headers["Accept"] = contentType;
+      GetBinaryAsync(uri, headers, successCallback, failureCallback, payload);
+    }
 
-    void GetJsonAsync(const std::string& uri,
-                      MessageHandler<NewGetJsonResponseReadyMessage>* successCallback,
-                      MessageHandler<NewHttpErrorMessage>* failureCallback = NULL);
+    // schedule a GET request expecting a binary response.
+    void GetBinaryAsync(const std::string& uri,
+                        const IWebService::Headers& headers,
+                        MessageHandler<BinaryResponseReadyMessage>* successCallback,
+                        MessageHandler<HttpErrorMessage>* failureCallback = NULL,
+                        Orthanc::IDynamicObject* payload = NULL);
 
+    // schedule a POST request expecting a JSON response.
+    void PostBinaryAsyncExpectJson(const std::string& uri,
+                                   const std::string& body,
+                                   MessageHandler<JsonResponseReadyMessage>* successCallback,
+                                   MessageHandler<HttpErrorMessage>* failureCallback = NULL,
+                                   Orthanc::IDynamicObject* payload = NULL);
 
 
   };