8
8
9
9
#include " NanoleafController.h"
10
10
#include " LogManager.h"
11
- #include < curl/curl.h >
11
+ #include " httplib.h "
12
12
13
13
std::size_t WriteMemoryCallback (const char * in, std::size_t size, std::size_t num, std::string* out)
14
14
{
@@ -19,76 +19,62 @@ std::size_t WriteMemoryCallback(const char* in, std::size_t size, std::size_t nu
19
19
20
20
long APIRequest (std::string method, std::string location, std::string URI, json* request_data = nullptr , json* response_data = nullptr )
21
21
{
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);
37
23
38
24
/* -------------------------------------------------------------*\
39
- | Don't wait forever, time out after 10 seconds. |
25
+ | Create httplib Client and variables to hold result |
40
26
\*-------------------------------------------------------------*/
41
- curl_easy_setopt (curl, CURLOPT_TIMEOUT, 10 );
27
+ httplib::Client client (url.c_str ());
28
+ int status = 0 ;
29
+ std::string body = " " ;
42
30
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" )
49
32
{
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 ;
51
37
}
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" );
52
43
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 ());
58
50
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 ());
63
58
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
+ }
70
62
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 ());
78
64
79
- if ((httpCode / 100 ) == 2 )
65
+ if ((status / 100 ) == 2 )
80
66
{
81
67
if (response_data)
82
68
{
83
- *response_data = json::parse (*httpData. get () );
69
+ *response_data = json::parse (body );
84
70
}
85
71
}
86
72
else
87
73
{
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() );
89
75
}
90
76
91
- return httpCode ;
77
+ return status ;
92
78
}
93
79
94
80
NanoleafController::NanoleafController (std::string a_address, int a_port, std::string a_auth_token)
0 commit comments