pull ids from product catalog service

This commit is contained in:
Dan Sanche 2018-06-20 23:26:32 -07:00
parent c753f9a6ff
commit 933a05205f

View file

@ -8,11 +8,18 @@ import os
class RecommendationService(demo_pb2_grpc.RecommendationServiceServicer):
def ListRecommendations(self, request, context):
max_responses = 5
print("handling request")
# get list of products
cat_response = stub.ListProducts(demo_pb2.Empty())
num_prodcuts = len(cat_response.products)
indices = random.sample(range(num_prodcuts), min(max_responses, num_prodcuts))
prod_list = [str(cat_response.products[i].id) for i in indices]
print(indices, prod_list)
response = demo_pb2.ListRecommendationsResponse()
prod_list = []
for i in range(5):
prod_list.append(str(random.randint(1,100)))
response.product_ids.extend(prod_list)
return response
@ -25,6 +32,10 @@ if __name__ == "__main__":
print("product catalog address: " + catalog_addr)
print("listening on port: " + port)
# stub for product catalog service
channel = grpc.insecure_channel(catalog_addr)
stub = demo_pb2_grpc.ProductCatalogServiceStub(channel)
# create gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))