From 7e0752956b65ac9828f910f4613b1cd039094ec1 Mon Sep 17 00:00:00 2001 From: Nipuna Marcus Date: Sat, 16 Nov 2019 13:19:13 +0530 Subject: [PATCH] Add requested changes --- .../RecommendationService_sample_service.bal | 57 ++++++++++--------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/src/recommendationservice_ballerina/src/recommendationservice/RecommendationService_sample_service.bal b/src/recommendationservice_ballerina/src/recommendationservice/RecommendationService_sample_service.bal index a24d422..f206de9 100644 --- a/src/recommendationservice_ballerina/src/recommendationservice/RecommendationService_sample_service.bal +++ b/src/recommendationservice_ballerina/src/recommendationservice/RecommendationService_sample_service.bal @@ -1,9 +1,10 @@ import ballerina/grpc; -import ballerina/io; +import ballerina/log; import ballerina/kubernetes; // Product catalog client. -ProductCatalogServiceBlockingClient productCat = new ("http://productcatalogservice:3550"); +ProductCatalogServiceBlockingClient catalogClient = new ("http://productcatalogservice:3550"); +final Empty req = {}; @kubernetes:Service { serviceType: "ClusterIP", @@ -12,59 +13,61 @@ ProductCatalogServiceBlockingClient productCat = new ("http://productcatalogserv service RecommendationService on new grpc:Listener(8080) { resource function ListRecommendations(grpc:Caller caller, ListRecommendationsRequest value) { - Empty req = {}; // Fetch list of products from product catalog stub - var products = productCat->ListProducts(req); + var products = catalogClient->ListProducts(req); if (products is grpc:Error) { - io:println("Error from Connector: " + products.reason() + " - " + log:printError("Error when retrieving products: " + products.reason() + " - " + products.detail()["message"]); - // You should return a ListRecommendationsResponse - ListRecommendationsRequest resp = { + // Return a fixed set of recommendations on error. + ListRecommendationsRequest response = { user_id: value.user_id, product_ids: ["9SIQT8TOJO", "6E92ZMYYFZ", "LS4PSXUNUM"] }; - io:println(resp); - var e = caller->send(resp); + var e = caller->send(response); e = caller->complete(); + + if (e is error) { + log:printError("Error when sending recommendations: " + e.reason() + " - " + + e.detail()["message"]); + } } else { - ListProductsResponse listProductResponse; - grpc:Headers headers; - [listProductResponse, headers] = products; + // Get the ListProductResponse from the union typed value. + ListProductsResponse listProductResponse = products[0]; Product[] productList = listProductResponse.products; // Extract product id from the product list. string[] productIds = []; - int i = 0; - foreach Product v in productList { - productIds[i] = v.id; - i += 1; + foreach Product product in productList { + productIds.push(product.id); } // Filter products which already available in the request. - string[] filtered_products = []; - int j = 0; - foreach string item in productIds { + string[] filteredProducts = []; + foreach string productId in productIds { boolean isExist = false; - foreach string v in value.product_ids { - if (item == v) { + foreach string availableId in value.product_ids { + if (productId == availableId) { isExist = true; } } if (!isExist) { - filtered_products[j] = item; - j += 1; + filteredProducts.push(productId); } } // Send the list of recommentations - ListRecommendationsRequest resp = { + ListRecommendationsRequest response = { user_id: value.user_id, - product_ids: filtered_products.reverse() + product_ids: filteredProducts.reverse() }; - io:println(resp); - var e = caller->send(resp); + var e = caller->send(response); e = caller->complete(); + + if (e is error) { + log:printError("Error when sending recommendations: " + e.reason() + " - " + + e.detail()["message"]); + } } } }