userver: userver/clients/http/plugin.hpp Source File
Loading...
Searching...
No Matches
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 clients::http {
15
16class RequestState;
17class Response;
18
19/// @brief Auxiliary entity that allows editing request to a client
20/// from plugins
21class PluginRequest final {
22public:
23 /// @cond
24 explicit PluginRequest(RequestState& state);
25 /// @endcond
26
27 void SetHeader(std::string_view name, std::string_view value);
28
29 void AddQueryParams(std::string_view params);
30
31 void SetTimeout(std::chrono::milliseconds ms);
32
33private:
34 RequestState& state_;
35};
36
37/// @brief Base class for HTTP Client plugins
38class Plugin {
39public:
40 explicit Plugin(std::string name);
41
42 virtual ~Plugin() = default;
43
44 /// @brief Get plugin name
45 const std::string& GetName() const;
46
47 /// @brief The hook is called before actual HTTP request sending and before
48 /// DNS name resolution. You might want to use the hook for most of the
49 /// hook job.
50 virtual void HookPerformRequest(PluginRequest& request) = 0;
51
52 /// @brief The hook is called just after the "external" Span is created.
53 /// You might want to add custom tags from the hook.
54 virtual void HookCreateSpan(PluginRequest& request) = 0;
55
56 /// @brief The hook is called after the HTTP response is received or the
57 /// timeout is passed.
58 ///
59 /// @warning The hook is called in libev thread, not in coroutine context! Do
60 /// not do any heavy work here, offload it to other hooks.
61 virtual void HookOnCompleted(PluginRequest& request, Response& response) = 0;
62
63private:
64 const std::string name_;
65};
66
67namespace impl {
68
69class PluginPipeline final {
70public:
71 PluginPipeline(const std::vector<utils::NotNull<Plugin*>>& plugins);
72
73 void HookPerformRequest(RequestState& request);
74
75 void HookCreateSpan(RequestState& request);
76
77 void HookOnCompleted(RequestState& request, Response& response);
78
79private:
80 const std::vector<utils::NotNull<Plugin*>> plugins_;
81};
82
83} // namespace impl
84
85} // namespace clients::http
86
87USERVER_NAMESPACE_END