Mechanically convert std::unexpected(Api::Error(ErrorCode::X, msg[, src])) to Api::fail(ErrorCode::X, msg[, src]) across Core. Pure syntax; forwards of existing errors (std::unexpected(x.error())) are unchanged. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
238 lines
6.6 KiB
C++
238 lines
6.6 KiB
C++
#include "echoion/core/Logger.hpp"
|
|
#include "echoion/core/NetworkClient.hpp"
|
|
#include <Void/Functional/Retry.hpp>
|
|
#include <Void/Net/Http.hpp>
|
|
#include <algorithm>
|
|
#include <format>
|
|
|
|
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)
|
|
{
|
|
proxy_ = proxy;
|
|
initialized_ = true;
|
|
ECHOION_LOG_INFO(std::format("NetworkClient initialized (proxy: {})",
|
|
proxy.has_value() ? "enabled" : "disabled"));
|
|
return {};
|
|
}
|
|
|
|
void NetworkClient::shutdown() { initialized_ = false; }
|
|
|
|
Api::Result<Api::HttpResponse> NetworkClient::executeRequest(
|
|
const Api::HttpRequest& 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));
|
|
}
|
|
|
|
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);
|
|
if (!result) {
|
|
return Api::fail(Api::ErrorCode::NetworkError,
|
|
result.error().message());
|
|
}
|
|
return to_api_response(std::move(*result));
|
|
}
|
|
|
|
Api::Result<Api::HttpResponse> NetworkClient::executeWithRetry(
|
|
const Api::HttpRequest& 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) {
|
|
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;
|
|
}
|
|
}
|
|
return false;
|
|
});
|
|
}
|
|
|
|
Api::Result<Api::HttpResponse>
|
|
NetworkClient::request(const Api::HttpRequest& req)
|
|
{
|
|
return executeWithRetry(req);
|
|
}
|
|
|
|
Api::Result<Api::HttpResponse>
|
|
NetworkClient::get(std::string_view url,
|
|
const std::vector<Api::HttpHeader>& headers)
|
|
{
|
|
Api::HttpRequest req;
|
|
req.url = std::string(url);
|
|
req.method = Api::HttpMethod::GET;
|
|
req.headers = headers;
|
|
return request(req);
|
|
}
|
|
|
|
Api::Result<Api::HttpResponse>
|
|
NetworkClient::post(std::string_view url, std::string_view body,
|
|
const std::vector<Api::HttpHeader>& headers)
|
|
{
|
|
Api::HttpRequest req;
|
|
req.url = std::string(url);
|
|
req.method = Api::HttpMethod::POST;
|
|
req.headers = headers;
|
|
req.body = std::vector<uint8_t>(body.begin(), body.end());
|
|
return request(req);
|
|
}
|
|
|
|
std::future<Api::Result<Api::HttpResponse>>
|
|
NetworkClient::getAsync(std::string_view url,
|
|
const std::vector<Api::HttpHeader>& headers)
|
|
{
|
|
return std::async(std::launch::async,
|
|
[this, u = std::string(url), headers] {
|
|
return get(u, headers);
|
|
});
|
|
}
|
|
|
|
std::future<Api::Result<Api::HttpResponse>>
|
|
NetworkClient::postAsync(std::string_view url, std::string_view body,
|
|
const std::vector<Api::HttpHeader>& headers)
|
|
{
|
|
return std::async(std::launch::async,
|
|
[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)
|
|
{
|
|
return std::async(std::launch::async,
|
|
[this, req] { return request(req); });
|
|
}
|
|
|
|
std::vector<Api::Result<Api::HttpResponse>>
|
|
NetworkClient::requestBatch(
|
|
const std::vector<Api::HttpRequest>& requests)
|
|
{
|
|
std::vector<std::future<Api::Result<Api::HttpResponse>>> futures;
|
|
futures.reserve(requests.size());
|
|
for (const auto& req : requests) {
|
|
futures.push_back(requestAsync(req));
|
|
}
|
|
std::vector<Api::Result<Api::HttpResponse>> results;
|
|
results.reserve(requests.size());
|
|
for (auto& f : futures) {
|
|
results.push_back(f.get());
|
|
}
|
|
return results;
|
|
}
|
|
|
|
void NetworkClient::setProxy(const std::optional<Api::ProxyConfig>& proxy)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
proxy_ = proxy;
|
|
}
|
|
|
|
std::optional<Api::ProxyConfig> NetworkClient::getProxy() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return proxy_;
|
|
}
|
|
|
|
void NetworkClient::clearProxy()
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
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
|