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

View file

@ -10,16 +10,19 @@
using namespace telebotxx;
BotApi::BotApi(const std::string& token)
: token_(token)
class BotApi::Impl
{
public:
Impl(const std::string& token)
: token_(token)
{
telegramMainUrl_ = "https://api.telegram.org/bot" + 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
using namespace rapidjson;
StringBuffer s;
@ -58,10 +61,10 @@ void BotApi::sendMessage(const std::string &chat, const std::string &text)
// Perform request
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
curlpp::Easy request;
std::list<std::string> headers;
@ -83,6 +86,27 @@ void BotApi::sendPhoto(const std::string& chat, const std::istream& file, const
// Perform request
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);
}