2626import argparse
2727import 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
3131from 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'\t Error with message "{ error .message } ".' )
91- if error .location :
92- for field_path_element in error .location .field_path_elements :
93- print (f"\t \t On 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
143127if __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'\t Error with message "{ error .message } ".' )
164+ if error .location :
165+ for field_path_element in error .location .field_path_elements :
166+ print (f"\t \t On field: { field_path_element .field_name } " )
167+ sys .exit (1 )
0 commit comments