diff --git a/src/recommendationservice/recommendation_server.py b/src/recommendationservice/recommendation_server.py index 834f5ae..97246db 100644 --- a/src/recommendationservice/recommendation_server.py +++ b/src/recommendationservice/recommendation_server.py @@ -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))