Skip to content

Commit 7faba3c

Browse files
authored
converted two old examples to use search_stream (googleads#253)
* converted two old examples to use search_stream * using shorter variable name to increase readability * addressed review comments * display 'None' value is blank * revert copyright year
1 parent fbfc576 commit 7faba3c

File tree

2 files changed

+56
-61
lines changed

2 files changed

+56
-61
lines changed

examples/basic_operations/get_expanded_text_ads.py

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

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

2324

24-
_DEFAULT_PAGE_SIZE = 1000
25-
26-
27-
def main(client, customer_id, page_size, ad_group_id=None):
25+
def main(client, customer_id, ad_group_id=None):
2826
ga_service = client.get_service('GoogleAdsService', version='v3')
27+
ad_group_ad_status_enum = client.get_type(
28+
'AdGroupAdStatusEnum', version='v3').AdGroupAdStatus
2929

3030
query = ('SELECT ad_group.id, ad_group_ad.ad.id, '
3131
'ad_group_ad.ad.expanded_text_ad.headline_part1, '
@@ -34,39 +34,39 @@ def main(client, customer_id, page_size, ad_group_id=None):
3434
'WHERE ad_group_ad.ad.type = EXPANDED_TEXT_AD')
3535

3636
if ad_group_id:
37-
query = '%s AND ad_group.id = %s' % (query, ad_group_id)
37+
query += f' AND ad_group.id = {ad_group_id}'
3838

39-
results = ga_service.search(customer_id, query=query, page_size=page_size)
39+
response = ga_service.search_stream(customer_id, query=query)
4040

4141
try:
42-
for row in results:
43-
ad = row.ad_group_ad.ad
44-
45-
if ad.expanded_text_ad:
46-
expanded_text_ad_info = ad.expanded_text_ad
47-
48-
print('Expanded text ad with ID %s, status %s, and headline '
49-
'%s - %s was found in ad group with ID %s.'
50-
% (ad.id, row.ad_group_ad.status,
51-
expanded_text_ad_info.headline_part1,
52-
expanded_text_ad_info.headline_part2,
53-
row.ad_group.id))
54-
except google.ads.google_ads.errors.GoogleAdsException as ex:
55-
print('Request with ID "%s" failed with status "%s" and includes the '
56-
'following errors:' % (ex.request_id, ex.error.code().name))
42+
for batch in response:
43+
for row in batch.results:
44+
ad = row.ad_group_ad.ad
45+
46+
if ad.expanded_text_ad:
47+
expanded_text_ad_info = ad.expanded_text_ad
48+
49+
print(f'Expanded text ad with ID {ad.id.value}, status '
50+
f'"{ad_group_ad_status_enum.Name(row.ad_group_ad.status)}", '
51+
'and headline '
52+
f'"{expanded_text_ad_info.headline_part1.value}"'
53+
f' - "{expanded_text_ad_info.headline_part2.value}" was '
54+
f'found in ad group with ID {row.ad_group.id.value}.')
55+
except GoogleAdsException as ex:
56+
print(f'Request with ID "{ex.request_id}" failed with status '
57+
f'"{ex.error.code().name}" and includes the following errors:')
5758
for error in ex.failure.errors:
58-
print('\tError with message "%s".' % error.message)
59+
print(f'\tError with message "{error.message}".')
5960
if error.location:
6061
for field_path_element in error.location.field_path_elements:
61-
print('\t\tOn field: %s' % field_path_element.field_name)
62+
print(f'\t\tOn field: {field_path_element.field_name}')
6263
sys.exit(1)
6364

6465

6566
if __name__ == '__main__':
6667
# GoogleAdsClient will read the google-ads.yaml configuration file in the
6768
# home directory if none is specified.
68-
google_ads_client = (google.ads.google_ads.client.GoogleAdsClient
69-
.load_from_storage())
69+
google_ads_client = GoogleAdsClient.load_from_storage()
7070

7171
parser = argparse.ArgumentParser(
7272
description='List ad groups for specified customer.')
@@ -77,5 +77,4 @@ def main(client, customer_id, page_size, ad_group_id=None):
7777
required=False, help='The ad group ID. ')
7878
args = parser.parse_args()
7979

80-
main(google_ads_client, args.customer_id, _DEFAULT_PAGE_SIZE,
81-
ad_group_id=args.ad_group_id)
80+
main(google_ads_client, args.customer_id, ad_group_id=args.ad_group_id)

