/** * @file restclient.cpp * @brief implementation of the restclient class * * This just provides static wrappers around the Connection class REST * methods. However since I didn't want to have to pull in a whole URI parsing * library just now, the Connection constructor is passed an empty string and * the full URL is passed to the REST methods. All those methods to is * concatenate them anyways. So this should do for now. * * @author Daniel Schauenberg */ #include "restclient-cpp/restclient.h" #include #include #include "restclient-cpp/connection.h" #include "system/Thread.h" #include "manager/ConfigManager.h" /** * @brief global init function. Call this before you start any threads. */ int RestClient::init() { CURLcode res = curl_global_init(CURL_GLOBAL_ALL); if (res == CURLE_OK) { return 0; } else { return 1; } } /** * @brief global disable function. Call this before you terminate your * program. */ void RestClient::disable() { curl_global_cleanup(); } /** * @brief HTTP GET method * * @param url to query * * @return response struct */ RestClient::Response RestClient::get(const std::string& url) { RestClient::Response ret; RestClient::Connection *conn = new RestClient::Connection(""); conn->SetTimeout(3); if (strstr(url.c_str(), "https://") == url.c_str()) { conn->SetCAInfoFilePath(ConfigManager::getInstance()->getResFilePath("cacert.pem")); } ret = conn->get(url); delete conn; return ret; } /** * @brief HTTP POST method * * @param url to query * @param ctype content type as string * @param data HTTP POST body * * @return response struct */ RestClient::Response RestClient::post(const std::string& url, const std::string& ctype, const std::string& data) { RestClient::Response ret; RestClient::Connection *conn = new RestClient::Connection(""); conn->SetTimeout(3); if (strstr(url.c_str(), "https://") == url.c_str()) { conn->SetCAInfoFilePath(ConfigManager::getInstance()->getResFilePath("cacert.pem")); } conn->AppendHeader("Content-Type", ctype); ret = conn->post(url, data); delete conn; return ret; } /** * @brief HTTP PUT method * * @param url to query * @param ctype content type as string * @param data HTTP PUT body * * @return response struct */ RestClient::Response RestClient::put(const std::string& url, const std::string& ctype, const std::string& data) { RestClient::Response ret; RestClient::Connection *conn = new RestClient::Connection(""); if (strstr(url.c_str(), "https://") == url.c_str()) { conn->SetCAInfoFilePath(ConfigManager::getInstance()->getResFilePath("cacert.pem")); } conn->AppendHeader("Content-Type", ctype); ret = conn->put(url, data); delete conn; return ret; } /** * @brief HTTP DELETE method * * @param url to query * * @return response struct */ RestClient::Response RestClient::del(const std::string& url) { RestClient::Response ret; RestClient::Connection *conn = new RestClient::Connection(""); if (strstr(url.c_str(), "https://") == url.c_str()) { conn->SetCAInfoFilePath(ConfigManager::getInstance()->getResFilePath("cacert.pem")); } ret = conn->del(url); delete conn; return ret; } /** * @brief HTTP HEAD method * * @param url to query * * @return response struct */ RestClient::Response RestClient::head(const std::string& url) { RestClient::Response ret; RestClient::Connection *conn = new RestClient::Connection(""); if (strstr(url.c_str(), "https://") == url.c_str()) { conn->SetCAInfoFilePath(ConfigManager::getInstance()->getResFilePath("cacert.pem")); } ret = conn->head(url); delete conn; return ret; } RestClient::Response RestClient::download(const std::string& url, std::string path_to_save) { RestClient::Response ret; RestClient::Connection *conn = new RestClient::Connection(""); conn->SetTimeout(10); if (strstr(url.c_str(), "https://") == url.c_str()) { conn->SetCAInfoFilePath(ConfigManager::getInstance()->getResFilePath("cacert.pem")); } ret = conn->download(url, path_to_save); delete conn; return ret; }