Added initial CurlConnection class in anticipation of downloading motor data from ThrustCurve.org
This commit is contained in:
parent
0f45b11d43
commit
2a81392482
@ -25,6 +25,7 @@ enable_testing()
|
||||
|
||||
add_subdirectory(src/gui)
|
||||
add_subdirectory(src/sim)
|
||||
add_subdirectory(src/Utils)
|
||||
|
||||
#find_package(wxWidgets REQUIRED gl core base)
|
||||
#include(${wxWidgets_USE_FILE})
|
||||
|
6
src/Utils/CMakeLists.txt
Normal file
6
src/Utils/CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
find_package(CURL)
|
||||
|
||||
add_library(utils
|
||||
CurlConnection.cpp)
|
||||
|
||||
target_link_libraries(utils curl jsoncpp ssl crypto)
|
71
src/Utils/CurlConnection.cpp
Normal file
71
src/Utils/CurlConnection.cpp
Normal file
@ -0,0 +1,71 @@
|
||||
#include "CurlConnection.h"
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#include <curl/curl.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
size_t curlCallback(void* content, size_t size, size_t nmemb, std::string* buffer)
|
||||
{
|
||||
buffer->append(static_cast<char*>(content), size*nmemb);
|
||||
return size*nmemb;
|
||||
}
|
||||
}
|
||||
|
||||
namespace Utils
|
||||
{
|
||||
|
||||
CurlConnection::CurlConnection()
|
||||
{
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
curl = curl_easy_init();
|
||||
}
|
||||
|
||||
std::string CurlConnection::get(const std::string& url,
|
||||
const std::vector<std::string>& extraHttpHeaders)
|
||||
{
|
||||
std::string str_result;
|
||||
std::string post_data = "";
|
||||
std::string action = "GET";
|
||||
if(curl)
|
||||
{
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallback);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str_result);
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip");
|
||||
|
||||
if(!extraHttpHeaders.empty())
|
||||
{
|
||||
struct curl_slist *chunk = NULL;
|
||||
for(int i = 0; i < extraHttpHeaders.size(); ++i)
|
||||
{
|
||||
chunk = curl_slist_append(chunk, extraHttpHeaders[i].c_str());
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
|
||||
}
|
||||
|
||||
if(post_data.size() > 0 || action == "POST" || action == "PUT" || action == "DELETE")
|
||||
{
|
||||
if(action == "PUT" || action == "DELETE")
|
||||
{
|
||||
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, action.c_str());
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data.c_str());
|
||||
}
|
||||
res = curl_easy_perform(curl);
|
||||
|
||||
if(res != CURLE_OK)
|
||||
{
|
||||
std::cout << "There was an error: " << curl_easy_strerror(res) << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
return str_result;
|
||||
}
|
||||
|
||||
} // namespace Utils
|
29
src/Utils/CurlConnection.h
Normal file
29
src/Utils/CurlConnection.h
Normal file
@ -0,0 +1,29 @@
|
||||
#ifndef _APPRESETAPI_H_
|
||||
#define _APPRESETAPI_H_
|
||||
|
||||
#include <string>
|
||||
#include <curl/curl.h>
|
||||
#include <vector>
|
||||
|
||||
namespace Utils
|
||||
{
|
||||
|
||||
class CurlConnection
|
||||
{
|
||||
public:
|
||||
CurlConnection();
|
||||
|
||||
std::string get(const std::string& url, const std::vector<std::string>& extraHttpHeaders);
|
||||
|
||||
|
||||
private:
|
||||
CURL* curl;
|
||||
CURLcode res;
|
||||
|
||||
};
|
||||
|
||||
void initCurl(const std::string& host);
|
||||
|
||||
} // namespace utils
|
||||
|
||||
#endif // _APPRESETAPI_H_
|
Loading…
x
Reference in New Issue
Block a user