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 .AccountBudgetProposal ;
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 com .google .ads .googleads .v0 .services .SearchGoogleAdsRequest ;
27+ import java .io .FileNotFoundException ;
28+ import java .io .IOException ;
29+
30+ /**
31+ * This example gets all account budget proposals. To add an account budget proposal, run
32+ * AddAccountBudgetProposal.java.
33+ */
34+ public class GetAccountBudgetProposals {
35+
36+ private static final int PAGE_SIZE = 1_000 ;
37+
38+ private static class GetAccountBudgetProposalsParams extends CodeSampleParams {
39+
40+ @ Parameter (names = ArgumentNames .CUSTOMER_ID , required = true )
41+ private Long customerId ;
42+ }
43+
44+ public static void main (String [] args ) {
45+ GetAccountBudgetProposalsParams params = new GetAccountBudgetProposalsParams ();
46+ if (!params .parseArguments (args )) {
47+
48+ // Either pass the required parameters for this example on the command line, or insert them
49+ // into the code here. See the parameter class definition above for descriptions.
50+ params .customerId = Long .parseLong ("INSERT_CUSTOMER_ID_HERE" );
51+ }
52+
53+ GoogleAdsClient googleAdsClient ;
54+ try {
55+ googleAdsClient = GoogleAdsClient .newBuilder ().fromPropertiesFile ().build ();
56+ } catch (FileNotFoundException fnfe ) {
57+ System .err .printf (
58+ "Failed to load GoogleAdsClient configuration from file. Exception: %s%n" , fnfe );
59+ return ;
60+ } catch (IOException ioe ) {
61+ System .err .printf ("Failed to create GoogleAdsClient. Exception: %s%n" , ioe );
62+ return ;
63+ }
64+
65+ try {
66+ new GetAccountBudgetProposals ().runExample (googleAdsClient , params .customerId );
67+ } catch (GoogleAdsException gae ) {
68+ // GoogleAdsException is the base class for most exceptions thrown by an API request.
69+ // Instances of this exception have a message and a GoogleAdsFailure that contains a
70+ // collection of GoogleAdsErrors that indicate the underlying causes of the
71+ // GoogleAdsException.
72+ System .err .printf (
73+ "Request ID %s failed due to GoogleAdsException. Underlying errors:%n" ,
74+ gae .getRequestId ());
75+ int i = 0 ;
76+ for (GoogleAdsError googleAdsError : gae .getGoogleAdsFailure ().getErrorsList ()) {
77+ System .err .printf (" Error %d: %s%n" , i ++, googleAdsError );
78+ }
79+ }
80+ }
81+
82+ /**
83+ * Runs the example.
84+ * @param googleAdsClient the Google Ads API client.
85+ * @param customerId the customer ID for which to retrieve AccountBudgetProposals.
86+ */
87+ private void runExample (GoogleAdsClient googleAdsClient , long customerId ) {
88+ // Construct a GAQL query which will retrieve AccountBudgetProposals.
89+ String queryString =
90+ "SELECT account_budget_proposal.id, "
91+ + " account_budget_proposal.account_budget, "
92+ + " account_budget_proposal.billing_setup, "
93+ + " account_budget_proposal.status, "
94+ + " account_budget_proposal.proposed_name, "
95+ + " account_budget_proposal.proposed_notes, "
96+ + " account_budget_proposal.proposed_purchase_order_number, "
97+ + " account_budget_proposal.proposal_type, "
98+ + " account_budget_proposal.approval_date_time, "
99+ + " account_budget_proposal.creation_date_time "
100+ + "FROM account_budget_proposal" ;
101+
102+ // Creates a request that will retrieve all account budget proposals using pages of the
103+ // specified page size.
104+ SearchGoogleAdsRequest request =
105+ SearchGoogleAdsRequest .newBuilder ()
106+ .setCustomerId (Long .toString (customerId ))
107+ .setPageSize (PAGE_SIZE )
108+ .setQuery (queryString )
109+ .build ();
110+
111+ try (GoogleAdsServiceClient googleAdsServiceClient =
112+ googleAdsClient .getGoogleAdsServiceClient ()) {
113+ // Issues the search request.
114+ SearchPagedResponse searchPagedResponse = googleAdsServiceClient .search (request );
115+
116+ // Iterates over all rows in all pages and prints the requested field values for the account
117+ // budget in each row.
118+ for (GoogleAdsRow googleAdsRow : searchPagedResponse .iterateAll ()) {
119+ AccountBudgetProposal proposal = googleAdsRow .getAccountBudgetProposal ();
120+ System .out .printf (
121+ "Account budget proposal with "
122+ + "ID '%s' "
123+ + "status '%s' "
124+ + "account_budget '%s' "
125+ + "billing_setup '%s' "
126+ + "proposed_name '%s' "
127+ + "proposed_notes '%s' "
128+ + "proposed_po_number '%s' "
129+ + "proposal_type '%s' "
130+ + "approval_date_time '%s' "
131+ + "creation_date_time '%s'.%n" ,
132+ proposal .getId ().getValue (),
133+ proposal .getStatus ().name (),
134+ proposal .getAccountBudget ().getValue (),
135+ proposal .getBillingSetup ().getValue (),
136+ proposal .getProposedName ().getValue (),
137+ proposal .getProposedNotes ().getValue (),
138+ proposal .getProposedPurchaseOrderNumber ().getValue (),
139+ proposal .getProposalType ().name (),
140+ proposal .getApprovalDateTime ().getValue (),
141+ proposal .getCreationDateTime ().getValue ());
142+ }
143+ }
144+ }
145+ }
0 commit comments