Migrate to VoidLibrary (toolchain + API groundwork)

This commit is contained in:
faumaray committed 2026-06-22 15:53:18 +03:00
1 parent 5e1e3d66c4
commit e17cfed56f
38 files changed
+3077 -2505

No files matched your search

@@ -0,0 +1,56 @@
#pragma once
#include "NetworkTypes.hpp"
#include "Types.hpp"
#include <future>
#include <optional>
#include <string>
#include <vector>
namespace Echoion::Api
{
// Network client interface
class INetworkClient
{
public:
virtual ~INetworkClient() = default;
// Initialize with optional proxy
virtual Result<void> initialize(
const std::optional<ProxyConfig>& proxy = std::nullopt) = 0;
virtual void shutdown() = 0;
// Synchronous requests
[[nodiscard]] virtual Result<HttpResponse>
get(std::string_view url,
const std::vector<HttpHeader>& headers = {}) = 0;
[[nodiscard]] virtual Result<HttpResponse>
post(std::string_view url, std::string_view body,
const std::vector<HttpHeader>& headers = {}) = 0;
[[nodiscard]] virtual Result<HttpResponse>
request(const HttpRequest& req) = 0;
// Asynchronous requests
[[nodiscard]] virtual std::future<Result<HttpResponse>>
getAsync(std::string_view url,
const std::vector<HttpHeader>& headers = {}) = 0;
[[nodiscard]] virtual std::future<Result<HttpResponse>>
postAsync(std::string_view url, std::string_view body,
const std::vector<HttpHeader>& headers = {}) = 0;
[[nodiscard]] virtual std::future<Result<HttpResponse>>
requestAsync(const HttpRequest& req) = 0;
// Proxy configuration
virtual void setProxy(const std::optional<ProxyConfig>& proxy) = 0;
[[nodiscard]] virtual std::optional<ProxyConfig>
getProxy() const = 0;
virtual void clearProxy() = 0;
// Retry configuration
virtual void setRetryConfig(const RetryConfig& config) = 0;
[[nodiscard]] virtual RetryConfig getRetryConfig() const = 0;
};
} // namespace Echoion::Api
@@ -0,0 +1,167 @@
#pragma once
#include "Types.hpp"
#include <cctype>
#include <chrono>
#include <map>
#include <optional>
#include <string>
#include <vector>
namespace Echoion::Api
{
enum class HttpMethod : uint8_t {
GET = 0,
POST,
PUT,
DELETE,
PATCH,
HEAD,
OPTIONS
};
enum class ProxyType : uint8_t { HTTP = 0, SOCKS5 };
struct ProxyConfig {
ProxyType type = ProxyType::HTTP;
std::string host;
uint16_t port;
std::optional<std::string> username;
std::optional<std::string> password;
std::optional<std::chrono::milliseconds> timeout;
[[nodiscard]] bool isValid() const noexcept
{
return !host.empty() && port > 0;
}
[[nodiscard]] bool requiresAuth() const noexcept
{
return username.has_value() && password.has_value();
}
[[nodiscard]] bool isSocks5() const noexcept
{
return type == ProxyType::SOCKS5;
}
[[nodiscard]] bool
operator==(const ProxyConfig& other) const = default;
[[nodiscard]] bool
operator!=(const ProxyConfig& other) const = default;
};
struct HttpHeader {
std::string name;
std::string value;
};
struct HttpRequest {
std::string url;
HttpMethod method = HttpMethod::GET;
std::vector<HttpHeader> headers;
std::optional<std::vector<uint8_t>> body;
std::optional<ProxyConfig> proxy;
std::chrono::milliseconds timeout = 30000ms;
bool followRedirects = true;
uint32_t maxRedirects = 10;
};
struct HttpResponse {
int32_t statusCode;
// Using vector to support multiple headers with same name (e.g.,
// Set-Cookie)
std::vector<std::pair<std::string, std::string>> headers;
std::string body;
std::chrono::milliseconds responseTime;
std::optional<std::string> contentType;
bool fromCache = false;
[[nodiscard]] bool isSuccess() const noexcept
{
return statusCode >= 200 && statusCode < 300;
}
[[nodiscard]] bool isRedirect() const noexcept
{
return statusCode >= 300 && statusCode < 400;
}
[[nodiscard]] bool isError() const noexcept
{
return statusCode >= 400 || statusCode == 0;
}
[[nodiscard]] std::string bodyAsString() const
{
return std::string(body.begin(), body.end());
}
// Helper to get first header value by name (case-insensitive)
[[nodiscard]] std::optional<std::string>
getHeader(std::string_view name) const
{
for (const auto& [key, value] : headers) {
if (key.size() == name.size()) {
bool match = true;
for (size_t i = 0; i < key.size(); ++i) {
if (std::tolower(
static_cast<unsigned char>(key[i])) !=
std::tolower(
static_cast<unsigned char>(name[i]))) {
match = false;
break;
}
}
if (match) {
return value;
}
}
}
return std::nullopt;
}
// Helper to get all header values by name (case-insensitive) -
// useful for Set-Cookie
[[nodiscard]] std::vector<std::string>
getHeaders(std::string_view name) const
{
std::vector<std::string> result;
for (const auto& [key, value] : headers) {
if (key.size() == name.size()) {
bool match = true;
for (size_t i = 0; i < key.size(); ++i) {
if (std::tolower(
static_cast<unsigned char>(key[i])) !=
std::tolower(
static_cast<unsigned char>(name[i]))) {
match = false;
break;
}
}
if (match) {
result.push_back(value);
}
}
}
return result;
}
};
struct RateLimitInfo {
uint32_t limit;
uint32_t remaining;
std::optional<std::chrono::system_clock::time_point> resetTime;
};
struct RetryConfig {
uint32_t maxRetries = 3;
std::chrono::milliseconds initialDelay = 1000ms;
std::chrono::milliseconds maxDelay = 30000ms;
float backoffMultiplier = 2.0f;
std::vector<int32_t> retryableStatusCodes = {408, 429, 500,
502, 503, 504};
};
} // namespace Echoion::Api