Creating Protobuf/JSON Services Part 2

In a previous installment we built a simple JSON service to add a store to our workspace. In this article we will extend this further by also adding a service that lists existing stores and returns the list as JSON objects.

To kick this off, this is the end result (output reformatted slightly for readability):

The first message describes the request message. Since no request parameters are required, this message is empty. The StoreListResponse message describes our service response. We want it to return just one thing: a list of stores. For this we use a repeated property named stores. Since we’re not really interested in the order of the result, and because our results will always be a set (and to make our life easier writing the logic later on), we add a blox.options.set attribute to this repeated property. This marks stores as a set.

We want to return more information about a store than just a name (in which case a string property would have sufficed). For this reason we introduce an auxiliary message named Store that contains all the properties we want to return, in this case just two: name and city.

To compile the protocol buffers run:

$ lb config
$ make

The deeply burried build/sepcom/project/proto-gen/stores.module/stores.logic files is now updated with our new messages. Here are the relevant parts:

Nothing too surprising here. The one thing of note is StoreListResponse_stores, which is the LogiQL version of our repeated Store property. This predicate suggests that to return multiple stores, we create Store entities and pulse StoreListResponse_stores facts for each where the first argument is the response and the second the Store entity.

Time to write the logic to implement the service based on these definitions.

Into the protobuf representation. To do so, we use another auxiliary predicate in our service logic file (the location:* predicates are aliases for our internal predicates and stores:* are aliases to the LogiQL predicates generated from our protobufs):