userver: userver/ugrpc/client/simple_client_component.hpp Source File
Loading...
Searching...
No Matches
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 <memory>
7
8#include <userver/components/component_base.hpp>
9#include <userver/utils/not_null.hpp>
10
11#include <userver/ugrpc/client/client_factory.hpp>
12#include <userver/ugrpc/client/fwd.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace ugrpc::client {
17
18namespace impl {
19
20class SimpleClientComponentAny : public components::ComponentBase {
21public:
22 using components::ComponentBase::ComponentBase;
23
24 static yaml_config::Schema GetStaticConfigSchema();
25
26protected:
27 static ClientFactory& FindFactory(
28 const components::ComponentConfig& config,
29 const components::ComponentContext& context
30 );
31
32 static ClientSettings MakeClientSettings(
33 const components::ComponentConfig& config,
34 const dynamic_config::Key<ClientQos>* client_qos
35 );
36};
37
38} // namespace impl
39
40/// @ingroup userver_components
41///
42/// @brief Template class for a simple gRPC client
43///
44/// The component is used as a storage of a gRPC client if you're OK with
45/// generated client and don't need to wrap it. The client can be fetched using
46/// `GetClient` method.
47///
48/// Example usage:
49///
50/// ```cpp
51/// int main(...)
52/// {
53/// ...
54/// component_list.Append<ugrpc::client::SimpleClientComponent<HelloClient>>("hello-client");
55/// ...
56/// }
57///
58/// MyComponent::MyComponent(const components::ComponentConfig& config,
59/// const components::ComponentContext& context)
60/// {
61/// HelloClient& client = context.FindComponent<
62/// ugrpc::client::SimpleClientComponent<HelloClient>>("hello-client").GetClient();
63/// ... use client ...
64/// }
65/// ```
66///
67///
68/// ## Static options of ugrpc::client::CommonComponent :
69/// @include{doc} scripts/docs/en/components_schema/grpc/src/ugrpc/client/simple_client_component.md
70///
71/// Options inherited from @ref components::ComponentBase :
72/// @include{doc} scripts/docs/en/components_schema/core/src/components/impl/component_base.md
73template <typename Client>
74class SimpleClientComponent : public impl::SimpleClientComponentAny {
75public:
76 /// Main component's constructor.
77 SimpleClientComponent(const components::ComponentConfig& config, const components::ComponentContext& context)
78 : SimpleClientComponentAny(config, context),
79 client_(utils::MakeSharedRef<
80 Client>(FindFactory(config, context).MakeClient<Client>(MakeClientSettings(config, nullptr))))
81 {}
82
83 /// To use a ClientQos config, derive from SimpleClientComponent and provide the parameter at construction:
84 /// @snippet samples/grpc_service/src/greeter_client.hpp component
86 const components::ComponentConfig& config,
87 const components::ComponentContext& context,
88 const dynamic_config::Key<ClientQos>& client_qos
89 )
90 : SimpleClientComponentAny(config, context),
91 client_(utils::MakeSharedRef<
92 Client>(FindFactory(config, context).MakeClient<Client>(MakeClientSettings(config, &client_qos))))
93 {}
94
95 /// @brief Get gRPC service client.
96 Client& GetClient() noexcept { return *client_; }
97
98protected:
99 /// @brief Get gRPC service client ptr.
100 utils::SharedRef<Client> GetClientPtr() noexcept { return client_; }
101
102private:
103 utils::SharedRef<Client> client_;
104};
105
106} // namespace ugrpc::client
107
108namespace components {
109
110template <typename Client>
111inline constexpr bool kHasValidate<ugrpc::client::SimpleClientComponent<Client>> = true;
112
113} // namespace components
114
115USERVER_NAMESPACE_END