use of com.google.cloud.recommender.v1beta1.RecommenderClient in project java-recommender by googleapis.
the class ListRecommendations method listRecommendations.
// List recommendations for a specified project, location, and recommender
public static void listRecommendations(String projectId, String location, String recommender) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (RecommenderClient recommenderClient = RecommenderClient.create()) {
// / Build the request
String parent = String.format("projects/%s/locations/%s/recommenders/%s", projectId, location, recommender);
ListRecommendationsRequest request = ListRecommendationsRequest.newBuilder().setParent(parent).build();
try {
// Send the request
ListRecommendationsPagedResponse response = recommenderClient.listRecommendations(request);
// Print out each recommendation
for (Recommendation responseItem : response.iterateAll()) {
Recommendation recommendation = responseItem;
System.out.println("Recommendation name: " + recommendation.getName());
System.out.println("- description: " + recommendation.getDescription());
System.out.println("- primary_impact.category: " + recommendation.getPrimaryImpact().getCategory());
System.out.println("- state_info.state: " + recommendation.getStateInfo().getState());
System.out.println();
}
// Indicate the request was successful
System.out.println("List recommendations successful");
} catch (PermissionDeniedException e) {
System.out.println("Permission denied for project '" + projectId + "'. Ensure you have the appropriate permissions to list recommendations: \n" + e.toString());
} catch (InvalidArgumentException e) {
System.out.println(("Invalid argument for projectId. Ensure you have 'GOOGLE_CLOUD_PROJECT' set: \n" + e.toString()));
}
}
}
Aggregations