userver: userver/ugrpc/client/generic_client.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
generic_client.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ugrpc/client/generic_client.hpp
4/// @brief @copybrief ugrpc::client::GenericClient
5
6#include <optional>
7#include <string_view>
8
9#include <grpcpp/support/byte_buffer.h>
10
11#include <userver/ugrpc/client/call_options.hpp>
12#include <userver/ugrpc/client/generic_options.hpp>
13#include <userver/ugrpc/client/impl/client_data.hpp>
14#include <userver/ugrpc/client/response_future.hpp>
15#include <userver/ugrpc/impl/static_service_metadata.hpp>
16
17USERVER_NAMESPACE_BEGIN
18
19namespace ugrpc::client {
20
21/// @ingroup userver_clients
22///
23/// @brief Allows to talk to gRPC services (generic and normal) using dynamic
24/// method names.
25///
26/// Created using @ref ugrpc::client::ClientFactory::MakeClient.
27///
28/// `call_name` must be in the format `full.path.to.TheService/MethodName`.
29/// Note that unlike in base grpc++, there must be no initial `/` character.
30///
31/// The API is mainly intended for proxies, where the request-response body is
32/// passed unchanged, with settings taken solely from the RPC metadata.
33/// In cases where the code needs to operate on the actual messages,
34/// serialization of requests and responses is left as an exercise to the user.
35///
36/// Middlewares are customizable and are applied as usual, except that no
37/// message hooks are called, meaning that there won't be any logs of messages
38/// from the default middleware.
39///
40/// There are no per-call-name metrics by default,
41/// for details see @ref GenericOptions::metrics_call_name.
42///
43/// ## Example GenericClient usage with known message types
44///
45/// @snippet grpc/tests/generic_client_test.cpp sample
46///
47/// For a more complete sample, see @ref grpc_generic_api.
48class GenericClient final {
49public:
50 GenericClient(GenericClient&&) noexcept = default;
51 GenericClient& operator=(GenericClient&&) noexcept = delete;
52
53 /// Initiate a `single request -> single response` RPC with the given name.
54 ResponseFuture<grpc::ByteBuffer> AsyncUnaryCall(
55 std::string_view call_name,
56 const grpc::ByteBuffer& request,
57 CallOptions call_options = {},
58 GenericOptions generic_options = {}
59 ) const;
60
61 /// Initiate a `single request -> single response` RPC with the given name.
62 grpc::ByteBuffer UnaryCall(
63 std::string_view call_name,
64 const grpc::ByteBuffer& request,
65 CallOptions call_options = {},
66 GenericOptions generic_options = {}
67 ) const;
68
69 /// @cond
70 // For internal use only.
71 explicit GenericClient(impl::ClientInternals&&);
72
73 static std::optional<ugrpc::impl::StaticServiceMetadata> GetMetadata() { return std::nullopt; }
74 /// @endcond
75
76private:
77 template <typename Client>
78 friend impl::ClientData& impl::GetClientData(Client& client);
79
80 impl::ClientData impl_;
81};
82
83} // namespace ugrpc::client
84
85USERVER_NAMESPACE_END