feat(shorturl): complete P2 M-SHORTURL-01..04 V2 local, storage, and remote providers with frozen V1

This commit is contained in:
faumaray committed 2026-11-27 01:27:32 +03:00
1 parent 658c21a2fa
commit 691808eab6
13 files changed
+2317 -1

No files matched your search

+368
View File
@@ -0,0 +1,368 @@
#include <Void/ShortUrlRemote/RemoteProvider.hpp>
#include <Void/Data/StrictJson.hpp>
#include <charconv>
#include <format>
#include <string>
#include <string_view>
namespace Void::ShortUrl::V2
{
namespace
{
[[nodiscard]] std::uint32_t
parse_retry_after(const Void::Net::Response& resp) noexcept
{
auto hdr = resp.header("Retry-After");
if (!hdr.has_value() || hdr->empty()) {
return 0;
}
std::uint32_t sec = 0;
const auto [ptr, ec] =
std::from_chars(hdr->data(), hdr->data() + hdr->size(), sec);
if (ec != std::errc{} || ptr != hdr->data() + hdr->size()) {
return 0;
}
return sec;
}
[[nodiscard]] const std::string*
find_string(const Void::Json::Strict::Value::Object& obj,
std::string_view key) noexcept
{
const auto it = obj.find(key);
if (it == obj.end()) {
return nullptr;
}
return std::get_if<std::string>(&it->second.value);
}
[[nodiscard]] std::optional<std::int64_t>
find_i64(const Void::Json::Strict::Value::Object& obj,
std::string_view key) noexcept
{
const auto it = obj.find(key);
if (it == obj.end()) {
return std::nullopt;
}
if (const auto* v = std::get_if<std::int64_t>(&it->second.value)) {
return *v;
}
return std::nullopt;
}
[[nodiscard]] V2ErrorCode
map_http_status_to_v2(std::uint16_t status) noexcept
{
switch (status) {
case 400: return V2ErrorCode::InvalidRequest;
case 401:
case 403: return V2ErrorCode::Unauthorized;
case 404: return V2ErrorCode::NotFound;
case 408: return V2ErrorCode::Timeout;
case 409: return V2ErrorCode::Conflict;
case 410: return V2ErrorCode::Revoked;
case 413: return V2ErrorCode::ResponseTooLarge;
case 422: return V2ErrorCode::InvalidUrl;
case 429: return V2ErrorCode::RateLimited;
case 501: return V2ErrorCode::Unsupported;
case 503: return V2ErrorCode::Unavailable;
case 504: return V2ErrorCode::Timeout;
default: return V2ErrorCode::ProviderError;
}
}
} // namespace
RemoteProvider::RemoteProvider(RemoteProviderOptions options)
: m_options(std::move(options)), m_client(m_options.http_options)
{
while (m_options.base_url.size() > 1U &&
m_options.base_url.back() == '/') {
m_options.base_url.pop_back();
}
m_client.set_max_body_size(m_options.max_response_bytes);
}
std::string_view RemoteProvider::provider_id() const noexcept
{
return m_options.provider_id;
}
V1::Health RemoteProvider::health() const
{
if (m_options.base_url.empty()) {
return {V1::HealthStatus::unhealthy, "empty remote base_url"};
}
return {V1::HealthStatus::healthy, "remote provider configured"};
}
bool RemoteProvider::supports_resolve() const noexcept
{
return m_options.capabilities.supports_resolve;
}
std::uint32_t RemoteProvider::last_retry_after_seconds() const noexcept
{
return m_last_retry_after.load(std::memory_order_acquire);
}
Void::Result::Result<Link>
RemoteProvider::shorten(const CreateLink& request,
const ShortenOptions& options)
{
m_last_retry_after.store(0, std::memory_order_release);
// Check capability flags BEFORE any network mutation.
if (!request.custom_key.empty() &&
!m_options.capabilities.supports_custom_key) {
return std::unexpected{make_shorturl_error(
V2ErrorCode::Unsupported,
"ShortUrlRemote: provider does not support custom keys")};
}
if (request.expires_at_unix_seconds != 0 &&
!m_options.capabilities.supports_expiry) {
return std::unexpected{make_shorturl_error(
V2ErrorCode::Unsupported,
"ShortUrlRemote: provider does not support link expiry")};
}
if (auto u = validate_target_url(request.target_url); !u.has_value()) {
return std::unexpected{std::move(u.error())};
}
if (!request.custom_key.empty()) {
if (auto k = validate_custom_key(request.custom_key);
!k.has_value()) {
return std::unexpected{std::move(k.error())};
}
}
Void::Json::Strict::Value::Object obj;
obj.emplace("target_url",
Void::Json::Strict::Value{request.target_url});
if (!request.custom_key.empty()) {
obj.emplace("custom_key",
Void::Json::Strict::Value{request.custom_key});
}
if (request.expires_at_unix_seconds != 0) {
obj.emplace(
"expires_at_unix_seconds",
Void::Json::Strict::Value{request.expires_at_unix_seconds});
}
if (!options.operation_id.empty()) {
obj.emplace("operation_id",
Void::Json::Strict::Value{options.operation_id});
}
auto body = Void::Json::Strict::serialize(
Void::Json::Strict::Value{std::move(obj)});
if (!body.has_value()) {
return std::unexpected{make_shorturl_error(
V2ErrorCode::InvalidRequest,
std::format("ShortUrlRemote: serialize failed: {}",
body.error().message))};
}
Void::Net::Request req;
req.method = Void::Net::Method::Post;
req.url = m_options.base_url + "/v2/shorten";
req.headers = m_options.headers;
req.headers.push_back({"Content-Type", "application/json"});
if (!options.operation_id.empty()) {
req.headers.push_back({"Idempotency-Key", options.operation_id});
}
req.body = std::move(*body);
req.timeout = m_options.timeout;
auto resp = m_client.send(req);
if (!resp.has_value()) {
// Transport timeout or lost reply after mutation send: classify as
// UnknownOutcome without fabricating a local rollback or replay.
if (resp.error().kind() == Void::Result::ErrorKind::timeout ||
resp.error().kind() == Void::Result::ErrorKind::io) {
return std::unexpected{make_shorturl_error(
V2ErrorCode::UnknownOutcome,
std::format("ShortUrlRemote: shorten outcome unknown: {}",
resp.error().message()))};
}
if (resp.error().kind() == Void::Result::ErrorKind::exhausted) {
return std::unexpected{make_shorturl_error(
V2ErrorCode::ResponseTooLarge,
std::format("ShortUrlRemote: response too large: {}",
resp.error().message()))};
}
return std::unexpected{make_shorturl_error(
V2ErrorCode::Unavailable,
std::format("ShortUrlRemote: transport error: {}",
resp.error().message()))};
}
if (resp->body.size() > m_options.max_response_bytes) {
return std::unexpected{make_shorturl_error(
V2ErrorCode::ResponseTooLarge,
"ShortUrlRemote: response exceeds max_response_bytes")};
}
const std::uint32_t retry_after = parse_retry_after(*resp);
if (retry_after > 0) {
m_last_retry_after.store(retry_after, std::memory_order_release);
}
if (!resp->ok()) {
return std::unexpected{make_shorturl_error(
map_http_status_to_v2(resp->status),
std::format("ShortUrlRemote: shorten HTTP {}", resp->status))};
}
auto parsed = Void::Json::Strict::parse(resp->body);
if (!parsed.has_value()) {
return std::unexpected{make_shorturl_error(
V2ErrorCode::ProviderError,
"ShortUrlRemote: invalid JSON response")};
}
const auto* root =
std::get_if<Void::Json::Strict::Value::Object>(&parsed->value);
if (root == nullptr) {
return std::unexpected{make_shorturl_error(
V2ErrorCode::ProviderError,
"ShortUrlRemote: expected JSON object")};
}
const auto* key = find_string(*root, "key");
const auto* short_url = find_string(*root, "short_url");
if (key == nullptr || key->empty() || short_url == nullptr ||
short_url->empty()) {
return std::unexpected{make_shorturl_error(
V2ErrorCode::ProviderError,
"ShortUrlRemote: response missing key or short_url")};
}
const auto* target = find_string(*root, "target_url");
return Link{
.key = *key,
.short_url = *short_url,
.target_url = target ? *target : request.target_url,
.expires_at_unix_seconds =
find_i64(*root, "expires_at_unix_seconds")
.value_or(request.expires_at_unix_seconds),
.retry_after_seconds = retry_after,
};
}
Void::Result::Result<Link> RemoteProvider::resolve(std::string_view key)
{
m_last_retry_after.store(0, std::memory_order_release);
if (!m_options.capabilities.supports_resolve) {
return std::unexpected{make_shorturl_error(
V2ErrorCode::Unsupported,
"ShortUrlRemote: provider does not support resolve")};
}
if (auto k = validate_custom_key(key); !k.has_value()) {
return std::unexpected{std::move(k.error())};
}
Void::Net::Request req;
req.method = Void::Net::Method::Get;
req.url = std::format("{}/v2/resolve/{}", m_options.base_url, key);
req.headers = m_options.headers;
req.timeout = m_options.timeout;
auto resp = m_client.send(req);
if (!resp.has_value()) {
if (resp.error().kind() == Void::Result::ErrorKind::timeout) {
return std::unexpected{make_shorturl_error(
V2ErrorCode::Timeout,
std::format("ShortUrlRemote: resolve timed out: {}",
resp.error().message()))};
}
if (resp.error().kind() == Void::Result::ErrorKind::exhausted) {
return std::unexpected{make_shorturl_error(
V2ErrorCode::ResponseTooLarge,
"ShortUrlRemote: resolve response too large")};
}
return std::unexpected{make_shorturl_error(
V2ErrorCode::Unavailable,
std::format("ShortUrlRemote: resolve transport failed: {}",
resp.error().message()))};
}
const std::uint32_t retry_after = parse_retry_after(*resp);
if (retry_after > 0) {
m_last_retry_after.store(retry_after, std::memory_order_release);
}
if (!resp->ok()) {
return std::unexpected{make_shorturl_error(
map_http_status_to_v2(resp->status),
std::format("ShortUrlRemote: resolve HTTP {}", resp->status))};
}
auto parsed = Void::Json::Strict::parse(resp->body);
if (!parsed.has_value()) {
return std::unexpected{make_shorturl_error(
V2ErrorCode::ProviderError,
"ShortUrlRemote: invalid JSON resolve response")};
}
const auto* root =
std::get_if<Void::Json::Strict::Value::Object>(&parsed->value);
if (root == nullptr) {
return std::unexpected{make_shorturl_error(
V2ErrorCode::ProviderError,
"ShortUrlRemote: expected JSON object")};
}
const auto* target = find_string(*root, "target_url");
if (target == nullptr || target->empty()) {
return std::unexpected{make_shorturl_error(
V2ErrorCode::ProviderError,
"ShortUrlRemote: missing target_url in resolve response")};
}
const auto* short_url = find_string(*root, "short_url");
return Link{
.key = std::string{key},
.short_url = short_url ? *short_url : std::string{},
.target_url = *target,
.expires_at_unix_seconds =
find_i64(*root, "expires_at_unix_seconds").value_or(0),
.retry_after_seconds = retry_after,
};
}
Void::Result::Result<void> RemoteProvider::revoke(std::string_view key)
{
m_last_retry_after.store(0, std::memory_order_release);
if (!m_options.capabilities.supports_revoke) {
return std::unexpected{make_shorturl_error(
V2ErrorCode::Unsupported,
"ShortUrlRemote: provider does not support revoke")};
}
if (auto k = validate_custom_key(key); !k.has_value()) {
return std::unexpected{std::move(k.error())};
}
Void::Net::Request req;
req.method = Void::Net::Method::Delete;
req.url = std::format("{}/v2/links/{}", m_options.base_url, key);
req.headers = m_options.headers;
req.timeout = m_options.timeout;
auto resp = m_client.send(req);
if (!resp.has_value()) {
if (resp.error().kind() == Void::Result::ErrorKind::timeout ||
resp.error().kind() == Void::Result::ErrorKind::io) {
return std::unexpected{make_shorturl_error(
V2ErrorCode::UnknownOutcome,
std::format("ShortUrlRemote: revoke outcome unknown: {}",
resp.error().message()))};
}
return std::unexpected{make_shorturl_error(
V2ErrorCode::Unavailable,
std::format("ShortUrlRemote: revoke failed: {}",
resp.error().message()))};
}
const std::uint32_t retry_after = parse_retry_after(*resp);
if (retry_after > 0) {
m_last_retry_after.store(retry_after, std::memory_order_release);
}
if (!resp->ok()) {
return std::unexpected{make_shorturl_error(
map_http_status_to_v2(resp->status),
std::format("ShortUrlRemote: revoke HTTP {}", resp->status))};
}
return {};
}
} // namespace Void::ShortUrl::V2
+3
View File
@@ -0,0 +1,3 @@
void_add_module(ShortUrlRemote
INTERNAL_DEPS ShortUrl Net Data
)