Refactor: perform and check status in sep funcs
This commit is contained in:
parent
6232f9d593
commit
8a7f92179d
@ -1,7 +1,7 @@
|
||||
LDLIBS := -lcurl -lsclogging -lscterm
|
||||
|
||||
MAJOR := 1
|
||||
MINOR := 0
|
||||
PATCH := 1
|
||||
MINOR := 1
|
||||
PATCH := 0
|
||||
|
||||
GENERATELIBHEADER := 1
|
||||
|
@ -45,20 +45,8 @@ requester& requester::operator=(requester&& other) {
|
||||
std::string requester::get(const std::string &url) {
|
||||
_text = "";
|
||||
curl_easy_setopt(_h.get(), CURLOPT_URL, url.c_str());
|
||||
curl_easy_setopt(_h.get(), CURLOPT_WRITEDATA, this);
|
||||
auto success = curl_easy_perform(_h.get());
|
||||
if (success != CURLE_OK) {
|
||||
std::ostringstream oss;
|
||||
oss << "could not get remote data: " << curl_easy_strerror(success);
|
||||
throw std::runtime_error {oss.str()};
|
||||
}
|
||||
long status;
|
||||
success = curl_easy_getinfo(_h.get(), CURLINFO_RESPONSE_CODE, &status);
|
||||
if (success != CURLE_OK || status != 200) {
|
||||
std::ostringstream oss;
|
||||
oss << "could not get remote data (" << status << ")";
|
||||
throw std::runtime_error {oss.str()};
|
||||
}
|
||||
perform();
|
||||
check_status(200);
|
||||
return _text;
|
||||
}
|
||||
|
||||
@ -72,14 +60,26 @@ std::string requester::post(const std::string& url, const std::string& json) {
|
||||
headers = curl_slist_append(headers, "Content-Type: application/json");
|
||||
std::unique_ptr<struct curl_slist, void(*)(struct curl_slist*)> hdrs {headers, curl_slist_free_all};
|
||||
curl_easy_setopt(_h.get(), CURLOPT_HTTPHEADER, headers);
|
||||
curl_easy_setopt(_h.get(), CURLOPT_WRITEDATA, this);
|
||||
perform();
|
||||
return _text;
|
||||
}
|
||||
|
||||
void requester::perform() {
|
||||
_text = "";
|
||||
curl_easy_setopt(_h.get(), CURLOPT_WRITEDATA, this);
|
||||
auto success = curl_easy_perform(_h.get());
|
||||
if (success != CURLE_OK) {
|
||||
std::ostringstream oss;
|
||||
oss << "could not post data: " << curl_easy_strerror(success);
|
||||
oss << "could not perform request: " << curl_easy_strerror(success);
|
||||
throw std::runtime_error {oss.str()};
|
||||
}
|
||||
}
|
||||
|
||||
void requester::check_status(long status) {
|
||||
auto success = curl_easy_getinfo(_h.get(), CURLINFO_RESPONSE_CODE, &_status);
|
||||
if (success != CURLE_OK || _status != status) {
|
||||
std::ostringstream oss;
|
||||
oss << "could not get remote data (" << _status << ")";
|
||||
throw std::runtime_error {oss.str()};
|
||||
}
|
||||
return _text;
|
||||
}
|
||||
|
@ -38,6 +38,10 @@ namespace sc {
|
||||
std::unique_ptr<CURL, void(*)(CURL*)> _h {nullptr, curl_easy_cleanup};
|
||||
sc::logger* _logger {nullptr};
|
||||
std::string _text;
|
||||
long _status;
|
||||
|
||||
void perform();
|
||||
void check_status(long status);
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user