From d8f152f5bb361ef1b486a5d2df94a4869ed00f91 Mon Sep 17 00:00:00 2001 From: Bader Date: Mon, 25 Sep 2023 22:47:17 +0100 Subject: [PATCH] Now Api is using one cpr::Session instead of making a new one everytime a new request is sent --- include/tgbotxx/Api.hpp | 5 +++-- src/Api.cpp | 35 ++++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/include/tgbotxx/Api.hpp b/include/tgbotxx/Api.hpp index e93028b66..5cc294713 100644 --- a/include/tgbotxx/Api.hpp +++ b/include/tgbotxx/Api.hpp @@ -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. diff --git a/src/Api.cpp b/src/Api.cpp index 0fa21b03d..98dfce50b 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -7,31 +7,40 @@ #include 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, "")) { throw Exception("Failed to get a JSON response from Telegram API. Did you enter the correct bot token?");