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
+148
-131
@@ -1,144 +1,161 @@
|
||||
#include "echoion/core/UrlShortener.hpp"
|
||||
#include "echoion/core/Logger.hpp"
|
||||
#include "echoion/core/UrlShortener.hpp"
|
||||
#include <Void/Data/Json.hpp>
|
||||
#include <format>
|
||||
#include <glaze/glaze.hpp>
|
||||
|
||||
namespace Echoion::Core {
|
||||
namespace Echoion::Core
|
||||
{
|
||||
|
||||
UrlShortener::UrlShortener(std::shared_ptr<NetworkClient> networkClient)
|
||||
: networkClient_(std::move(networkClient)) {
|
||||
}
|
||||
UrlShortener::UrlShortener(std::shared_ptr<NetworkClient> networkClient)
|
||||
: networkClient_(std::move(networkClient))
|
||||
{
|
||||
}
|
||||
|
||||
UrlShortener::~UrlShortener() = default;
|
||||
UrlShortener::~UrlShortener() = default;
|
||||
|
||||
Api::Result<std::string> UrlShortener::shortenYouTubeUrl(std::string_view videoId) {
|
||||
std::string originalUrl = std::format("https://www.youtube.com/watch?v={}", videoId);
|
||||
return shortenUrl(originalUrl);
|
||||
}
|
||||
Api::Result<std::string>
|
||||
UrlShortener::shortenYouTubeUrl(std::string_view videoId)
|
||||
{
|
||||
std::string originalUrl =
|
||||
std::format("https://www.youtube.com/watch?v={}", videoId);
|
||||
return shortenUrl(originalUrl);
|
||||
}
|
||||
|
||||
Api::Result<std::string> UrlShortener::shortenUrl(std::string_view originalUrl) {
|
||||
// Check cache first
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(cacheMutex_);
|
||||
auto it = cache_.find(std::string(originalUrl));
|
||||
if (it != cache_.end()) {
|
||||
auto now = std::chrono::system_clock::now();
|
||||
if (now - it->second.timestamp < cacheTtl_) {
|
||||
ECHOION_LOG_DEBUG(std::format("URL cache hit: {} -> {}",
|
||||
originalUrl, it->second.shortenedUrl));
|
||||
return it->second.shortenedUrl;
|
||||
}
|
||||
// Expired, remove from cache
|
||||
cache_.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
if (!networkClient_) {
|
||||
return std::unexpected(Api::Error(Api::ErrorCode::ServiceUnavailable,
|
||||
"Network client not available"));
|
||||
}
|
||||
|
||||
// faumaray.ru API endpoint for URL shortening
|
||||
// POST /shorten with JSON body {"longUrl": "https://..."}
|
||||
// Response: {"shortUrl": "https://faumaray.ru/xxxxx"}
|
||||
std::string apiUrl = "https://faumaray.ru/shorten";
|
||||
|
||||
// Build JSON request body - API expects "longUrl" field
|
||||
std::string jsonBody = std::format(R"({{"longUrl":"{}"}})", originalUrl);
|
||||
|
||||
std::vector<Api::HttpHeader> headers = {
|
||||
{"Content-Type", "application/json"},
|
||||
{"User-Agent", "EchoionBot/1.0"}
|
||||
};
|
||||
|
||||
ECHOION_LOG_DEBUG(std::format("Shortening URL via faumaray.ru: {}", originalUrl));
|
||||
|
||||
auto response = networkClient_->post(apiUrl, jsonBody, headers);
|
||||
|
||||
if (!response) {
|
||||
ECHOION_LOG_WARNING(std::format("URL shortening failed: {}",
|
||||
response.error().message));
|
||||
return std::unexpected(Api::Error(Api::ErrorCode::NetworkError,
|
||||
"Failed to shorten URL"));
|
||||
}
|
||||
|
||||
if (!response->isSuccess()) {
|
||||
ECHOION_LOG_WARNING(std::format("URL shortening returned HTTP {}",
|
||||
response->statusCode));
|
||||
return std::unexpected(Api::Error(Api::ErrorCode::NetworkError,
|
||||
std::format("Shortening service returned HTTP {}", response->statusCode)));
|
||||
}
|
||||
|
||||
try {
|
||||
// Parse response - expecting {"shortUrl": "https://faumaray.ru/xxxxx"}
|
||||
glz::generic json;
|
||||
if (glz::read_json(json, response->body)) {
|
||||
ECHOION_LOG_WARNING("Failed to parse URL shortening response");
|
||||
return std::unexpected(Api::Error(Api::ErrorCode::ParseError,
|
||||
"Failed to parse shortening response"));
|
||||
}
|
||||
|
||||
std::string shortenedUrl;
|
||||
if (json.contains("shortUrl")) {
|
||||
shortenedUrl = json["shortUrl"].get<std::string>();
|
||||
} else {
|
||||
ECHOION_LOG_WARNING("No shortUrl field in response");
|
||||
return std::unexpected(Api::Error(Api::ErrorCode::ParseError,
|
||||
"No shortUrl in response"));
|
||||
}
|
||||
|
||||
// Cache the result
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(cacheMutex_);
|
||||
cache_[std::string(originalUrl)] = {
|
||||
.shortenedUrl = shortenedUrl,
|
||||
.timestamp = std::chrono::system_clock::now()
|
||||
};
|
||||
}
|
||||
|
||||
ECHOION_LOG_DEBUG(std::format("URL shortened: {} -> {}", originalUrl, shortenedUrl));
|
||||
return shortenedUrl;
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
ECHOION_LOG_WARNING(std::format("Exception parsing shortening response: {}",
|
||||
e.what()));
|
||||
return std::unexpected(Api::Error(Api::ErrorCode::ParseError,
|
||||
"Exception during URL shortening"));
|
||||
}
|
||||
}
|
||||
Api::Result<std::string>
|
||||
UrlShortener::shortenUrl(std::string_view originalUrl)
|
||||
{
|
||||
// Check cache first
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(cacheMutex_);
|
||||
auto it = cache_.find(std::string(originalUrl));
|
||||
if (it != cache_.end()) {
|
||||
auto now = std::chrono::system_clock::now();
|
||||
if (now - it->second.timestamp < cacheTtl_) {
|
||||
ECHOION_LOG_DEBUG(std::format("URL cache hit: {} -> {}",
|
||||
originalUrl,
|
||||
it->second.shortenedUrl));
|
||||
return it->second.shortenedUrl;
|
||||
}
|
||||
// Expired, remove from cache
|
||||
cache_.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::string> UrlShortener::getCachedUrl(std::string_view originalUrl) const {
|
||||
std::lock_guard<std::mutex> lock(cacheMutex_);
|
||||
auto it = cache_.find(std::string(originalUrl));
|
||||
if (it != cache_.end()) {
|
||||
auto now = std::chrono::system_clock::now();
|
||||
if (now - it->second.timestamp < cacheTtl_) {
|
||||
return it->second.shortenedUrl;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
if (!networkClient_) {
|
||||
return std::unexpected(
|
||||
Api::Error(Api::ErrorCode::ServiceUnavailable,
|
||||
"Network client not available"));
|
||||
}
|
||||
|
||||
void UrlShortener::clearCache() {
|
||||
std::lock_guard<std::mutex> lock(cacheMutex_);
|
||||
cache_.clear();
|
||||
}
|
||||
// faumaray.ru API endpoint for URL shortening
|
||||
// POST /shorten with JSON body {"longUrl": "https://..."}
|
||||
// Response: {"shortUrl": "https://faumaray.ru/xxxxx"}
|
||||
std::string apiUrl = "https://faumaray.ru/shorten";
|
||||
|
||||
void UrlShortener::setCacheTtl(std::chrono::hours ttl) {
|
||||
cacheTtl_ = ttl;
|
||||
}
|
||||
// Build JSON request body - API expects "longUrl" field
|
||||
std::string jsonBody =
|
||||
std::format(R"({{"longUrl":"{}"}})", originalUrl);
|
||||
|
||||
void UrlShortener::cleanExpiredCache() {
|
||||
std::lock_guard<std::mutex> lock(cacheMutex_);
|
||||
auto now = std::chrono::system_clock::now();
|
||||
|
||||
for (auto it = cache_.begin(); it != cache_.end();) {
|
||||
if (now - it->second.timestamp >= cacheTtl_) {
|
||||
it = cache_.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::vector<Api::HttpHeader> headers = {
|
||||
{"Content-Type", "application/json"},
|
||||
{"User-Agent", "EchoionBot/1.0"}};
|
||||
|
||||
ECHOION_LOG_DEBUG(
|
||||
std::format("Shortening URL via faumaray.ru: {}", originalUrl));
|
||||
|
||||
auto response = networkClient_->post(apiUrl, jsonBody, headers);
|
||||
|
||||
if (!response) {
|
||||
ECHOION_LOG_WARNING(std::format("URL shortening failed: {}",
|
||||
response.error().message));
|
||||
return std::unexpected(Api::Error(Api::ErrorCode::NetworkError,
|
||||
"Failed to shorten URL"));
|
||||
}
|
||||
|
||||
if (!response->isSuccess()) {
|
||||
ECHOION_LOG_WARNING(std::format("URL shortening returned HTTP {}",
|
||||
response->statusCode));
|
||||
return std::unexpected(
|
||||
Api::Error(Api::ErrorCode::NetworkError,
|
||||
std::format("Shortening service returned HTTP {}",
|
||||
response->statusCode)));
|
||||
}
|
||||
|
||||
try {
|
||||
// Parse response - expecting {"shortUrl":
|
||||
// "https://faumaray.ru/xxxxx"}
|
||||
auto parsed = Void::Json::parse(response->body);
|
||||
if (!parsed) {
|
||||
ECHOION_LOG_WARNING("Failed to parse URL shortening response");
|
||||
return std::unexpected(
|
||||
Api::Error(Api::ErrorCode::ParseError,
|
||||
"Failed to parse shortening response"));
|
||||
}
|
||||
|
||||
std::string shortenedUrl;
|
||||
const Void::Json::Value* shortUrl = parsed->find("shortUrl");
|
||||
if (shortUrl && shortUrl->is_string()) {
|
||||
shortenedUrl = shortUrl->as_string();
|
||||
} else {
|
||||
ECHOION_LOG_WARNING("No shortUrl field in response");
|
||||
return std::unexpected(Api::Error(Api::ErrorCode::ParseError,
|
||||
"No shortUrl in response"));
|
||||
}
|
||||
|
||||
// Cache the result
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(cacheMutex_);
|
||||
cache_[std::string(originalUrl)] = {
|
||||
.shortenedUrl = shortenedUrl,
|
||||
.timestamp = std::chrono::system_clock::now()};
|
||||
}
|
||||
|
||||
ECHOION_LOG_DEBUG(std::format("URL shortened: {} -> {}",
|
||||
originalUrl, shortenedUrl));
|
||||
return shortenedUrl;
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
ECHOION_LOG_WARNING(std::format(
|
||||
"Exception parsing shortening response: {}", e.what()));
|
||||
return std::unexpected(Api::Error(
|
||||
Api::ErrorCode::ParseError, "Exception during URL shortening"));
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::string>
|
||||
UrlShortener::getCachedUrl(std::string_view originalUrl) const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(cacheMutex_);
|
||||
auto it = cache_.find(std::string(originalUrl));
|
||||
if (it != cache_.end()) {
|
||||
auto now = std::chrono::system_clock::now();
|
||||
if (now - it->second.timestamp < cacheTtl_) {
|
||||
return it->second.shortenedUrl;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void UrlShortener::clearCache()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(cacheMutex_);
|
||||
cache_.clear();
|
||||
}
|
||||
|
||||
void UrlShortener::setCacheTtl(std::chrono::hours ttl) { cacheTtl_ = ttl; }
|
||||
|
||||
void UrlShortener::cleanExpiredCache()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(cacheMutex_);
|
||||
auto now = std::chrono::system_clock::now();
|
||||
|
||||
for (auto it = cache_.begin(); it != cache_.end();) {
|
||||
if (now - it->second.timestamp >= cacheTtl_) {
|
||||
it = cache_.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Echoion::Core
|
||||
Reference in new issue
Block a user