Skip to content

Commit 36a7f07

Browse files
authored
Merge pull request #280 from reo7sp/revert-262-repeat-http-request-on-fail
Revert "Repeat the http request on fail"
2 parents 9343618 + f35c769 commit 36a7f07

2 files changed

Lines changed: 13 additions & 50 deletions

File tree

include/tgbot/net/HttpClient.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,6 @@ class TGBOT_API HttpClient {
2929
virtual std::string makeRequest(const Url& url, const std::vector<HttpReqArg>& args) const = 0;
3030

3131
std::int32_t _timeout = 25;
32-
33-
/**
34-
* @brief Get the maximum number of makeRequest() retries before giving up and throwing an exception.
35-
*/
36-
virtual int getRequestMaxRetries() const {
37-
return requestMaxRetries;
38-
}
39-
40-
/**
41-
* @brief Get the makeRequest() backoff duration between retries, in seconds.
42-
*/
43-
virtual int getRequestBackoff() const {
44-
return requestBackoff;
45-
}
46-
47-
private:
48-
int requestMaxRetries = 3;
49-
int requestBackoff = 1;
5032
};
5133

5234
}

src/Api.cpp

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#include "tgbot/Api.h"
22

3-
#include <chrono>
4-
#include <thread>
5-
63
namespace TgBot {
74

85
Api::Api(std::string token, const HttpClient& httpClient, const std::string& url)
@@ -2507,36 +2504,20 @@ boost::property_tree::ptree Api::sendRequest(const std::string& method, const st
25072504
url += "/";
25082505
url += method;
25092506

2510-
int requestRetryBackoff = _httpClient.getRequestBackoff();
2511-
int retries = 0;
2512-
while (1)
2513-
{
2514-
try {
2515-
std::string serverResponse = _httpClient.makeRequest(url, args);
2516-
if (!serverResponse.compare(0, 6, "<html>")) {
2517-
throw TgException("tgbot-cpp library have got html page instead of json response. Maybe you entered wrong bot token.");
2518-
}
2519-
2520-
boost::property_tree::ptree result = _tgTypeParser.parseJson(serverResponse);
2521-
try {
2522-
if (result.get<bool>("ok", false)) {
2523-
return result.get_child("result");
2524-
} else {
2525-
throw TgException(result.get("description", ""));
2526-
}
2527-
} catch (boost::property_tree::ptree_error& e) {
2528-
throw TgException("tgbot-cpp library can't parse json response. " + std::string(e.what()));
2529-
}
2530-
} catch (...) {
2531-
int max_retries = _httpClient.getRequestMaxRetries();
2532-
if ((max_retries >= 0) && (retries == max_retries)) {
2533-
throw;
2534-
} else {
2535-
std::this_thread::sleep_for(std::chrono::seconds(requestRetryBackoff));
2536-
retries++;
2537-
continue;
2538-
}
2507+
std::string serverResponse = _httpClient.makeRequest(url, args);
2508+
if (!serverResponse.compare(0, 6, "<html>")) {
2509+
throw TgException("tgbot-cpp library have got html page instead of json response. Maybe you entered wrong bot token.");
2510+
}
2511+
2512+
boost::property_tree::ptree result = _tgTypeParser.parseJson(serverResponse);
2513+
try {
2514+
if (result.get<bool>("ok", false)) {
2515+
return result.get_child("result");
2516+
} else {
2517+
throw TgException(result.get("description", ""));
25392518
}
2519+
} catch (boost::property_tree::ptree_error& e) {
2520+
throw TgException("tgbot-cpp library can't parse json response. " + std::string(e.what()));
25402521
}
25412522
}
25422523
}

0 commit comments

Comments
 (0)