diff options
author | Dmitry Mikushin <dmitry@kernelgen.org> | 2023-02-11 02:03:44 +0100 |
---|---|---|
committer | Dmitry Mikushin <dmitry@kernelgen.org> | 2023-02-11 02:03:44 +0100 |
commit | 0519ebae651d8d7466d4392a2ddb718c19c5c650 (patch) | |
tree | 46593298640dad41159b1ac89bf805fa276c2222 /src | |
parent | 4356f747596a42dd04766f9c7234fd1aded2eeda (diff) |
Repeat the http request on fail, do not crash with an exception. This is a better design, because Telegram end point sometimes may not respond
Diffstat (limited to 'src')
-rw-r--r-- | src/Api.cpp | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/src/Api.cpp b/src/Api.cpp index 54854f8..dcb2b36 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -1,5 +1,8 @@ #include "tgbot/Api.h" +#include <chrono> +#include <thread> + namespace TgBot { Api::Api(std::string token, const HttpClient& httpClient, const std::string& url) @@ -2496,20 +2499,25 @@ boost::property_tree::ptree Api::sendRequest(const std::string& method, const st url += "/"; url += method; - std::string serverResponse = _httpClient.makeRequest(url, args); - if (!serverResponse.compare(0, 6, "<html>")) { - throw TgException("tgbot-cpp library have got html page instead of json response. Maybe you entered wrong bot token."); - } + while(1) + { + std::string serverResponse = _httpClient.makeRequest(url, args); + if (!serverResponse.compare(0, 6, "<html>")) { + throw TgException("tgbot-cpp library have got html page instead of json response. Maybe you entered wrong bot token."); + } - boost::property_tree::ptree result = _tgTypeParser.parseJson(serverResponse); - try { - if (result.get<bool>("ok", false)) { - return result.get_child("result"); - } else { - throw TgException(result.get("description", "")); + boost::property_tree::ptree result = _tgTypeParser.parseJson(serverResponse); + try { + if (result.get<bool>("ok", false)) { + return result.get_child("result"); + } else { + std::this_thread::sleep_for(std::chrono::seconds(1)); + continue; + //throw TgException(result.get("description", "")); + } + } catch (boost::property_tree::ptree_error& e) { + throw TgException("tgbot-cpp library can't parse json response. " + std::string(e.what())); } - } catch (boost::property_tree::ptree_error& e) { - throw TgException("tgbot-cpp library can't parse json response. " + std::string(e.what())); } } } |