userver: /data/code/userver/libraries/grpc-proto-structs/include/userver/grpc-proto-structs/client/response_future.hpp Source File
Loading...
Searching...
No Matches
response_future.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @brief Structs future wrapper over Vanilla ResponseFufure.
5
6#include <userver/proto-structs/convert.hpp>
7#include <userver/ugrpc/client/response_future.hpp>
8#include <userver/ugrpc/client/stream.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace grpc_proto_structs::client {
13
14/// @brief proto-struct response future.
15template <typename Response>
16class ResponseFuture final {
17public:
18 using VanillaResponse = proto_structs::traits::CompatibleMessageType<Response>;
19 using VanillaFuture = ugrpc::client::ResponseFuture<VanillaResponse>;
20
21 explicit ResponseFuture(VanillaFuture&& future)
22 : future_{std::move(future)}
23 {}
24
25 /// @brief Checks if the asynchronous call has completed.
26 /// Note, that once user gets result, IsReady should not be called.
27 /// @return true if result ready.
28 [[nodiscard]] bool IsReady() const { return future_.IsReady(); }
29
30 /// @brief Await response until specified timepoint.
31 ///
32 /// @throws ugrpc::client::RpcError on an RPC error.
33 [[nodiscard]] engine::FutureStatus WaitUntil(engine::Deadline deadline) const noexcept {
34 return future_.WaitUntil(deadline);
35 }
36
37 /// @brief Await and read the response.
38 ///
39 /// `Get` should not be called multiple times for the same UnaryFuture.
40 ///
41 /// The connection is not closed, it will be reused for new RPCs.
42 ///
43 /// @returns the response on success.
44 /// @throws ugrpc::client::RpcError on an RPC error.
45 /// @throws ugrpc::client::RpcCancelledError on task cancellation.
46 Response Get() { return proto_structs::MessageToStruct<Response>(future_.Get()); }
47
48 /// @brief Cancel call.
49 void Cancel() { return future_.Cancel(); }
50
51 /// @brief Get call context, useful e.g. for accessing metadata.
52 ugrpc::client::CallContext& GetContext() { return future_.GetContext(); }
53
54 /// @overload
55 const ugrpc::client::CallContext& GetContext() const { return future_.GetContext(); }
56
57private:
58 VanillaFuture future_;
59};
60
61} // namespace grpc_proto_structs::client
62
63USERVER_NAMESPACE_END