Files
EchoionCore/include/echoion/core/NetworkClient.hpp
T
faumarayandClaude Opus 4.8 9d56a6ff01 Consume Void::Net directly in NetworkClient and callers
Delete NetworkClient's to_void_method/to_void_proxy/to_api_response bridge
and the Api::RetryConfig member; the client builds Void::Net::Request, sends
via Void::Net::HttpClient, returns Void::Net::Response, and retries via an
internal Void::Retry policy + retryable-status list.

Call sites updated to Void::Net member names: isSuccess()->ok(),
statusCode->status, getHeader()->header() (optional<string_view>),
isRedirect()->status range check; proxy isValid()->enabled(),
requiresAuth()->has_auth(), isSocks5()->kind == Kind::Socks5. Proxy fields
flow through YtDlpManager/AudioDecoder/MusicManager unchanged in spelling.
On-disk proxy shape is now Void::Net::ProxyConfig (kind: None/Http/Socks5);
config.example.json updated and proxy round-trip verified. Build green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-23 13:49:26 +03:00

150 lines
4.8 KiB
C++

#pragma once
#include <NetworkInterface.hpp>
#include <Types.hpp>
#include <Void/Async/ThreadPool.hpp>
#include <Void/Data/Json.hpp>
#include <Void/Data/Reflect.hpp>
#include <Void/Functional/Retry.hpp>
#include <Void/Net/Http.hpp>
#include <atomic>
#include <format>
#include <future>
#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;
[[nodiscard]] Api::Result<Void::Net::Response>
request(const Void::Net::Request& req) override;
// Asynchronous requests
[[nodiscard]] std::future<Api::Result<Void::Net::Response>>
getAsync(std::string_view url,
const std::vector<Void::Net::Header>& headers = {})
override;
[[nodiscard]] std::future<Api::Result<Void::Net::Response>>
postAsync(std::string_view url, std::string_view body,
const std::vector<Void::Net::Header>& headers = {})
override;
[[nodiscard]] std::future<Api::Result<Void::Net::Response>>
requestAsync(const Void::Net::Request& req) override;
// Concurrent batch requests
[[nodiscard]] std::vector<Api::Result<Void::Net::Response>>
requestBatch(const std::vector<Void::Net::Request>& requests);
// JSON helpers using Void::Json reflection
template <typename T>
[[nodiscard]] Api::Result<T>
getJson(std::string_view url,
const std::vector<Void::Net::Header>& headers = {})
{
auto response = get(url, headers);
if (!response) {
return std::unexpected(response.error());
}
return parseJson<T>(response->body);
}
template <typename T, typename Body>
[[nodiscard]] Api::Result<T>
postJson(std::string_view url, const Body& body,
const std::vector<Void::Net::Header>& 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<T>(response->body);
}
// 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:
template <typename T>
[[nodiscard]] static Api::Result<T>
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<T>(*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<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};
// 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