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