#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>();
context->set_deadline(
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)) {
"Missing responses before WritesDone");
}
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)) {
"Extra responses after WritesDone");
}
return result;
}
GreeterClientComponent::GreeterClientComponent(
: ComponentBase(config, context),
client_factory_(
client_(client_factory_.MakeClient<api::GreeterServiceClient>(
"greeter", config[
"endpoint"].
As<std::string>())) {}
const GreeterClient& GreeterClientComponent::GetClient() const {
return client_;
}
return yaml_config::MergeSchemas<components::ComponentBase>(R"(
type: object
description: >
a user-defined wrapper around api::GreeterServiceClient that provides
a simplified interface.
additionalProperties: false
properties:
endpoint:
type: string
description: >
the service endpoint (URI). We talk to our own service,
which is kind of pointless, but works for an example
)");
}
}