Writing gRPC descriptors

Cloudstate descriptors use the Proto3 new tab Protocol Buffers language. In a .proto file, you specify the service API. This includes the command messages and data items associated with your entity.

Descriptor elements

The examples in the following sections specify the service API for a shopping cart entity.

Syntax

The first line of our .proto file defines the version of Protocol Buffer syntax:

syntax = "proto3";

Imports

The following imports provide additional Google and Cloudstate functionality:

import "google/protobuf/empty.proto";
import "google/api/annotations.proto";
import "cloudstate/entity_key.proto";
import "cloudstate/eventing.proto";

Package

The package specifies a namespace for this service definition—​names must be unique within each namespace:

package com.example.shoppingcart;

Messages

You define messages that can be sent to or returned from the service. Each message that is input to a rpc command, must contain an entity key. In the example, this includes AddLineItem, RemoveLineItem, and GetShoppingCart where the user_id is the cloudstate.entity_key. Cloudstate uses the entity_key to know which instance of an entity a message is for. If more than one field is specified as an entity key, the fields are concatenated together.

message AddLineItem {
    string user_id = 1 [(.cloudstate.entity_key) = true];
    string product_id = 2;
    string name = 3;
    int32 quantity = 4;
 }

message RemoveLineItem {
    string user_id = 1 [(.cloudstate.entity_key) = true];
    string product_id = 2;
}

message GetShoppingCart {
    string user_id = 1 [(.cloudstate.entity_key) = true];
}

message LineItem {
    string product_id = 1;
    string name = 2;
    int32 quantity = 3;
}

message Cart {
    repeated LineItem items = 1;
}

Cloudstate entity keys must be strings. When a non string type is specified as the entity key, it is converted to a string. It’s recommended therefore, for maximum portability, that only strings are used as entity keys.

Service

This section declares the API of the service itself, along with each function or method and their parameters and return types. When a command is received for a given entity key, Cloudstate will establish a gRPC streamed call to the service implementation using that entity’s type’s protocol if one isn’t already established. Any commands received for the entity key will be sent through that call.

The AddItem and RemoveItem methods have no return value (the Empty type).

service ShoppingCart {
    rpc AddItem(AddLineItem) returns (google.protobuf.Empty) {
        option (google.api.http) = {
        post: "/com.example.shoppingcart.ShoppingCart/cart/{user_id}/items/add",
        body: "*",
    };
        option (.cloudstate.eventing).in = "items";
}

    rpc RemoveItem(RemoveLineItem) returns (google.protobuf.Empty) {
        option (google.api.http).post =
            "/com.example.shoppingcart.ShoppingCart/cart/{user_id}/items/{product_id}/remove";
    }

    rpc GetCart(GetShoppingCart) returns (Cart) {
        option (google.api.http) = {
        get: "/com.example.shoppingcart.ShoppingCart/carts/{user_id}",
        additional_bindings: {
        get: "/carts/{user_id}/items",
        response_body: "items"
        }
    };
    }
}

Transcoding HTTP

Cloudstate supports transcoding gRPC to HTTP/JSON, using the Google transcoding annotations described here. You can use transcoding to consume your entities' gRPC interfaces using HTTP/JSON.

Below is an example of the service definition with the optional transcoding annotations to bind the various endpoints to HTTP.

service ShoppingCart {
    rpc AddItem(AddLineItem) returns (google.protobuf.Empty) {
        option (google.api.http) = {   (1)
            post: "/com.example.shoppingcart.ShoppingCart/cart/{user_id}/items/add",
            body: "*",
        };
    }

    rpc RemoveItem(RemoveLineItem) returns (google.protobuf.Empty) {
        option (google.api.http).post = "/com.example.shoppingcart.ShoppingCart/cart/{user_id}/items/{product_id}/remove";  (2)
    }

    rpc GetCart(GetShoppingCart) returns (Cart) {
        option (google.api.http) = {  (3)
          get: "/com.example.shoppingcart.ShoppingCart/carts/{user_id}",
          additional_bindings: {
            get: "/carts/{user_id}/items",
            response_body: "items"
          }
        };
    }
}
1 This extra annotation specifies that we can call this endpoint using the POST method with the URI /com.example.shoppingcart.ShoppingCart/cart/{user_id}/items/add, where {user_id} is the actual user id we want the cart for.
2 A URL that we can POST to which will remove line item.
3 A more complex example where the entire Cart can be retrieved (on the first URI) or the items in the cart using the one ending in "/items".