|
| 1 | +// Copyright 2018 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 | +package com.google.ads.googleads.examples.billing; |
| 15 | + |
| 16 | +import com.beust.jcommander.Parameter; |
| 17 | +import com.google.ads.googleads.examples.utils.ArgumentNames; |
| 18 | +import com.google.ads.googleads.examples.utils.CodeSampleParams; |
| 19 | +import com.google.ads.googleads.lib.GoogleAdsClient; |
| 20 | +import com.google.ads.googleads.lib.GoogleAdsException; |
| 21 | +import com.google.ads.googleads.v0.errors.GoogleAdsError; |
| 22 | +import com.google.ads.googleads.v0.resources.AccountBudget; |
| 23 | +import com.google.ads.googleads.v0.services.GoogleAdsRow; |
| 24 | +import com.google.ads.googleads.v0.services.GoogleAdsServiceClient; |
| 25 | +import com.google.ads.googleads.v0.services.GoogleAdsServiceClient.SearchPagedResponse; |
| 26 | +import java.io.FileNotFoundException; |
| 27 | +import java.io.IOException; |
| 28 | + |
| 29 | +/** This example retrieves all account budgets for a Google Ads customer. */ |
| 30 | +public class GetAccountBudgets { |
| 31 | + |
| 32 | + private static class GetAccountBudgetParams extends CodeSampleParams { |
| 33 | + |
| 34 | + @Parameter(names = ArgumentNames.CUSTOMER_ID, required = true) |
| 35 | + private Long customerId; |
| 36 | + } |
| 37 | + |
| 38 | + public static void main(String[] args) { |
| 39 | + GetAccountBudgetParams params = new GetAccountBudgetParams(); |
| 40 | + if (!params.parseArguments(args)) { |
| 41 | + |
| 42 | + // Either pass the required parameters for this example on the command line, or insert them |
| 43 | + // into the code here. See the parameter class definition above for descriptions. |
| 44 | + params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE"); |
| 45 | + } |
| 46 | + |
| 47 | + GoogleAdsClient googleAdsClient; |
| 48 | + try { |
| 49 | + googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build(); |
| 50 | + } catch (FileNotFoundException fnfe) { |
| 51 | + System.err.printf( |
| 52 | + "Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe); |
| 53 | + return; |
| 54 | + } catch (IOException ioe) { |
| 55 | + System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + try { |
| 60 | + new GetAccountBudgets().runExample(googleAdsClient, params.customerId); |
| 61 | + } catch (GoogleAdsException gae) { |
| 62 | + // GoogleAdsException is the base class for most exceptions thrown by an API request. |
| 63 | + // Instances of this exception have a message and a GoogleAdsFailure that contains a |
| 64 | + // collection of GoogleAdsErrors that indicate the underlying causes of the |
| 65 | + // GoogleAdsException. |
| 66 | + System.err.printf( |
| 67 | + "Request ID %s failed due to GoogleAdsException. Underlying errors:%n", |
| 68 | + gae.getRequestId()); |
| 69 | + int i = 0; |
| 70 | + for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) { |
| 71 | + System.err.printf(" Error %d: %s%n", i++, googleAdsError); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Runs the example. |
| 78 | + * |
| 79 | + * @param googleAdsClient the Google Ads API client. |
| 80 | + * @param customerId the customer ID for which to retrieve account budgets. |
| 81 | + */ |
| 82 | + private void runExample(GoogleAdsClient googleAdsClient, long customerId) { |
| 83 | + String query = |
| 84 | + "SELECT " |
| 85 | + + "account_budget.status, " |
| 86 | + + "account_budget.billing_setup, " |
| 87 | + + "account_budget.approved_spending_limit_micros, " |
| 88 | + + "account_budget.approved_spending_limit_type, " |
| 89 | + + "account_budget.proposed_spending_limit_micros, " |
| 90 | + + "account_budget.proposed_spending_limit_type, " |
| 91 | + + "account_budget.approved_start_date_time, " |
| 92 | + + "account_budget.proposed_start_date_time, " |
| 93 | + + "account_budget.approved_end_date_time, " |
| 94 | + + "account_budget.approved_end_time_type," |
| 95 | + + "account_budget.proposed_end_date_time, " |
| 96 | + + "account_budget.proposed_end_time_type " |
| 97 | + + "FROM account_budget"; |
| 98 | + try (GoogleAdsServiceClient googleAdsServiceClient = |
| 99 | + googleAdsClient.getGoogleAdsServiceClient()) { |
| 100 | + // Issues the search request. |
| 101 | + SearchPagedResponse searchPagedResponse = |
| 102 | + googleAdsServiceClient.search(Long.toString(customerId), query); |
| 103 | + |
| 104 | + // Iterates over all rows in all pages and prints the requested field values for the account |
| 105 | + // budget in each row. |
| 106 | + for (GoogleAdsRow googleAdsRow : searchPagedResponse.iterateAll()) { |
| 107 | + AccountBudget accountBudget = googleAdsRow.getAccountBudget(); |
| 108 | + |
| 109 | + System.out.printf( |
| 110 | + "Account budget '%s' with " |
| 111 | + + "status '%s' " |
| 112 | + + "billing setup '%s' " |
| 113 | + + "amount served %.2f " |
| 114 | + + "total adjustments %.2f " |
| 115 | + + "approved spending limit '%s' " |
| 116 | + + "(proposed '%s') " |
| 117 | + + "approved start time '%s' " |
| 118 | + + "(proposed '%s') " |
| 119 | + + "approved end time '%s' " |
| 120 | + + "(proposed '%s')" |
| 121 | + + ".%n", |
| 122 | + accountBudget.getResourceName(), |
| 123 | + accountBudget.getStatus(), |
| 124 | + accountBudget.getBillingSetup().getValue(), |
| 125 | + accountBudget.getAmountServedMicros().getValue() / 1_000_000.0, |
| 126 | + accountBudget.getTotalAdjustmentsMicros().getValue() / 1_000_000.0, |
| 127 | + accountBudget.hasApprovedSpendingLimitMicros() |
| 128 | + ? String.format( |
| 129 | + "%.2f", accountBudget.getApprovedSpendingLimitMicros().getValue() / 1_000_000.0) |
| 130 | + : accountBudget.getApprovedSpendingLimitType().name(), |
| 131 | + accountBudget.hasProposedSpendingLimitMicros() |
| 132 | + ? String.format( |
| 133 | + "%.2f", accountBudget.getProposedSpendingLimitMicros().getValue() / 1_000_000.0) |
| 134 | + : accountBudget.getProposedSpendingLimitType().name(), |
| 135 | + accountBudget.getApprovedStartDateTime().getValue(), |
| 136 | + accountBudget.getProposedStartDateTime().getValue(), |
| 137 | + accountBudget.hasApprovedEndDateTime() |
| 138 | + ? accountBudget.getApprovedEndDateTime().getValue() |
| 139 | + : accountBudget.getApprovedEndTimeType(), |
| 140 | + accountBudget.hasProposedEndDateTime() |
| 141 | + ? accountBudget.getProposedEndDateTime().getValue() |
| 142 | + : accountBudget.getProposedEndTimeType()); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments