#include <greeter_client.hpp>
#include <chrono>
#include <memory>
#include <string>
#include <utility>
namespace samples {
GreeterClient::GreeterClient(api::GreeterServiceClient&& raw_client) : raw_client_(std::move(raw_client)) {}
std::string GreeterClient::SayHello(std::string name) const {
api::GreetingRequest request;
request.set_name(std::move(name));
auto stream = raw_client_.SayHello(request, MakeClientContext());
api::GreetingResponse response = stream.Finish();
return std::move(*response.mutable_greeting());
}
std::unique_ptr<grpc::ClientContext> GreeterClient::MakeClientContext() {
auto context = std::make_unique<grpc::ClientContext>();
return context;
}
std::vector<std::string> GreeterClient::SayHelloResponseStream(std::string name) const {
api::GreetingRequest request;
request.set_name(std::move(name));
auto stream = raw_client_.SayHelloResponseStream(request, MakeClientContext());
api::GreetingResponse response;
std::vector<std::string> result;
constexpr auto kCountSend = 5;
for (int i = 0; i < kCountSend; i++) {
if (!stream.Read(response)) {
}
result.push_back(std::move(*response.mutable_greeting()));
}
if (stream.Read(response)) {
}
return result;
}
std::string GreeterClient::SayHelloRequestStream(const std::vector<std::string_view>& names) const {
auto stream = raw_client_.SayHelloRequestStream(MakeClientContext());
for (const auto& name : names) {
api::GreetingRequest request;
request.set_name(grpc::string(name));
stream.WriteAndCheck(request);
}
auto response = stream.Finish();
return std::move(*response.mutable_greeting());
}
std::vector<std::string> GreeterClient::SayHelloStreams(const std::vector<std::string_view>& names) const {
auto stream = raw_client_.SayHelloStreams(MakeClientContext());
std::vector<std::string> result;
api::GreetingResponse response;
for (const auto& name : names) {
api::GreetingRequest request;
request.set_name(grpc::string(name));
stream.WriteAndCheck(request);
if (!stream.Read(response)) {
}
result.push_back(std::move(*response.mutable_greeting()));
}
const bool is_success = stream.WritesDone();
LOG_DEBUG() <<
"Write task finish: " << is_success;
if (stream.Read(response)) {
}
return result;
}
}