|
18 | 18 | #include "utils/logger.hpp" |
19 | 19 |
|
20 | 20 | namespace colistener { |
21 | | -CommonAction::CommonAction() : curl_(nullptr), headers_(nullptr), dev_null_(nullptr) { |
22 | | - curl_ = curl_easy_init(); |
23 | | - if (!curl_) { |
24 | | - throw std::runtime_error("Failed to initialize CURL"); |
25 | | - } |
26 | | - |
27 | | - dev_null_ = fopen("/dev/null", "w"); |
28 | | - if (!dev_null_) { |
29 | | - curl_easy_cleanup(curl_); |
30 | | - throw std::runtime_error("Failed to open /dev/null"); |
31 | | - } |
32 | | - |
33 | | - headers_ = curl_slist_append(nullptr, "Content-Type: application/json"); |
| 21 | +CommonAction::CommonAction() { |
| 22 | + headers_["Content-Type"] = "application/json"; |
| 23 | + headers_["User-Agent"] = "coListener/1.0"; |
| 24 | + |
34 | 25 | endpoint_ = std::string(DEFAULT_URL) + |
35 | 26 | ":" + |
36 | 27 | std::string(DEFAULT_PORT) + |
37 | | - std::string(DEFAULT_ROUTE); |
38 | | - |
39 | | - curl_easy_setopt(curl_, CURLOPT_WRITEDATA, dev_null_); |
| 28 | + std::string(SEND_MESSAGES); |
40 | 29 | } |
41 | 30 |
|
42 | | -CommonAction::~CommonAction() { |
43 | | - if (headers_) { |
44 | | - curl_slist_free_all(headers_); |
45 | | - } |
46 | | - if (curl_) { |
47 | | - curl_easy_cleanup(curl_); |
48 | | - } |
49 | | - if (dev_null_) { |
50 | | - fclose(dev_null_); |
51 | | - } |
52 | | -} |
| 31 | +CommonAction::~CommonAction() = default; |
53 | 32 |
|
54 | 33 | bool CommonAction::execute(const std::vector<MessageCache>& messages) { |
55 | 34 | if (messages.empty()) return true; |
@@ -82,31 +61,16 @@ bool CommonAction::execute(const std::vector<MessageCache>& messages) { |
82 | 61 |
|
83 | 62 | // Log request details |
84 | 63 | COLOG_DEBUG("Sending request to endpoint: %s", endpoint_.c_str()); |
85 | | - COLOG_DEBUG("Request payload size: %zu bytes", json_str.length()); |
86 | 64 | // Log detailed request payload for debugging |
87 | | - COLOG_DEBUG("Request payload: %s", json_str.c_str()); |
| 65 | + COLOG_INFO("Request payload: %s", json_str.c_str()); |
88 | 66 |
|
89 | | - curl_easy_setopt(curl_, CURLOPT_URL, endpoint_.c_str()); |
90 | | - curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, headers_); |
91 | | - curl_easy_setopt(curl_, CURLOPT_POSTFIELDS, json_str.c_str()); |
92 | | - curl_easy_setopt(curl_, CURLOPT_POSTFIELDSIZE, json_str.length()); |
93 | 67 |
|
94 | | - const CURLcode res = curl_easy_perform(curl_); |
95 | | - if (res != CURLE_OK) { |
96 | | - // Log CURL errors |
97 | | - COLOG_ERROR("CURL request failed: %s", curl_easy_strerror(res)); |
98 | | - return false; |
99 | | - } |
| 68 | + HttpResponse post_response = curl_client_.post(endpoint_, root, headers_); |
100 | 69 |
|
101 | | - int64_t http_code = 0; |
102 | | - curl_easy_getinfo(curl_, CURLINFO_RESPONSE_CODE, &http_code); |
103 | | - if (http_code >= 200 && http_code < 300) { |
104 | | - // Log successful response |
105 | | - COLOG_INFO("Request successful, HTTP code: %ld", http_code); |
| 70 | + if (post_response.success) { |
106 | 71 | return true; |
107 | 72 | } else { |
108 | | - // Log failed response |
109 | | - COLOG_ERROR("Request failed with HTTP code: %ld", http_code); |
| 73 | + COLOG_ERROR("POST request failed: %s", post_response.error_message.c_str()); |
110 | 74 | return false; |
111 | 75 | } |
112 | 76 | } |
|
0 commit comments