sparkle/libs/network/network.hpp

70 lines
2.8 KiB
C++
Raw Normal View History

2024-12-31 02:20:56 +05:00
#ifndef TLS_NETWORK_HPP_
#define TLS_NETWORK_HPP_
2025-01-25 13:22:59 +05:00
#include <includes.hpp>
2025-01-26 17:34:10 +05:00
#include <gateway/websocket.hpp>
2025-01-18 04:37:54 +05:00
#include <curl/curl.h>
2024-12-31 02:20:56 +05:00
class NetworkManager {
private:
2025-01-18 04:37:54 +05:00
CURL* curl;
CURLcode res;
2024-12-31 05:58:15 +05:00
WebSocket& web;
2025-01-26 23:33:52 +05:00
//std::chrono::duration<double, std::milli> duration;
2025-01-13 14:40:21 +05:00
NetworkManager& operator=(const NetworkManager&) = delete;
2025-01-26 16:12:25 +05:00
NetworkManager& operator=(NetworkManager&&) = delete;
NetworkManager(NetworkManager&&) = delete;
2025-01-13 14:40:21 +05:00
NetworkManager(const NetworkManager&) = delete;
2025-01-14 05:18:08 +05:00
NetworkManager() : web(WebSocket::getInstance()) {
2025-01-19 04:35:20 +05:00
Log::create(INFO, NETWORK, "Network init");
2025-01-18 04:37:54 +05:00
curl_global_init(CURL_GLOBAL_DEFAULT);
2025-01-26 23:33:52 +05:00
curl = curl_easy_init();
if (!curl) {
2025-01-25 11:24:56 +05:00
Log::create(CRITICAL, NETWORK, "Failed to initialize CURL");
2025-01-18 04:37:54 +05:00
abort();
2024-12-31 02:20:56 +05:00
}
}
2025-01-18 04:37:54 +05:00
~NetworkManager() {
if (curl) {
curl_easy_cleanup(curl);
2025-01-13 14:40:21 +05:00
}
2025-01-18 04:37:54 +05:00
curl_global_cleanup();
}
static unsigned long WriteCallback(void* contents, unsigned long size, unsigned long nmemb, void* userp) {
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
2025-01-13 14:40:21 +05:00
}
public:
static NetworkManager& getInstance() {
2025-01-26 23:33:52 +05:00
Log::create(INFO, NETWORK, "Instance event");
2025-01-13 14:40:21 +05:00
static NetworkManager instance;
2024-12-31 02:20:56 +05:00
return instance;
}
2025-01-25 11:24:56 +05:00
unsigned long getLatency() const {
2025-01-26 23:33:52 +05:00
return 0;
2025-01-25 11:24:56 +05:00
}
2025-01-18 04:37:54 +05:00
std::string request(const std::string& method, const std::string& path, const std::string& data = "") {
std::string response;
if (curl) {
2025-01-19 04:35:20 +05:00
curl_easy_setopt(curl, CURLOPT_URL, path.c_str());
2025-01-18 04:37:54 +05:00
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
2025-01-25 11:24:56 +05:00
curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "Accept: application/json");
2025-01-18 04:37:54 +05:00
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, ("Authorization: " + web.getToken()).c_str());
2025-01-19 04:35:20 +05:00
headers = curl_slist_append(headers, "User-Agent: DiscordBot");
2025-01-18 04:37:54 +05:00
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
2025-01-19 04:35:20 +05:00
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_3ONLY);
2025-01-26 23:33:52 +05:00
//auto start = std::chrono::steady_clock::now();
2025-01-19 04:35:20 +05:00
if ((res = curl_easy_perform(curl)) != 0) Log::create(ERROR, NETWORK, "curl_easy_perform() failed: " + std::string(curl_easy_strerror(res)));
2025-01-18 04:37:54 +05:00
curl_slist_free_all(headers);
2025-01-26 23:33:52 +05:00
//duration = std::chrono::steady_clock::now() - start;
2025-01-18 04:37:54 +05:00
}
2025-01-25 11:24:56 +05:00
#ifdef DEBUG
Log::create(INFO, NETWORK, response);
#endif
2025-01-18 04:37:54 +05:00
return response;
2024-12-31 02:20:56 +05:00
}
};
#endif