|
| 1 | +// Copyright 2019 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.ads.googleads.examples.recommendations; |
| 16 | + |
| 17 | +import com.beust.jcommander.Parameter; |
| 18 | +import com.google.ads.googleads.examples.utils.ArgumentNames; |
| 19 | +import com.google.ads.googleads.examples.utils.CodeSampleParams; |
| 20 | +import com.google.ads.googleads.lib.GoogleAdsClient; |
| 21 | +import com.google.ads.googleads.lib.GoogleAdsException; |
| 22 | +import com.google.ads.googleads.lib.utils.ResourceNames; |
| 23 | +import com.google.ads.googleads.v0.errors.GoogleAdsError; |
| 24 | +import com.google.ads.googleads.v0.services.DismissRecommendationRequest.DismissRecommendationOperation; |
| 25 | +import com.google.ads.googleads.v0.services.DismissRecommendationResponse; |
| 26 | +import com.google.ads.googleads.v0.services.DismissRecommendationResponse.DismissRecommendationResult; |
| 27 | +import com.google.ads.googleads.v0.services.RecommendationServiceClient; |
| 28 | +import java.io.FileNotFoundException; |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Arrays; |
| 31 | + |
| 32 | +/** |
| 33 | + * This example dismisses a given recommendation. To retrieve recommendations for text ads, |
| 34 | + * run GetTextAdRecommendations.java. |
| 35 | + */ |
| 36 | +public class DismissRecommendation { |
| 37 | + |
| 38 | + private static class DismissRecommendationParams extends CodeSampleParams { |
| 39 | + |
| 40 | + @Parameter(names = ArgumentNames.CUSTOMER_ID, required = true) |
| 41 | + private Long customerId; |
| 42 | + |
| 43 | + @Parameter(names = ArgumentNames.RECOMMENDATION_ID, required = true) |
| 44 | + private String recommendationId; |
| 45 | + } |
| 46 | + |
| 47 | + public static void main(String[] args) { |
| 48 | + DismissRecommendationParams params = new DismissRecommendationParams(); |
| 49 | + if (!params.parseArguments(args)) { |
| 50 | + |
| 51 | + // Either pass the required parameters for this example on the command line, or insert them |
| 52 | + // into the code here. See the parameter class definition above for descriptions. |
| 53 | + params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE"); |
| 54 | + |
| 55 | + // Recommendation ID is the last alphanumeric portion of the value from |
| 56 | + // Recommendation.getResourceName(), which has the format of |
| 57 | + // `customers/<customer_id>/recommendations/<recommendation_id>`. |
| 58 | + // Its example can be retrieved from GetTextAdRecommendations.java. |
| 59 | + params.recommendationId = "INSERT_RECOMMENDATION_ID_HERE"; |
| 60 | + } |
| 61 | + |
| 62 | + GoogleAdsClient googleAdsClient; |
| 63 | + try { |
| 64 | + googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build(); |
| 65 | + } catch (FileNotFoundException fnfe) { |
| 66 | + System.err.printf( |
| 67 | + "Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe); |
| 68 | + return; |
| 69 | + } catch (IOException ioe) { |
| 70 | + System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + try { |
| 75 | + new DismissRecommendation() |
| 76 | + .runExample(googleAdsClient, params.customerId, params.recommendationId); |
| 77 | + } catch (GoogleAdsException gae) { |
| 78 | + // GoogleAdsException is the base class for most exceptions thrown by an API request. |
| 79 | + // Instances of this exception have a message and a GoogleAdsFailure that contains a |
| 80 | + // collection of GoogleAdsErrors that indicate the underlying causes of the |
| 81 | + // GoogleAdsException. |
| 82 | + System.err.printf( |
| 83 | + "Request ID %s failed due to GoogleAdsException. Underlying errors:%n", |
| 84 | + gae.getRequestId()); |
| 85 | + int i = 0; |
| 86 | + for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) { |
| 87 | + System.err.printf(" Error %d: %s%n", i++, googleAdsError); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Runs the example. |
| 94 | + */ |
| 95 | + private void runExample(GoogleAdsClient googleAdsClient, Long customerId, |
| 96 | + String recommendationId) { |
| 97 | + // Converts the customer and recommendation ID to a resource name. |
| 98 | + String resourceName = ResourceNames.recommendation(customerId, recommendationId); |
| 99 | + |
| 100 | + // Constructs a dismiss operation for the recommendation. |
| 101 | + DismissRecommendationOperation operation = DismissRecommendationOperation.newBuilder() |
| 102 | + .setResourceName(resourceName) |
| 103 | + .build(); |
| 104 | + |
| 105 | + // Issues a mutate request to dismiss the recommendation. |
| 106 | + try (RecommendationServiceClient client = googleAdsClient.getRecommendationServiceClient()) { |
| 107 | + DismissRecommendationResponse response = client |
| 108 | + .dismissRecommendation(customerId.toString(), Arrays.asList(operation)); |
| 109 | + |
| 110 | + DismissRecommendationResult result = response.getResultsList().get(0); |
| 111 | + |
| 112 | + System.out |
| 113 | + .printf("Dismissed recommendation with resource name: '%s'%n", result.getResourceName()); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments