Skip to content

Commit fbfc576

Browse files
authored
updating get_account_budgets for adjusted budgets and streaming (googleads#256)
* updating get_account_budgets for adjusted budgets and streaming * including adjusted values in output * added explicit version to get_type
1 parent 049932f commit fbfc576

1 file changed

Lines changed: 52 additions & 53 deletions

File tree

examples/billing/get_account_budgets.py

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@
1818
import argparse
1919
import sys
2020

21+
from google.ads.google_ads.client import GoogleAdsClient
22+
from google.ads.google_ads.errors import GoogleAdsException
2123

22-
import google.ads.google_ads.client
2324

24-
25-
_DEFAULT_PAGE_SIZE = 1000
26-
27-
28-
def main(client, customer_id, page_size):
25+
def main(client, customer_id):
2926
ga_service = client.get_service('GoogleAdsService', version='v3')
3027

3128
query = ('SELECT account_budget.status, '
@@ -34,6 +31,8 @@ def main(client, customer_id, page_size):
3431
'account_budget.approved_spending_limit_type, '
3532
'account_budget.proposed_spending_limit_micros, '
3633
'account_budget.proposed_spending_limit_type, '
34+
'account_budget.adjusted_spending_limit_micros, '
35+
'account_budget.adjusted_spending_limit_type, '
3736
'account_budget.approved_start_date_time, '
3837
'account_budget.proposed_start_date_time, '
3938
'account_budget.approved_end_date_time, '
@@ -42,68 +41,68 @@ def main(client, customer_id, page_size):
4241
'account_budget.proposed_end_time_type '
4342
'FROM account_budget')
4443

45-
results = ga_service.search(customer_id, query=query, page_size=page_size)
44+
response = ga_service.search_stream(customer_id, query=query)
4645

4746
try:
4847
# Use the enum type to determine the enum names from the values.
4948
budget_status_enum = client.get_type(
50-
'AccountBudgetStatusEnum').AccountBudgetStatus
51-
52-
for row in results:
53-
budget = row.account_budget
54-
approved_spending_limit = (
55-
micros_to_currency(budget.approved_spending_limit_micros.value)
56-
if budget.approved_spending_limit_micros
57-
else budget.approved_spending_limit_type.name)
58-
proposed_spending_limit = (
59-
micros_to_currency(budget.proposed_spending_limit_micros.value)
60-
if budget.proposed_spending_limit_micros
61-
else budget.proposed_spending_limit_type.name)
62-
approved_end_date_time = (
63-
budget.approved_end_date_time.value
64-
if budget.approved_end_date_time
65-
else budget.approved_end_date_time_type)
66-
proposed_end_date_time = (
67-
budget.proposed_end_date_time.value
68-
if budget.proposed_end_date_time
69-
else budget.proposed_end_date_time_type)
70-
71-
print('Account budget "%s", with status "%s", billing setup "%s", '
72-
'amount served %.2f, total adjustments %.2f, '
73-
'approved spending limit "%s" (proposed "%s"), '
74-
'approved start time "%s" (proposed "%s"), '
75-
'approved end time "%s" (proposed "%s").'
76-
% (budget.resource_name,
77-
budget_status_enum.Name(budget.status),
78-
budget.billing_setup.value,
79-
micros_to_currency(budget.amount_served_micros.value),
80-
micros_to_currency(budget.total_adjustments_micros.value),
81-
approved_spending_limit,
82-
proposed_spending_limit,
83-
budget.approved_start_date_time,
84-
budget.proposed_start_date_time,
85-
approved_end_date_time,
86-
proposed_end_date_time))
87-
except google.ads.google_ads.errors.GoogleAdsException as ex:
88-
print('Request with ID "%s" failed with status "%s" and includes the '
89-
'following errors:' % (ex.request_id, ex.error.code().name))
49+
'AccountBudgetStatusEnum', version='v3').AccountBudgetStatus
50+
51+
for batch in response:
52+
for row in batch.results:
53+
budget = row.account_budget
54+
approved_spending_limit = (
55+
_micros_to_currency(budget.approved_spending_limit_micros.value)
56+
if budget.approved_spending_limit_micros
57+
else budget.approved_spending_limit_type.name)
58+
proposed_spending_limit = (
59+
_micros_to_currency(budget.proposed_spending_limit_micros.value)
60+
if budget.proposed_spending_limit_micros
61+
else budget.proposed_spending_limit_type.name)
62+
adjusted_spending_limit = (
63+
_micros_to_currency(budget.adjusted_spending_limit_micros.value)
64+
if budget.adjusted_spending_limit_micros
65+
else budget.adjusted_spending_limit_type.name)
66+
approved_end_date_time = (
67+
budget.approved_end_date_time.value
68+
if budget.approved_end_date_time
69+
else budget.approved_end_date_time_type)
70+
proposed_end_date_time = (
71+
budget.proposed_end_date_time.value
72+
if budget.proposed_end_date_time
73+
else budget.proposed_end_date_time_type)
74+
75+
print(f'Account budget "{budget.resource_name}", '
76+
f'with status "{budget_status_enum.Name(budget.status)}" ',
77+
f'billing setup "{budget.billing_setup.value}", '
78+
f'amount served {_micros_to_currency(budget.amount_served_micros.value):.2f}, '
79+
f'total adjustments {_micros_to_currency(budget.total_adjustments_micros.value):.2f}, '
80+
f'approved spending limit "{approved_spending_limit}" '
81+
f'(proposed "{proposed_spending_limit}" -- '
82+
f'adjusted "{adjusted_spending_limit}"), '
83+
f'approved start time "{budget.approved_start_date_time.value}" '
84+
f'(proposed "{budget.proposed_start_date_time.value}"), '
85+
f'approved end time "{approved_end_date_time}" '
86+
f'(proposed "{proposed_end_date_time}").')
87+
except GoogleAdsException as ex:
88+
print(f'Request with ID "{ex.request_id}" failed with status '
89+
f'"{ex.error.code().name}" and includes the following errors:')
9090
for error in ex.failure.errors:
91-
print('\tError with message "%s".' % error.message)
91+
print(f'\tError with message "{error.message}".')
9292
if error.location:
9393
for field_path_element in error.location.field_path_elements:
94-
print('\t\tOn field: %s' % field_path_element.field_name)
94+
print(f'\t\tOn field: {field_path_element.field_name}')
9595
sys.exit(1)
9696

9797

98-
def micros_to_currency(micros):
98+
def _micros_to_currency(micros):
9999
return micros / 1000000.0
100100

101101

102102
if __name__ == '__main__':
103103
# GoogleAdsClient will read the google-ads.yaml configuration file in the
104104
# home directory if none is specified.
105-
google_ads_client = (google.ads.google_ads.client.GoogleAdsClient
106-
.load_from_storage())
105+
google_ads_client = GoogleAdsClient.load_from_storage()
107106

108107
parser = argparse.ArgumentParser(
109108
description=('Lists all account budgets for given Google Ads customer '
@@ -113,4 +112,4 @@ def micros_to_currency(micros):
113112
required=True, help='The Google Ads customer ID.')
114113
args = parser.parse_args()
115114

116-
main(google_ads_client, args.customer_id, _DEFAULT_PAGE_SIZE)
115+
main(google_ads_client, args.customer_id)

0 commit comments

Comments
 (0)