Replace curl with httplib

This commit is contained in:
Adam Honse 2022-05-03 19:26:02 -05:00
parent aeb1e364c4
commit 2cb08ec4b4
5 changed files with 8267 additions and 59 deletions

View file

@ -8,7 +8,7 @@
#include "NanoleafController.h"
#include "LogManager.h"
#include <curl/curl.h>
#include "httplib.h"
std::size_t WriteMemoryCallback(const char* in, std::size_t size, std::size_t num, std::string* out)
{
@ -19,76 +19,62 @@ std::size_t WriteMemoryCallback(const char* in, std::size_t size, std::size_t nu
long APIRequest(std::string method, std::string location, std::string URI, json* request_data = nullptr, json* response_data = nullptr)
{
const std::string url("http://"+location+URI);
CURL* curl = curl_easy_init();
const std::string url("http://"+location);
/*-------------------------------------------------------------*\
| Set remote URL. |
| Create httplib Client and variables to hold result |
\*-------------------------------------------------------------*/
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str());
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
httplib::Client client(url.c_str());
int status = 0;
std::string body = "";
/*-------------------------------------------------------------*\
| Don't bother trying IPv6, which would increase DNS resolution |
| time. |
\*-------------------------------------------------------------*/
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
/*-------------------------------------------------------------*\
| Don't wait forever, time out after 10 seconds. |
\*-------------------------------------------------------------*/
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
/*-------------------------------------------------------------*\
| Follow HTTP redirects if necessary. |
\*-------------------------------------------------------------*/
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
if(request_data)
if(method == "GET")
{
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, request_data->dump().c_str());
httplib::Result result = client.Get(URI.c_str());
status = result->status;
body = result->body;
}
else if(method == "PUT")
{
if(request_data)
{
httplib::Result result = client.Put(URI.c_str(), request_data->dump(), "application/json");
status = result->status;
body = result->body;
}
else
{
httplib::Result result = client.Put(URI.c_str());
status = result->status;
body = result->body;
}
}
else if(method == "DELETE")
{
httplib::Result result = client.Delete(URI.c_str());
status = result->status;
body = result->body;
}
/*-------------------------------------------------------------*\
| Response information. |
\*-------------------------------------------------------------*/
long httpCode(0);
std::unique_ptr<std::string> httpData(new std::string());
LOG_DEBUG("[Nanoleaf] Result %d %s", status, body.c_str());
/*-------------------------------------------------------------*\
| Hook up data handling function. |
\*-------------------------------------------------------------*/
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/*-------------------------------------------------------------*\
| Hook up data container (will be passed as the last parameter |
| to the callback handling function). Can be any pointer type, |
| since it will internally be passed as a void pointer. |
\*-------------------------------------------------------------*/
curl_easy_setopt(curl, CURLOPT_WRITEDATA, httpData.get());
/*-------------------------------------------------------------*\
| Run our HTTP GET command, capture the HTTP response code, and |
| clean up. |
\*-------------------------------------------------------------*/
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
curl_easy_cleanup(curl);
if((httpCode / 100) == 2)
if((status / 100) == 2)
{
if(response_data)
{
*response_data = json::parse(*httpData.get());
*response_data = json::parse(body);
}
}
else
{
LOG_DEBUG("[Nanoleaf] HTTP %i:Could not %s from %s", httpCode, method, url);
//LOG_DEBUG("[Nanoleaf] HTTP %i:Could not %s from %s", httpCode, method.c_str(), url.c_str());
}
return httpCode;
return status;
}
NanoleafController::NanoleafController(std::string a_address, int a_port, std::string a_auth_token)

View file

@ -30,7 +30,7 @@ void DetectNanoleafControllers(std::vector<RGBController*> &rgb_controllers)
}
catch(...)
{
LOG_DEBUG("[Nanoleaf] Could not connect to device at %s:%s using auth_token %s", device["ip"], device["port"], device["auth_token"]);
LOG_DEBUG("[Nanoleaf] Could not connect to device at %s:%s using auth_token %s", device["ip"].get<std::string>().c_str(), device["port"].get<std::string>().c_str(), device["auth_token"].get<std::string>().c_str());
}
}
}

View file

@ -9,7 +9,6 @@
#include "RGBController_Nanoleaf.h"
#include "ResourceManager.h"
#include "LogManager.h"
#include <curl/curl.h>
#include "json.hpp"
using json = nlohmann::json;

View file

@ -61,6 +61,7 @@ INCLUDEPATH +=
dependencies/CRCpp/ \
dependencies/hueplusplus-1.0.0/include \
dependencies/hueplusplus-1.0.0/include/hueplusplus \
dependencies/httplib \
dependencies/json/ \
dependencies/libe131/src/ \
dependencies/libcmmk/include/ \
@ -1116,9 +1117,6 @@ FORMS +=
qt/OpenRGBZonesBulkResizer.ui \
qt/TabLabel.ui \
LIBS += \
-lcurl \
#-----------------------------------------------------------------------------------------------#
# Windows-specific Configuration #
#-----------------------------------------------------------------------------------------------#
@ -1257,6 +1255,7 @@ win32:HEADERS +=
win32:contains(QMAKE_TARGET.arch, x86_64) {
LIBS += \
-lws2_32 \
-liphlpapi \
-L"$$PWD/dependencies/winring0/x64/" -lWinRing0x64 \
-L"$$PWD/dependencies/libusb-1.0.22/MS64/dll" -llibusb-1.0 \
-L"$$PWD/dependencies/hidapi-win/x64/" -lhidapi \
@ -1265,6 +1264,7 @@ win32:contains(QMAKE_TARGET.arch, x86_64) {
win32:contains(QMAKE_TARGET.arch, x86) {
LIBS += \
-lws2_32 \
-liphlpapi \
-L"$$PWD/dependencies/winring0/Win32/" -lWinRing0 \
-L"$$PWD/dependencies/libusb-1.0.22/MS32/dll" -llibusb-1.0 \
-L"$$PWD/dependencies/hidapi-win/x86/" -lhidapi \

8223
dependencies/httplib/httplib.h vendored Normal file

File diff suppressed because it is too large Load diff