Skip to content

Commit

Permalink
Added protoc example
Browse files Browse the repository at this point in the history
  • Loading branch information
tomakehurst committed Sep 17, 2024
1 parent f56b19d commit 287a112
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 3 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

This project demonstrates an automated workflow for pushing a gRPC API definition into WireMock Cloud so that it can be mocked.

Currently, the only example uses Maven, and does the following:
## Building with Maven

The example in the `maven` directory does the following:
* Build the set of .proto files under `src/main/proto` into a descriptor file.
* Push this into a specific WireMock Cloud mock API via the WireMock Cloud's API.
* Push this into a specific WireMock Cloud mock API via the WireMock Cloud's API. The API ID is determined from the `MOCK_API_ID` env var.

A [GitHub Actions workflow](.github/workflows/maven-build-and-push.yml) does this on each push to `main`.


## Building directly with protoc

A [GitHub Actions workflow](.github/workflows/maven-build-and-push.yml) does this on each push to `main`.
The example in the `protoc` directory does the following:
* Build the set of .proto files under `proto` into the descriptor file `build/services.dsc`.
* Push this into a specific WireMock Cloud mock API via the WireMock Cloud's API. The API ID is determined from the `MOCK_API_ID` env var.
1 change: 1 addition & 0 deletions protoc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
40 changes: 40 additions & 0 deletions protoc/proto/ExampleServices.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2016 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// 1. syntax, package, option
syntax = "proto3";

package com.example.grpc;

option java_multiple_files = true;
import "request/helloRequest.proto";
import "response/helloResponse.proto";
import "google/protobuf/empty.proto";
import "google/protobuf/any.proto";

// 4. service, unary request/response
service GreetingService {
rpc greeting(com.example.grpc.request.HelloRequest) returns (com.example.grpc.response.HelloResponse);
rpc manyGreetingsOneReply(stream com.example.grpc.request.HelloRequest) returns (com.example.grpc.response.HelloResponse);
rpc oneGreetingManyReplies(com.example.grpc.request.HelloRequest) returns (stream com.example.grpc.response.HelloResponse);
rpc oneGreetingEmptyReply(com.example.grpc.request.HelloRequest) returns (google.protobuf.Empty);
rpc greetingAnyRequest(google.protobuf.Any) returns ( com.example.grpc.response.HelloResponse) ;
rpc greetingAnyResponse(com.example.grpc.request.HelloRequest) returns (google.protobuf.Any) ;
}

service AnotherGreetingService {
rpc differentGreeting(com.example.grpc.request.HelloRequest) returns (com.example.grpc.response.HelloResponse);
}
15 changes: 15 additions & 0 deletions protoc/proto/request/helloRequest.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";

package com.example.grpc.request;

option java_multiple_files = true;
import "request/models/sentiment.proto";

message HelloRequest {
string name = 1;
int32 age = 2;
repeated string hobbies = 3;
map<string, string> bagOfTricks = 4;
com.example.grpc.request.models.Sentiment sentiment = 5;
}

11 changes: 11 additions & 0 deletions protoc/proto/request/models/sentiment.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

package com.example.grpc.request.models;

option java_multiple_files = true;

enum Sentiment {
HAPPY = 0;
SLEEPY = 1;
ANGRY = 2;
}
9 changes: 9 additions & 0 deletions protoc/proto/response/helloResponse.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";

package com.example.grpc.response;

option java_multiple_files = true;

message HelloResponse {
string greeting = 1;
}
23 changes: 23 additions & 0 deletions protoc/push-to-wmc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

set -eo pipefail

if [ -z "$WIREMOCK_API_TOKEN" ]; then
echo "Environment variable WIREMOCK_API_TOKEN must be set with a valid API token"
exit 1
fi

if [ -z "$MOCK_API_ID" ]; then
echo "Environment variable MOCK_API_ID must be set with the target API's ID string e.g. ek2z9"
exit 1
fi

mkdir -p build
protoc --descriptor_set_out=build/services.dsc --include_imports --include_source_info --proto_path=proto proto/*.proto

curl "https://api.wiremock.cloud/v1/mock-apis/$MOCK_API_ID/__admin/ext/grpc/descriptor" \
-X PUT \
--data-binary @build/services.dsc \
-H 'content-type:application/octet-stream' \
-H "authorization:Token $WIREMOCK_API_TOKEN"

0 comments on commit 287a112

Please sign in to comment.