// This is the server implementation of the unary blocking/unblocking scenario.
import ballerina/grpc;
import ballerina/log;

listener grpc:Listener ep = new (9090);

@grpc:ServiceDescriptor {
    descriptor: ROOT_DESCRIPTOR,
    descMap: getDescriptorMap()
}
service "HelloWorld" on ep {
    remote function hello(ContextString request) returns ContextString|error {
        log:printInfo("Invoked the hello RPC call.");
        // Reads the request content.
        string message = "Hello " + request.content;

        // Reads custom headers in request message.
        string reqHeader = check grpc:getHeader(request.headers,
        "client_header_key");
        log:printInfo("Server received header value: " + reqHeader);

        // Sends response with custom headers.
        return {content: message, headers: {server_header_key:
        "Response Header value"}};
    }
}
// This is client implementation for unary blocking scenario.
import ballerina/grpc;
import ballerina/io;

public function main (string... args) returns error? {
    HelloWorldClient ep = check new("http://localhost:9090");
    // Setting the custom headers.
    ContextString requestMessage =
    {content: "WSO2", headers: {client_header_key: "0987654321"}};
    // Executing the unary call.
    ContextString result = check ep->helloContext(requestMessage);
    // Print the content.
    io:println(result.content);
    // Print a header value.
    io:println(check grpc:getHeader(result.headers, "server_header_key"));
}

Unary Blocking

The gRPC Server Connector exposes the gRPC service over http2. This sample demonstrates how the gRPC unary service interacts with the gRPC blocking client, and how header values are handled.

For more information on the underlying module, see the GRPC module.

syntax = "proto3";
import "google/protobuf/wrappers.proto";
service HelloWorld {
	rpc hello (google.protobuf.StringValue)
			returns (google.protobuf.StringValue);
}

This is the service definition for the unary blocking/unblocking scenario.

# Create new Protocol Buffers definition file `grpc_unary_blocking.proto` and add service definition.
# Run the command below in the Ballerina tools distribution for stub generation.
bal grpc --input grpc_unary_blocking.proto  --output stubs
# Once you run the command, `grpc_unary_blocking_pb.bal` file is generated inside stubs directory.
# Please refer example `Proto To Ballerina` to get information on how to use Ballerina Protocol Buffers tool.
import ballerina/grpc;
import ballerina/log;

This is the server implementation of the unary blocking/unblocking scenario.

listener grpc:Listener ep = new (9090);
@grpc:ServiceDescriptor {
    descriptor: ROOT_DESCRIPTOR,
    descMap: getDescriptorMap()
}
service "HelloWorld" on ep {
    remote function hello(ContextString request) returns ContextString|error {
        log:printInfo("Invoked the hello RPC call.");
        string message = "Hello " + request.content;

Reads the request content.

        string reqHeader = check grpc:getHeader(request.headers,
        "client_header_key");
        log:printInfo("Server received header value: " + reqHeader);

Reads custom headers in request message.

        return {content: message, headers: {server_header_key:
        "Response Header value"}};
    }
}

Sends response with custom headers.

# Create a Ballerina package.
# Copy the generated `grpc_unary_blocking_pb.bal` stub file to the package.
# For example, if you create a package named `service`, copy the stub file to the `service` package.
# Create a new `grpc_unary_blocking.bal` Ballerina file inside the `service` package and add the service implementation.
# Execute the command below to build the 'service' package.
bal build service
# Run the service using the command below.
bal run service/target/bin/service.jar
import ballerina/grpc;
import ballerina/io;

This is client implementation for unary blocking scenario.

public function main (string... args) returns error? {
    HelloWorldClient ep = check new("http://localhost:9090");
    ContextString requestMessage =
    {content: "WSO2", headers: {client_header_key: "0987654321"}};

Setting the custom headers.

    ContextString result = check ep->helloContext(requestMessage);

Executing the unary call.

    io:println(result.content);

Print the content.

    io:println(check grpc:getHeader(result.headers, "server_header_key"));
}

Print a header value.

# Create a Ballerina package.
# Copy the generated `grpc_unary_blocking_pb.bal` stub file to the package.
# For example, if you create a package named `client`, copy the stub file to the `client` package.
# Create a new `grpc_unary_blocking_client.bal` Ballerina file inside the `client` package and add the client implementation.
# Execute the command below to build the 'client' package.
bal build client
# Run the client using the command below.
bal run client/target/bin/client.jar