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) : future_{std::move(future)} {}
22
23 /// @brief Checks if the asynchronous call has completed.
24 /// Note, that once user gets result, IsReady should not be called.
25 /// @return true if result ready.
26 [[nodiscard]] bool IsReady() const { return future_->IsReady(); }
27
28 /// @brief Await response until specified timepoint.
29 ///
30 /// @throws ugrpc::client::RpcError on an RPC error.
31 [[nodiscard]] engine::FutureStatus WaitUntil(engine::Deadline deadline) const noexcept {
32 return future_->WaitUntil(deadline);
33 }
34
35 /// @brief Await and read the response.
36 ///
37 /// `Get` should not be called multiple times for the same UnaryFuture.
38 ///
39 /// The connection is not closed, it will be reused for new RPCs.
40 ///
41 /// @returns the response on success.
42 /// @throws ugrpc::client::RpcError on an RPC error.
43 /// @throws ugrpc::client::RpcCancelledError on task cancellation.
44 Response Get() {
45 const auto response = future_->Get();
46 return proto_structs::MessageToStruct(response);
47 }
48
49 /// @brief Cancel call.
50 void Cancel() { return future_->Cancel(); }
51
52 /// @brief Get call context, useful e.g. for accessing metadata.
53 ugrpc::client::CallContext& GetContext() { return future_->GetContext(); }
54
55 /// @overload
56 const ugrpc::client::CallContext& GetContext() const { return future_->GetContext(); }
57
58private:
59 VanillaFuture future_;
60};
61
62} // namespace grpc_proto_structs::client
63
64USERVER_NAMESPACE_END