Skip to content

Commit 6c242d3

Browse files
authored
Migrate to microgenerator, update all examples, add interface improvements (#399)
1 parent 969eff5 commit 6c242d3

File tree

8,403 files changed

+520385
-640235
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

8,403 files changed

+520385
-640235
lines changed

ChangeLog

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
* 10.0.0
2+
- Revamp of protobuf message interface to improve usability. See:
3+
https://developers.google.com/google-ads/api/docs/client-libs/python/library-version-10
4+
- Remove ResourceName utility
5+
- Add ability to specify API version at client level, which overrides setting
6+
at service or type level.
7+
- GoogleAdsClient.get_type is now an instance, not class, level method
8+
- Add copy_from helper method to GoogleAdsClient.
9+
- Add "enums" attribute to GoogleAdsClient for easier Enum accessing.
10+
- Various updates to all examples
11+
112
* 9.0.0
213
- Google Ads v6_1 release
314
- Deprecate v3_0

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Authors
4242
* `David Wihl`_
4343
* `Ben Karl`_
4444
* `Andrew Burke`_
45+
* `Laura Chevalier`_
4546

4647
.. |build-status| image:: https://storage.googleapis.com/gaa-clientlibs/badges/google-ads-python/buildstatus_ubuntu.svg
4748
.. _Developer Site: https://developers.google.com/google-ads/api/docs/client-libs/python/
@@ -52,3 +53,4 @@ Authors
5253
.. _David Wihl: https://github.com/wihl
5354
.. _Ben Karl: https://github.com/BenRKarl
5455
.. _Andrew Burke: https://github.com/AndrewMBurke
56+
.. _Laura Chevalier: https://github.com/laurachevalier4

examples/account_management/approve_merchant_center_link.py

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import argparse
2727
import sys
2828

29-
from google.ads.google_ads.client import GoogleAdsClient
30-
from google.ads.google_ads.errors import GoogleAdsException
29+
from google.ads.googleads.client import GoogleAdsClient
30+
from google.ads.googleads.errors import GoogleAdsException
3131
from google.api_core import protobuf_helpers
3232

3333

@@ -41,57 +41,45 @@ def main(client, customer_id, merchant_center_account_id):
4141
is to be approved.
4242
"""
4343
merchant_center_link_service = client.get_service(
44-
"MerchantCenterLinkService", version="v6"
44+
"MerchantCenterLinkService"
4545
)
4646

47-
try:
48-
# [START approve_merchant_center_link]
49-
# Retrieve all the existing Merchant Center links.
50-
response = merchant_center_link_service.list_merchant_center_links(
51-
customer_id
52-
)
47+
# [START approve_merchant_center_link]
48+
# Retrieve all the existing Merchant Center links.
49+
response = merchant_center_link_service.list_merchant_center_links(
50+
customer_id=customer_id
51+
)
52+
print(
53+
f"{len(response.merchant_center_links)} Merchant Center link(s) "
54+
"found with the following details:"
55+
)
56+
# [END approve_merchant_center_link]
57+
58+
merchant_center_link_status_enum = client.get_type(
59+
"MerchantCenterLinkStatusEnum"
60+
).MerchantCenterLinkStatus
61+
62+
# Iterate through the results and filter for links with pending statuses.
63+
for merchant_center_link in response.merchant_center_links:
64+
# [START approve_merchant_center_link_1]
5365
print(
54-
f"{len(response.merchant_center_links)} Merchant Center link(s) "
55-
"found with the following details:"
66+
f"Link '{merchant_center_link.resource_name}' has status "
67+
f"'{merchant_center_link.status.name}'."
5668
)
57-
# [END approve_merchant_center_link]
58-
59-
merchant_center_link_status_enum = client.get_type(
60-
"MerchantCenterLinkStatusEnum", version="v6"
61-
).MerchantCenterLinkStatus
62-
63-
# Iterate through the results and filter for links with pending states.
64-
for merchant_center_link in response.merchant_center_links:
65-
# [START approve_merchant_center_link_1]
66-
print(
67-
f"Link '{merchant_center_link.resource_name}' has status "
68-
f"'{merchant_center_link_status_enum.Name(merchant_center_link.status)}'."
69+
# [END approve_merchant_center_link_1]
70+
71+
if (
72+
merchant_center_link.status
73+
== merchant_center_link_status_enum.PENDING
74+
and str(merchant_center_link.id) == merchant_center_account_id
75+
):
76+
_update_merchant_center_link_status(
77+
client,
78+
customer_id,
79+
merchant_center_link_service,
80+
merchant_center_link,
81+
merchant_center_link_status_enum.ENABLED,
6982
)
70-
# [END approve_merchant_center_link_1]
71-
72-
if (
73-
merchant_center_link.status
74-
== merchant_center_link_status_enum.PENDING
75-
and str(merchant_center_link.id) == merchant_center_account_id
76-
):
77-
_update_merchant_center_link_status(
78-
client,
79-
customer_id,
80-
merchant_center_link_service,
81-
merchant_center_link,
82-
merchant_center_link_status_enum.ENABLED,
83-
)
84-
except GoogleAdsException as ex:
85-
print(
86-
f'Request with ID "{ex.request_id}" failed with status'
87-
f'"{ex.error.code().name}" and includes the following errors:'
88-
)
89-
for error in ex.failure.errors:
90-
print(f'\tError with message "{error.message}".')
91-
if error.location:
92-
for field_path_element in error.location.field_path_elements:
93-
print(f"\t\tOn field: {field_path_element.field_name}")
94-
sys.exit(1)
9583

9684

9785
# [START approve_merchant_center_link_2]
@@ -112,38 +100,34 @@ def _update_merchant_center_link_status(
112100
status: The updated status to apply to the merchant center link.
113101
"""
114102
# Creates an operation.
115-
operation = client.get_type("MerchantCenterLinkOperation", version="v6")
103+
operation = client.get_type("MerchantCenterLinkOperation")
116104
link_to_update = operation.update
117105
link_to_update.resource_name = merchant_center_link.resource_name
118106
# Enables the pending link.
119107
link_to_update.status = status
120-
121-
field_mask = protobuf_helpers.field_mask(None, link_to_update)
122-
operation.update_mask.CopyFrom(field_mask)
108+
client.copy_from(
109+
operation.update_mask,
110+
protobuf_helpers.field_mask(None, link_to_update._pb),
111+
)
123112

124113
# Updates the link.
125114
mutate_response = merchant_center_link_service.mutate_merchant_center_link(
126-
customer_id, operation
115+
customer_id=customer_id, operation=operation
127116
)
128117

129-
merchant_center_link_status_enum = client.get_type(
130-
"MerchantCenterLinkStatusEnum", version="v6"
131-
).MerchantCenterLinkStatus
132-
133118
# Displays the result.
134119
print(
135120
"The status of Merchant Center Link with resource name "
136121
f"'{mutate_response.result.resource_name}' to Google Ads account : "
137-
f"{customer_id} was updated to "
138-
f"{merchant_center_link_status_enum.Name(status)}."
122+
f"{customer_id} was updated to {status.name}."
139123
)
140124
# [END approve_merchant_center_link_2]
141125

142126

143127
if __name__ == "__main__":
144128
# GoogleAdsClient will read the google-ads.yaml configuration file in the
145129
# home directory if none is specified.
146-
google_ads_client = GoogleAdsClient.load_from_storage()
130+
googleads_client = GoogleAdsClient.load_from_storage(version="v6")
147131

148132
parser = argparse.ArgumentParser(
149133
description=("Approves a Merchant Center link request.")
@@ -166,4 +150,18 @@ def _update_merchant_center_link_status(
166150
)
167151
args = parser.parse_args()
168152

169-
main(google_ads_client, args.customer_id, args.merchant_center_account_id)
153+
try:
154+
main(
155+
googleads_client, args.customer_id, args.merchant_center_account_id
156+
)
157+
except GoogleAdsException as ex:
158+
print(
159+
f'Request with ID "{ex.request_id}" failed with status'
160+
f'"{ex.error.code().name}" and includes the following errors:'
161+
)
162+
for error in ex.failure.errors:
163+
print(f'\tError with message "{error.message}".')
164+
if error.location:
165+
for field_path_element in error.location.field_path_elements:
166+
print(f"\t\tOn field: {field_path_element.field_name}")
167+
sys.exit(1)

examples/account_management/create_customer.py

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,17 @@
2525
import sys
2626
from datetime import datetime
2727

28-
import google.ads.google_ads.client
29-
28+
from google.ads.googleads.client import GoogleAdsClient
29+
from google.ads.googleads.errors import GoogleAdsException
3030

3131
# [START create_customer]
3232
def main(client, manager_customer_id):
33-
customer_service = client.get_service("CustomerService", version="v6")
34-
customer = client.get_type("Customer", version="v6")
35-
today = datetime.today().strftime("%Y%m%d %H:%M:%S")
36-
customer.descriptive_name = (
37-
"Account created with " "CustomerService on %s" % today
38-
)
33+
customer_service = client.get_service("CustomerService")
34+
customer = client.get_type("Customer")
35+
now = datetime.today().strftime("%Y%m%d %H:%M:%S")
36+
customer.descriptive_name = f"Account created with CustomerService on {now}"
3937
# For a list of valid currency codes and time zones see this documentation:
40-
# https://developers.google.com/adwords/api/docs/appendix/codes-formats
38+
# https://developers.google.com/google-ads/api/reference/data/codes-formats
4139
customer.currency_code = "USD"
4240
customer.time_zone = "America/New_York"
4341
# The below values are optional. For more information about URL
@@ -46,39 +44,21 @@ def main(client, manager_customer_id):
4644
customer.final_url_suffix = (
4745
"keyword={keyword}&matchtype={matchtype}" "&adgroupid={adgroupid}"
4846
)
49-
customer.has_partners_badge = False
5047

51-
try:
52-
response = customer_service.create_customer_client(
53-
manager_customer_id, customer
54-
)
55-
print(
56-
(
57-
'Customer created with resource name "%s" under manager account '
58-
'with customer ID "%s"'
59-
)
60-
% (response.resource_name, manager_customer_id)
61-
)
62-
except google.ads.google_ads.errors.GoogleAdsException as ex:
63-
print(
64-
'Request with ID "%s" failed with status "%s" and includes the '
65-
"following errors:" % (ex.request_id, ex.error.code().name)
66-
)
67-
for error in ex.failure.errors:
68-
print('\tError with message "%s".' % error.message)
69-
if error.location:
70-
for field_path_element in error.location.field_path_elements:
71-
print("\t\tOn field: %s" % field_path_element.field_name)
72-
sys.exit(1)
73-
# [END create_customer]
48+
response = customer_service.create_customer_client(
49+
customer_id=manager_customer_id, customer_client=customer
50+
)
51+
print(
52+
f'Customer created with resource name "{response.resource_name}" '
53+
f'under manager account with ID "{manager_customer_id}".'
54+
)
55+
# [END create_customer]
7456

7557

7658
if __name__ == "__main__":
7759
# GoogleAdsClient will read the google-ads.yaml configuration file in the
7860
# home directory if none is specified.
79-
google_ads_client = (
80-
google.ads.google_ads.client.GoogleAdsClient.load_from_storage()
81-
)
61+
googleads_client = GoogleAdsClient.load_from_storage(version="v6")
8262

8363
parser = argparse.ArgumentParser(
8464
description=("Creates a new client under the given manager.")
@@ -95,4 +75,16 @@ def main(client, manager_customer_id):
9575
)
9676
args = parser.parse_args()
9777

98-
main(google_ads_client, args.manager_customer_id)
78+
try:
79+
main(googleads_client, args.manager_customer_id)
80+
except GoogleAdsException as ex:
81+
print(
82+
f'Request with ID "{ex.request_id}" failed with status '
83+
f'"{ex.error.code().name}" and includes the following errors:'
84+
)
85+
for error in ex.failure.errors:
86+
print(f' Error with message "{error.message}".')
87+
if error.location:
88+
for field_path_element in error.location.field_path_elements:
89+
print(f"\t\tOn field: {field_path_element.field_name}")
90+
sys.exit(1)

0 commit comments

Comments
 (0)