Files
Library/include/Void/ShortUrl/ShortUrl.hpp
T
faumaray 053ee5b296
CI / GCC 16 / C++26 / sanitizers (push) Failing after 4s
CI / GCC 16 / C++26 / werror (push) Failing after 3s
CI / GCC 16 / C++26 / default (push) Failing after 1m48s
update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-25 23:42:52 +03:00

166 lines
3.1 KiB
C++

#pragma once
#include <Void/Service/Service.hpp>
#include <cstdint>
#include <string>
#include <string_view>
#include <utility>
namespace Void::ShortUrl::V1
{
inline constexpr std::string_view kServiceId{"void.short-url"};
inline constexpr std::uint32_t kServiceVersion{1};
enum class ErrorCode : std::uint32_t {
none = 0,
invalid_request,
unsupported,
not_found,
expired,
conflict,
unauthorized,
rate_limited,
timeout,
unavailable,
provider_error,
internal_error,
invalid_url,
revoked,
storage,
response_too_large,
};
struct Error {
ErrorCode code{ErrorCode::none};
std::string message;
std::uint32_t retry_after_seconds{0};
[[nodiscard]] bool ok() const noexcept
{
return code == ErrorCode::none;
}
[[nodiscard]] explicit operator bool() const noexcept
{
return !ok();
}
};
struct ShortenRequest {
std::string target_url;
std::string custom_key;
std::int64_t expires_at_unix_seconds{0};
};
struct ShortenResult {
std::string short_url;
std::string key;
std::int64_t expires_at_unix_seconds{0};
Error error;
[[nodiscard]] bool ok() const noexcept
{
return error.ok() && !short_url.empty();
}
[[nodiscard]] explicit operator bool() const noexcept
{
return ok();
}
};
enum class HealthStatus : std::uint32_t {
unknown = 0,
healthy,
degraded,
unhealthy,
};
struct Health {
HealthStatus status{HealthStatus::unknown};
std::string message;
};
struct ResolveRequest {
std::string key;
};
struct ResolveResult {
std::string target_url;
std::int64_t expires_at_unix_seconds{0};
Error error;
[[nodiscard]] bool ok() const noexcept
{
return error.ok() && !target_url.empty();
}
[[nodiscard]] explicit operator bool() const noexcept
{
return ok();
}
};
class IShortener : public Service::IService
{
public:
~IShortener() override = default;
[[nodiscard]] std::string_view service_name() const final
{
return kServiceId;
}
[[nodiscard]] virtual std::uint32_t service_version() const noexcept
{
return kServiceVersion;
}
[[nodiscard]] virtual std::string_view
provider_id() const noexcept = 0;
[[nodiscard]] virtual Health health() const = 0;
[[nodiscard]] virtual ShortenResult
shorten(const ShortenRequest& request) = 0;
[[nodiscard]] virtual bool supports_resolve() const noexcept
{
return false;
}
[[nodiscard]] virtual ResolveResult resolve(const ResolveRequest&)
{
return {
.target_url = {},
.expires_at_unix_seconds = 0,
.error =
{
.code = ErrorCode::unsupported,
.message =
"this shortener does not support resolution",
},
};
}
};
} // namespace Void::ShortUrl::V1
namespace Void::ShortUrl
{
using V1::Error;
using V1::ErrorCode;
using V1::Health;
using V1::HealthStatus;
using V1::IShortener;
using V1::ResolveRequest;
using V1::ResolveResult;
using V1::ShortenRequest;
using V1::ShortenResult;
inline constexpr auto kServiceId = V1::kServiceId;
inline constexpr auto kServiceVersion = V1::kServiceVersion;
} // namespace Void::ShortUrl