Migrate core infrastructure to VoidLibrary
This commit is contained in:
1 parent
53801f2b53
commit
2bdb63766d
65 files changed
+16109
-12011
No files matched your search
@@ -1,202 +1,238 @@
|
||||
#pragma once
|
||||
|
||||
#include "boost/asio/ssl/context.hpp"
|
||||
#include <echoion/api.hpp>
|
||||
#include <glaze/glaze.hpp>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <future>
|
||||
#include <queue>
|
||||
#include <NetworkInterface.hpp>
|
||||
#include <NetworkTypes.hpp>
|
||||
#include <Types.hpp>
|
||||
#include <Void/Data/Json.hpp>
|
||||
#include <Void/Data/Reflect.hpp>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <future>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <thread>
|
||||
|
||||
// Boost.Beast includes needed for template function
|
||||
#include <boost/beast.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
|
||||
namespace Echoion::Core
|
||||
{
|
||||
|
||||
// Forward declaration for URL parsing
|
||||
struct UrlComponents;
|
||||
|
||||
class NetworkClient : public Api::INetworkClient
|
||||
{
|
||||
public:
|
||||
|
||||
NetworkClient();
|
||||
~NetworkClient() override;
|
||||
|
||||
namespace Echoion::Core {
|
||||
// Initialize with optional proxy
|
||||
Api::Result<void> initialize(
|
||||
const std::optional<Api::ProxyConfig>& proxy = std::nullopt);
|
||||
void shutdown();
|
||||
|
||||
// Forward declaration for URL parsing
|
||||
struct UrlComponents;
|
||||
// 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);
|
||||
|
||||
class NetworkClient : public Api::INetworkClient {
|
||||
public:
|
||||
NetworkClient();
|
||||
~NetworkClient() override;
|
||||
// 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);
|
||||
|
||||
// Initialize with optional proxy
|
||||
Api::Result<void> initialize(const std::optional<Api::ProxyConfig>& proxy = std::nullopt);
|
||||
void shutdown();
|
||||
// Concurrent batch requests
|
||||
[[nodiscard]] std::vector<Api::Result<Api::HttpResponse>>
|
||||
requestBatch(const std::vector<Api::HttpRequest>& requests);
|
||||
|
||||
// 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);
|
||||
// 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());
|
||||
}
|
||||
|
||||
// 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);
|
||||
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());
|
||||
}
|
||||
|
||||
// Concurrent batch requests
|
||||
[[nodiscard]] std::vector<Api::Result<Api::HttpResponse>> requestBatch(
|
||||
const std::vector<Api::HttpRequest>& requests);
|
||||
private:
|
||||
|
||||
// JSON helpers using glaze
|
||||
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());
|
||||
}
|
||||
|
||||
T result;
|
||||
auto ec = glz::read_json(result, response->bodyAsString());
|
||||
if (ec) {
|
||||
return std::unexpected(Api::Error(Api::ErrorCode::ParseError,
|
||||
std::format("JSON parse error: {}", glz::format_error(ec, response->bodyAsString()))));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
// Parse a response body into a reflectable type T via Void::Json.
|
||||
template <typename T>
|
||||
[[nodiscard]] static Api::Result<T>
|
||||
parseJson(const std::string& body)
|
||||
{
|
||||
auto parsed = Void::Json::parse(body);
|
||||
if (!parsed) {
|
||||
return std::unexpected(Api::Error(
|
||||
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 std::unexpected(Api::Error(
|
||||
Api::ErrorCode::ParseError,
|
||||
std::format("JSON decode error: field '{}': {}",
|
||||
value.error().field, value.error().message)));
|
||||
}
|
||||
return std::move(*value);
|
||||
}
|
||||
|
||||
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 = glz::write_json(body);
|
||||
auto response = post(url, jsonBody, headers);
|
||||
if (!response) {
|
||||
return std::unexpected(response.error());
|
||||
}
|
||||
|
||||
T result;
|
||||
auto ec = glz::read_json(result, response->bodyAsString());
|
||||
if (ec) {
|
||||
return std::unexpected(Api::Error(Api::ErrorCode::ParseError,
|
||||
std::format("JSON parse error: {}", glz::format_error(ec, response->bodyAsString()))));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public:
|
||||
|
||||
// Proxy configuration
|
||||
void setProxy(const std::optional<Api::ProxyConfig>& proxy);
|
||||
[[nodiscard]] std::optional<Api::ProxyConfig> getProxy() const;
|
||||
void clearProxy();
|
||||
// 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;
|
||||
// Retry configuration
|
||||
void setRetryConfig(const Api::RetryConfig& config);
|
||||
[[nodiscard]] Api::RetryConfig getRetryConfig() const;
|
||||
|
||||
private:
|
||||
// Request execution
|
||||
[[nodiscard]] Api::Result<Api::HttpResponse> executeWithRetry(const Api::HttpRequest& req);
|
||||
[[nodiscard]] Api::Result<Api::HttpResponse> executeRequest(const Api::HttpRequest& req);
|
||||
|
||||
// Proxy and direct execution methods
|
||||
[[nodiscard]] Api::HttpResponse executeWithProxy(
|
||||
boost::asio::io_context& ioc,
|
||||
boost::asio::ssl::context& ctx,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req,
|
||||
const Api::ProxyConfig& proxy,
|
||||
std::chrono::seconds timeout);
|
||||
|
||||
[[nodiscard]] Api::HttpResponse executeWithHttpProxy(
|
||||
boost::asio::io_context& ioc,
|
||||
boost::asio::ssl::context& ctx,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req,
|
||||
const Api::ProxyConfig& proxy,
|
||||
std::chrono::seconds timeout);
|
||||
|
||||
[[nodiscard]] Api::HttpResponse executeWithSocks5Proxy(
|
||||
boost::asio::io_context& ioc,
|
||||
boost::asio::ssl::context& ctx,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req,
|
||||
const Api::ProxyConfig& proxy,
|
||||
std::chrono::seconds timeout);
|
||||
|
||||
[[nodiscard]] Api::HttpResponse executeDirect(
|
||||
boost::asio::io_context& ioc,
|
||||
boost::asio::ssl::context& ctx,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req,
|
||||
std::chrono::seconds timeout);
|
||||
|
||||
// HTTP request methods
|
||||
// Note: sendHttpsRequest is a template, implemented in header below
|
||||
template<typename Stream>
|
||||
[[nodiscard]] Api::HttpResponse sendHttpsRequest(
|
||||
Stream& stream,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req);
|
||||
|
||||
[[nodiscard]] Api::HttpResponse sendHttpRequest(
|
||||
boost::asio::ip::tcp::socket& socket,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req);
|
||||
|
||||
[[nodiscard]] Api::HttpResponse sendHttpRequestThroughProxy(
|
||||
boost::asio::ip::tcp::socket& socket,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req,
|
||||
const Api::ProxyConfig& proxy);
|
||||
|
||||
// Request/Response building
|
||||
[[nodiscard]] boost::beast::http::request<boost::beast::http::string_body> buildBeastRequest(
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req);
|
||||
|
||||
[[nodiscard]] Api::HttpResponse convertBeastResponse(
|
||||
const boost::beast::http::response<boost::beast::http::dynamic_body>& beastResponse);
|
||||
|
||||
[[nodiscard]] std::vector<Api::HttpHeader> addDefaultHeaders(const std::vector<Api::HttpHeader>& headers);
|
||||
private:
|
||||
|
||||
std::optional<Api::ProxyConfig> proxy_;
|
||||
Api::RetryConfig retryConfig_;
|
||||
mutable std::mutex mutex_;
|
||||
std::atomic<bool> initialized_{false};
|
||||
std::atomic<uint32_t> requestId_{0};
|
||||
|
||||
// Thread pool for async operations
|
||||
std::vector<std::jthread> threadPool_;
|
||||
std::queue<std::packaged_task<Api::Result<Api::HttpResponse>()>> taskQueue_;
|
||||
std::mutex taskMutex_;
|
||||
std::condition_variable taskCv_;
|
||||
std::atomic<bool> stopThreads_{false};
|
||||
static constexpr size_t THREAD_POOL_SIZE = 4;
|
||||
};
|
||||
// Request execution
|
||||
[[nodiscard]] Api::Result<Api::HttpResponse>
|
||||
executeWithRetry(const Api::HttpRequest& req);
|
||||
[[nodiscard]] Api::Result<Api::HttpResponse>
|
||||
executeRequest(const Api::HttpRequest& req);
|
||||
|
||||
// Template implementation for sendHttpsRequest
|
||||
template<typename Stream>
|
||||
Api::HttpResponse NetworkClient::sendHttpsRequest(
|
||||
Stream& stream,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req) {
|
||||
namespace http = boost::beast::http;
|
||||
|
||||
// Build request
|
||||
http::request<http::string_body> request = buildBeastRequest(urlComp, req);
|
||||
|
||||
// Send
|
||||
http::write(stream, request);
|
||||
|
||||
// Read response
|
||||
boost::beast::flat_buffer buffer;
|
||||
http::response<http::dynamic_body> response;
|
||||
http::read(stream, buffer, response);
|
||||
|
||||
// Convert to Api::HttpResponse
|
||||
return convertBeastResponse(response);
|
||||
}
|
||||
// Proxy and direct execution methods
|
||||
[[nodiscard]] Api::HttpResponse
|
||||
executeWithProxy(boost::asio::io_context& ioc,
|
||||
boost::asio::ssl::context& ctx,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req,
|
||||
const Api::ProxyConfig& proxy,
|
||||
std::chrono::seconds timeout);
|
||||
|
||||
[[nodiscard]] Api::HttpResponse
|
||||
executeWithHttpProxy(boost::asio::io_context& ioc,
|
||||
boost::asio::ssl::context& ctx,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req,
|
||||
const Api::ProxyConfig& proxy,
|
||||
std::chrono::seconds timeout);
|
||||
|
||||
[[nodiscard]] Api::HttpResponse
|
||||
executeWithSocks5Proxy(boost::asio::io_context& ioc,
|
||||
boost::asio::ssl::context& ctx,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req,
|
||||
const Api::ProxyConfig& proxy,
|
||||
std::chrono::seconds timeout);
|
||||
|
||||
[[nodiscard]] Api::HttpResponse
|
||||
executeDirect(boost::asio::io_context& ioc,
|
||||
boost::asio::ssl::context& ctx,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req,
|
||||
std::chrono::seconds timeout);
|
||||
|
||||
// HTTP request methods
|
||||
// Note: sendHttpsRequest is a template, implemented in header below
|
||||
template <typename Stream>
|
||||
[[nodiscard]] Api::HttpResponse
|
||||
sendHttpsRequest(Stream& stream,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req);
|
||||
|
||||
[[nodiscard]] Api::HttpResponse
|
||||
sendHttpRequest(boost::asio::ip::tcp::socket& socket,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req);
|
||||
|
||||
[[nodiscard]] Api::HttpResponse sendHttpRequestThroughProxy(
|
||||
boost::asio::ip::tcp::socket& socket,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req,
|
||||
const Api::ProxyConfig& proxy);
|
||||
|
||||
// Request/Response building
|
||||
[[nodiscard]] boost::beast::http::request<
|
||||
boost::beast::http::string_body>
|
||||
buildBeastRequest(const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req);
|
||||
|
||||
[[nodiscard]] Api::HttpResponse convertBeastResponse(
|
||||
const boost::beast::http::response<
|
||||
boost::beast::http::dynamic_body>& beastResponse);
|
||||
|
||||
[[nodiscard]] std::vector<Api::HttpHeader>
|
||||
addDefaultHeaders(const std::vector<Api::HttpHeader>& headers);
|
||||
|
||||
std::optional<Api::ProxyConfig> proxy_;
|
||||
Api::RetryConfig retryConfig_;
|
||||
mutable std::mutex mutex_;
|
||||
std::atomic<bool> initialized_{false};
|
||||
std::atomic<uint32_t> requestId_{0};
|
||||
|
||||
// Thread pool for async operations
|
||||
std::vector<std::jthread> threadPool_;
|
||||
std::queue<std::packaged_task<Api::Result<Api::HttpResponse>()>>
|
||||
taskQueue_;
|
||||
std::mutex taskMutex_;
|
||||
std::condition_variable taskCv_;
|
||||
std::atomic<bool> stopThreads_{false};
|
||||
static constexpr size_t THREAD_POOL_SIZE = 4;
|
||||
};
|
||||
|
||||
// Template implementation for sendHttpsRequest
|
||||
template <typename Stream>
|
||||
Api::HttpResponse
|
||||
NetworkClient::sendHttpsRequest(Stream& stream,
|
||||
const UrlComponents& urlComp,
|
||||
const Api::HttpRequest& req)
|
||||
{
|
||||
namespace http = boost::beast::http;
|
||||
|
||||
// Build request
|
||||
http::request<http::string_body> request =
|
||||
buildBeastRequest(urlComp, req);
|
||||
|
||||
// Send
|
||||
http::write(stream, request);
|
||||
|
||||
// Read response
|
||||
boost::beast::flat_buffer buffer;
|
||||
http::response<http::dynamic_body> response;
|
||||
http::read(stream, buffer, response);
|
||||
|
||||
// Convert to Api::HttpResponse
|
||||
return convertBeastResponse(response);
|
||||
}
|
||||
|
||||
} // namespace Echoion::Core
|
||||
Reference in new issue
Block a user