Class BotApi transformed according to PIMPL idiom.

This commit is contained in:
Kirill Kirilenko 2016-09-26 14:13:51 +03:00
parent ce87968bbc
commit e8e7f9b5bd
2 changed files with 95 additions and 68 deletions

View file

@ -2,6 +2,7 @@
#define TELEBOTXX_BOTAPI_H #define TELEBOTXX_BOTAPI_H
#include <string> #include <string>
#include <memory>
namespace telebotxx namespace telebotxx
{ {
@ -11,6 +12,8 @@ namespace telebotxx
/// \param [in] token bot's secret token /// \param [in] token bot's secret token
BotApi(const std::string& token); BotApi(const std::string& token);
~BotApi();
/// \brief Send text message /// \brief Send text message
/// \param [in] chat chat identifier /// \param [in] chat chat identifier
/// \param [in] text message text /// \param [in] text message text
@ -24,8 +27,8 @@ namespace telebotxx
private: private:
std::string token_; class Impl;
std::string telegramMainUrl_; std::unique_ptr<Impl> impl_;
}; };
} }

View file

@ -10,16 +10,19 @@
using namespace telebotxx; using namespace telebotxx;
BotApi::BotApi(const std::string& token) class BotApi::Impl
: token_(token)
{ {
public:
Impl(const std::string& token)
: token_(token)
{
telegramMainUrl_ = "https://api.telegram.org/bot" + token_; telegramMainUrl_ = "https://api.telegram.org/bot" + token_;
/// \todo run getMe command to check token /// \todo run getMe command to check token
} }
void BotApi::sendMessage(const std::string &chat, const std::string &text) void sendMessage(const std::string& chat, const std::string& text)
{ {
// Construct JSON body and istream // Construct JSON body and istream
using namespace rapidjson; using namespace rapidjson;
StringBuffer s; StringBuffer s;
@ -58,10 +61,10 @@ void BotApi::sendMessage(const std::string &chat, const std::string &text)
// Perform request // Perform request
request.perform(); request.perform();
} }
void BotApi::sendPhoto(const std::string& chat, const std::istream& file, const std::string& caption) void sendPhoto(const std::string& chat, const std::istream& file, const std::string& caption)
{ {
// Construct HTTP request // Construct HTTP request
curlpp::Easy request; curlpp::Easy request;
std::list<std::string> headers; std::list<std::string> headers;
@ -83,6 +86,27 @@ void BotApi::sendPhoto(const std::string& chat, const std::istream& file, const
// Perform request // Perform request
request.perform(); request.perform();
}
private:
std::string token_;
std::string telegramMainUrl_;
};
BotApi::BotApi(const std::string& token)
: impl_(std::make_unique<Impl>(token))
{
} }
BotApi::~BotApi() = default;
void BotApi::sendMessage(const std::string& chat, const std::string& text)
{
return impl_->sendMessage(chat, text);
}
void BotApi::sendPhoto(const std::string& chat, const std::istream& file, const std::string& caption)
{
return impl_->sendPhoto(chat, file, caption);
}