comparison Plugins/Samples/Common/OrthancPluginCppWrapper.cpp @ 2264:8e5e0de75839

primitives for HTTP client in plugin C++ wrapper
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 13 Feb 2017 10:39:14 +0100
parents b9775db0fd9b
children e46b0ee6c19d
comparison
equal deleted inserted replaced
2263:b9775db0fd9b 2264:8e5e0de75839
49 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code); 49 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code);
50 } 50 }
51 } 51 }
52 52
53 53
54 bool MemoryBuffer::CheckHttp(OrthancPluginErrorCode code)
55 {
56 if (code != OrthancPluginErrorCode_Success)
57 {
58 // Prevent using garbage information
59 buffer_.data = NULL;
60 buffer_.size = 0;
61 }
62
63 if (code == OrthancPluginErrorCode_Success)
64 {
65 return true;
66 }
67 else if (code == OrthancPluginErrorCode_UnknownResource ||
68 code == OrthancPluginErrorCode_InexistentItem)
69 {
70 return false;
71 }
72 else
73 {
74 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code);
75 }
76 }
77
78
54 MemoryBuffer::MemoryBuffer(OrthancPluginContext* context) : 79 MemoryBuffer::MemoryBuffer(OrthancPluginContext* context) :
55 context_(context) 80 context_(context)
56 { 81 {
57 buffer_.data = NULL; 82 buffer_.data = NULL;
58 buffer_.size = 0; 83 buffer_.size = 0;
117 bool MemoryBuffer::RestApiGet(const std::string& uri, 142 bool MemoryBuffer::RestApiGet(const std::string& uri,
118 bool applyPlugins) 143 bool applyPlugins)
119 { 144 {
120 Clear(); 145 Clear();
121 146
122 OrthancPluginErrorCode error;
123
124 if (applyPlugins) 147 if (applyPlugins)
125 { 148 {
126 error = OrthancPluginRestApiGetAfterPlugins(context_, &buffer_, uri.c_str()); 149 return CheckHttp(OrthancPluginRestApiGetAfterPlugins(context_, &buffer_, uri.c_str()));
127 } 150 }
128 else 151 else
129 { 152 {
130 error = OrthancPluginRestApiGet(context_, &buffer_, uri.c_str()); 153 return CheckHttp(OrthancPluginRestApiGet(context_, &buffer_, uri.c_str()));
131 }
132
133 if (error == OrthancPluginErrorCode_Success)
134 {
135 return true;
136 }
137 else if (error == OrthancPluginErrorCode_UnknownResource ||
138 error == OrthancPluginErrorCode_InexistentItem)
139 {
140 return false;
141 }
142 else
143 {
144 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error);
145 } 154 }
146 } 155 }
147 156
148 157
149 bool MemoryBuffer::RestApiPost(const std::string& uri, 158 bool MemoryBuffer::RestApiPost(const std::string& uri,
151 size_t bodySize, 160 size_t bodySize,
152 bool applyPlugins) 161 bool applyPlugins)
153 { 162 {
154 Clear(); 163 Clear();
155 164
156 OrthancPluginErrorCode error;
157
158 if (applyPlugins) 165 if (applyPlugins)
159 { 166 {
160 error = OrthancPluginRestApiPostAfterPlugins(context_, &buffer_, uri.c_str(), body, bodySize); 167 return CheckHttp(OrthancPluginRestApiPostAfterPlugins(context_, &buffer_, uri.c_str(), body, bodySize));
161 } 168 }
162 else 169 else
163 { 170 {
164 error = OrthancPluginRestApiPost(context_, &buffer_, uri.c_str(), body, bodySize); 171 return CheckHttp(OrthancPluginRestApiPost(context_, &buffer_, uri.c_str(), body, bodySize));
165 }
166
167 if (error == OrthancPluginErrorCode_Success)
168 {
169 return true;
170 }
171 else if (error == OrthancPluginErrorCode_UnknownResource ||
172 error == OrthancPluginErrorCode_InexistentItem)
173 {
174 return false;
175 }
176 else
177 {
178 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error);
179 } 172 }
180 } 173 }
181 174
182 175
183 bool MemoryBuffer::RestApiPut(const std::string& uri, 176 bool MemoryBuffer::RestApiPut(const std::string& uri,
185 size_t bodySize, 178 size_t bodySize,
186 bool applyPlugins) 179 bool applyPlugins)
187 { 180 {
188 Clear(); 181 Clear();
189 182
190 OrthancPluginErrorCode error;
191
192 if (applyPlugins) 183 if (applyPlugins)
193 { 184 {
194 error = OrthancPluginRestApiPutAfterPlugins(context_, &buffer_, uri.c_str(), body, bodySize); 185 return CheckHttp(OrthancPluginRestApiPutAfterPlugins(context_, &buffer_, uri.c_str(), body, bodySize));
195 } 186 }
196 else 187 else
197 { 188 {
198 error = OrthancPluginRestApiPut(context_, &buffer_, uri.c_str(), body, bodySize); 189 return CheckHttp(OrthancPluginRestApiPut(context_, &buffer_, uri.c_str(), body, bodySize));
199 }
200
201 if (error == OrthancPluginErrorCode_Success)
202 {
203 return true;
204 }
205 else if (error == OrthancPluginErrorCode_UnknownResource ||
206 error == OrthancPluginErrorCode_InexistentItem)
207 {
208 return false;
209 }
210 else
211 {
212 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error);
213 } 190 }
214 } 191 }
215 192
216 193
217 bool MemoryBuffer::RestApiPost(const std::string& uri, 194 bool MemoryBuffer::RestApiPost(const std::string& uri,
320 OrthancString str(context_); 297 OrthancString str(context_);
321 str.Assign(OrthancPluginDicomBufferToJson(context_, GetData(), GetSize(), format, flags, maxStringLength)); 298 str.Assign(OrthancPluginDicomBufferToJson(context_, GetData(), GetSize(), format, flags, maxStringLength));
322 str.ToJson(target); 299 str.ToJson(target);
323 } 300 }
324 301
302
303 bool MemoryBuffer::HttpGet(const std::string& url,
304 const std::string& username,
305 const std::string& password)
306 {
307 Clear();
308 return CheckHttp(OrthancPluginHttpGet(context_, &buffer_, url.c_str(),
309 username.empty() ? NULL : username.c_str(),
310 password.empty() ? NULL : password.c_str()));
311 }
312
313
314 bool MemoryBuffer::HttpPost(const std::string& url,
315 const std::string& body,
316 const std::string& username,
317 const std::string& password)
318 {
319 Clear();
320 return CheckHttp(OrthancPluginHttpGet(context_, &buffer_, url.c_str(),
321 username.empty() ? NULL : username.c_str(),
322 password.empty() ? NULL : password.c_str()));
323 }
324
325
326 bool MemoryBuffer::HttpPut(const std::string& url,
327 const std::string& body,
328 const std::string& username,
329 const std::string& password)
330 {
331 Clear();
332 return CheckHttp(OrthancPluginHttpPut(context_, &buffer_, url.c_str(),
333 body.empty() ? NULL : body.c_str(),
334 body.size(),
335 username.empty() ? NULL : username.c_str(),
336 password.empty() ? NULL : password.c_str()));
337 }
338
339
340 bool HttpDelete(OrthancPluginContext* context_,
341 const std::string& url,
342 const std::string& username,
343 const std::string& password)
344 {
345 OrthancPluginErrorCode error = OrthancPluginHttpDelete
346 (context_, url.c_str(),
347 username.empty() ? NULL : username.c_str(),
348 password.empty() ? NULL : password.c_str());
349
350 if (error == OrthancPluginErrorCode_Success)
351 {
352 return true;
353 }
354 else if (error == OrthancPluginErrorCode_UnknownResource ||
355 error == OrthancPluginErrorCode_InexistentItem)
356 {
357 return false;
358 }
359 else
360 {
361 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(error);
362 }
363 }
325 364
326 365
327 OrthancConfiguration::OrthancConfiguration(OrthancPluginContext* context) : 366 OrthancConfiguration::OrthancConfiguration(OrthancPluginContext* context) :
328 context_(context) 367 context_(context)
329 { 368 {