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>
131 lines
4.0 KiB
C++
131 lines
4.0 KiB
C++
#pragma once
|
|
|
|
#include <NetworkInterface.hpp>
|
|
#include <NetworkTypes.hpp>
|
|
#include <Types.hpp>
|
|
#include <Void/Data/Json.hpp>
|
|
#include <Void/Data/Reflect.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.
|
|
class NetworkClient : public Api::INetworkClient
|
|
{
|
|
public:
|
|
|
|
NetworkClient();
|
|
~NetworkClient() override;
|
|
|
|
Api::Result<void> initialize(
|
|
const std::optional<Api::ProxyConfig>& proxy = std::nullopt);
|
|
void shutdown();
|
|
|
|
// Synchronous requests
|
|
[[nodiscard]] Api::Result<Api::HttpResponse>
|
|
get(std::string_view url,
|
|
const std::vector<Api::HttpHeader>& headers = {});
|
|
[[nodiscard]] Api::Result<Api::HttpResponse>
|
|
post(std::string_view url, std::string_view body,
|
|
const std::vector<Api::HttpHeader>& headers = {});
|
|
[[nodiscard]] Api::Result<Api::HttpResponse>
|
|
request(const Api::HttpRequest& req);
|
|
|
|
// Asynchronous requests
|
|
[[nodiscard]] std::future<Api::Result<Api::HttpResponse>>
|
|
getAsync(std::string_view url,
|
|
const std::vector<Api::HttpHeader>& headers = {});
|
|
[[nodiscard]] std::future<Api::Result<Api::HttpResponse>>
|
|
postAsync(std::string_view url, std::string_view body,
|
|
const std::vector<Api::HttpHeader>& headers = {});
|
|
[[nodiscard]] std::future<Api::Result<Api::HttpResponse>>
|
|
requestAsync(const Api::HttpRequest& req);
|
|
|
|
// Concurrent batch requests
|
|
[[nodiscard]] std::vector<Api::Result<Api::HttpResponse>>
|
|
requestBatch(const std::vector<Api::HttpRequest>& requests);
|
|
|
|
// JSON helpers using Void::Json reflection
|
|
template <typename T>
|
|
[[nodiscard]] Api::Result<T>
|
|
getJson(std::string_view url,
|
|
const std::vector<Api::HttpHeader>& headers = {})
|
|
{
|
|
auto response = get(url, headers);
|
|
if (!response) {
|
|
return std::unexpected(response.error());
|
|
}
|
|
return parseJson<T>(response->bodyAsString());
|
|
}
|
|
|
|
template <typename T, typename Body>
|
|
[[nodiscard]] Api::Result<T>
|
|
postJson(std::string_view url, const Body& body,
|
|
const std::vector<Api::HttpHeader>& 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->bodyAsString());
|
|
}
|
|
|
|
// Proxy configuration
|
|
void setProxy(const std::optional<Api::ProxyConfig>& proxy);
|
|
[[nodiscard]] std::optional<Api::ProxyConfig> getProxy() const;
|
|
void clearProxy();
|
|
|
|
// Retry configuration
|
|
void setRetryConfig(const Api::RetryConfig& config);
|
|
[[nodiscard]] Api::RetryConfig getRetryConfig() const;
|
|
|
|
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<Api::HttpResponse>
|
|
executeWithRetry(const Api::HttpRequest& req);
|
|
[[nodiscard]] Api::Result<Api::HttpResponse>
|
|
executeRequest(const Api::HttpRequest& req);
|
|
|
|
std::optional<Api::ProxyConfig> proxy_;
|
|
Api::RetryConfig retryConfig_;
|
|
mutable std::mutex mutex_;
|
|
std::atomic<bool> initialized_{false};
|
|
};
|
|
|
|
} // namespace Echoion::Core
|