1818import argparse
1919import sys
2020import uuid
21+ from typing import Any , List
2122
2223from google .ads .googleads .client import GoogleAdsClient
2324from google .ads .googleads .errors import GoogleAdsException
24-
25-
26- def main (client , customer_id , campaign_id ):
25+ from google .ads .googleads .v19 .services .types .ad_group_service import (
26+ AdGroupServiceClient ,
27+ )
28+ from google .ads .googleads .v19 .services .types .campaign_service import (
29+ CampaignServiceClient ,
30+ )
31+ from google .ads .googleads .v19 .services .types .ad_group_operation import (
32+ AdGroupOperation ,
33+ )
34+ from google .ads .googleads .v19 .services .types .mutate_ad_groups_response import (
35+ MutateAdGroupsResponse ,
36+ )
37+ from google .ads .googleads .v19 .services .types .mutate_ad_groups_request import (
38+ MutateAdGroupsRequest ,
39+ )
40+
41+
42+ def main (
43+ client : GoogleAdsClient , customer_id : str , campaign_id : str
44+ ) -> None :
2745 """Runs the example code, which demonstrates how to handle partial failures.
2846
2947 The example creates three Ad Groups, two of which intentionally fail in
@@ -36,7 +54,9 @@ def main(client, customer_id, campaign_id):
3654 campaign_id: The ID for a campaign to create Ad Groups under.
3755 """
3856 try :
39- ad_group_response = create_ad_groups (client , customer_id , campaign_id )
57+ ad_group_response : MutateAdGroupsResponse = create_ad_groups (
58+ client , customer_id , campaign_id
59+ )
4060 except GoogleAdsException as ex :
4161 print (
4262 f'Request with ID "{ ex .request_id } " failed with status '
@@ -53,7 +73,9 @@ def main(client, customer_id, campaign_id):
5373
5474
5575# [START handle_partial_failure]
56- def create_ad_groups (client , customer_id , campaign_id ):
76+ def create_ad_groups (
77+ client : GoogleAdsClient , customer_id : str , campaign_id : str
78+ ) -> MutateAdGroupsResponse :
5779 """Creates three Ad Groups, two of which intentionally generate errors.
5880
5981 Args:
@@ -63,35 +85,41 @@ def create_ad_groups(client, customer_id, campaign_id):
6385
6486 Returns: A MutateAdGroupsResponse message instance.
6587 """
66- ad_group_service = client .get_service ("AdGroupService" )
67- campaign_service = client .get_service ("CampaignService" )
68- resource_name = campaign_service .campaign_path (customer_id , campaign_id )
88+ ad_group_service : AdGroupServiceClient = client .get_service (
89+ "AdGroupService"
90+ )
91+ campaign_service : CampaignServiceClient = client .get_service (
92+ "CampaignService"
93+ )
94+ resource_name : str = campaign_service .campaign_path (
95+ customer_id , campaign_id
96+ )
6997
70- invalid_resource_name = campaign_service .campaign_path (customer_id , 0 )
71- ad_group_operations = []
98+ invalid_resource_name : str = campaign_service .campaign_path (customer_id , 0 )
99+ ad_group_operations : List [ AdGroupOperation ] = []
72100
73101 # This AdGroup should be created successfully - assuming the campaign in
74102 # the params exists.
75- ad_group_op1 = client .get_type ("AdGroupOperation" )
103+ ad_group_op1 : AdGroupOperation = client .get_type ("AdGroupOperation" )
76104 ad_group_op1 .create .name = f"Valid AdGroup: { uuid .uuid4 ()} "
77105 ad_group_op1 .create .campaign = resource_name
78106 ad_group_operations .append (ad_group_op1 )
79107
80108 # This AdGroup will always fail - campaign ID 0 in resource names is
81109 # never valid.
82- ad_group_op2 = client .get_type ("AdGroupOperation" )
110+ ad_group_op2 : AdGroupOperation = client .get_type ("AdGroupOperation" )
83111 ad_group_op2 .create .name = f"Broken AdGroup: { uuid .uuid4 ()} "
84112 ad_group_op2 .create .campaign = invalid_resource_name
85113 ad_group_operations .append (ad_group_op2 )
86114
87115 # This AdGroup will always fail - duplicate ad group names are not allowed.
88- ad_group_op3 = client .get_type ("AdGroupOperation" )
116+ ad_group_op3 : AdGroupOperation = client .get_type ("AdGroupOperation" )
89117 ad_group_op3 .create .name = ad_group_op1 .create .name
90118 ad_group_op3 .create .campaign = resource_name
91119 ad_group_operations .append (ad_group_op3 )
92120
93121 # Issue a mutate request, setting partial_failure=True.
94- request = client .get_type ("MutateAdGroupsRequest" )
122+ request : MutateAdGroupsRequest = client .get_type ("MutateAdGroupsRequest" )
95123 request .customer_id = customer_id
96124 request .operations = ad_group_operations
97125 request .partial_failure = True
@@ -100,7 +128,7 @@ def create_ad_groups(client, customer_id, campaign_id):
100128
101129
102130# [START handle_partial_failure_1]
103- def is_partial_failure_error_present (response ) :
131+ def is_partial_failure_error_present (response : MutateAdGroupsResponse ) -> bool :
104132 """Checks whether a response message has a partial failure error.
105133
106134 In Python the partial_failure_error attr is always present on a response
@@ -115,14 +143,16 @@ def is_partial_failure_error_present(response):
115143 Returns: A boolean, whether or not the response message has a partial
116144 failure error.
117145 """
118- partial_failure = getattr (response , "partial_failure_error" , None )
119- code = getattr (partial_failure , "code" , None )
146+ partial_failure : Any = getattr (response , "partial_failure_error" , None )
147+ code : int = int ( getattr (partial_failure , "code" , 0 )) # Default to 0 if None
120148 return code != 0
121149 # [END handle_partial_failure_1]
122150
123151
124152# [START handle_partial_failure_2]
125- def print_results (client , response ):
153+ def print_results (
154+ client : GoogleAdsClient , response : MutateAdGroupsResponse
155+ ) -> None :
126156 """Prints partial failure errors and success messages from a response.
127157
128158 This function shows how to retrieve partial_failure errors from a response
@@ -163,17 +193,19 @@ def print_results(client, response):
163193 if is_partial_failure_error_present (response ):
164194 print ("Partial failures occurred. Details will be shown below.\n " )
165195 # Prints the details of the partial failure errors.
166- partial_failure = getattr (response , "partial_failure_error" , None )
196+ partial_failure : Any = getattr (response , "partial_failure_error" , None )
167197 # partial_failure_error.details is a repeated field and iterable
168- error_details = getattr (partial_failure , "details" , [])
198+ error_details : List [ Any ] = getattr (partial_failure , "details" , [])
169199
170200 for error_detail in error_details :
171201 # Retrieve an instance of the GoogleAdsFailure class from the client
172- failure_message = client .get_type ("GoogleAdsFailure" )
202+ failure_message : Any = client .get_type ("GoogleAdsFailure" )
173203 # Parse the string into a GoogleAdsFailure message instance.
174204 # To access class-only methods on the message we retrieve its type.
175- GoogleAdsFailure = type (failure_message )
176- failure_object = GoogleAdsFailure .deserialize (error_detail .value )
205+ GoogleAdsFailure : Any = type (failure_message )
206+ failure_object : Any = GoogleAdsFailure .deserialize (
207+ error_detail .value
208+ )
177209
178210 for error in failure_object .errors :
179211 # Construct and print a string that details which element in
@@ -222,6 +254,8 @@ def print_results(client, response):
222254
223255 # GoogleAdsClient will read the google-ads.yaml configuration file in the
224256 # home directory if none is specified.
225- googleads_client = GoogleAdsClient .load_from_storage (version = "v20" )
257+ googleads_client : GoogleAdsClient = GoogleAdsClient .load_from_storage (
258+ version = "v20"
259+ )
226260
227261 main (googleads_client , args .customer_id , args .campaign_id )
0 commit comments