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() {
47 const auto response = future_->Get();
48 return proto_structs::MessageToStruct(response);
49 }
50
51 /// @brief Cancel call.
52 void Cancel() { return future_->Cancel(); }
53
54 /// @brief Get call context, useful e.g. for accessing metadata.
55 ugrpc::client::CallContext& GetContext() { return future_->GetContext(); }
56
57 /// @overload
58 const ugrpc::client::CallContext& GetContext() const { return future_->GetContext(); }
59
60private:
61 VanillaFuture future_;
62};
63
64} // namespace grpc_proto_structs::client
65
66USERVER_NAMESPACE_END