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 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 before actual HTTP request sending and before
52 /// DNS name resolution. You might want to use the hook for most of the
53 /// hook job.
54 virtual void HookPerformRequest(PluginRequest& request) = 0;
55
56 /// @brief The hook is called just after the "external" Span is created.
57 /// You might want to add custom tags from the hook.
58 virtual void HookCreateSpan(PluginRequest& request, tracing::Span& span) = 0;
59
60 /// @brief The hook is called after the HTTP response is received or the
61 /// timeout is passed.
62 ///
63 /// @warning The hook is called in libev thread, not in coroutine context! Do
64 /// not do any heavy work here, offload it to other hooks.
65 virtual void HookOnCompleted(PluginRequest& request, Response& response) = 0;
66
67private:
68 const std::string name_;
69};
70
71namespace impl {
72
73class PluginPipeline final {
74public:
75 PluginPipeline(const std::vector<utils::NotNull<Plugin*>>& plugins);
76
77 void HookPerformRequest(RequestState& request);
78
79 void HookCreateSpan(RequestState& request, tracing::Span& span);
80
81 void HookOnCompleted(RequestState& request, Response& response);
82
83private:
84 const std::vector<utils::NotNull<Plugin*>> plugins_;
85};
86
87} // namespace impl
88
89} // namespace clients::http
90
91USERVER_NAMESPACE_END