Skip to content

Commit

Permalink
Merge pull request #282 from reo7sp/revert-280-revert-262-repeat-http…
Browse files Browse the repository at this point in the history
…-request-on-fail

Revert "Revert "Repeat the http request on fail""
  • Loading branch information
reo7sp authored Jul 2, 2023
2 parents 36a7f07 + 019c557 commit 7825962
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
18 changes: 18 additions & 0 deletions include/tgbot/net/HttpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ class TGBOT_API HttpClient {
virtual std::string makeRequest(const Url& url, const std::vector<HttpReqArg>& args) const = 0;

std::int32_t _timeout = 25;

/**
* @brief Get the maximum number of makeRequest() retries before giving up and throwing an exception.
*/
virtual int getRequestMaxRetries() const {
return requestMaxRetries;
}

/**
* @brief Get the makeRequest() backoff duration between retries, in seconds.
*/
virtual int getRequestBackoff() const {
return requestBackoff;
}

private:
int requestMaxRetries = 3;
int requestBackoff = 1;
};

}
Expand Down
45 changes: 32 additions & 13 deletions src/Api.cpp
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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()));
}
}
}

0 comments on commit 7825962

Please sign in to comment.