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