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,79 +10,103 @@
using namespace telebotxx; using namespace telebotxx;
BotApi::BotApi(const std::string& token) class BotApi::Impl
: token_(token)
{ {
telegramMainUrl_ = "https://api.telegram.org/bot" + token_; public:
Impl(const std::string& token)
: token_(token)
{
telegramMainUrl_ = "https://api.telegram.org/bot" + token_;
/// \todo run getMe command to check token /// \todo run getMe command to check token
}
void sendMessage(const std::string& chat, const std::string& text)
{
// Construct JSON body and istream
using namespace rapidjson;
StringBuffer s;
Writer<StringBuffer> writer(s);
writer.StartObject();
writer.String("chat_id");
writer.String(chat.c_str());
writer.String("text");
writer.String(text.c_str());
writer.EndObject();
std::istringstream myStream(s.GetString());
std::cout << myStream.str() << std::endl;
auto size = myStream.str().size();
// Construct HTTP request
curlpp::Easy request;
std::list<std::string> headers;
// Content-Type
headers.push_back("Content-Type: application/json");
// Content-Length
std::ostringstream ss;
ss << "Content-Length: " << size;
headers.push_back(ss.str());
// Set options
request.setOpt(new curlpp::Options::Url(telegramMainUrl_ + "/sendMessage"));
request.setOpt(new curlpp::Options::Verbose(true));
request.setOpt(new curlpp::Options::ReadStream(&myStream));
request.setOpt(new curlpp::Options::InfileSize(size));
request.setOpt(new curlpp::options::HttpHeader(headers));
request.setOpt(new curlpp::Options::Post(true));
// Perform request
request.perform();
}
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;
{
// Forms takes ownership of pointers!
curlpp::Forms formParts;
formParts.push_back(new curlpp::FormParts::Content("chat_id", chat));
formParts.push_back(new curlpp::FormParts::Content("photo", "value2"));
request.setOpt(new curlpp::options::HttpPost(formParts));
}
// Set options
request.setOpt(new curlpp::Options::Url(telegramMainUrl_ + "/sendPhoto"));
request.setOpt(new curlpp::Options::Verbose(true));
request.setOpt(new curlpp::options::HttpHeader(headers));
request.setOpt(new curlpp::Options::Post(true));
// Perform request
request.perform();
}
private:
std::string token_;
std::string telegramMainUrl_;
};
BotApi::BotApi(const std::string& token)
: impl_(std::make_unique<Impl>(token))
{
} }
void BotApi::sendMessage(const std::string &chat, const std::string &text) BotApi::~BotApi() = default;
void BotApi::sendMessage(const std::string& chat, const std::string& text)
{ {
// Construct JSON body and istream return impl_->sendMessage(chat, text);
using namespace rapidjson;
StringBuffer s;
Writer<StringBuffer> writer(s);
writer.StartObject();
writer.String("chat_id");
writer.String(chat.c_str());
writer.String("text");
writer.String(text.c_str());
writer.EndObject();
std::istringstream myStream(s.GetString());
std::cout << myStream.str() << std::endl;
auto size = myStream.str().size();
// Construct HTTP request
curlpp::Easy request;
std::list<std::string> headers;
// Content-Type
headers.push_back("Content-Type: application/json");
// Content-Length
std::ostringstream ss;
ss << "Content-Length: " << size;
headers.push_back(ss.str());
// Set options
request.setOpt(new curlpp::Options::Url(telegramMainUrl_ + "/sendMessage"));
request.setOpt(new curlpp::Options::Verbose(true));
request.setOpt(new curlpp::Options::ReadStream(&myStream));
request.setOpt(new curlpp::Options::InfileSize(size));
request.setOpt(new curlpp::options::HttpHeader(headers));
request.setOpt(new curlpp::Options::Post(true));
// Perform request
request.perform();
} }
void BotApi::sendPhoto(const std::string& chat, const std::istream& file, const std::string& caption) void BotApi::sendPhoto(const std::string& chat, const std::istream& file, const std::string& caption)
{ {
// Construct HTTP request return impl_->sendPhoto(chat, file, caption);
curlpp::Easy request;
std::list<std::string> headers;
{
// Forms takes ownership of pointers!
curlpp::Forms formParts;
formParts.push_back(new curlpp::FormParts::Content("chat_id", chat));
formParts.push_back(new curlpp::FormParts::Content("photo", "value2"));
request.setOpt(new curlpp::options::HttpPost(formParts));
}
// Set options
request.setOpt(new curlpp::Options::Url(telegramMainUrl_ + "/sendPhoto"));
request.setOpt(new curlpp::Options::Verbose(true));
request.setOpt(new curlpp::options::HttpHeader(headers));
request.setOpt(new curlpp::Options::Post(true));
// Perform request
request.perform();
} }