Skip to content

Commit

Permalink
Now Api is using one cpr::Session instead of making a new one everyti…
Browse files Browse the repository at this point in the history
…me a new request is sent
  • Loading branch information
baderouaich committed Sep 25, 2023
1 parent 3ef2729 commit d8f152f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
5 changes: 3 additions & 2 deletions include/tgbotxx/Api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ namespace tgbotxx {

class Api {
inline static const std::string BASE_URL = "https://api.telegram.org";
inline static const cpr::Timeout TIMEOUT = 25 * 1000; // 25s (Telegram server can take up to 25s to reply us (should be longer than long poll timeout))
inline static const cpr::Timeout TIMEOUT = 25 * 1000; // 25s (Telegram server can take up to 25s to reply us (should be longer than long poll timeout)). Max long polling timeout seems to be 50s.
inline static const cpr::ConnectTimeout CONNECT_TIMEOUT = 20 * 1000; // 20s (Telegram server can take up to 20s to connect with us)
inline static const std::int32_t LONG_POLL_TIMEOUT = 10; // 10s (calling getUpdates() every 10 seconds)
std::string m_token;
const std::string m_token;
mutable cpr::Session m_session;

public:
/// @brief Constructs Api object.
Expand Down
35 changes: 22 additions & 13 deletions src/Api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,40 @@
#include <cpr/cpr.h>
using namespace tgbotxx;

Api::Api(const std::string& token) : m_token(std::move(token)) {

Api::Api(const std::string& token) : m_token(token) {
m_session.SetTimeout(TIMEOUT);
m_session.SetConnectTimeout(CONNECT_TIMEOUT);
m_session.SetHeader(cpr::Header{
{"Connection", "close"}, // disable keep-alive
{"Accept", "application/json"},
{"Content-Type", "application/x-www-form-urlencoded"},
});
}



nl::json Api::sendRequest(const std::string& endpoint, const cpr::Multipart& data) const {
std::ostringstream url{};
url << BASE_URL << "/bot" << m_token << '/' << endpoint; // workaround: token should have a prefix botTOKEN. see https://stackoverflow.com/a/41460083

cpr::Header headers = {
{"Connection", "close"} // disable keep-alive
};
m_session.SetUrl(cpr::Url{url.str()});
m_session.SetMultipart(data);

bool isMultipart = not data.parts.empty();
if(isMultipart) // we have files?
headers.insert({"Content-Type", "multipart/form-data"});
else
headers.insert({"Content-Type", "application/x-www-form-urlencoded"});
{
m_session.UpdateHeader(cpr::Header{{
{"Content-Type", "multipart/form-data"}
}});

} else {
m_session.UpdateHeader(cpr::Header{{
{"Content-Type", "application/x-www-form-urlencoded"}
}});
}

cpr::Response res{};
if(isMultipart)
res = cpr::Post(cpr::Url{url.str()}, headers, data, CONNECT_TIMEOUT, TIMEOUT);
res = m_session.Post();
else
res = cpr::Get(cpr::Url{url.str()}, headers, data, CONNECT_TIMEOUT, TIMEOUT);
res = m_session.Get();

if(!res.text.compare(0, 6, "<html>")) {
throw Exception("Failed to get a JSON response from Telegram API. Did you enter the correct bot token?");
Expand Down

0 comments on commit d8f152f

Please sign in to comment.