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>
This commit is contained in:
1 parent
3af9aecbec
commit
9d56a6ff01
12 files changed
+158
-223
No files matched your search
+46
-124
@@ -7,56 +7,11 @@
|
||||
|
||||
namespace Echoion::Core
|
||||
{
|
||||
namespace
|
||||
{
|
||||
[[nodiscard]] Void::Net::Method to_void_method(Api::HttpMethod m)
|
||||
{
|
||||
switch (m) {
|
||||
case Api::HttpMethod::GET: return Void::Net::Method::Get;
|
||||
case Api::HttpMethod::POST: return Void::Net::Method::Post;
|
||||
case Api::HttpMethod::PUT: return Void::Net::Method::Put;
|
||||
case Api::HttpMethod::DELETE: return Void::Net::Method::Delete;
|
||||
case Api::HttpMethod::PATCH: return Void::Net::Method::Patch;
|
||||
case Api::HttpMethod::HEAD: return Void::Net::Method::Head;
|
||||
case Api::HttpMethod::OPTIONS: return Void::Net::Method::Options;
|
||||
}
|
||||
return Void::Net::Method::Get;
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Net::ProxyConfig
|
||||
to_void_proxy(const Api::ProxyConfig& p)
|
||||
{
|
||||
Void::Net::ProxyConfig out;
|
||||
out.kind = p.isSocks5() ? Void::Net::ProxyConfig::Kind::Socks5
|
||||
: Void::Net::ProxyConfig::Kind::Http;
|
||||
out.host = p.host;
|
||||
out.port = p.port;
|
||||
out.username = p.username;
|
||||
out.password = p.password;
|
||||
return out;
|
||||
}
|
||||
|
||||
[[nodiscard]] Api::HttpResponse
|
||||
to_api_response(Void::Net::Response&& r)
|
||||
{
|
||||
Api::HttpResponse out;
|
||||
out.statusCode = static_cast<int32_t>(r.status);
|
||||
out.responseTime = r.elapsed;
|
||||
out.body = std::move(r.body);
|
||||
out.headers.reserve(r.headers.size());
|
||||
for (auto& h : r.headers) {
|
||||
out.headers.emplace_back(std::move(h.name),
|
||||
std::move(h.value));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
NetworkClient::NetworkClient() = default;
|
||||
NetworkClient::~NetworkClient() { shutdown(); }
|
||||
|
||||
Api::Result<void>
|
||||
NetworkClient::initialize(const std::optional<Api::ProxyConfig>& proxy)
|
||||
Api::Result<void> NetworkClient::initialize(
|
||||
const std::optional<Void::Net::ProxyConfig>& proxy)
|
||||
{
|
||||
proxy_ = proxy;
|
||||
initialized_ = true;
|
||||
@@ -67,131 +22,109 @@ namespace Echoion::Core
|
||||
|
||||
void NetworkClient::shutdown() { initialized_ = false; }
|
||||
|
||||
Api::Result<Api::HttpResponse> NetworkClient::executeRequest(
|
||||
const Api::HttpRequest& req)
|
||||
Api::Result<Void::Net::Response>
|
||||
NetworkClient::executeRequest(const Void::Net::Request& req)
|
||||
{
|
||||
Void::Net::HttpClient client;
|
||||
const auto active = req.proxy.has_value() ? req.proxy : proxy_;
|
||||
if (active && active->isValid()) {
|
||||
client.set_proxy(to_void_proxy(*active));
|
||||
const auto active = proxy_;
|
||||
if (active && active->enabled()) {
|
||||
client.set_proxy(*active);
|
||||
}
|
||||
|
||||
Void::Net::Request vreq;
|
||||
vreq.method = to_void_method(req.method);
|
||||
vreq.url = req.url;
|
||||
vreq.timeout =
|
||||
std::chrono::duration_cast<std::chrono::seconds>(req.timeout);
|
||||
for (const auto& h : req.headers) {
|
||||
vreq.headers.push_back({h.name, h.value});
|
||||
}
|
||||
if (req.body) {
|
||||
vreq.body = std::string(req.body->begin(), req.body->end());
|
||||
}
|
||||
|
||||
auto result = client.send(vreq);
|
||||
auto result = client.send(req);
|
||||
if (!result) {
|
||||
return Api::fail(Api::ErrorCode::NetworkError,
|
||||
result.error().message());
|
||||
result.error().message());
|
||||
}
|
||||
return to_api_response(std::move(*result));
|
||||
return std::move(*result);
|
||||
}
|
||||
|
||||
Api::Result<Api::HttpResponse> NetworkClient::executeWithRetry(
|
||||
const Api::HttpRequest& req)
|
||||
Api::Result<Void::Net::Response>
|
||||
NetworkClient::executeWithRetry(const Void::Net::Request& req)
|
||||
{
|
||||
const Void::Retry::Policy<Void::Retry::ExponentialBackoff> policy{
|
||||
.max_attempts =
|
||||
static_cast<std::size_t>(retryConfig_.maxRetries) + 1,
|
||||
.backoff = Void::Retry::ExponentialBackoff{
|
||||
.base = retryConfig_.initialDelay,
|
||||
.max = retryConfig_.maxDelay,
|
||||
.multiplier =
|
||||
static_cast<double>(retryConfig_.backoffMultiplier)}};
|
||||
|
||||
return Void::Retry::retry_while(
|
||||
policy, [&] { return executeRequest(req); },
|
||||
[this](const Api::Result<Api::HttpResponse>& result) {
|
||||
retryPolicy_, [&] { return executeRequest(req); },
|
||||
[this](const Api::Result<Void::Net::Response>& result) {
|
||||
if (!result) {
|
||||
ECHOION_LOG_WARNING(std::format(
|
||||
"Request error: {}, retrying", result.error().message));
|
||||
return true;
|
||||
}
|
||||
if (result->isError()) {
|
||||
const auto& codes = retryConfig_.retryableStatusCodes;
|
||||
if (std::find(codes.begin(), codes.end(),
|
||||
result->statusCode) != codes.end()) {
|
||||
ECHOION_LOG_WARNING(std::format(
|
||||
"Request failed with status {}, retrying",
|
||||
result->statusCode));
|
||||
return true;
|
||||
}
|
||||
// Retry only on the configured retryable HTTP statuses.
|
||||
const auto& codes = retryableStatusCodes_;
|
||||
if (std::find(codes.begin(), codes.end(), result->status) !=
|
||||
codes.end()) {
|
||||
ECHOION_LOG_WARNING(
|
||||
std::format("Request failed with status {}, retrying",
|
||||
result->status));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
Api::Result<Api::HttpResponse>
|
||||
NetworkClient::request(const Api::HttpRequest& req)
|
||||
Api::Result<Void::Net::Response>
|
||||
NetworkClient::request(const Void::Net::Request& req)
|
||||
{
|
||||
return executeWithRetry(req);
|
||||
}
|
||||
|
||||
Api::Result<Api::HttpResponse>
|
||||
Api::Result<Void::Net::Response>
|
||||
NetworkClient::get(std::string_view url,
|
||||
const std::vector<Api::HttpHeader>& headers)
|
||||
const std::vector<Void::Net::Header>& headers)
|
||||
{
|
||||
Api::HttpRequest req;
|
||||
Void::Net::Request req;
|
||||
req.url = std::string(url);
|
||||
req.method = Api::HttpMethod::GET;
|
||||
req.method = Void::Net::Method::Get;
|
||||
req.headers = headers;
|
||||
return request(req);
|
||||
}
|
||||
|
||||
Api::Result<Api::HttpResponse>
|
||||
Api::Result<Void::Net::Response>
|
||||
NetworkClient::post(std::string_view url, std::string_view body,
|
||||
const std::vector<Api::HttpHeader>& headers)
|
||||
const std::vector<Void::Net::Header>& headers)
|
||||
{
|
||||
Api::HttpRequest req;
|
||||
Void::Net::Request req;
|
||||
req.url = std::string(url);
|
||||
req.method = Api::HttpMethod::POST;
|
||||
req.method = Void::Net::Method::Post;
|
||||
req.headers = headers;
|
||||
req.body = std::vector<uint8_t>(body.begin(), body.end());
|
||||
req.body = std::string(body);
|
||||
return request(req);
|
||||
}
|
||||
|
||||
std::future<Api::Result<Api::HttpResponse>>
|
||||
std::future<Api::Result<Void::Net::Response>>
|
||||
NetworkClient::getAsync(std::string_view url,
|
||||
const std::vector<Api::HttpHeader>& headers)
|
||||
const std::vector<Void::Net::Header>& headers)
|
||||
{
|
||||
return pool_.submit([this, u = std::string(url), headers] {
|
||||
return get(u, headers);
|
||||
});
|
||||
}
|
||||
|
||||
std::future<Api::Result<Api::HttpResponse>>
|
||||
std::future<Api::Result<Void::Net::Response>>
|
||||
NetworkClient::postAsync(std::string_view url, std::string_view body,
|
||||
const std::vector<Api::HttpHeader>& headers)
|
||||
const std::vector<Void::Net::Header>& headers)
|
||||
{
|
||||
return pool_.submit([this, u = std::string(url), b = std::string(body),
|
||||
headers] { return post(u, b, headers); });
|
||||
}
|
||||
|
||||
std::future<Api::Result<Api::HttpResponse>>
|
||||
NetworkClient::requestAsync(const Api::HttpRequest& req)
|
||||
std::future<Api::Result<Void::Net::Response>>
|
||||
NetworkClient::requestAsync(const Void::Net::Request& req)
|
||||
{
|
||||
return pool_.submit([this, req] { return request(req); });
|
||||
}
|
||||
|
||||
std::vector<Api::Result<Api::HttpResponse>>
|
||||
std::vector<Api::Result<Void::Net::Response>>
|
||||
NetworkClient::requestBatch(
|
||||
const std::vector<Api::HttpRequest>& requests)
|
||||
const std::vector<Void::Net::Request>& requests)
|
||||
{
|
||||
std::vector<std::future<Api::Result<Api::HttpResponse>>> futures;
|
||||
std::vector<std::future<Api::Result<Void::Net::Response>>> futures;
|
||||
futures.reserve(requests.size());
|
||||
for (const auto& req : requests) {
|
||||
futures.push_back(requestAsync(req));
|
||||
}
|
||||
std::vector<Api::Result<Api::HttpResponse>> results;
|
||||
std::vector<Api::Result<Void::Net::Response>> results;
|
||||
results.reserve(requests.size());
|
||||
for (auto& f : futures) {
|
||||
results.push_back(f.get());
|
||||
@@ -199,13 +132,14 @@ namespace Echoion::Core
|
||||
return results;
|
||||
}
|
||||
|
||||
void NetworkClient::setProxy(const std::optional<Api::ProxyConfig>& proxy)
|
||||
void NetworkClient::setProxy(
|
||||
const std::optional<Void::Net::ProxyConfig>& proxy)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
proxy_ = proxy;
|
||||
}
|
||||
|
||||
std::optional<Api::ProxyConfig> NetworkClient::getProxy() const
|
||||
std::optional<Void::Net::ProxyConfig> NetworkClient::getProxy() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return proxy_;
|
||||
@@ -217,16 +151,4 @@ namespace Echoion::Core
|
||||
proxy_ = std::nullopt;
|
||||
}
|
||||
|
||||
void NetworkClient::setRetryConfig(const Api::RetryConfig& config)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
retryConfig_ = config;
|
||||
}
|
||||
|
||||
Api::RetryConfig NetworkClient::getRetryConfig() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return retryConfig_;
|
||||
}
|
||||
|
||||
} // namespace Echoion::Core
|
||||
Reference in new issue
Block a user