#include #include "echoion/core/NetworkClient.hpp" #include #include #include #include namespace Echoion::Core { NetworkClient::NetworkClient() = default; NetworkClient::~NetworkClient() { shutdown(); } Api::Result NetworkClient::initialize( const std::optional& proxy) { { std::lock_guard lock(mutex_); proxy_ = proxy; } initialized_ = true; Void::Log::info("NetworkClient", "{}", std::format("NetworkClient initialized (proxy: {})", proxy.has_value() ? "enabled" : "disabled")); return {}; } void NetworkClient::shutdown() { initialized_ = false; } Api::Result NetworkClient::executeRequest(const Void::Net::Request& req) { Void::Net::HttpClient client; // Snapshot the proxy under the lock. setProxy()/initialize() can run on // other threads (config reload, init), and a torn read of the // optional (it holds std::string host/user/pass) could // yield an empty host -> enabled()==false -> a silent direct connection. std::optional active; { std::lock_guard lock(mutex_); active = proxy_; } if (active && active->enabled()) { client.set_proxy(*active); } auto result = client.send(req); if (!result) { return Api::fail(Api::ErrorCode::NetworkError, result.error().message()); } return std::move(*result); } Api::Result NetworkClient::executeWithRetry(const Void::Net::Request& req) { return Void::Retry::retry_while( retryPolicy_, [&] { return executeRequest(req); }, [this](const Api::Result& result) { if (!result) { Void::Log::warn("NetworkClient", "{}", std::format( "Request error: {}, retrying", result.error().message)); return true; } // Retry only on the configured retryable HTTP statuses. const auto& codes = retryableStatusCodes_; if (std::find(codes.begin(), codes.end(), result->status) != codes.end()) { Void::Log::warn("NetworkClient", "{}", std::format("Request failed with status {}, retrying", result->status)); return true; } return false; }); } Api::Result NetworkClient::request(const Void::Net::Request& req) { return executeWithRetry(req); } Api::Result NetworkClient::getDirect(std::string_view url, const std::vector& headers) { Void::Net::Request req; req.url = std::string(url); req.method = Void::Net::Method::Get; req.headers = headers; // Deliberately no proxy and no retry (see header). Void::Net::HttpClient client; auto result = client.send(req); if (!result) { return Api::fail(Api::ErrorCode::NetworkError, result.error().message()); } return std::move(*result); } Api::Result NetworkClient::get(std::string_view url, const std::vector& headers) { Void::Net::Request req; req.url = std::string(url); req.method = Void::Net::Method::Get; req.headers = headers; return request(req); } Api::Result NetworkClient::post(std::string_view url, std::string_view body, const std::vector& headers) { Void::Net::Request req; req.url = std::string(url); req.method = Void::Net::Method::Post; req.headers = headers; req.body = std::string(body); return request(req); } void NetworkClient::setProxy( const std::optional& proxy) { std::lock_guard lock(mutex_); proxy_ = proxy; } std::optional NetworkClient::getProxy() const { std::lock_guard lock(mutex_); return proxy_; } void NetworkClient::clearProxy() { std::lock_guard lock(mutex_); proxy_ = std::nullopt; } } // namespace Echoion::Core