diff --git a/premake.make b/premake.make index 0bba8fd..acf816c 100644 --- a/premake.make +++ b/premake.make @@ -1,7 +1,7 @@ LDLIBS := -lcurl -lsclogging -lscterm MAJOR := 1 -MINOR := 0 -PATCH := 1 +MINOR := 1 +PATCH := 0 GENERATELIBHEADER := 1 diff --git a/src/requester.cpp b/src/requester.cpp index 33436b4..7893c39 100644 --- a/src/requester.cpp +++ b/src/requester.cpp @@ -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 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; } diff --git a/src/requester.hpp b/src/requester.hpp index 166480e..440c0ac 100644 --- a/src/requester.hpp +++ b/src/requester.hpp @@ -38,6 +38,10 @@ namespace sc { std::unique_ptr _h {nullptr, curl_easy_cleanup}; sc::logger* _logger {nullptr}; std::string _text; + long _status; + + void perform(); + void check_status(long status); }; }