userver: userver/clients/http/request.hpp Source File
Loading...
Searching...
No Matches
request.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <chrono>
7#include <initializer_list>
8#include <memory>
9#include <string>
10#include <string_view>
11#include <type_traits>
12#include <unordered_map>
13#include <utility>
14#include <vector>
15
19#include <userver/clients/http/expected_status_codes.hpp>
26#include <userver/utils/impl/internal_tag_fwd.hpp>
27#include <userver/utils/impl/source_location.hpp>
31
32USERVER_NAMESPACE_BEGIN
33
34namespace tracing {
36} // namespace tracing
37
38namespace utils::impl {
39class WaitTokenStorageLock;
40} // namespace utils::impl
41
43namespace clients::http {
44
45class RequestState;
48class ConnectTo;
49class Form;
51class RequestStats;
52class DestinationStatistics;
53struct TestsuiteConfig;
54class MiddlewareBase;
55
56namespace impl {
57class EasyWrapper;
58class RequestBackend;
59} // namespace impl
60
62enum class HttpMethod { kGet, kPost, kHead, kPut, kDelete, kPatch, kOptions };
63
65std::string_view ToStringView(HttpMethod method);
66
68HttpMethod HttpMethodFromString(std::string_view method_str);
69
70using USERVER_NAMESPACE::http::HttpVersion;
71
83
96
97ProxyAuthType ProxyAuthTypeFromString(std::string_view auth_name);
98
100class Request final {
101public:
103 using Cookies = std::unordered_map<std::string, std::string, utils::StrCaseHash>;
104
108 explicit Request(std::shared_ptr<impl::RequestBackend> backend);
109
111 // For internal use only.
112 explicit Request(
113 impl::EasyWrapper&&,
114 RequestStats&& req_stats,
115 DestinationStatistics& dest_stats,
116 clients::dns::Resolver* resolver,
117 const tracing::TracingManagerBase& tracing_manager
118 );
120
125
131 Request& get(std::string url) &;
133 Request get(std::string url) &&;
134
140 Request& head(std::string url) &;
142 Request head(std::string url) &&;
143
149 Request& post(std::string url, std::string data = {}) &;
151 Request post(std::string url, std::string data = {}) &&;
153 Request& post(std::string url, Form&& form) &;
155 Request post(std::string url, Form&& form) &&;
156
162 Request& put(std::string url, std::string data = {}) &;
164 Request put(std::string url, std::string data = {}) &&;
165
171 Request& patch(std::string url, std::string data = {}) &;
173 Request patch(std::string url, std::string data = {}) &&;
174
180 Request& delete_method(std::string url) &;
182 Request delete_method(std::string url) &&;
184 Request& delete_method(std::string url, std::string data) &;
186 Request delete_method(std::string url, std::string data) &&;
187
192
194 Request& url(std::string url) &;
196 Request url(std::string url) &&;
197
199 Request& data(std::string data) &;
201 Request data(std::string data) &&;
202
207
213 Request& headers(const std::initializer_list<std::pair<utils::zstring_view, utils::zstring_view>>& headers) &;
215 Request headers(const std::initializer_list<std::pair<utils::zstring_view, utils::zstring_view>>& headers) &&;
216
219 HttpAuthType value,
220 bool auth_only,
222 utils::zstring_view password
223 ) &;
226 HttpAuthType value,
227 bool auth_only,
229 utils::zstring_view password
230 ) &&;
231
237 Request& proxy_headers(const std::initializer_list<std::pair<utils::zstring_view, utils::zstring_view>>& headers) &;
239 Request proxy_headers(const std::initializer_list<std::pair<utils::zstring_view, utils::zstring_view>>& headers) &&;
240
245
253
258
263
265 Request& cookies(const std::unordered_map<std::string, std::string>& cookies) &;
267 Request cookies(const std::unordered_map<std::string, std::string>& cookies) &&;
268
270 Request& follow_redirects(bool follow = true) &;
272 Request follow_redirects(bool follow = true) &&;
273
276 Request& timeout(long timeout_ms) &;
278 Request timeout(long timeout_ms) &&;
280 Request& timeout(std::chrono::milliseconds timeout_ms) & { return timeout(timeout_ms.count()); }
282 Request timeout(std::chrono::milliseconds timeout_ms) && { return std::move(this->timeout(timeout_ms.count())); }
283
285 Request& verify(bool verify = true) &;
287 Request verify(bool verify = true) &&;
288
293
298
303
310
315
321 Request& retry(short retries = 3, bool on_fails = true) &;
322 Request retry(short retries = 3, bool on_fails = true) &&;
323
329
334
339
345
347 template <typename T>
348 requires std::is_same_v<ConnectTo, T>
350 static_assert(!sizeof(T), "ConnectTo argument must not be temporary, it must outlive Request");
351 return *this;
352 }
353
356
368
371 Request& SetLoggedUrl(std::string url) &;
373 Request SetLoggedUrl(std::string url) &&;
374
384
387 Request& SetUrlTemplate(std::string url_template) &;
389 Request SetUrlTemplate(std::string url_template) &&;
390
394 Request& SetDestinationMetricName(const std::string& destination) &;
396 Request SetDestinationMetricName(const std::string& destination) &&;
397
399 // Set testsuite related settings. For internal use only.
400 void SetTestsuiteConfig(const std::shared_ptr<const TestsuiteConfig>& config) &;
401
402 void SetAllowedUrlsExtra(const std::vector<std::string>& urls) &;
403
404 // Set deadline propagation settings. For internal use only.
405 void SetDeadlinePropagationConfig(const DeadlinePropagationConfig& deadline_propagation_config) &;
406
407 void SetWaitToken(utils::impl::InternalTag, utils::impl::WaitTokenStorageLock&&);
409
414
415 void SetCancellationPolicy(CancellationPolicy cp);
416
421
434 utils::impl::SourceLocation location = utils::impl::SourceLocation::Current()
435 );
436
441 const std::shared_ptr<concurrent::StringStreamQueue>& queue,
442 utils::impl::SourceLocation location = utils::impl::SourceLocation::Current()
443 );
444
453 [[nodiscard]] std::shared_ptr<Response> perform(
454 utils::impl::SourceLocation location = utils::impl::SourceLocation::Current()
455 );
456
461 utils::impl::SourceLocation location = utils::impl::SourceLocation::Current()
462 );
463
465 const std::string& GetUrl() const&;
467 const std::string& GetUrl() && = delete;
468
470 const std::string& GetData() const&;
472 const std::string& GetData() && = delete;
473
475 std::string ExtractData();
476
486 void SetUrlUserinfoAllowed(bool allowed);
487
488private:
489 std::shared_ptr<RequestState> pimpl_;
490 std::shared_ptr<impl::RequestBackend> alternative_;
491};
492
493} // namespace clients::http
494
495USERVER_NAMESPACE_END