#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Echoion::Core { // HTTP client implementing Api::INetworkClient. A thin adapter over // Void::Net::HttpClient (HTTP/HTTPS, TLS, SOCKS5 + HTTP-CONNECT proxies); // retry/backoff is layered here via Void::Retry. class NetworkClient : public Api::INetworkClient { public: NetworkClient(); ~NetworkClient() override; Api::Result initialize( const std::optional& proxy = std::nullopt); void shutdown(); // Synchronous requests [[nodiscard]] Api::Result get(std::string_view url, const std::vector& headers = {}); [[nodiscard]] Api::Result post(std::string_view url, std::string_view body, const std::vector& headers = {}); [[nodiscard]] Api::Result request(const Api::HttpRequest& req); // Asynchronous requests [[nodiscard]] std::future> getAsync(std::string_view url, const std::vector& headers = {}); [[nodiscard]] std::future> postAsync(std::string_view url, std::string_view body, const std::vector& headers = {}); [[nodiscard]] std::future> requestAsync(const Api::HttpRequest& req); // Concurrent batch requests [[nodiscard]] std::vector> requestBatch(const std::vector& requests); // JSON helpers using Void::Json reflection template [[nodiscard]] Api::Result getJson(std::string_view url, const std::vector& headers = {}) { auto response = get(url, headers); if (!response) { return std::unexpected(response.error()); } return parseJson(response->bodyAsString()); } template [[nodiscard]] Api::Result postJson(std::string_view url, const Body& body, const std::vector& headers = {}) { std::string jsonBody = Void::Json::serialize(Void::Json::to_json(body)); auto response = post(url, jsonBody, headers); if (!response) { return std::unexpected(response.error()); } return parseJson(response->bodyAsString()); } // Proxy configuration void setProxy(const std::optional& proxy); [[nodiscard]] std::optional getProxy() const; void clearProxy(); // Retry configuration void setRetryConfig(const Api::RetryConfig& config); [[nodiscard]] Api::RetryConfig getRetryConfig() const; private: template [[nodiscard]] static Api::Result parseJson(const std::string& body) { auto parsed = Void::Json::parse(body); if (!parsed) { return Api::fail( Api::ErrorCode::ParseError, std::format("JSON parse error ({}:{}): {}", parsed.error().line, parsed.error().col, parsed.error().message)); } auto value = Void::Json::from_json(*parsed); if (!value) { return Api::fail( Api::ErrorCode::ParseError, std::format("JSON decode error: field '{}': {}", value.error().field, value.error().message)); } return std::move(*value); } // Execute with retry/backoff (Void::Retry); single attempt via // Void::Net::HttpClient. [[nodiscard]] Api::Result executeWithRetry(const Api::HttpRequest& req); [[nodiscard]] Api::Result executeRequest(const Api::HttpRequest& req); std::optional proxy_; Api::RetryConfig retryConfig_; mutable std::mutex mutex_; std::atomic initialized_{false}; // Worker pool backing the *Async / requestBatch fan-outs, replacing // a per-call std::async OS-thread spawn with bounded thread reuse. Void::Concurrency::ThreadPool pool_; }; } // namespace Echoion::Core