userver: userver/ugrpc/client/simple_client_component.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
simple_client_component.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ugrpc/client/simple_client_component.hpp
4/// @brief @copybrief ugrpc::client::SimpleClientComponent
5
6#include <userver/components/component_config.hpp>
7#include <userver/components/component_context.hpp>
8
9#include <userver/ugrpc/client/client_factory_component.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace ugrpc::client {
14
15namespace impl {
16
17class SimpleClientComponentAny : public components::LoggableComponentBase {
18 public:
19 using components::LoggableComponentBase::LoggableComponentBase;
20
21 static yaml_config::Schema GetStaticConfigSchema();
22};
23
24} // namespace impl
25
26// clang-format off
27
28/// @ingroup userver_components
29///
30/// @brief Template class for a simple gRPC client
31///
32/// The component is used as a storage of a gRPC client if you're OK with
33/// generated client and don't need to wrap it. The client can be fetched using
34/// `GetClient` method.
35///
36/// Example usage:
37///
38/// ```cpp
39/// int main(...)
40/// {
41/// ...
42/// component_list.Append<ugrpc::client::SimpleClientComponent<MyClient>>();
43/// ...
44/// }
45///
46/// MyComponent::MyComponent(const components::ComponentConfig& config,
47/// const components::ComponentContext& context)
48/// {
49/// auto& component = context.FindComponent<ugrpc::client::SimpleClientComponent<MyClient>>();
50/// MyClient& client = component.GetClient();
51/// ... use client ...
52/// }
53/// ```
54
55// clang-format on
56
57template <typename Client>
58class SimpleClientComponent final : public impl::SimpleClientComponentAny {
59 public:
60 SimpleClientComponent(const components::ComponentConfig& config,
61 const components::ComponentContext& context)
62 : SimpleClientComponentAny(config, context),
63 client_(context
64 .FindComponent<ClientFactoryComponent>(
65 config["factory-component"].As<std::string>(
66 ClientFactoryComponent::kName))
67 .GetFactory()
68 .MakeClient<Client>(
69 config.Name(), config["endpoint"].As<std::string>())) {}
70
71 /// @@brief Get gRPC service client
72 Client& GetClient() { return client_; }
73
74 private:
75 Client client_;
76};
77
78} // namespace ugrpc::client
79
80namespace components {
81
82template <typename Client>
83inline constexpr bool
84 kHasValidate<ugrpc::client::SimpleClientComponent<Client>> = true;
85
86} // namespace components
87
88USERVER_NAMESPACE_END