Add requested changes

This commit is contained in:
Nipuna Marcus 2019-11-16 13:19:13 +05:30
parent 23dc2cb521
commit 7e0752956b

View file

@ -1,9 +1,10 @@
import ballerina/grpc; import ballerina/grpc;
import ballerina/io; import ballerina/log;
import ballerina/kubernetes; import ballerina/kubernetes;
// Product catalog client. // Product catalog client.
ProductCatalogServiceBlockingClient productCat = new ("http://productcatalogservice:3550"); ProductCatalogServiceBlockingClient catalogClient = new ("http://productcatalogservice:3550");
final Empty req = {};
@kubernetes:Service { @kubernetes:Service {
serviceType: "ClusterIP", serviceType: "ClusterIP",
@ -12,59 +13,61 @@ ProductCatalogServiceBlockingClient productCat = new ("http://productcatalogserv
service RecommendationService on new grpc:Listener(8080) { service RecommendationService on new grpc:Listener(8080) {
resource function ListRecommendations(grpc:Caller caller, ListRecommendationsRequest value) { resource function ListRecommendations(grpc:Caller caller, ListRecommendationsRequest value) {
Empty req = {};
// Fetch list of products from product catalog stub // Fetch list of products from product catalog stub
var products = productCat->ListProducts(req); var products = catalogClient->ListProducts(req);
if (products is grpc:Error) { if (products is grpc:Error) {
io:println("Error from Connector: " + products.reason() + " - " log:printError("Error when retrieving products: " + products.reason() + " - "
+ <string>products.detail()["message"]); + <string>products.detail()["message"]);
// You should return a ListRecommendationsResponse // Return a fixed set of recommendations on error.
ListRecommendationsRequest resp = { ListRecommendationsRequest response = {
user_id: value.user_id, user_id: value.user_id,
product_ids: ["9SIQT8TOJO", "6E92ZMYYFZ", "LS4PSXUNUM"] product_ids: ["9SIQT8TOJO", "6E92ZMYYFZ", "LS4PSXUNUM"]
}; };
io:println(resp); var e = caller->send(response);
var e = caller->send(resp);
e = caller->complete(); e = caller->complete();
if (e is error) {
log:printError("Error when sending recommendations: " + e.reason() + " - "
+ <string>e.detail()["message"]);
}
} else { } else {
ListProductsResponse listProductResponse; // Get the ListProductResponse from the union typed value.
grpc:Headers headers; ListProductsResponse listProductResponse = products[0];
[listProductResponse, headers] = products;
Product[] productList = listProductResponse.products; Product[] productList = listProductResponse.products;
// Extract product id from the product list. // Extract product id from the product list.
string[] productIds = []; string[] productIds = [];
int i = 0; foreach Product product in productList {
foreach Product v in productList { productIds.push(product.id);
productIds[i] = v.id;
i += 1;
} }
// Filter products which already available in the request. // Filter products which already available in the request.
string[] filtered_products = []; string[] filteredProducts = [];
int j = 0; foreach string productId in productIds {
foreach string item in productIds {
boolean isExist = false; boolean isExist = false;
foreach string v in value.product_ids { foreach string availableId in value.product_ids {
if (item == v) { if (productId == availableId) {
isExist = true; isExist = true;
} }
} }
if (!isExist) { if (!isExist) {
filtered_products[j] = item; filteredProducts.push(productId);
j += 1;
} }
} }
// Send the list of recommentations // Send the list of recommentations
ListRecommendationsRequest resp = { ListRecommendationsRequest response = {
user_id: value.user_id, user_id: value.user_id,
product_ids: filtered_products.reverse() product_ids: filteredProducts.reverse()
}; };
io:println(resp); var e = caller->send(response);
var e = caller->send(resp);
e = caller->complete(); e = caller->complete();
if (e is error) {
log:printError("Error when sending recommendations: " + e.reason() + " - "
+ <string>e.detail()["message"]);
}
} }
} }
} }