#pragma once #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; // Security-sensitive HTTPS request. Unlike legacy media scraping // requests, this always verifies the peer certificate and follows // no redirects so credentials cannot be forwarded to another // origin. [[nodiscard]] Api::Result postVerifiedTls( std::string_view url, std::string_view body, const std::vector& headers = {}); [[nodiscard]] Api::Result request(const Void::Net::Request& req) override; // GET that never uses the configured proxy and does not retry — // for endpoints that must be reached directly and cheaply // (Invidious/Piped instance probes and API calls, public-IP // detection). Callers implement their own fallback across // instances, so retrying a dead one is counterproductive. [[nodiscard]] Api::Result getDirect(std::string_view url, const std::vector& headers = {}); // Proxy configuration void setProxy( const std::optional& proxy) override; [[nodiscard]] std::optional getProxy() const override; void clearProxy() override; private: // 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}; }; } // namespace Echoion::Core