123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- /**
- * @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 <d@unwiredcouch.com>
- */
- #include "restclient-cpp/restclient.h"
- #include <string.h>
- #include <curl/curl.h>
- #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;
- }
|