tgbot-cpp
Loading...
Searching...
No Matches
HttpClient.h
Go to the documentation of this file.
1#pragma once
2
4#include "tgbot/export.h"
5
6#include <atomic>
7#include <cstdint>
8#include <span>
9#include <stdexcept>
10#include <string>
11#include <string_view>
12
13namespace TgBot {
14
20class TGBOT_API RequestCancelled : public std::runtime_error {
21public:
23 explicit RequestCancelled(std::string_view request);
24};
25
32public:
33 virtual ~HttpClient() = default;
34
40 virtual std::string makeRequest(const std::string& url, std::span<const HttpFormField> fields) const = 0;
41
42 std::int32_t _timeout = 25;
43
47 virtual int getRequestMaxRetries() const {
48 return requestMaxRetries;
49 }
50
54 virtual int getRequestBackoffSeconds() const {
55 return requestBackoffSeconds;
56 }
57
64 virtual void cancel(const bool eternal = false) const {
65 if (eternal) {
66 _isEternalCancel.store(true);
67 } else {
68 _cancelEpoch.fetch_add(1);
69 }
70 }
71
75 virtual bool isEternalCancelled() const {
76 return _isEternalCancel.load();
77 }
78
79protected:
83 mutable std::atomic<bool> _isEternalCancel { false };
84
88 mutable std::atomic<uint64_t> _cancelEpoch { 0 };
89
90private:
91 int requestMaxRetries = 3;
92 int requestBackoffSeconds = 1;
93};
94
95} // namespace TgBot
This class makes http requests.
Definition HttpClient.h:31
virtual int getRequestBackoffSeconds() const
Get the makeRequest() backoff duration between retries, in seconds.
Definition HttpClient.h:54
virtual void cancel(const bool eternal=false) const
Cancels the requests.
Definition HttpClient.h:64
virtual ~HttpClient()=default
std::atomic< uint64_t > _cancelEpoch
Counter used to invalidate current requests.
Definition HttpClient.h:88
virtual bool isEternalCancelled() const
Checks if the HTTP client is permanently disabled.
Definition HttpClient.h:75
virtual std::string makeRequest(const std::string &url, std::span< const HttpFormField > fields) const =0
Sends a request to the url.
std::int32_t _timeout
Definition HttpClient.h:42
virtual int getRequestMaxRetries() const
Get the maximum number of makeRequest() retries before giving up and throwing an exception.
Definition HttpClient.h:47
std::atomic< bool > _isEternalCancel
Flag indicating whether the HTTP client is permanently disabled.
Definition HttpClient.h:83
RequestCancelled(std::string_view request)
#define TGBOT_API
Definition export.h:5
Definition Api.h:18