generated from genyleap/Project-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetwork.cpp
45 lines (37 loc) · 1.4 KB
/
network.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "network.hpp"
#include "logger.hpp"
#include <curl/curl.h>
#include <format>
size_t Network::WriteCallback(void* contents, size_t size, size_t nmemb, std::string* outBuffer) {
size_t totalSize = size * nmemb;
outBuffer->append(static_cast<char*>(contents), totalSize);
return totalSize;
}
std::string Network::buildQueryString(const std::map<std::string, std::string>& params) const {
std::shared_lock lock(networkMutex);
std::string queryString;
for (const auto& [key, value] : params) {
queryString += std::format("{}={}&", key, value);
}
if (!queryString.empty()) queryString.pop_back(); // Remove trailing '&'
return queryString;
}
bool Network::sendRequest(const std::string& url, std::string& response, bool verbose) {
Logger::formattedInfo("Constructed URL: {}", url);
CURL* curl = curl_easy_init();
if (!curl) {
Logger::error("Failed to initialize CURL");
return false;
}
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
if (verbose) curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res != CURLE_OK) {
Logger::formattedError("CURL error: {}", curl_easy_strerror(res));
return false;
}
return true;
}