comparison Core/HttpClient.cpp @ 1606:31f4adefb88f

issuing HTTP requests from the plugin SDK
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 01 Sep 2015 17:37:26 +0200
parents ba0226474e22
children b1291df2f780
comparison
equal deleted inserted replaced
1605:fd0464ce1962 1606:31f4adefb88f
82 CURL* curl_; 82 CURL* curl_;
83 struct curl_slist *postHeaders_; 83 struct curl_slist *postHeaders_;
84 }; 84 };
85 85
86 86
87 static void ThrowException(HttpStatus status)
88 {
89 switch (status)
90 {
91 case HttpStatus_400_BadRequest:
92 throw OrthancException(ErrorCode_BadRequest);
93
94 case HttpStatus_401_Unauthorized:
95 throw OrthancException(ErrorCode_Unauthorized);
96
97 case HttpStatus_404_NotFound:
98 throw OrthancException(ErrorCode_InexistentItem);
99
100 default:
101 throw OrthancException(ErrorCode_NetworkProtocol);
102 }
103 }
104
105
106
87 static CURLcode CheckCode(CURLcode code) 107 static CURLcode CheckCode(CURLcode code)
88 { 108 {
89 if (code != CURLE_OK) 109 if (code != CURLE_OK)
90 { 110 {
91 LOG(ERROR) << "libCURL error: " + std::string(curl_easy_strerror(code)); 111 LOG(ERROR) << "libCURL error: " + std::string(curl_easy_strerror(code));
273 293
274 294
275 if (method_ == HttpMethod_Post || 295 if (method_ == HttpMethod_Post ||
276 method_ == HttpMethod_Put) 296 method_ == HttpMethod_Put)
277 { 297 {
278 if (postData_.size() > 0) 298 if (body_.size() > 0)
279 { 299 {
280 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, postData_.c_str())); 300 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, body_.c_str()));
281 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, postData_.size())); 301 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, body_.size()));
282 } 302 }
283 else 303 else
284 { 304 {
285 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL)); 305 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDS, NULL));
286 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, 0)); 306 CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POSTFIELDSIZE, 0));
390 void HttpClient::SetDefaultTimeout(long timeout) 410 void HttpClient::SetDefaultTimeout(long timeout)
391 { 411 {
392 LOG(INFO) << "Setting the default timeout for HTTP client connections: " << timeout << " seconds"; 412 LOG(INFO) << "Setting the default timeout for HTTP client connections: " << timeout << " seconds";
393 globalTimeout_ = timeout; 413 globalTimeout_ = timeout;
394 } 414 }
415
416
417 void HttpClient::ApplyAndThrowException(std::string& answer)
418 {
419 if (!Apply(answer))
420 {
421 ThrowException(GetLastStatus());
422 }
423 }
424
425 void HttpClient::ApplyAndThrowException(Json::Value& answer)
426 {
427 if (!Apply(answer))
428 {
429 ThrowException(GetLastStatus());
430 }
431 }
395 } 432 }