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/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() + " - "
+ <string>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() + " - "
+ <string>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() + " - "
+ <string>e.detail()["message"]);
}
}
}
}