|
1 | 1 | #include "tgbot/Api.h" |
2 | 2 |
|
| 3 | +#include <chrono> |
| 4 | +#include <thread> |
| 5 | + |
3 | 6 | namespace TgBot { |
4 | 7 |
|
5 | 8 | 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 |
2504 | 2507 | url += "/"; |
2505 | 2508 | url += method; |
2506 | 2509 |
|
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", "")); |
| 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 | + } |
2518 | 2539 | } |
2519 | | - } catch (boost::property_tree::ptree_error& e) { |
2520 | | - throw TgException("tgbot-cpp library can't parse json response. " + std::string(e.what())); |
2521 | 2540 | } |
2522 | 2541 | } |
2523 | 2542 | } |
0 commit comments