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

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 05 Nov 2018 16:18:27 +0100
parents c34784e5f299
children dd4c7e82b4be
comparison
equal deleted inserted replaced
376:70256a53ff21 377:8eb4fe74000f
19 **/ 19 **/
20 20
21 #include "OrthancApiClient.h" 21 #include "OrthancApiClient.h"
22 22
23 #include "MessagingToolbox.h" 23 #include "MessagingToolbox.h"
24 #include "Framework/Toolbox/MessagingToolbox.h"
25
24 #include <Core/OrthancException.h> 26 #include <Core/OrthancException.h>
25 #include "Framework/Toolbox/MessagingToolbox.h" 27
26 28 namespace OrthancStone
27 namespace OrthancStone { 29 {
28 30 const Orthanc::IDynamicObject& OrthancApiClient::JsonResponseReadyMessage::GetPayload() const
29 OrthancApiClient::OrthancApiClient(MessageBroker &broker, IWebService &orthanc) 31 {
30 : IObservable(broker), 32 if (HasPayload())
31 orthanc_(orthanc) 33 {
34 return *payload_;
35 }
36 else
37 {
38 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
39 }
40 }
41
42
43 const Orthanc::IDynamicObject& OrthancApiClient::BinaryResponseReadyMessage::GetPayload() const
44 {
45 if (HasPayload())
46 {
47 return *payload_;
48 }
49 else
50 {
51 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
52 }
53 }
54
55
56 const Orthanc::IDynamicObject& OrthancApiClient::EmptyResponseReadyMessage::GetPayload() const
57 {
58 if (HasPayload())
59 {
60 return *payload_;
61 }
62 else
63 {
64 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
65 }
66 }
67
68
69 const Orthanc::IDynamicObject& OrthancApiClient::HttpErrorMessage::GetPayload() const
70 {
71 if (HasPayload())
72 {
73 return *payload_;
74 }
75 else
76 {
77 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
78 }
79 }
80
81
82 OrthancApiClient::OrthancApiClient(MessageBroker &broker, IWebService &orthanc) :
83 IObservable(broker),
84 orthanc_(orthanc)
32 { 85 {
33 } 86 }
34 87
35 // performs the translation between IWebService messages and OrthancApiClient messages 88 // performs the translation between IWebService messages and OrthancApiClient messages
36 // TODO: handle destruction of this object (with shared_ptr ?::delete_later ???) 89 // TODO: handle destruction of this object (with shared_ptr ?::delete_later ???)
52 } 105 }
53 106
54 void ConvertResponseToJson(const IWebService::HttpRequestSuccessMessage& message) 107 void ConvertResponseToJson(const IWebService::HttpRequestSuccessMessage& message)
55 { 108 {
56 Json::Value response; 109 Json::Value response;
57 if (MessagingToolbox::ParseJson(response, message.answer_, message.answerSize_)) 110 if (MessagingToolbox::ParseJson(response, message.GetAnswer(), message.GetAnswerSize()))
58 { 111 {
59 if (orthancApiSuccessCallback_.get() != NULL) 112 if (orthancApiSuccessCallback_.get() != NULL)
60 { 113 {
61 orthancApiSuccessCallback_->Apply(OrthancApiClient::JsonResponseReadyMessage(message.uri_, response, message.payload_)); 114 orthancApiSuccessCallback_->Apply(OrthancApiClient::JsonResponseReadyMessage
115 (message.GetUri(), response, message.GetPayloadPointer()));
62 } 116 }
63 } 117 }
64 else if (orthancApiFailureCallback_.get() != NULL) 118 else if (orthancApiFailureCallback_.get() != NULL)
65 { 119 {
66 orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.uri_, message.payload_)); 120 orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage
121 (message.GetUri(), message.GetPayloadPointer()));
67 } 122 }
68 123
69 delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this) 124 delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this)
70 } 125 }
71 126
72 void ConvertError(const IWebService::HttpRequestErrorMessage& message) 127 void ConvertError(const IWebService::HttpRequestErrorMessage& message)
73 { 128 {
74 if (orthancApiFailureCallback_.get() != NULL) 129 if (orthancApiFailureCallback_.get() != NULL)
75 { 130 {
76 orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.uri_)); 131 orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.GetUri(), message.GetPayloadPointer()));
77 } 132 }
78 133
79 delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this) 134 delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this)
80 } 135 }
81 }; 136 };
101 156
102 void ConvertResponseToBinary(const IWebService::HttpRequestSuccessMessage& message) 157 void ConvertResponseToBinary(const IWebService::HttpRequestSuccessMessage& message)
103 { 158 {
104 if (orthancApiSuccessCallback_.get() != NULL) 159 if (orthancApiSuccessCallback_.get() != NULL)
105 { 160 {
106 orthancApiSuccessCallback_->Apply(OrthancApiClient::BinaryResponseReadyMessage(message.uri_, message.answer_, message.answerSize_, message.payload_)); 161 orthancApiSuccessCallback_->Apply(OrthancApiClient::BinaryResponseReadyMessage
162 (message.GetUri(), message.GetAnswer(),
163 message.GetAnswerSize(), message.GetPayloadPointer()));
107 } 164 }
108 else if (orthancApiFailureCallback_.get() != NULL) 165 else if (orthancApiFailureCallback_.get() != NULL)
109 { 166 {
110 orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.uri_, message.payload_)); 167 orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage
168 (message.GetUri(), message.GetPayloadPointer()));
111 } 169 }
112 170
113 delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this) 171 delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this)
114 } 172 }
115 173
116 void ConvertError(const IWebService::HttpRequestErrorMessage& message) 174 void ConvertError(const IWebService::HttpRequestErrorMessage& message)
117 { 175 {
118 if (orthancApiFailureCallback_.get() != NULL) 176 if (orthancApiFailureCallback_.get() != NULL)
119 { 177 {
120 orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.uri_)); 178 orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.GetUri(), message.GetPayloadPointer()));
121 } 179 }
122 180
123 delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this) 181 delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this)
124 } 182 }
125 }; 183 };
145 203
146 void ConvertResponseToEmpty(const IWebService::HttpRequestSuccessMessage& message) 204 void ConvertResponseToEmpty(const IWebService::HttpRequestSuccessMessage& message)
147 { 205 {
148 if (orthancApiSuccessCallback_.get() != NULL) 206 if (orthancApiSuccessCallback_.get() != NULL)
149 { 207 {
150 orthancApiSuccessCallback_->Apply(OrthancApiClient::EmptyResponseReadyMessage(message.uri_, message.payload_)); 208 orthancApiSuccessCallback_->Apply(OrthancApiClient::EmptyResponseReadyMessage
209 (message.GetUri(), message.GetPayloadPointer()));
151 } 210 }
152 else if (orthancApiFailureCallback_.get() != NULL) 211 else if (orthancApiFailureCallback_.get() != NULL)
153 { 212 {
154 orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.uri_, message.payload_)); 213 orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage
214 (message.GetUri(), message.GetPayloadPointer()));
155 } 215 }
156 216
157 delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this) 217 delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this)
158 } 218 }
159 219
160 void ConvertError(const IWebService::HttpRequestErrorMessage& message) 220 void ConvertError(const IWebService::HttpRequestErrorMessage& message)
161 { 221 {
162 if (orthancApiFailureCallback_.get() != NULL) 222 if (orthancApiFailureCallback_.get() != NULL)
163 { 223 {
164 orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.uri_)); 224 orthancApiFailureCallback_->Apply(OrthancApiClient::HttpErrorMessage(message.GetUri(), message.GetPayloadPointer()));
165 } 225 }
166 226
167 delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this) 227 delete this; // hack untill we find someone to take ownership of this object (https://isocpp.org/wiki/faq/freestore-mgmt#delete-this)
168 } 228 }
169 }; 229 };