88
99#include " NanoleafController.h"
1010#include " LogManager.h"
11- #include < curl/curl.h >
11+ #include " httplib.h "
1212
1313std::size_t WriteMemoryCallback (const char * in, std::size_t size, std::size_t num, std::string* out)
1414{
@@ -19,76 +19,62 @@ std::size_t WriteMemoryCallback(const char* in, std::size_t size, std::size_t nu
1919
2020long APIRequest (std::string method, std::string location, std::string URI, json* request_data = nullptr , json* response_data = nullptr )
2121{
22- const std::string url (" http://" +location+URI);
23-
24- CURL* curl = curl_easy_init ();
25-
26- /* -------------------------------------------------------------*\
27- | Set remote URL. |
28- \*-------------------------------------------------------------*/
29- curl_easy_setopt (curl, CURLOPT_CUSTOMREQUEST, method.c_str ());
30- curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
31-
32- /* -------------------------------------------------------------*\
33- | Don't bother trying IPv6, which would increase DNS resolution |
34- | time. |
35- \*-------------------------------------------------------------*/
36- curl_easy_setopt (curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
22+ const std::string url (" http://" +location);
3723
3824 /* -------------------------------------------------------------*\
39- | Don't wait forever, time out after 10 seconds. |
25+ | Create httplib Client and variables to hold result |
4026 \*-------------------------------------------------------------*/
41- curl_easy_setopt (curl, CURLOPT_TIMEOUT, 10 );
27+ httplib::Client client (url.c_str ());
28+ int status = 0 ;
29+ std::string body = " " ;
4230
43- /* -------------------------------------------------------------*\
44- | Follow HTTP redirects if necessary. |
45- \*-------------------------------------------------------------*/
46- curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L );
47-
48- if (request_data)
31+ if (method == " GET" )
4932 {
50- curl_easy_setopt (curl, CURLOPT_COPYPOSTFIELDS, request_data->dump ().c_str ());
33+ httplib::Result result = client.Get (URI.c_str ());
34+
35+ status = result->status ;
36+ body = result->body ;
5137 }
38+ else if (method == " PUT" )
39+ {
40+ if (request_data)
41+ {
42+ httplib::Result result = client.Put (URI.c_str (), request_data->dump (), " application/json" );
5243
53- /* -------------------------------------------------------------*\
54- | Response information. |
55- \*-------------------------------------------------------------*/
56- long httpCode (0 );
57- std::unique_ptr<std::string> httpData (new std::string ());
44+ status = result->status ;
45+ body = result->body ;
46+ }
47+ else
48+ {
49+ httplib::Result result = client.Put (URI.c_str ());
5850
59- /* -------------------------------------------------------------*\
60- | Hook up data handling function. |
61- \*-------------------------------------------------------------*/
62- curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
51+ status = result->status ;
52+ body = result->body ;
53+ }
54+ }
55+ else if (method == " DELETE" )
56+ {
57+ httplib::Result result = client.Delete (URI.c_str ());
6358
64- /* -------------------------------------------------------------*\
65- | Hook up data container (will be passed as the last parameter |
66- | to the callback handling function). Can be any pointer type, |
67- | since it will internally be passed as a void pointer. |
68- \*-------------------------------------------------------------*/
69- curl_easy_setopt (curl, CURLOPT_WRITEDATA, httpData.get ());
59+ status = result->status ;
60+ body = result->body ;
61+ }
7062
71- /* -------------------------------------------------------------*\
72- | Run our HTTP GET command, capture the HTTP response code, and |
73- | clean up. |
74- \*-------------------------------------------------------------*/
75- curl_easy_perform (curl);
76- curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &httpCode);
77- curl_easy_cleanup (curl);
63+ LOG_DEBUG (" [Nanoleaf] Result %d %s" , status, body.c_str ());
7864
79- if ((httpCode / 100 ) == 2 )
65+ if ((status / 100 ) == 2 )
8066 {
8167 if (response_data)
8268 {
83- *response_data = json::parse (*httpData. get () );
69+ *response_data = json::parse (body );
8470 }
8571 }
8672 else
8773 {
88- LOG_DEBUG (" [Nanoleaf] HTTP %i:Could not %s from %s" , httpCode, method, url);
74+ // LOG_DEBUG("[Nanoleaf] HTTP %i:Could not %s from %s", httpCode, method.c_str() , url.c_str() );
8975 }
9076
91- return httpCode ;
77+ return status ;
9278}
9379
9480NanoleafController::NanoleafController (std::string a_address, int a_port, std::string a_auth_token)
0 commit comments