Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Commit

Permalink
Merge pull request #40 from The-B1T-Foundation/added_meme_api
Browse files Browse the repository at this point in the history
Added error handling
  • Loading branch information
b1tflyyyy authored May 8, 2024
2 parents 9d7c78a + e11743c commit 3dd32fc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ AMeme_API_Controller::~AMeme_API_Controller()
}

// ---------------------------------------------------------------------------------------------------------------------
std::string AMeme_API_Controller::Get_Meme()
std::optional<std::string> AMeme_API_Controller::Get_Meme()
{
if (Meme_Links_Stack.empty())
{
Send_Request("GET");
if (!Send_Request("GET"))
{
return std::nullopt;
}

for (std::size_t i{}; !Json_Response.empty() && i < 12; ++i) // 12 - default count of meme links
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <stack>
#include <vector>
#include <string>
#include <optional>

#include <controller/api/base_api/base_api_controller.hpp>

Expand All @@ -38,7 +39,7 @@ class AMeme_API_Controller : public ABase_API_Controller
explicit AMeme_API_Controller(const AMeme_API_Config& meme_api_config);
~AMeme_API_Controller() override;

std::string Get_Meme();
std::optional<std::string> Get_Meme();

private:
bool Send_Request(std::string_view request_type);
Expand Down
8 changes: 7 additions & 1 deletion source/view/handlers/message_handler/message_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,13 @@ void AMessage_Handler::Bind_Commands()
std::lock_guard<std::mutex> locker(Mutex);
++Metrics.Meme_Request_Count;

TG_Bot.getApi().sendMessage(message->chat->id, Meme_API_Controller.Get_Meme());
if (auto meme{ Meme_API_Controller.Get_Meme() }; meme != std::nullopt)
{
TG_Bot.getApi().sendMessage(message->chat->id, meme.value());
return;
}

TG_Bot.getApi().sendMessage(message->chat->id, AMessage_Reply::Get_Server_Internal_Error());
});
TG_Bot.getEvents().onCommand(SMessage_Commands::Metrics_Range.data(), [this](TgBot::Message::Ptr message) -> void
{
Expand Down
6 changes: 6 additions & 0 deletions source/view/texts/message_reply/message_reply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,10 @@ std::string AMessage_Reply::Get_Synonym_Msg(std::string_view primary_word, std::
std::string AMessage_Reply::Get_Not_Found_Synonym_Msg()
{
return std::string{ "Мені не вдалося знайти синонім до твого слова) " };
}

// ---------------------------------------------------------------------------------------------------------------------
std::string AMessage_Reply::Get_Server_Internal_Error()
{
return std::string{ "Сталася помилка на стороні сервера!" };
}
1 change: 1 addition & 0 deletions source/view/texts/message_reply/message_reply.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ class AMessage_Reply
static std::string Get_Not_Found_Antonym_Msg();
static std::string Get_Synonym_Msg(std::string_view primary_word, std::string_view synonym);
static std::string Get_Not_Found_Synonym_Msg();
static std::string Get_Server_Internal_Error();
};

0 comments on commit 3dd32fc

Please sign in to comment.