examples/billing/get_billing_setup.py

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
import argparse
1919
import sys
2020

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

2324

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

3028
query = (
@@ -37,47 +35,45 @@ def main(client, customer_id, page_size):
3735
'billing_setup.payments_account_info.secondary_payments_profile_id '
3836
'FROM billing_setup')
3937

40-
results = ga_service.search(customer_id, query=query, page_size=page_size)
38+
response = ga_service.search_stream(customer_id, query=query)
4139

4240
try:
4341
# Use the enum type to determine the enum name from the value.
44-
billing_setup_status_enum = (
45-
client.get_type('BillingSetupStatusEnum', version='v3')
46-
.BillingSetupStatus)
42+
billing_setup_status_enum = client.get_type(
43+
'BillingSetupStatusEnum', version='v3').BillingSetupStatus
4744

4845
print('Found the following billing setup results:')
49-
for row in results:
50-
billing_setup = row.billing_setup
51-
payments_account_info = billing_setup.payments_account_info
52-
print('Billing setup with ID "%s", status "%s", '
53-
'payments_account "%s", payments_account_id "%s", '
54-
'payments_account_name "%s", payments_profile_id "%s", '
55-
'payments_profile_name "%s", '
56-
'secondary_payments_profile_id "%s".'
57-
% (billing_setup.id.value,
58-
billing_setup_status_enum.Name(billing_setup.status),
59-
billing_setup.payments_account.value,
60-
payments_account_info.payments_account_id.value,
61-
payments_account_info.payments_account_name.value,
62-
payments_account_info.payments_profile_id.value,
63-
payments_account_info.payments_profile_name.value,
64-
payments_account_info.secondary_payments_profile_id.value))
65-
except google.ads.google_ads.errors.GoogleAdsException as ex:
66-
print('Request with ID "%s" failed with status "%s" and includes the '
67-
'following errors:' % (ex.request_id, ex.error.code().name))
46+
for batch in response:
47+
for row in batch.results:
48+
billing_setup = row.billing_setup
49+
pai = billing_setup.payments_account_info
50+
if pai.secondary_payments_profile_id.value:
51+
secondary_payments_profile_id = pai.secondary_payments_profile_id.value
52+
else:
53+
secondary_payments_profile_id = "None"
54+
print(f'Billing setup with ID {billing_setup.id.value}, '
55+
f'status "{billing_setup_status_enum.Name(billing_setup.status)}", '
56+
f'payments_account "{billing_setup.payments_account.value}" '
57+
f'payments_account_id {pai.payments_account_id.value}, '
58+
f'payments_account_name "{pai.payments_account_name.value}", '
59+
f'payments_profile_id {pai.payments_profile_id.value}, '
60+
f'payments_profile_name "{pai.payments_profile_name.value}", '
61+
f'secondary_payments_profile_id {secondary_payments_profile_id}.')
62+
except GoogleAdsException as ex:
63+
print(f'Request with ID "{ex.request_id}" failed with status '
64+
f'"{ex.error.code().name}" and includes the following errors:')
6865
for error in ex.failure.errors:
69-
print('\tError with message "%s".' % error.message)
66+
print(f'\tError with message "{error.message}".')
7067
if error.location:
7168
for field_path_element in error.location.field_path_elements:
72-
print('\t\tOn field: %s' % field_path_element.field_name)
69+
print(f'\t\tOn field: {field_path_element.field_name}')
7370
sys.exit(1)
7471

7572

7673
if __name__ == '__main__':
7774
# GoogleAdsClient will read the google-ads.yaml configuration file in the
7875
# home directory if none is specified.
79-
google_ads_client = (google.ads.google_ads.client.GoogleAdsClient
80-
.load_from_storage())
76+
google_ads_client = GoogleAdsClient.load_from_storage()
8177

8278
parser = argparse.ArgumentParser(
8379
description='Lists all billing setup objects for specified customer.')
@@ -86,4 +82,4 @@ def main(client, customer_id, page_size):
8682
required=True, help='The Google Ads customer ID.')
8783
args = parser.parse_args()
8884

89-
main(google_ads_client, args.customer_id, _DEFAULT_PAGE_SIZE)
85+
main(google_ads_client, args.customer_id)

0 commit comments

Comments
 (0)