#include "BotToServer.hpp" BotToServer::BotToServer() { apiconfiguration = std::make_shared(); const char* envUrl = getenv("NYANIMEDBBASEURL"); if (!envUrl) { std::runtime_error("Environment variable NYANIMEDBBASEURL is not set"); } apiconfiguration->setBaseUrl(utility::conversions::to_string_t(envUrl)); apiconfiguration->setUserAgent(utility::conversions::to_string_t("OpenAPI Client")); apiclient = std::make_shared(apiconfiguration); api = std::make_shared(apiclient); } // Вспомогательная функция: преобразует UserTitle → BotStructs::Title static BotStructs::Title mapUserTitleToBotTitle( const std::shared_ptr& userTitle ) { if (!userTitle || !userTitle->titleIsSet()) { return BotStructs::Title{0, "Invalid", "", -1}; } auto apiTitle = userTitle->getTitle(); if (!apiTitle) { return BotStructs::Title{0, "No Title", "", -1}; } int64_t id = apiTitle->getId(); std::string name = "Untitled"; auto titleNames = apiTitle->getTitleNames(); utility::string_t ru = U("ru"); if (titleNames.find(ru) != titleNames.end() && !titleNames.at(ru).empty()) { name = utility::conversions::to_utf8string(titleNames.at(ru).front()); } else if (!titleNames.empty()) { const auto& firstLang = *titleNames.begin(); if (!firstLang.second.empty()) { name = utility::conversions::to_utf8string(firstLang.second.front()); } } std::string description = ""; // описание пока не поддерживается в OpenAPI-модели return BotStructs::Title{id, name, description, -1}; } pplx::task> BotToServer::fetchUserTitlesAsync(const std::string& userId) { utility::string_t userIdW = utility::conversions::to_string_t(userId); int32_t limit = static_cast(BotConstants::DISP_TITLES_NUM); auto responseTask = api->getUserTitles( userIdW, boost::none, // cursor boost::none, // sort boost::none, // sortForward boost::none, // word boost::none, // status boost::none, // watchStatus boost::none, // rating boost::none, // myRate boost::none, // releaseYear boost::none, // releaseSeason limit, boost::none // fields ); return responseTask.then([=](pplx::task> task) { try { auto response = task.get(); if (!response) { throw std::runtime_error("Null response from getUserTitles"); } const auto& userTitles = response->getData(); std::vector result; result.reserve(userTitles.size()); for (size_t i = 0; i < userTitles.size(); ++i) { BotStructs::Title botTitle = mapUserTitleToBotTitle(userTitles[i]); botTitle.num = static_cast(i); // 0-based индекс result.push_back(botTitle); } return result; } catch (const web::http::http_exception& e) { std::cerr << "HTTP error in fetchUserTitlesAsync: " << e.what() << std::endl; throw; } catch (const std::exception& e) { std::cerr << "Error in fetchUserTitlesAsync: " << e.what() << std::endl; throw; } }); }