-
Notifications
You must be signed in to change notification settings - Fork 6.6k
add grpc bookstore python samples #660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
832e133
e47fbf6
54f2273
18e58b1
7590e45
c06814f
b9f1fdc
506b4cb
1dbcc66
65f7d68
1a19f36
d811495
667e4c0
ca82046
d8f83e3
aa5bff7
f4fab1c
74da41a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# The Google Cloud Platform Python runtime is based on Debian Jessie | ||
# You can read more about the runtime at: | ||
# https://github.com/GoogleCloudPlatform/python-runtime | ||
FROM gcr.io/google_appengine/python | ||
|
||
# Create a virtualenv for dependencies. This isolates these packages from | ||
# system-level packages. | ||
RUN virtualenv /env | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I should've mentioned this during the last review, but use python 3, please. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done, is this what you meant? |
||
|
||
# Setting these environment variables are the same as running | ||
# source /env/bin/activate. | ||
ENV VIRTUAL_ENV -p python3.5 /env | ||
ENV PATH /env/bin:$PATH | ||
|
||
ADD . /bookstore/ | ||
|
||
WORKDIR /bookstore | ||
RUN pip install -r requirements.txt | ||
|
||
EXPOSE 8000 | ||
|
||
ENTRYPOINT ["python", "/bookstore/bookstore_server.py"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Google Cloud Endpoints Bookstore App in Python | ||
|
||
## Installing the dependencies using virtualenv: | ||
|
||
virtualenv bookstore-env | ||
source bookstore-env/bin/activate | ||
|
||
Install all the Python dependencies: | ||
|
||
pip install -r requirements.txt | ||
|
||
Install the [gRPC libraries and tools](http://www.grpc.io/docs/quickstart/python.html#prerequisites) | ||
|
||
## Running the Server Locally | ||
|
||
To run the server: | ||
|
||
python bookstore_server.py | ||
|
||
The `-h` command line flag shows the various settings available. | ||
|
||
## Running the Client Locally | ||
|
||
To run the client: | ||
|
||
python bookstore_client.py | ||
|
||
As with the server, the `-h` command line flag shows the various settings | ||
available. | ||
|
||
## Regenerating the API stubs | ||
|
||
The bookstore gRPC API is defined by `bookstore.proto` | ||
The API client stubs and server interfaces are generated by tools. | ||
|
||
To make it easier to get something up and running, we've included the generated | ||
code in the sample distribution. To modify the sample or create your own gRPC | ||
API definition, you'll need to update the generated code. To do this, once the | ||
gRPC libraries and tools are installed, run: | ||
|
||
python -m grpc.tools.protoc --python_out=generated_pb2 \ | ||
--grpc_python_out=generated_pb2 --proto_path=. bookstore.proto |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright 2016 Google Inc. All Rights Reserved. | ||
// | ||
// 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. | ||
// | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
syntax = "proto3"; | ||
|
||
package endpoints.examples.bookstore; | ||
|
||
option java_multiple_files = true; | ||
option java_outer_classname = "BookstoreProto"; | ||
option java_package = "com.google.endpoints.examples.bookstore"; | ||
|
||
|
||
import "google/protobuf/empty.proto"; | ||
|
||
// A simple Bookstore API. | ||
// | ||
// The API manages shelves and books resources. Shelves contain books. | ||
service Bookstore { | ||
// Returns a list of all shelves in the bookstore. | ||
rpc ListShelves(google.protobuf.Empty) returns (ListShelvesResponse) {} | ||
// Creates a new shelf in the bookstore. | ||
rpc CreateShelf(CreateShelfRequest) returns (Shelf) {} | ||
// Returns a specific bookstore shelf. | ||
rpc GetShelf(GetShelfRequest) returns (Shelf) {} | ||
// Deletes a shelf, including all books that are stored on the shelf. | ||
rpc DeleteShelf(DeleteShelfRequest) returns (google.protobuf.Empty) {} | ||
// Returns a list of books on a shelf. | ||
rpc ListBooks(ListBooksRequest) returns (ListBooksResponse) {} | ||
// Creates a new book. | ||
rpc CreateBook(CreateBookRequest) returns (Book) {} | ||
// Returns a specific book. | ||
rpc GetBook(GetBookRequest) returns (Book) {} | ||
// Deletes a book from a shelf. | ||
rpc DeleteBook(DeleteBookRequest) returns (google.protobuf.Empty) {} | ||
} | ||
|
||
// A shelf resource. | ||
message Shelf { | ||
// A unique shelf id. | ||
int64 id = 1; | ||
// A theme of the shelf (fiction, poetry, etc). | ||
string theme = 2; | ||
} | ||
|
||
// A book resource. | ||
message Book { | ||
// A unique book id. | ||
int64 id = 1; | ||
// An author of the book. | ||
string author = 2; | ||
// A book title. | ||
string title = 3; | ||
} | ||
|
||
// Response to ListShelves call. | ||
message ListShelvesResponse { | ||
// Shelves in the bookstore. | ||
repeated Shelf shelves = 1; | ||
} | ||
|
||
// Request message for CreateShelf method. | ||
message CreateShelfRequest { | ||
// The shelf resource to create. | ||
Shelf shelf = 1; | ||
} | ||
|
||
// Request message for GetShelf method. | ||
message GetShelfRequest { | ||
// The ID of the shelf resource to retrieve. | ||
int64 shelf = 1; | ||
} | ||
|
||
// Request message for DeleteShelf method. | ||
message DeleteShelfRequest { | ||
// The ID of the shelf to delete. | ||
int64 shelf = 1; | ||
} | ||
|
||
// Request message for ListBooks method. | ||
message ListBooksRequest { | ||
// ID of the shelf which books to list. | ||
int64 shelf = 1; | ||
} | ||
|
||
// Response message to ListBooks method. | ||
message ListBooksResponse { | ||
// The books on the shelf. | ||
repeated Book books = 1; | ||
} | ||
|
||
// Request message for CreateBook method. | ||
message CreateBookRequest { | ||
// The ID of the shelf on which to create a book. | ||
int64 shelf = 1; | ||
// A book resource to create on the shelf. | ||
Book book = 2; | ||
} | ||
|
||
// Request message for GetBook method. | ||
message GetBookRequest { | ||
// The ID of the shelf from which to retrieve a book. | ||
int64 shelf = 1; | ||
// The ID of the book to retrieve. | ||
int64 book = 2; | ||
} | ||
|
||
// Request message for DeleteBook method. | ||
message DeleteBookRequest { | ||
// The ID of the shelf from which to delete a book. | ||
int64 shelf = 1; | ||
// The ID of the book to delete. | ||
int64 book = 2; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Copyright 2016 Google Inc. All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
import threading | ||
|
||
|
||
class ShelfInfo(object): | ||
"""The contents of a single shelf.""" | ||
def __init__(self, shelf): | ||
self._shelf = shelf | ||
self._last_book_id = 0 | ||
self._books = dict() | ||
|
||
|
||
class Bookstore(object): | ||
"""An in-memory backend for storing Bookstore data.""" | ||
|
||
def __init__(self): | ||
self._last_shelf_id = 0 | ||
self._shelves = dict() | ||
self._lock = threading.Lock() | ||
|
||
def list_shelf(self): | ||
with self._lock: | ||
return [s._shelf for (_, s) in self._shelves.iteritems()] | ||
|
||
def create_shelf(self, shelf): | ||
with self._lock: | ||
self._last_shelf_id += 1 | ||
shelf_id = self._last_shelf_id | ||
shelf.id = shelf_id | ||
self._shelves[shelf_id] = ShelfInfo(shelf) | ||
return (shelf, shelf_id) | ||
|
||
def get_shelf(self, shelf_id): | ||
with self._lock: | ||
return self._shelves[shelf_id]._shelf | ||
|
||
def delete_shelf(self, shelf_id): | ||
with self._lock: | ||
del self._shelves[shelf_id] | ||
|
||
def list_books(self, shelf_id): | ||
with self._lock: | ||
return [book for ( | ||
_, book) in self._shelves[shelf_id]._books.iteritems()] | ||
|
||
def create_book(self, shelf_id, book): | ||
with self._lock: | ||
shelf_info = self._shelves[shelf_id] | ||
shelf_info._last_book_id += 1 | ||
book_id = shelf_info._last_book_id | ||
book.id = book_id | ||
shelf_info._books[book_id] = book | ||
return book | ||
|
||
def get_book(self, shelf_id, book_id): | ||
with self._lock: | ||
return self._shelves[shelf_id]._books[book_id] | ||
|
||
def delete_book(self, shelf_id, book_id): | ||
with self._lock: | ||
del self._shelves[shelf_id]._books[book_id] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Copyright 2016 Google Inc. All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
"""The Python gRPC Bookstore Client Example.""" | ||
|
||
import argparse | ||
|
||
from google.protobuf import empty_pb2 | ||
|
||
from generated_pb2 import bookstore_pb2 | ||
import grpc | ||
|
||
|
||
def run(host, port, api_key, timeout): | ||
"""Makes a basic ListShelves call against a gRPC Bookstore server.""" | ||
|
||
channel = grpc.insecure_channel('{}:{}'.format(host, port)) | ||
|
||
stub = bookstore_pb2.BookstoreStub(channel) | ||
metadata = [('x-api-key', api_key)] if api_key else None | ||
shelves = stub.ListShelves(empty_pb2.Empty(), timeout, metadata=metadata) | ||
print('ListShelves: {}'.format(shelves)) | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, | ||
formatter_class=argparse.RawDescriptionHelpFormatter) | ||
parser.add_argument( | ||
'--host', default='localhost', help='The host to connect to') | ||
parser.add_argument( | ||
'--port', type=int, default=8000, help='The port to connect to') | ||
parser.add_argument( | ||
'--timeout', type=int, default=10, help='The call timeout, in seconds') | ||
parser.add_argument( | ||
'--api_key', default=None, help='The API key to use for the call') | ||
args = parser.parse_args() | ||
run(args.host, args.port, args.api_key, args.timeout) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely link to the Python runtime page on github: https://github.com/GoogleCloudPlatform/python-runtime
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you mean adding a comment in this Dockerfile? something like this:
https://github.com/GoogleCloudPlatform/python-runtime
Or adding this link in the README.md?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dockerfile
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done