summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorOleg Morozenkov <m@oleg.rocks>2023-05-11 00:31:09 +0400
committerGitHub <noreply@github.com>2023-05-11 00:31:09 +0400
commitfb7535a34947eac61419beaf7ba7937bff50c182 (patch)
treec89bca3fda15c7045e1686f4e86762a6e709cdfa /src
parentefe43f54f830702d59fb355e0503c27d6478b538 (diff)
parent6da263a5cf24503b1c87a7777bc79a2aab9d553b (diff)
Merge pull request #262 from dmikushin/repeat-http-request-on-fail
Repeat the http request on fail
Diffstat (limited to 'src')
-rw-r--r--src/Api.cpp45
1 files changed, 32 insertions, 13 deletions
diff --git a/src/Api.cpp b/src/Api.cpp
index 7bd9e64..f238a70 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)
@@ -2504,20 +2507,36 @@ 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.");
- }
-
- 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", ""));
+ int requestRetryBackoff = _httpClient.getRequestBackoff();
+ int retries = 0;
+ while (1)
+ {
+ try {
+ 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", ""));
+ }
+ } catch (boost::property_tree::ptree_error& e) {
+ throw TgException("tgbot-cpp library can't parse json response. " + std::string(e.what()));
+ }
+ } catch (...) {
+ int max_retries = _httpClient.getRequestMaxRetries();
+ if ((max_retries >= 0) && (retries == max_retries)) {
+ throw;
+ } else {
+ std::this_thread::sleep_for(std::chrono::seconds(requestRetryBackoff));
+ retries++;
+ continue;
+ }
}
- } catch (boost::property_tree::ptree_error& e) {
- throw TgException("tgbot-cpp library can't parse json response. " + std::string(e.what()));
}
}
}