Skip to content

Commit 96c9dcb

Browse files
kryzthovSimon Zeltser
authored and
Simon Zeltser
committed
Example for Endpoints gRPC backend with HTTP/JSON transcoding. (GoogleCloudPlatform#1501)
There are enough differences from the pure gRPC example to create a specific example with transcoding enabled. Signed-off-by: Christophe Taton <christophe.taton@gmail.com>
1 parent d85c65f commit 96c9dcb

File tree

12 files changed

+1423
-0
lines changed

12 files changed

+1423
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# The Google Cloud Platform Python runtime is based on Debian Jessie
2+
# You can read more about the runtime at:
3+
# https://github.com/GoogleCloudPlatform/python-runtime
4+
FROM gcr.io/google_appengine/python
5+
6+
# Create a virtualenv for dependencies. This isolates these packages from
7+
# system-level packages.
8+
RUN virtualenv -p python3.6 /env
9+
10+
# Setting these environment variables are the same as running
11+
# source /env/bin/activate.
12+
ENV VIRTUAL_ENV /env
13+
ENV PATH /env/bin:$PATH
14+
15+
ADD . /bookstore/
16+
17+
WORKDIR /bookstore
18+
RUN pip install -r requirements.txt
19+
20+
EXPOSE 8000
21+
22+
ENTRYPOINT ["python", "/bookstore/bookstore_server.py"]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Google Cloud Endpoints gRPC with Transcoding Bookstore in Python
2+
3+
[![Open in Cloud Shell][shell_img]][shell_link]
4+
5+
[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png
6+
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=endpoints/bookstore-grpc-transcoding/README.md
7+
8+
This example demonstrates how to implement a gRPC server
9+
with Google Cloud Endpoints and HTTP/JSON Transcoding.
10+
11+
## Installing the dependencies using virtualenv:
12+
13+
virtualenv bookstore-env
14+
source bookstore-env/bin/activate
15+
16+
Install all the Python dependencies:
17+
18+
pip install -r requirements.txt
19+
20+
Install the [gRPC libraries and tools](http://www.grpc.io/docs/quickstart/python.html#prerequisites)
21+
22+
## Running the Server Locally
23+
24+
To run the server:
25+
26+
python bookstore_server.py
27+
28+
The `-h` command line flag shows the various settings available.
29+
30+
## Running the Client Locally
31+
32+
To run the client:
33+
34+
python bookstore_client.py
35+
36+
As with the server, the `-h` command line flag shows the various settings
37+
available.
38+
39+
## Generating a JWT token from a service account file
40+
41+
To run the script:
42+
43+
python jwt_token_gen.py --file=account_file --audiences=audiences --issuer=issuer
44+
45+
The output can be used as "--auth_token" for bookstore_client.py
46+
47+
## Regenerating the Protocol Buffer and gRPC API stubs
48+
49+
The bookstore gRPC API is defined by `bookstore.proto`
50+
The API client stubs and server interfaces are generated by tools.
51+
52+
To make it easier to get something up and running, we've included the generated
53+
code in the sample distribution. To modify the sample or create your own gRPC
54+
API definition, you'll need to update the generated code. To do this, once the
55+
gRPC libraries and tools are installed, run:
56+
57+
GOOGLEAPIS=/path/to/googleapis
58+
git clone https://github.com/googleapis/googleapis $GOOGLEAPIS
59+
60+
python -m grpc.tools.protoc \
61+
--include_imports \
62+
--include_source_info \
63+
--proto_path=. \
64+
--proto_path=$GOOGLEAPIS \
65+
--python_out=. \
66+
--grpc_python_out=. \
67+
--descriptor_set_out=api_descriptor.pb \
68+
bookstore.proto
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
#
16+
# A Bookstore example API configuration.
17+
#
18+
# Below, replace MY_PROJECT_ID with your Google Cloud Project ID.
19+
#
20+
21+
# The configuration schema is defined by service.proto file
22+
# https://github.com/googleapis/googleapis/blob/master/google/api/service.proto
23+
type: google.api.Service
24+
config_version: 3
25+
26+
#
27+
# Name of the service configuration.
28+
#
29+
name: bookstore.endpoints.<MY_PROJECT_ID>.cloud.goog
30+
31+
#
32+
# API title to appear in the user interface (Google Cloud Console).
33+
#
34+
title: Bookstore gRPC API
35+
apis:
36+
- name: endpoints.examples.bookstore.Bookstore
37+
38+
#
39+
# API usage restrictions.
40+
#
41+
usage:
42+
rules:
43+
# ListShelves methods can be called without an API Key.
44+
- selector: endpoints.examples.bookstore.Bookstore.ListShelves
45+
allow_unregistered_calls: true
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
#
16+
# A Bookstore example API configuration.
17+
#
18+
# Below, replace MY_PROJECT_ID with your Google Cloud Project ID.
19+
#
20+
21+
# The configuration schema is defined by service.proto file
22+
# https://github.com/googleapis/googleapis/blob/master/google/api/service.proto
23+
type: google.api.Service
24+
config_version: 3
25+
26+
#
27+
# Name of the service configuration.
28+
#
29+
name: bookstore.endpoints.<MY_PROJECT_ID>.cloud.goog
30+
31+
#
32+
# API title to appear in the user interface (Google Cloud Console).
33+
#
34+
title: Bookstore gRPC API
35+
apis:
36+
- name: endpoints.examples.bookstore.Bookstore
37+
38+
#
39+
# API usage restrictions.
40+
#
41+
usage:
42+
rules:
43+
- selector: "*"
44+
allow_unregistered_calls: true
45+
46+
#
47+
# Request authentication.
48+
#
49+
authentication:
50+
providers:
51+
- id: google_service_account
52+
# Replace SERVICE-ACCOUNT-ID with your service account's email address.
53+
issuer: SERVICE-ACCOUNT-ID
54+
jwks_uri: https://www.googleapis.com/robot/v1/metadata/x509/SERVICE-ACCOUNT-ID
55+
rules:
56+
# This auth rule will apply to all methods.
57+
- selector: "*"
58+
requirements:
59+
- provider_id: google_service_account
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// Copyright 2016 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
////////////////////////////////////////////////////////////////////////////////
16+
17+
syntax = "proto3";
18+
19+
package endpoints.examples.bookstore;
20+
21+
option java_multiple_files = true;
22+
option java_outer_classname = "BookstoreProto";
23+
option java_package = "com.google.endpoints.examples.bookstore";
24+
25+
26+
import "google/api/annotations.proto";
27+
import "google/protobuf/empty.proto";
28+
29+
// A simple Bookstore API.
30+
//
31+
// The API manages shelves and books resources. Shelves contain books.
32+
service Bookstore {
33+
// Returns a list of all shelves in the bookstore.
34+
rpc ListShelves(google.protobuf.Empty) returns (ListShelvesResponse) {
35+
// Define HTTP mapping.
36+
// Client example (Assuming your service is hosted at the given 'DOMAIN_NAME'):
37+
// curl http://DOMAIN_NAME/v1/shelves
38+
option (google.api.http) = { get: "/v1/shelves" };
39+
}
40+
// Creates a new shelf in the bookstore.
41+
rpc CreateShelf(CreateShelfRequest) returns (Shelf) {
42+
// Client example:
43+
// curl -d '{"theme":"Music"}' http://DOMAIN_NAME/v1/shelves
44+
option (google.api.http) = {
45+
post: "/v1/shelves"
46+
body: "shelf"
47+
};
48+
}
49+
// Returns a specific bookstore shelf.
50+
rpc GetShelf(GetShelfRequest) returns (Shelf) {
51+
// Client example - returns the first shelf:
52+
// curl http://DOMAIN_NAME/v1/shelves/1
53+
option (google.api.http) = { get: "/v1/shelves/{shelf}" };
54+
}
55+
// Deletes a shelf, including all books that are stored on the shelf.
56+
rpc DeleteShelf(DeleteShelfRequest) returns (google.protobuf.Empty) {
57+
// Client example - deletes the second shelf:
58+
// curl -X DELETE http://DOMAIN_NAME/v1/shelves/2
59+
option (google.api.http) = { delete: "/v1/shelves/{shelf}" };
60+
}
61+
// Returns a list of books on a shelf.
62+
rpc ListBooks(ListBooksRequest) returns (ListBooksResponse) {
63+
// Client example - list the books from the first shelf:
64+
// curl http://DOMAIN_NAME/v1/shelves/1/books
65+
option (google.api.http) = { get: "/v1/shelves/{shelf}/books" };
66+
}
67+
// Creates a new book.
68+
rpc CreateBook(CreateBookRequest) returns (Book) {
69+
// Client example - create a new book in the first shelf:
70+
// curl -d '{"author":"foo","title":"bar"}' http://DOMAIN_NAME/v1/shelves/1/books
71+
option (google.api.http) = {
72+
post: "/v1/shelves/{shelf}/books"
73+
body: "book"
74+
};
75+
}
76+
// Returns a specific book.
77+
rpc GetBook(GetBookRequest) returns (Book) {
78+
// Client example - get the first book from the second shelf:
79+
// curl http://DOMAIN_NAME/v1/shelves/2/books/1
80+
option (google.api.http) = { get: "/v1/shelves/{shelf}/books/{book}" };
81+
}
82+
// Deletes a book from a shelf.
83+
rpc DeleteBook(DeleteBookRequest) returns (google.protobuf.Empty) {
84+
// Client example - delete the first book from the first shelf:
85+
// curl -X DELETE http://DOMAIN_NAME/v1/shelves/1/books/1
86+
option (google.api.http) = { delete: "/v1/shelves/{shelf}/books/{book}" };
87+
}
88+
}
89+
90+
// A shelf resource.
91+
message Shelf {
92+
// A unique shelf id.
93+
int64 id = 1;
94+
// A theme of the shelf (fiction, poetry, etc).
95+
string theme = 2;
96+
}
97+
98+
// A book resource.
99+
message Book {
100+
// A unique book id.
101+
int64 id = 1;
102+
// An author of the book.
103+
string author = 2;
104+
// A book title.
105+
string title = 3;
106+
}
107+
108+
// Response to ListShelves call.
109+
message ListShelvesResponse {
110+
// Shelves in the bookstore.
111+
repeated Shelf shelves = 1;
112+
}
113+
114+
// Request message for CreateShelf method.
115+
message CreateShelfRequest {
116+
// The shelf resource to create.
117+
Shelf shelf = 1;
118+
}
119+
120+
// Request message for GetShelf method.
121+
message GetShelfRequest {
122+
// The ID of the shelf resource to retrieve.
123+
int64 shelf = 1;
124+
}
125+
126+
// Request message for DeleteShelf method.
127+
message DeleteShelfRequest {
128+
// The ID of the shelf to delete.
129+
int64 shelf = 1;
130+
}
131+
132+
// Request message for ListBooks method.
133+
message ListBooksRequest {
134+
// ID of the shelf which books to list.
135+
int64 shelf = 1;
136+
}
137+
138+
// Response message to ListBooks method.
139+
message ListBooksResponse {
140+
// The books on the shelf.
141+
repeated Book books = 1;
142+
}
143+
144+
// Request message for CreateBook method.
145+
message CreateBookRequest {
146+
// The ID of the shelf on which to create a book.
147+
int64 shelf = 1;
148+
// A book resource to create on the shelf.
149+
Book book = 2;
150+
}
151+
152+
// Request message for GetBook method.
153+
message GetBookRequest {
154+
// The ID of the shelf from which to retrieve a book.
155+
int64 shelf = 1;
156+
// The ID of the book to retrieve.
157+
int64 book = 2;
158+
}
159+
160+
// Request message for DeleteBook method.
161+
message DeleteBookRequest {
162+
// The ID of the shelf from which to delete a book.
163+
int64 shelf = 1;
164+
// The ID of the book to delete.
165+
int64 book = 2;
166+
}

0 commit comments

Comments
 (0)