userver: userver/clients/http/plugin.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
plugin.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/clients/http/plugin.hpp
4/// @brief @copybrief clients::http::Plugin
5
6#include <chrono>
7#include <string>
8#include <vector>
9
10#include <userver/utils/not_null.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace tracing {
15class Span;
16}
17
18namespace clients::http {
19
20class RequestState;
21class Response;
22
23/// @brief Auxiliary entity that allows editing request to a client
24/// from plugins
25class PluginRequest final {
26public:
27 /// @cond
28 explicit PluginRequest(RequestState& state);
29 /// @endcond
30
31 void SetHeader(std::string_view name, std::string_view value);
32
33 void AddQueryParams(std::string_view params);
34
35 void SetTimeout(std::chrono::milliseconds ms);
36
37private:
38 RequestState& state_;
39};
40
41/// @brief Base class for HTTP Client plugins
42class Plugin {
43public:
44 explicit Plugin(std::string name);
45
46 virtual ~Plugin() = default;
47
48 /// @brief Get plugin name
49 const std::string& GetName() const;
50
51 /// @brief The hook is called just after the "external" Span is created.
52 /// You might want to add custom tags from the hook.
53 /// The hook is called only once before any network interaction and is NOT called before each retry.
54 /// The hook is executed in the context of the parent task which created the request.
55 virtual void HookCreateSpan(PluginRequest& request, tracing::Span& span) = 0;
56
57 /// @brief The hook is called before actual HTTP request sending and before
58 /// DNS name resolution.
59 /// This hook is called on each retry.
60 ///
61 /// @warning The hook is called in libev thread, not in coroutine context! Do
62 /// not do any heavy work here, offload it to other hooks.
63 virtual void HookPerformRequest(PluginRequest& request) = 0;
64
65 /// @brief The hook is called after the HTTP response is received or the
66 /// timeout is passed.
67 ///
68 /// @warning The hook is called in libev thread, not in coroutine context! Do
69 /// not do any heavy work here, offload it to other hooks.
70 virtual void HookOnCompleted(PluginRequest& request, Response& response) = 0;
71
72private:
73 const std::string name_;
74};
75
76namespace impl {
77
78class PluginPipeline final {
79public:
80 PluginPipeline(const std::vector<utils::NotNull<Plugin*>>& plugins);
81
82 void HookPerformRequest(RequestState& request);
83
84 void HookCreateSpan(RequestState& request, tracing::Span& span);
85
86 void HookOnCompleted(RequestState& request, Response& response);
87
88private:
89 const std::vector<utils::NotNull<Plugin*>> plugins_;
90};
91
92} // namespace impl
93
94} // namespace clients::http
95
96USERVER_NAMESPACE_END