#pragma once #include #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. Speaks Void::Net types // directly — no echoion-side HTTP mirror. class NetworkClient : public Api::INetworkClient { public: NetworkClient(); ~NetworkClient() override; Api::Result initialize( const std::optional& proxy = std::nullopt) override; void shutdown() override; // Synchronous requests [[nodiscard]] Api::Result get(std::string_view url, const std::vector& headers = {}) override; [[nodiscard]] Api::Result post(std::string_view url, std::string_view body, const std::vector& headers = {}) override; [[nodiscard]] Api::Result request(const Void::Net::Request& req) override; // Asynchronous requests [[nodiscard]] std::future> getAsync(std::string_view url, const std::vector& headers = {}) override; [[nodiscard]] std::future> postAsync(std::string_view url, std::string_view body, const std::vector& headers = {}) override; [[nodiscard]] std::future> requestAsync(const Void::Net::Request& req) override; // 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->body); } 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->body); } // Proxy configuration void setProxy( const std::optional& proxy) override; [[nodiscard]] std::optional getProxy() const override; void clearProxy() override; 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 Void::Net::Request& req); [[nodiscard]] Api::Result executeRequest(const Void::Net::Request& req); std::optional proxy_; mutable std::mutex mutex_; std::atomic initialized_{false}; // Retry policy + the HTTP statuses worth retrying. Internal // defaults — retry tuning is not part of the public client API. Void::Retry::Policy retryPolicy_{ .max_attempts = 4, .backoff = Void::Retry::ExponentialBackoff{ .base = std::chrono::milliseconds(1000), .max = std::chrono::milliseconds(30000), .multiplier = 2.0}}; std::vector retryableStatusCodes_{408, 429, 500, 502, 503, 504}; // 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