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 .v20 .services .services .ad_group_service import (
26+ AdGroupServiceClient ,
27+ )
28+ from google .ads .googleads .v20 .services .services .campaign_service import (
29+ CampaignServiceClient ,
30+ )
31+ from google .ads .googleads .v20 .services .types .ad_group_service import (
32+ AdGroupOperation ,
33+ MutateAdGroupsResponse ,
34+ MutateAdGroupsRequest ,
35+ )
36+
37+
38+ def main (client : GoogleAdsClient , customer_id : str , campaign_id : str ) -> None :
2739 """Runs the example code, which demonstrates how to handle partial failures.
2840
2941 The example creates three Ad Groups, two of which intentionally fail in
@@ -36,7 +48,9 @@ def main(client, customer_id, campaign_id):
3648 campaign_id: The ID for a campaign to create Ad Groups under.
3749 """
3850 try :
39- ad_group_response = create_ad_groups (client , customer_id , campaign_id )
51+ ad_group_response : MutateAdGroupsResponse = create_ad_groups (
52+ client , customer_id , campaign_id
53+ )
4054 except GoogleAdsException as ex :
4155 print (
4256 f'Request with ID "{ ex .request_id } " failed with status '
@@ -53,7 +67,9 @@ def main(client, customer_id, campaign_id):
5367
5468
5569# [START handle_partial_failure]
56- def create_ad_groups (client , customer_id , campaign_id ):
70+ def create_ad_groups (
71+ client : GoogleAdsClient , customer_id : str , campaign_id : str
72+ ) -> MutateAdGroupsResponse :
5773 """Creates three Ad Groups, two of which intentionally generate errors.
5874
5975 Args:
@@ -63,35 +79,41 @@ def create_ad_groups(client, customer_id, campaign_id):
6379
6480 Returns: A MutateAdGroupsResponse message instance.
6581 """
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 )
82+ ad_group_service : AdGroupServiceClient = client .get_service (
83+ "AdGroupService"
84+ )
85+ campaign_service : CampaignServiceClient = client .get_service (
86+ "CampaignService"
87+ )
88+ resource_name : str = campaign_service .campaign_path (
89+ customer_id , campaign_id
90+ )
6991
70- invalid_resource_name = campaign_service .campaign_path (customer_id , 0 )
71- ad_group_operations = []
92+ invalid_resource_name : str = campaign_service .campaign_path (customer_id , 0 )
93+ ad_group_operations : List [ AdGroupOperation ] = []
7294
7395 # This AdGroup should be created successfully - assuming the campaign in
7496 # the params exists.
75- ad_group_op1 = client .get_type ("AdGroupOperation" )
97+ ad_group_op1 : AdGroupOperation = client .get_type ("AdGroupOperation" )
7698 ad_group_op1 .create .name = f"Valid AdGroup: { uuid .uuid4 ()} "
7799 ad_group_op1 .create .campaign = resource_name
78100 ad_group_operations .append (ad_group_op1 )
79101
80102 # This AdGroup will always fail - campaign ID 0 in resource names is
81103 # never valid.
82- ad_group_op2 = client .get_type ("AdGroupOperation" )
104+ ad_group_op2 : AdGroupOperation = client .get_type ("AdGroupOperation" )
83105 ad_group_op2 .create .name = f"Broken AdGroup: { uuid .uuid4 ()} "
84106 ad_group_op2 .create .campaign = invalid_resource_name
85107 ad_group_operations .append (ad_group_op2 )
86108
87109 # This AdGroup will always fail - duplicate ad group names are not allowed.
88- ad_group_op3 = client .get_type ("AdGroupOperation" )
110+ ad_group_op3 : AdGroupOperation = client .get_type ("AdGroupOperation" )
89111 ad_group_op3 .create .name = ad_group_op1 .create .name
90112 ad_group_op3 .create .campaign = resource_name
91113 ad_group_operations .append (ad_group_op3 )
92114
93115 # Issue a mutate request, setting partial_failure=True.
94- request = client .get_type ("MutateAdGroupsRequest" )
116+ request : MutateAdGroupsRequest = client .get_type ("MutateAdGroupsRequest" )
95117 request .customer_id = customer_id
96118 request .operations = ad_group_operations
97119 request .partial_failure = True
@@ -100,7 +122,7 @@ def create_ad_groups(client, customer_id, campaign_id):
100122
101123
102124# [START handle_partial_failure_1]
103- def is_partial_failure_error_present (response ) :
125+ def is_partial_failure_error_present (response : MutateAdGroupsResponse ) -> bool :
104126 """Checks whether a response message has a partial failure error.
105127
106128 In Python the partial_failure_error attr is always present on a response
@@ -115,14 +137,16 @@ def is_partial_failure_error_present(response):
115137 Returns: A boolean, whether or not the response message has a partial
116138 failure error.
117139 """
118- partial_failure = getattr (response , "partial_failure_error" , None )
119- code = getattr (partial_failure , "code" , None )
140+ partial_failure : Any = getattr (response , "partial_failure_error" , None )
141+ code : int = int ( getattr (partial_failure , "code" , 0 )) # Default to 0 if None
120142 return code != 0
121143 # [END handle_partial_failure_1]
122144
123145
124146# [START handle_partial_failure_2]
125- def print_results (client , response ):
147+ def print_results (
148+ client : GoogleAdsClient , response : MutateAdGroupsResponse
149+ ) -> None :
126150 """Prints partial failure errors and success messages from a response.
127151
128152 This function shows how to retrieve partial_failure errors from a response
@@ -163,17 +187,19 @@ def print_results(client, response):
163187 if is_partial_failure_error_present (response ):
164188 print ("Partial failures occurred. Details will be shown below.\n " )
165189 # Prints the details of the partial failure errors.
166- partial_failure = getattr (response , "partial_failure_error" , None )
190+ partial_failure : Any = getattr (response , "partial_failure_error" , None )
167191 # partial_failure_error.details is a repeated field and iterable
168- error_details = getattr (partial_failure , "details" , [])
192+ error_details : List [ Any ] = getattr (partial_failure , "details" , [])
169193
170194 for error_detail in error_details :
171195 # Retrieve an instance of the GoogleAdsFailure class from the client
172- failure_message = client .get_type ("GoogleAdsFailure" )
196+ failure_message : Any = client .get_type ("GoogleAdsFailure" )
173197 # Parse the string into a GoogleAdsFailure message instance.
174198 # 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 )
199+ GoogleAdsFailure : Any = type (failure_message )
200+ failure_object : Any = GoogleAdsFailure .deserialize (
201+ error_detail .value
202+ )
177203
178204 for error in failure_object .errors :
179205 # Construct and print a string that details which element in
@@ -222,6 +248,8 @@ def print_results(client, response):
222248
223249 # GoogleAdsClient will read the google-ads.yaml configuration file in the
224250 # home directory if none is specified.
225- googleads_client = GoogleAdsClient .load_from_storage (version = "v20" )
251+ googleads_client : GoogleAdsClient = GoogleAdsClient .load_from_storage (
252+ version = "v20"
253+ )
226254
227255 main (googleads_client , args .customer_id , args .campaign_id )
0 commit comments