92 lines
3.2 KiB
C++
92 lines
3.2 KiB
C++
#pragma once
|
|
|
|
#include <NetworkInterface.hpp>
|
|
#include <Types.hpp>
|
|
#include <Void/Functional/Retry.hpp>
|
|
#include <Void/Net/Http.hpp>
|
|
#include <atomic>
|
|
#include <cstdint>
|
|
#include <mutex>
|
|
#include <optional>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
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<void>
|
|
initialize(const std::optional<Void::Net::ProxyConfig>& proxy =
|
|
std::nullopt) override;
|
|
void shutdown() override;
|
|
|
|
// Synchronous requests
|
|
[[nodiscard]] Api::Result<Void::Net::Response> get(
|
|
std::string_view url,
|
|
const std::vector<Void::Net::Header>& headers = {}) override;
|
|
[[nodiscard]] Api::Result<Void::Net::Response> post(
|
|
std::string_view url, std::string_view body,
|
|
const std::vector<Void::Net::Header>& 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<Void::Net::Response> postVerifiedTls(
|
|
std::string_view url, std::string_view body,
|
|
const std::vector<Void::Net::Header>& headers = {});
|
|
[[nodiscard]] Api::Result<Void::Net::Response>
|
|
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<Void::Net::Response>
|
|
getDirect(std::string_view url,
|
|
const std::vector<Void::Net::Header>& headers = {});
|
|
|
|
// Proxy configuration
|
|
void setProxy(
|
|
const std::optional<Void::Net::ProxyConfig>& proxy) override;
|
|
[[nodiscard]] std::optional<Void::Net::ProxyConfig>
|
|
getProxy() const override;
|
|
void clearProxy() override;
|
|
|
|
private:
|
|
|
|
// Execute with retry/backoff (Void::Retry); single attempt via
|
|
// Void::Net::HttpClient.
|
|
[[nodiscard]] Api::Result<Void::Net::Response>
|
|
executeWithRetry(const Void::Net::Request& req);
|
|
[[nodiscard]] Api::Result<Void::Net::Response>
|
|
executeRequest(const Void::Net::Request& req);
|
|
|
|
std::optional<Void::Net::ProxyConfig> proxy_;
|
|
mutable std::mutex mutex_;
|
|
std::atomic<bool> initialized_{false};
|
|
|
|
// Retry policy + the HTTP statuses worth retrying. Internal
|
|
// defaults — retry tuning is not part of the public client API.
|
|
Void::Retry::Policy<Void::Retry::ExponentialBackoff> retryPolicy_{
|
|
.max_attempts = 4,
|
|
.backoff = Void::Retry::ExponentialBackoff{
|
|
.base = std::chrono::milliseconds(1000),
|
|
.max = std::chrono::milliseconds(30000),
|
|
.multiplier = 2.0}};
|
|
std::vector<std::uint16_t> retryableStatusCodes_{408, 429, 500,
|
|
502, 503, 504};
|
|
};
|
|
|
|
} // namespace Echoion::Core
|