Search in sources :

Example 1 with RecommenderClient

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()));
        }
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) RecommenderClient(com.google.cloud.recommender.v1beta1.RecommenderClient) PermissionDeniedException(com.google.api.gax.rpc.PermissionDeniedException) ListRecommendationsRequest(com.google.cloud.recommender.v1beta1.ListRecommendationsRequest) ListRecommendationsPagedResponse(com.google.cloud.recommender.v1beta1.RecommenderClient.ListRecommendationsPagedResponse) Recommendation(com.google.cloud.recommender.v1beta1.Recommendation)

Aggregations

InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)1 PermissionDeniedException (com.google.api.gax.rpc.PermissionDeniedException)1 ListRecommendationsRequest (com.google.cloud.recommender.v1beta1.ListRecommendationsRequest)1 Recommendation (com.google.cloud.recommender.v1beta1.Recommendation)1 RecommenderClient (com.google.cloud.recommender.v1beta1.RecommenderClient)1 ListRecommendationsPagedResponse (com.google.cloud.recommender.v1beta1.RecommenderClient.ListRecommendationsPagedResponse)1