tgbot-cpp
Toggle main menu visibility
Loading...
Searching...
No Matches
HttpClient.h
Go to the documentation of this file.
1
#pragma once
2
3
#include "
tgbot/HttpFormField.h
"
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
13
namespace
TgBot
{
14
20
class
TGBOT_API
RequestCancelled
:
public
std::runtime_error {
21
public
:
22
RequestCancelled
();
23
explicit
RequestCancelled
(std::string_view request);
24
};
25
31
class
TGBOT_API
HttpClient
{
32
public
:
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
79
protected
:
83
mutable
std::atomic<bool>
_isEternalCancel
{
false
};
84
88
mutable
std::atomic<uint64_t>
_cancelEpoch
{ 0 };
89
90
private
:
91
int
requestMaxRetries = 3;
92
int
requestBackoffSeconds = 1;
93
};
94
95
}
// namespace TgBot
HttpFormField.h
TgBot::HttpClient
This class makes http requests.
Definition
HttpClient.h:31
TgBot::HttpClient::getRequestBackoffSeconds
virtual int getRequestBackoffSeconds() const
Get the makeRequest() backoff duration between retries, in seconds.
Definition
HttpClient.h:54
TgBot::HttpClient::cancel
virtual void cancel(const bool eternal=false) const
Cancels the requests.
Definition
HttpClient.h:64
TgBot::HttpClient::~HttpClient
virtual ~HttpClient()=default
TgBot::HttpClient::_cancelEpoch
std::atomic< uint64_t > _cancelEpoch
Counter used to invalidate current requests.
Definition
HttpClient.h:88
TgBot::HttpClient::isEternalCancelled
virtual bool isEternalCancelled() const
Checks if the HTTP client is permanently disabled.
Definition
HttpClient.h:75
TgBot::HttpClient::makeRequest
virtual std::string makeRequest(const std::string &url, std::span< const HttpFormField > fields) const =0
Sends a request to the url.
TgBot::HttpClient::_timeout
std::int32_t _timeout
Definition
HttpClient.h:42
TgBot::HttpClient::getRequestMaxRetries
virtual int getRequestMaxRetries() const
Get the maximum number of makeRequest() retries before giving up and throwing an exception.
Definition
HttpClient.h:47
TgBot::HttpClient::_isEternalCancel
std::atomic< bool > _isEternalCancel
Flag indicating whether the HTTP client is permanently disabled.
Definition
HttpClient.h:83
TgBot::RequestCancelled::RequestCancelled
RequestCancelled(std::string_view request)
TgBot::RequestCancelled::RequestCancelled
RequestCancelled()
export.h
TGBOT_API
#define TGBOT_API
Definition
export.h:5
TgBot
Definition
Api.h:18
include
tgbot
HttpClient.h
Generated on
for tgbot-cpp by
1.18.0